Skip to content

Commit

Permalink
chore(Unit tests): unit tests for all functionality
Browse files Browse the repository at this point in the history
- Add unit tests for the user login endpoint
Add unit tests for the user signup endpoint

[Finishes #165273457]
  • Loading branch information
sanya-kenneth committed Apr 30, 2019
1 parent 5f8500a commit f4e1c7f
Show file tree
Hide file tree
Showing 14 changed files with 562 additions and 288 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__/
*.py[cod]
*$py.class
.env

# C extensions
*.so
Expand Down
2 changes: 1 addition & 1 deletion authors/apps/articles/fixtures/article.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
"author_id": "jeanmarcus"
}
}
]
]
3 changes: 1 addition & 2 deletions authors/apps/articles/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def setUp(self):
# Initialize the Testclient for the tests
self.client = APIClient()


self.login_url = reverse('authentication:user_login')
self.login_url = reverse('user_login')

self.user = User.objects.create_user(
username="test1", email="test1@gmail.com", password="password")
Expand Down
8 changes: 4 additions & 4 deletions authors/apps/articles/tests/test_article_crud.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Django and Rest framework imports
from django.urls import reverse
from rest_framework import status


# Local imports
from authors.apps.articles.models import Article
from .base import BaseTestCase
Expand Down Expand Up @@ -178,7 +176,8 @@ def test_delete_article(self):
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json")
article_id = response.data['id']
url = '/api/articles/{}/'.format(article_id)
response = self.client.delete(url, HTTP_AUTHORIZATION=self.auth_header, format="json")
response = self.client.delete(
url, HTTP_AUTHORIZATION=self.auth_header, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_delete_article_not_found(self):
Expand All @@ -189,5 +188,6 @@ def test_delete_article_not_found(self):
response = self.client.post(
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json")
url = '/api/articles/1000/'
response = self.client.delete(url, HTTP_AUTHORIZATION=self.auth_header, format="json")
response = self.client.delete(
url, HTTP_AUTHORIZATION=self.auth_header, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
39 changes: 39 additions & 0 deletions authors/apps/authentication/fixtures/signup.json
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
}
}

]
33 changes: 33 additions & 0 deletions authors/apps/authentication/tests/base.py
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 authors/apps/authentication/tests/test_account_activation.py
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)
Loading

0 comments on commit f4e1c7f

Please sign in to comment.