Skip to content

Commit

Permalink
chore(write tests): Write tests for existing functionalities
Browse files Browse the repository at this point in the history
- Add tests for register.
- Add tests for login.

[starts #162949196]
  • Loading branch information
MbugwaSami committed Jan 14, 2019
1 parent 1e3a965 commit e578385
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 2 deletions.
Empty file.
82 changes: 82 additions & 0 deletions authors/apps/authentication/tests/base_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# django imports
from django.urls import reverse
from rest_framework.test import APITestCase, APIClient


class TestBase(APITestCase):
"""This class defines the basic setup for the tests
and the dummy data used for the tests.
"""

def setUp(self):
"""This method is used to initialize variables used by the tests."""

self.client = APIClient()
self.login_url = reverse('authentication:login_url')
self.user_url = reverse('authentication:signup_url')

self.user_data = {
"user": {
"username": "sam",
"email": "sam@gmail.com",
"password": "A23DVFRss@"
}}

self.test_username = {
"user": {
"username": "sam",
"email": "sam@gmail.com",
"password": "A23DVFRss@"
}}

self.test_username2 = {
"user": {
"username": "sam",
"email": "sly@gmail.com",
"password": "A23DVFRss@"
}}

self.empty_email = {
"user": {
"username": "sam",
"email": "",
"password": "A23DVFRss@"
}}

self.empty_password = {
"user": {
"username": "sam",
"email": "sami@gmail.com",
"password": ""
}}

self.empty_username = {
"user": {
"username": "",
"email": "sami@gmail.com",
"password": "904438283278"
}}

self.login_data = {
"user": {
"email": "sam@gmail.com",
"password": "A23DVFRss@"
}}

self.test_wrong_login = {
"user": {
"email": "sammy@gmail.com",
"password": "A23DVFRss@"
}}

self.test_empty_email = {
"user": {
"email": "",
"password": "A23@"
}}

self.test_login_unregistered = {
"user": {
"email": "sammy@gmail.com",
"password": "A23@"
}}
60 changes: 60 additions & 0 deletions authors/apps/authentication/tests/test_login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# python and django imports
import json
from rest_framework.views import status

# local imports
from .base_test import TestBase


class TestLogin(TestBase):
"""This class has tests for login test case."""

def test_login(self):
"""This is the test for login with correct login details."""

# register user
self.client.post(
self.user_url,
self.user_data,
format='json'
)
response = self.client.post(
self.login_url,
self.login_data,
format="json"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_wrong_login_details(self):
"""This is the test for login with wrong login details."""

# register user
self.client.post(
self.user_url,
self.user_data,
format='json'
)
response = self.client.post(
self.login_url,
self.test_wrong_login,
format="json"
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
json.loads(response.content)['errors']['error'][0],
'A user with this email and password was not found.'
)

def test_empty_email_login(self):
"""This is the test for login with empty email."""

response = self.client.post(
self.login_url,
self.test_empty_email,
format="json"
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
json.loads(response.content)['errors']['email'][0],
'This field may not be blank.'
)
81 changes: 81 additions & 0 deletions authors/apps/authentication/tests/test_register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# python and Django imports
from rest_framework.views import status

# local imports
from .base_test import TestBase


class TestRegister(TestBase):
"""This class has tests for user registration test case"""

def test_register_user(self):
"""This is the test for register with correct data."""

response = self.client.post(
self.user_url,
self.user_data,
format='json'
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_register_duplicate_email(self):
"""This is the test for register with duplicate email."""

# register user
self.client.post(
self.user_url,
self.user_data,
format='json'
)
response = self.client.post(
self.user_url,
self.user_data,
format='json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_register_duplicate_username(self):
"""This is the test for register with duplicate username."""

# register user
self.client.post(
self.user_url,
self.test_username,
format='json'
)
response = self.client.post(
self.user_url,
self.test_username2,
format='json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_register_empty_email(self):
"""This is the test for register with empty email."""

response = self.client.post(
self.user_url,
self.empty_email,
format='json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_register_empty_password(self):
"""This is the test for register with empty password."""

response = self.client.post(
self.user_url,
self.empty_password,
format='json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_register_empty_username(self):
"""This is the test for register with empty username."""

response = self.client.post(
self.user_url,
self.empty_password,
format='json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
4 changes: 2 additions & 2 deletions authors/apps/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

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

0 comments on commit e578385

Please sign in to comment.