-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(write tests): Write tests for existing functionalities
- Add tests for register. - Add tests for login. [starts #162949196]
- Loading branch information
1 parent
1e3a965
commit 5135e48
Showing
6 changed files
with
228 additions
and
2 deletions.
There are no files selected for viewing
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,83 @@ | ||
# 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@" | ||
}} |
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,61 @@ | ||
# 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.' | ||
) | ||
|
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,82 @@ | ||
# python and Django imports | ||
import json | ||
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) |
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