Skip to content

Commit

Permalink
feat(Registration): Validate user registration data
Browse files Browse the repository at this point in the history
- Ensure that email submitted by user is correct
- Ensure that password is more than 8 characters and alphanumeric
- Ensure the the email used has not been used to create a previous account
- Ensure that the username is not already in use within the system

[Finishes #164069216]
  • Loading branch information
wasibani roy committed Mar 5, 2019
2 parents 2601f39 + 2c89583 commit 9079eee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
15 changes: 10 additions & 5 deletions authors/apps/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class RegistrationSerializer(serializers.ModelSerializer):
password = serializers.CharField(
max_length=128,
min_length=8,
write_only=True
write_only=True,
error_messages={
"min_length": "Password should be atleast {min_length} characters"
}
)

# The client should not be able to send a token along with a registration
Expand Down Expand Up @@ -45,20 +48,22 @@ def validate_email(self, value):
return value

def validate_username(self, value):
# validate that username passed is correct

username_regex = (r'^[a-zA-Z]+$')
data = self.get_initial()
username = data.get("username")
user_query_set = User.objects.filter(username=username)

# validate that username passed is correct
if not re.search(username_regex, username):
raise serializers.ValidationError("The username is incorrect please use characters")
raise serializers.ValidationError(
"The username is invalid please use letters")

if user_query_set.exists():
raise serializers.ValidationError("This username already exists please choose another one")
raise serializers.ValidationError(
"This username already exists please choose another one")
return value


def create(self, validated_data):
# Use the `create_user` method we wrote earlier to create a new user.
return User.objects.create_user(**validated_data)
Expand Down
24 changes: 15 additions & 9 deletions authors/apps/authentication/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,14 @@ def test_empty_username(self):
self.assertEqual(
response.data['errors']['username'][0], "This field may not be blank.")

def test_invalid_email(self):
"""Tests if a user can not create an account with an invalid email."""

response = self.client.post(
self.registration_url, invalid_user_email, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data['errors']['email'][0], "Enter a valid email address.")

def test_short_password(self):
"""Tests if a user can not create an account with a short password."""
response = self.client.post(
self.registration_url, short_password, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['errors']['password']
[0], "Ensure this field has at least 8 characters.")
[0], "Password should be atleast 8 characters")

def test_email_exits(self):
"""Tests if a user can not create an account with a taken email."""
Expand All @@ -51,6 +43,13 @@ def test_email_exits(self):
self.assertEqual(
response.data['errors']['email'][0], "user with this email already exists.")

def test_register_invalid_email(self):
response = self.client.post(
self.registration_url, invalid_user_email, format='json')
data = response.json().get('errors')
self.assertEqual(400, response.status_code)
self.assertIn('Enter a valid email address.', data['email'][0])

def test_register_same_username(self):
self.client.post(self.registration_url, valid_user, format='json')
response = self.client.post(
Expand All @@ -60,3 +59,10 @@ def test_register_same_username(self):
self.assertIn('user with this username already exists.',
data['username'])

def test_register_invalid_username(self):
response = self.client.post(
self.registration_url, invalid_username, format='json')
data = response.json().get('errors')
self.assertEqual(400, response.status_code)
self.assertIn('The username is invalid please use letters',
data['username'])

0 comments on commit 9079eee

Please sign in to comment.