-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Unit tests): unit tests for all functionality
- Add unit tests for the user login endpoint Add unit tests for the user signup endpoint [Finishes #165273457]
- Loading branch information
1 parent
b4d59c2
commit b12a0e1
Showing
8 changed files
with
218 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
.env | ||
|
||
# C extensions | ||
*.so | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[ | ||
{ | ||
"model": "authentication.user", | ||
"pk": 1, | ||
"fields": { | ||
"username": "sanya", | ||
"email": "sanyakenneth@gmail.com", | ||
"password": "pbkdf2_sha256$120000$RLkaJYAyp0Dq$+2V+D1qXP5P1UuEjLxaRAorMiw2khCxKevg+/YrCAyI=", | ||
"created_at": "2013-03-23 00:03:00", | ||
"updated_at": "2013-03-23 01:02:00" | ||
} | ||
} | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Rest framework import | ||
from rest_framework.test import APIClient, APITestCase | ||
# Local import | ||
from .test_data import Test_Data | ||
|
||
|
||
class BaseTestCase(APITestCase): | ||
""" | ||
Base Test class for out tests in this app | ||
Class will also house the setup and teardown | ||
methods for our tests | ||
""" | ||
def setUp(self): | ||
# Initialize the Testclient for the tests | ||
self.client = APIClient() | ||
self.data = Test_Data() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
class Test_Data: | ||
""" | ||
class houses data that will be used for the | ||
different test scenarios. | ||
""" | ||
|
||
def __init__(self): | ||
self.signup_data = { | ||
"user": { | ||
"username": "sanya", | ||
"email": "sanyakenneth@gmail.com", | ||
"password": "sanya1234" | ||
} | ||
} | ||
self.blank_username_on_signup = { | ||
"user": { | ||
"username": "", | ||
"email": "sanyakenneth@gmail.com", | ||
"password": "sanya1234" | ||
} | ||
} | ||
self.invalid_email_on_signup = { | ||
"user": { | ||
"username": "sanya", | ||
"email": "sanyakennethgmail.com", | ||
"password": "sanya1234" | ||
} | ||
} | ||
self.invalid_password_on_signup = { | ||
"user": { | ||
"username": "sanya", | ||
"email": "sanyakenneth@gmail.com", | ||
"password": "s" | ||
} | ||
} | ||
self.invalid_password_length = { | ||
"user": { | ||
"username": "sanya", | ||
"email": "sanyakenneth@gmail.com", | ||
"password": "sa" | ||
} | ||
} | ||
self.login_data = { | ||
"user": { | ||
"email": "sanyakenneth@gmail.com", | ||
"password": "kensanya1234" | ||
} | ||
} | ||
self.wrong_login_data = { | ||
"user": { | ||
"email": "sanyakenneth@gmail.com", | ||
"password": "sanya1234" | ||
} | ||
} | ||
self.blank_email = { | ||
"user": { | ||
"email": "", | ||
"password": "sanya1234" | ||
} | ||
} | ||
self.blank_password = { | ||
"user": { | ||
"email": "sanyakenneth@gmail.com", | ||
"password": "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Django and Rest framework imports | ||
from django.urls import reverse | ||
from rest_framework import status | ||
# Local imports | ||
from .base import BaseTestCase | ||
from authors.apps.authentication.models import User | ||
|
||
|
||
class UserLoginTestCase(BaseTestCase): | ||
""" | ||
Class Test Case for Testing user Login functionality | ||
""" | ||
# Initialize fixture for the class Test Case | ||
fixtures = ['authors/apps/authentication/fixtures/signup.json'] | ||
|
||
def test_user_can_login(self): | ||
""" | ||
Method tests the user login functionality | ||
""" | ||
url = reverse('login') | ||
response = self.client.post(url, self.data.login_data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertTrue(response.data['token']) | ||
self.assertIsInstance(response.data['token'], str) | ||
self.assertEqual(response.data['email'], 'sanyakenneth@gmail.com') | ||
|
||
def test_returns_error_if_user_credentials_are_wrong_on_login(self): | ||
""" | ||
Method tests if the API returns an error | ||
if a user provides wrong credentials on login | ||
""" | ||
url = reverse('login') | ||
response = self.client.post( | ||
url, self.data.wrong_login_data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("A user with this email and password was not found.", | ||
response.data['errors']['error']) | ||
|
||
def test_returns_error_if_user_provides_blank_email_on_login(self): | ||
""" | ||
Method tests if the API returns an error | ||
if a user leaves email field blank on login | ||
""" | ||
url = reverse('login') | ||
response = self.client.post(url, self.data.blank_email, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("This field may not be blank.", | ||
response.data['errors']['email']) | ||
|
||
def test_returns_error_if_user_provides_blank_password_on_login(self): | ||
""" | ||
Method tests if the API returns an error | ||
if a user leaves password field blank on login | ||
""" | ||
url = reverse('login') | ||
response = self.client.post( | ||
url, self.data.blank_password, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("This field may not be blank.", | ||
response.data['errors']['password']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Django and restframework imports | ||
from django.urls import reverse | ||
from rest_framework import status | ||
# Local imports | ||
from authors.apps.authentication.models import User | ||
from authors.apps.authentication.serializers import UserSerializer | ||
from .base import BaseTestCase | ||
|
||
|
||
class UserSignUpTestCase(BaseTestCase): | ||
""" | ||
Class Test Case for testing user signup | ||
functionality | ||
Class also tests the user model | ||
""" | ||
|
||
def test_user_model(self): | ||
""" | ||
Method tests the user model for the API | ||
""" | ||
user = User( | ||
email=self.data.signup_data['user']['email'], | ||
username=self.data.signup_data['user']['username'], | ||
password=self.data.signup_data['user']['password'] | ||
) | ||
self.assertEqual(user.username, 'sanya') | ||
self.assertEqual(user.email, 'sanyakenneth@gmail.com') | ||
|
||
def test_can_sign_up_user(self): | ||
""" | ||
Method tests if the user can successfuly signup | ||
""" | ||
url = reverse('signup') | ||
response = self.client.post(url, self.data.signup_data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
self.assertEqual(response.data['username'], 'sanya') | ||
|
||
def test_returns_error_if_username_is_blank_on_signup(self): | ||
url = reverse('signup') | ||
response = self.client.post( | ||
url, self.data.blank_username_on_signup, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("This field may not be blank", | ||
response.data['errors']['username'][0]) | ||
|
||
def test_returns_error_if_password_is_short(self): | ||
url = reverse('signup') | ||
response = self.client.post( | ||
url, self.data.invalid_password_length, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("Ensure this field has at least 8 characters", | ||
response.data['errors']['password'][0]) | ||
|
||
def test_returns_error_if_email_is_invalid(self): | ||
url = reverse('signup') | ||
response = self.client.post( | ||
url, self.data.invalid_email_on_signup, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("Enter a valid email address", | ||
response.data['errors']['email'][0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters