-
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
5f8500a
commit f4e1c7f
Showing
14 changed files
with
562 additions
and
288 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 |
---|---|---|
|
@@ -14,4 +14,4 @@ | |
"author_id": "jeanmarcus" | ||
} | ||
} | ||
] | ||
] |
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
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
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,39 @@ | ||
[ | ||
{ | ||
"model": "authentication.user", | ||
"pk": 1, | ||
"fields": { | ||
"username": "sanya", | ||
"email": "sanyaken@gmail.com", | ||
"password": "pbkdf2_sha256$120000$Rf2yxsEFXOI8$095XPdmnuheMIofQPVnlMftNwb/gg2BB67VIJgKljuc=", | ||
"created_at": "2013-03-23 00:03:00", | ||
"updated_at": "2013-03-23 01:02:00" | ||
} | ||
}, | ||
|
||
{ | ||
"model": "authentication.user", | ||
"pk": 2, | ||
"fields": { | ||
"username": "kenned", | ||
"email": "kenned@gmail.com", | ||
"password": "pbkdf2_sha256$120000$FrOa2gzoWZ3m$v0T3Qk3hobNTxWrth6cHjNCREZtwWZiSDw357Hp3tbI=", | ||
"created_at": "2013-03-23 00:03:00", | ||
"updated_at": "2013-03-23 01:02:00" | ||
} | ||
}, | ||
{ | ||
"model": "authentication.user", | ||
"pk": 3, | ||
"fields": { | ||
"username": "adminuser", | ||
"email": "admin@gmail.com", | ||
"password": "pbkdf2_sha256$120000$Xh4W8kGbUrgM$f9UVM8xwSI+pEYNdhb6L/g4IEGUJQEJ2d1SARabmhKo=", | ||
"created_at": "2013-03-23 00:03:00", | ||
"updated_at": "2013-03-23 01:02:00", | ||
"is_active": true, | ||
"is_superuser": true | ||
} | ||
} | ||
|
||
] |
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,33 @@ | ||
# Rest framework import | ||
from rest_framework.test import APIClient, APITestCase | ||
from django.contrib.auth import get_user_model | ||
from django.urls import reverse | ||
# Local import | ||
from .test_data import TestData | ||
|
||
|
||
class BaseTestCase(APITestCase): | ||
""" | ||
Base Test class for out tests in this app | ||
Class will also house the setup and teardown | ||
methods for our tests | ||
""" | ||
# Initialize fixture for the class Test Case | ||
fixtures = ['authors/apps/authentication/fixtures/signup.json'] | ||
|
||
def setUp(self): | ||
# Initialize the Testclient for the tests | ||
self.client = APIClient() | ||
self.data = TestData() | ||
User = get_user_model() | ||
self.user = User.objects.create_user( | ||
username='test1', email='test1@example.com', password='12345678' | ||
) | ||
self.login_url = reverse('user_login') | ||
self.admin = User.objects.get(email="admin@gmail.com") | ||
setattr(self.admin, 'email_verified', True) | ||
self.admin.save() | ||
self.admin_login_response = self.client.post( | ||
self.login_url, self.data.login_data_admin, format='json') | ||
admin_test_token = self.admin_login_response.data['token'] | ||
self.auth_header = 'Bearer {}'.format(admin_test_token) |
86 changes: 86 additions & 0 deletions
86
authors/apps/authentication/tests/test_account_activation.py
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,86 @@ | ||
# Django and restframework imports | ||
from django.urls import reverse | ||
from django.contrib.auth import get_user_model | ||
from django.utils.http import urlsafe_base64_encode | ||
from django.utils.encoding import force_bytes | ||
from rest_framework import status | ||
# Local imports | ||
from .base import BaseTestCase | ||
|
||
|
||
class UserVerificationTestCase(BaseTestCase): | ||
"""User Verification TestCase.""" | ||
def test_create_user(self): | ||
"""Test create user.""" | ||
self.assertEqual(self.user.username, 'test1') | ||
self.assertTrue(self.user.is_active) | ||
self.assertFalse(self.user.is_staff) | ||
self.assertFalse(self.user.is_superuser) | ||
|
||
def test_return_full_name(self): | ||
"""Test return get full name.""" | ||
self.assertTrue(self.user.get_full_name) | ||
|
||
def test_return_short_name(self): | ||
"""Test return get short name.""" | ||
self.assertEqual(self.user.get_short_name(), 'test1') | ||
|
||
def test_create_superuser(self): | ||
"""Test create superuser.""" | ||
User = get_user_model() | ||
user = User.objects.create_superuser( | ||
username='admin', email='admin@example.com', password='12345678' | ||
) | ||
self.assertEqual(user.username, 'admin') | ||
self.assertTrue(user.is_active) | ||
self.assertTrue(user.is_staff) | ||
self.assertTrue(user.is_superuser) | ||
|
||
def test_activation_link(self): | ||
"""Test user can get activation link to activate account.""" | ||
User = get_user_model() | ||
user2 = User.objects.create_user( | ||
username='test3', email='test3@example.com', password='12345678' | ||
) | ||
uid = user2.username | ||
kwargs = { | ||
"uid": urlsafe_base64_encode(force_bytes(uid)).decode('utf-8') | ||
} | ||
activation_url = reverse('activation_link', | ||
kwargs=kwargs) | ||
response = self.client.get(activation_url, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_activation_link_invalid(self): | ||
"""Test user registration activation link is invalid.""" | ||
User = get_user_model() | ||
user3 = User.objects.create_user( | ||
username='test3', email='test3@example.com', password='12345678' | ||
) | ||
uid = user3.id | ||
kwargs = { | ||
"uid": urlsafe_base64_encode(force_bytes(uid)).decode('utf-8') | ||
} | ||
activation_url = reverse('activation_link', | ||
kwargs=kwargs) | ||
response = self.client.get(activation_url, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertTrue(user3) | ||
|
||
def test_activation_link_user_not_found(self): | ||
""" | ||
Method returns an error if the user is not found | ||
when they attempt to activate an account | ||
""" | ||
User = get_user_model() | ||
user3 = User.objects.create_user( | ||
username='test3', email='test3@example.com', password='12345678' | ||
) | ||
uid = user3.id | ||
kwargs = { | ||
"uid": urlsafe_base64_encode(force_bytes(uid)).decode('utf-8') | ||
} | ||
activation_url = reverse('activation_link', | ||
kwargs=kwargs) | ||
response = self.client.get(activation_url, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
Oops, something went wrong.