Skip to content

Commit 7d52dec

Browse files
author
Gabriel Monroy
committed
check for no creds on layer:scale, with tests
1 parent 129445b commit 7d52dec

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

api/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Provider(UuidAuditedModel):
115115
owner = models.ForeignKey(settings.AUTH_USER_MODEL)
116116
id = models.SlugField(max_length=64)
117117
type = models.SlugField(max_length=16, choices=PROVIDERS)
118-
creds = fields.CredentialsField()
118+
creds = fields.CredentialsField(blank=True)
119119

120120
class Meta:
121121
unique_together = (('owner', 'id'),)

api/tests/layer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,23 @@ def test_layer_scale(self):
158158
response = self.client.get(url)
159159
self.assertEqual(response.status_code, 200)
160160
self.assertEqual(response.data['layers'], json.dumps(body))
161+
162+
def test_layer_scale_no_creds(self):
163+
url = '/api/providers/autotest'
164+
body = {'creds': json.dumps({}) }
165+
response = self.client.patch(url, json.dumps(body), content_type='application/json')
166+
self.assertEqual(response.status_code, 200)
167+
# try to scale and a formation
168+
url = '/api/formations'
169+
body = {'id': 'autotest'}
170+
response = self.client.post(url, json.dumps(body), content_type='application/json')
171+
self.assertEqual(response.status_code, 201)
172+
formation_id = response.data['id'] # noqa
173+
url = '/api/formations/{formation_id}/layers'.format(**locals())
174+
body = {'id': 'runtime', 'flavor': 'autotest', 'run_list': 'recipe[deis::runtime]'}
175+
response = self.client.post(url, json.dumps(body), content_type='application/json')
176+
self.assertEqual(response.status_code, 201)
177+
url = '/api/formations/{formation_id}/scale/layers'.format(**locals())
178+
body = {'runtime': 1}
179+
response = self.client.post(url, json.dumps(body), content_type='application/json')
180+
self.assertEqual(response.status_code, 400)

api/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ def scale_layers(self, request, **kwargs):
174174
new_structure[target] = int(count)
175175
except ValueError:
176176
return Response('Invalid scaling format', status=HTTP_400_BAD_REQUEST)
177+
# check for empty credentials
178+
for p in models.Provider.objects.filter(owner=request.user):
179+
if p.creds:
180+
break
181+
else:
182+
return Response('No provider credentials available', status=HTTP_400_BAD_REQUEST)
177183
formation = self.get_object()
178184
formation.layers.update(new_structure)
179185
try:

0 commit comments

Comments
 (0)