Skip to content

Commit b3202fd

Browse files
author
Matthew Fisher
committed
set min username length to 4
1 parent f98fd5e commit b3202fd

4 files changed

Lines changed: 40 additions & 24 deletions

File tree

api/exceptions.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,38 @@
88
from rest_framework import status
99

1010

11-
class BuildNodeError(APIException):
11+
class AbstractDeisException(APIException):
1212
"""
13-
Indicates a problem in building or bootstrapping a node.
13+
Abstract class in which all Deis Exceptions and Errors should extend.
1414
15-
This exception is subclassed from rest_framework's APIException so it
16-
isn't reported as "500 SERVER ERROR."
15+
This exception is subclassed from rest_framework's APIException so that
16+
subclasses can change the status code to something different than
17+
"500 SERVER ERROR."
1718
"""
1819

19-
status_code = status.HTTP_401_UNAUTHORIZED
20-
2120
def __init__(self, detail=None):
2221
self.detail = detail
2322

23+
class Meta:
24+
abstract = True
2425

25-
class BuildFormationError(APIException):
26-
"""
27-
Indicates a problem in creating a formation.
2826

29-
This exception is subclassed from rest_framework's APIException so it
30-
isn't reported as "500 SERVER ERROR."
27+
class BuildNodeError(AbstractDeisException):
3128
"""
29+
Indicates a problem in building or bootstrapping a node.
30+
"""
31+
status_code = status.HTTP_401_UNAUTHORIZED
32+
3233

34+
class BuildFormationError(AbstractDeisException):
35+
"""
36+
Indicates a problem in creating a formation.
37+
"""
3338
status_code = status.HTTP_400_BAD_REQUEST
3439

35-
def __init__(self, detail=None):
36-
self.detail = detail
40+
41+
class UserRegistrationException(AbstractDeisException):
42+
"""
43+
Indicates that there was a problem registering the user.
44+
"""
45+
status_code = status.HTTP_400_BAD_REQUEST

api/tests/test_auth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def test_auth(self):
5656
self.assertFalse(response.data['is_staff'])
5757
self.assertTrue(
5858
self.client.login(username=username, password=password))
59+
# test with len(username) < 4
60+
submit['username'] = 'new'
61+
response = self.client.post(url, json.dumps(submit), content_type='application/json')
62+
self.assertEqual(response.status_code, 400)
5963
# test for default objects
6064
url = '/api/providers'
6165
response = self.client.get(url)

api/tests/test_perm.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_list(self):
7070

7171
def test_create(self):
7272
submit = {
73-
'username': 'one',
73+
'username': 'first',
7474
'password': 'password',
7575
'email': 'autotest@deis.io',
7676
}
@@ -79,7 +79,7 @@ def test_create(self):
7979
self.assertEqual(response.status_code, 201)
8080
self.assertTrue(response.data['is_superuser'])
8181
submit = {
82-
'username': 'two',
82+
'username': 'second',
8383
'password': 'password',
8484
'email': 'autotest@deis.io',
8585
}
@@ -88,20 +88,20 @@ def test_create(self):
8888
self.assertEqual(response.status_code, 201)
8989
self.assertFalse(response.data['is_superuser'])
9090
self.assertTrue(
91-
self.client.login(username='one', password='password'))
91+
self.client.login(username='first', password='password'))
9292
# grant user 2 the superuser perm
9393
url = '/api/admin/perms'
94-
body = {'username': 'two'}
94+
body = {'username': 'second'}
9595
response = self.client.post(url, json.dumps(body), content_type='application/json')
9696
self.assertEqual(response.status_code, 201)
9797
response = self.client.get(url)
9898
self.assertEqual(response.status_code, 200)
9999
self.assertEqual(len(response.data['results']), 2)
100-
self.assertIn('two', str(response.data['results']))
100+
self.assertIn('second', str(response.data['results']))
101101

102102
def test_delete(self):
103103
submit = {
104-
'username': 'uno',
104+
'username': 'first',
105105
'password': 'password',
106106
'email': 'autotest@deis.io',
107107
}
@@ -110,7 +110,7 @@ def test_delete(self):
110110
self.assertEqual(response.status_code, 201)
111111
self.assertTrue(response.data['is_superuser'])
112112
submit = {
113-
'username': 'dos',
113+
'username': 'second',
114114
'password': 'password',
115115
'email': 'autotest@deis.io',
116116
}
@@ -119,14 +119,14 @@ def test_delete(self):
119119
self.assertEqual(response.status_code, 201)
120120
self.assertFalse(response.data['is_superuser'])
121121
self.assertTrue(
122-
self.client.login(username='uno', password='password'))
122+
self.client.login(username='first', password='password'))
123123
# grant user 2 the superuser perm
124124
url = '/api/admin/perms'
125-
body = {'username': 'dos'}
125+
body = {'username': 'second'}
126126
response = self.client.post(url, json.dumps(body), content_type='application/json')
127127
self.assertEqual(response.status_code, 201)
128128
# revoke the superuser perm
129-
response = self.client.delete(url + '/dos')
129+
response = self.client.delete(url + '/second')
130130
self.assertEqual(response.status_code, 204)
131131
response = self.client.get(url)
132132
self.assertEqual(response.status_code, 200)

api/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from rest_framework.response import Response
2525

2626
from api import docker, models, serializers
27-
from .exceptions import BuildFormationError
27+
from .exceptions import BuildFormationError, UserRegistrationException
2828

2929
from deis import settings
3030

@@ -149,6 +149,9 @@ def pre_save(self, obj):
149149
obj.is_active = True
150150
obj.email = User.objects.normalize_email(obj.email)
151151
obj.set_password(obj.password)
152+
# FIXME: move this business logic to the model
153+
if len(obj.username) < 4:
154+
raise UserRegistrationException('username must be > 4 characters in length')
152155
# Make this first signup an admin / superuser
153156
if not User.objects.filter(is_superuser=True).exists():
154157
obj.is_superuser = obj.is_staff = True

0 commit comments

Comments
 (0)