Skip to content

Commit

Permalink
implement validations
Browse files Browse the repository at this point in the history
  • Loading branch information
huxaiphaer committed Sep 5, 2018
1 parent 51206e3 commit 5cefc15
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
59 changes: 57 additions & 2 deletions authors/apps/authentication/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib.auth import authenticate

from rest_framework import serializers

import re
from .models import User


Expand All @@ -19,6 +19,62 @@ class RegistrationSerializer(serializers.ModelSerializer):
# The client should not be able to send a token along with a registration
# request. Making `token` read-only handles that for us.

# validate registrattion.

def validate(self, data):
# The `validate` method is where we make sure that the current
# instance of `LoginSerializer` has "valid". In the case of logging a
# user in, this means validating that they've provided an email
# and password and that this combination matches one of the users in
# our database.
username = data.get('username',None)
email = data.get('email', None)
password = data.get('password', None)

# As mentioned above, an email is required. Raise an exception if an
# email is not provided.
if email is None:
raise serializers.ValidationError(
'An email address is required to register'
)

if re.compile('[!@#$%^&*:;?><.0-9]').match(username):
raise serializers.ValidationError(
'Invalid Username , it contains invalid characters'
)
if not re.match(r"([\w\.-]+)@([\w\.-]+)(\.[\w\.]+$)", email):
raise serializers.ValidationError(
'Enter a valid email please.'
)

if password.strip() == "" or password.strip() == " " or password.strip() == " ":
raise serializers.ValidationError(
'Enter a valid password please, remove spaces.'
)

if str(password).isdigit():
raise serializers.ValidationError(
'At least add one character like a,b,c'
)
if not re.search('[0-9]',password):
raise serializers.ValidationError(
'Enter at least one number in your password.'
)

# As mentioned above, a password is required. Raise an exception if a
# password is not provided.
if password is None:
raise serializers.ValidationError(
'A password is required to register.'
)

return {

'email': email,
'username': username,

}

class Meta:
model = User
# List all of the fields that could possibly be included in a request
Expand Down Expand Up @@ -116,7 +172,6 @@ class Meta:
# `max_length` properties too, but that isn't the case for the token
# field.


def update(self, instance, validated_data):
"""Performs an update on a User."""

Expand Down
75 changes: 75 additions & 0 deletions authors/apps/authentication/tests/test_login_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,78 @@ def test_wrong_email_on_login(self):
format="json")
self.assertEqual('A user with this email and password was not found.',
self.response.json()['errors']['error'][0])

def test_register_with_invalid_username(self):
"""Test if the username is invalid """
self.response = self.client.post(
"/api/users/",
{"user": {
"username": "&*@#$",
"email": 'kakecom@gmail.com',
"password": "irquoa12345678",
}
},
format="json"
)
self.assertEqual(self.response.status_code, status.HTTP_400_BAD_REQUEST)

def test_wrong_email(self):
"""Test if the email is wrong"""
self.response = self.client.post(
"/api/users/",
{"user": {
"username": "Iroqua",
"email": 'kakegmailcom',
"password": "irquoa12345678",
}
},
format="json"
)
self.assertEqual(self.response.status_code, status.HTTP_400_BAD_REQUEST)

def test_password_length(self):
"""Test if the password length is < than 8 """
self.response = self.client.post(
"/api/users/",
{"user": {
"username": "kake",
"email": 'kakegmail@gmail.com',
"password": "12lak",
}
},
format="json"
)
self.assertEqual(self.response.status_code, status.HTTP_400_BAD_REQUEST)

def test_password_is_not_alphanumeric(self):

"""Test if the password is alphanumeric """
self.response = self.client.post(
"/api/users/",
{"user": {
"username": "kake",
"email": 'kakegmail.com',
"password": "123445",
}
},
format="json"
)
self.assertEqual(self.response.status_code, status.HTTP_400_BAD_REQUEST)

def test_registration_successful(self):

"""Test if the password length is greater than 8 characters """
self.response = self.client.post(
"/api/users/",
{"user": {
"username": "kake",
"email": 'kake@gmail.com',
"password": "123445abcdefghijk",
}
},
format="json"
)
self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)



2 changes: 1 addition & 1 deletion authors/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("SECRET_KEY", None)
SECRET_KEY = os.environ.get("HOME", None)


# SECURITY WARNING: don't run with debug turned on in production!
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
atomicwrites==1.2.0
attrs==18.1.0
coverage==4.5.1
dj-database-url==0.5.0
Django==2.1
django-cors-headers==2.4.0
Expand All @@ -9,6 +10,7 @@ djangorestframework==3.8.2
djangorestframework-jwt==1.11.0
gunicorn==19.9.0
more-itertools==4.3.0
nose==1.3.7
pluggy==0.7.1
psycopg2==2.7.5
psycopg2-binary==2.7.5
Expand Down

0 comments on commit 5cefc15

Please sign in to comment.