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 29, 2019
1 parent b4d59c2 commit b12a0e1
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 3 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
13 changes: 13 additions & 0 deletions authors/apps/authentication/fixtures/signup.json
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.
16 changes: 16 additions & 0 deletions authors/apps/authentication/tests/base.py
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()
66 changes: 66 additions & 0 deletions authors/apps/authentication/tests/test_data.py
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": ""
}
}
60 changes: 60 additions & 0 deletions authors/apps/authentication/tests/test_user_login.py
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'])
60 changes: 60 additions & 0 deletions authors/apps/authentication/tests/test_user_signup.py
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])
5 changes: 2 additions & 3 deletions authors/apps/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView
)

app_name = "authentication"

urlpatterns = [
path('user/', UserRetrieveUpdateAPIView.as_view()),
path('users/', RegistrationAPIView.as_view()),
path('users/login/', LoginAPIView.as_view()),
path('users/', RegistrationAPIView.as_view(), name="signup"),
path('users/login/', LoginAPIView.as_view(), name="login"),
]

0 comments on commit b12a0e1

Please sign in to comment.