-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): write unit tests for implemented functionality
- write unit tests for models - write unit tests for views - write unit tests for serializers - install coverage to get coverage and create .coveragerc file to configure ignored files - add scripts for creating migrations on TravisCI database [finishes #164046234]
- Loading branch information
Showing
8 changed files
with
251 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[run] | ||
include = ./* | ||
omit = | ||
*migrations* | ||
*tests* | ||
manage.py | ||
*settings* | ||
|
||
branch = True | ||
|
||
[report] | ||
show_missing = 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
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,53 @@ | ||
from django.test import TestCase | ||
|
||
from authors.apps.authentication.models import User | ||
|
||
class UserManagerTest(TestCase): | ||
"""This class defines tests for UserManager class""" | ||
def test_successfully_create_user(self): | ||
"""""" | ||
user1 = User.objects.create_user(username="user1", email="user1@mail.com", password="password") | ||
self.assertIsInstance(user1, User) | ||
|
||
def test_registration_failure_due_to_no_username(self): | ||
"""Test if user can register without a username""" | ||
with self.assertRaises(TypeError) as e: | ||
User.objects.create_user(email="user1@mail.com", username=None, password="password") | ||
self.assertEqual(str(e.exception), 'Users must have a username.') | ||
|
||
|
||
def test_registration_failure_due_to_no_email(self): | ||
"""Test if a user can register without an email""" | ||
with self.assertRaises(TypeError) as e: | ||
User.objects.create_user(username="user2", email=None, password="password") | ||
self.assertEqual(str(e.exception), 'Users must have an email address.') | ||
|
||
def test_registration_superuser_fail_due_to_no_password(self): | ||
"""Test if a superuser can register without a password""" | ||
with self.assertRaises(TypeError) as e: | ||
User.objects.create_superuser(username="superuser", email="superuser@mail.com", password=None) | ||
self.assertEqual(str(e.exception), 'Superusers must have a password.') | ||
|
||
def test_successfully_create_superuser(self): | ||
"""Test if we can successfully create a superuser""" | ||
user1 = User.objects.create_superuser(username="superuser", email="superuser@mail.com", password="password") | ||
self.assertTrue(user1.is_superuser) | ||
self.assertTrue(user1.is_staff) | ||
|
||
|
||
class UserTest(TestCase): | ||
"""This class defines tests for user models""" | ||
def setUp(self): | ||
self.user1 = User.objects.create_user(username="user", email="user@mail.com", password="password") | ||
|
||
def test_user_string_representation(self): | ||
"""Test if proper string representation is returned for users""" | ||
self.assertEqual(str(self.user1), "user@mail.com") | ||
|
||
def test_get_full_name(self): | ||
"""Test if proper full name is returned for users""" | ||
self.assertEqual(self.user1.get_full_name, "user") | ||
|
||
def test_get_short_name(self): | ||
"""Test if proper short name is returned for users""" | ||
self.assertEqual(self.user1.get_short_name(), "user") |
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,104 @@ | ||
from django.test import TestCase | ||
|
||
from rest_framework.serializers import ValidationError | ||
|
||
from authors.apps.authentication.models import User | ||
from authors.apps.authentication.serializers import (LoginSerializer, | ||
RegistrationSerializer, | ||
UserSerializer) | ||
|
||
|
||
class RegistrationSerializerTests(TestCase): | ||
"""This class defines tests for the RegistrationSerializer class""" | ||
def setUp(self): | ||
self.user_data = { | ||
"username": "bob", | ||
"email": "bob@email.com", | ||
"password": "hardpassword" | ||
} | ||
|
||
def test_create_method(self): | ||
"""Test if the create method for the RegistrationSerializer works""" | ||
serializer = RegistrationSerializer(data=self.user_data) | ||
self.assertTrue(serializer.is_valid()) | ||
new_user = serializer.save() | ||
self.assertIsInstance(new_user, User) | ||
|
||
|
||
class LoginSerializerTests(TestCase): | ||
"""This class defines tests for the LoginSerializer class""" | ||
def test_validating_login_serializer_without_email(self): | ||
"""Test if users can login without providing an email""" | ||
login_data = {"password": "hardpassword"} | ||
serializer = LoginSerializer(data=login_data) | ||
|
||
with self.assertRaises(ValidationError) as e: | ||
serializer.validate(login_data) | ||
self.assertEqual(e.exception.detail[0], | ||
"An email address is required to log in.") | ||
|
||
def test_validating_login_serializer_without_password(self): | ||
"""Test if users can login without providing an email""" | ||
login_data = {"email": "bob@email.com"} | ||
serializer = LoginSerializer(data=login_data) | ||
|
||
with self.assertRaises(ValidationError) as e: | ||
serializer.validate(login_data) | ||
|
||
self.assertEqual(e.exception.detail[0], | ||
"A password is required to log in.") | ||
|
||
def test_validating_login_serializer_of_unexistant_user(self): | ||
"""Test if non-existant users are able to log in""" | ||
login_data = {"email": "bob@email.com", "password": "harpassword"} | ||
serializer = LoginSerializer(data=login_data) | ||
|
||
with self.assertRaises(ValidationError) as e: | ||
serializer.validate(login_data) | ||
|
||
self.assertEqual(e.exception.detail[0], | ||
"A user with this email and password was not found.") | ||
|
||
def test_creating_validating_correct_email_and_password(self): | ||
"""Test if the validate method works when valid information is passed""" | ||
login_data = {"email": "bob@email.com", "password": "hardpassword"} | ||
user = User.objects.create_user(username="bob", email="bob@email.com", | ||
password="hardpassword") | ||
user.save() | ||
serializer = LoginSerializer(data=login_data) | ||
|
||
returned_user_data = serializer.validate(login_data) | ||
self.assertEqual({ | ||
"email": "bob@email.com", | ||
"username": "bob"}, | ||
returned_user_data | ||
) | ||
|
||
|
||
class UserSerializersTests(TestCase): | ||
"""This class defines tests for the UserSerializer class""" | ||
|
||
def setUp(self): | ||
self.user = User.objects.create_user(username="bob", email="bob@email.com", | ||
password="hardpassword") | ||
self.user.save() | ||
self.serializer = UserSerializer() | ||
|
||
def test_updating_user_information_including_password(self): | ||
"""Test if we can update user information, including the password""" | ||
user_password = self.user.password | ||
user_update_data = {"username": "bobby", "email": "bobby@email.com", | ||
"password": "otherpassword"} | ||
updated_user = self.serializer.update(self.user, user_update_data) | ||
self.assertEqual(updated_user.username, "bobby") | ||
self.assertEqual(updated_user.email, "bobby@email.com") | ||
self.assertNotEqual(updated_user.password, user_password) | ||
|
||
def test_updating_user_information_excluding_password(self): | ||
"""Test if we can update user information but the password is not changed""" | ||
user_data = {"username": "robert", "email": "robert@email.com"} | ||
user_password = self.user.password | ||
updated_user = self.serializer.update(self.user, user_data) | ||
self.assertEqual(updated_user.username, "robert") | ||
self.assertEqual(updated_user.email, "robert@email.com") | ||
self.assertEqual(updated_user.password, user_password) |
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,74 @@ | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from rest_framework import status | ||
from rest_framework.test import APIClient, force_authenticate | ||
|
||
class RegistrationViewTest(TestCase): | ||
"""This class defines tests for the RegistrationView class""" | ||
def setUp(self): | ||
self.user1 = {"user": | ||
{"email": "user1@mail.com", | ||
"username": "user1", | ||
"password": "password"} | ||
} | ||
|
||
def test_user_can_register(self): | ||
"""Test if a user is able to register""" | ||
client = APIClient() | ||
response = client.post(reverse('authentication:register'), self.user1, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
class LoginViewTest(TestCase): | ||
"""This class defines tests for the LoginView class""" | ||
def setUp(self): | ||
self.user1 = {"user": | ||
{"email": "user1@mail.com", | ||
"username": "user1", | ||
"password": "password"} | ||
} | ||
self.user1_credentials = {"user": { | ||
"email": "user1@mail.com", | ||
"password": "password" | ||
}} | ||
|
||
def test_registered_user_can_log_in(self): | ||
"""Test if a registered user can log in""" | ||
client = APIClient() | ||
client.post(reverse('authentication:register'), self.user1, format='json') | ||
response = client.post(reverse('authentication:login'), self.user1_credentials, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
class UserRetrieveUpdateAPITest(TestCase): | ||
"""This class defines tests for the UserRetrieveUpdateAPIView""" | ||
def test_if_we_can_retrieve_user_list(self): | ||
"""Test if a logged in user can retrieve a user list""" | ||
user1 = {"user": | ||
{"email": "user1@mail.com", | ||
"username": "user1", | ||
"password": "password"} | ||
} | ||
client = APIClient() | ||
client.post(reverse('authentication:register'), user1, format='json') | ||
client.login(email="user1@mail.com", password="password") | ||
response = client.get(reverse('authentication:get users')) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_if_we_can_update_user_data(self): | ||
"""Test if we can update the user data""" | ||
user1 = {"user": | ||
{"email": "user1@mail.com", | ||
"username": "user1", | ||
"password": "password"} | ||
} | ||
|
||
update_info = {"user": { | ||
"email": "user1@mail.com", | ||
"bio": "I love to be tested", | ||
"values": "EPIC" | ||
}} | ||
client = APIClient() | ||
client.post(reverse('authentication:register'), user1, format='json') | ||
client.login(email="user1@mail.com", password="password") | ||
response = client.put(reverse('authentication:get users'), update_info, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) |
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