Skip to content

Commit

Permalink
chore(tests): write unit tests for implemented functionality
Browse files Browse the repository at this point in the history
- 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
MandelaK committed Feb 28, 2019
1 parent 9ad1d56 commit 3d883a1
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 8 deletions.
12 changes: 12 additions & 0 deletions .coveragerc
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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ install:
- pip install coverage

# command to run tests

script:
- python manage.py makemigrations --settings=authors.settings.test
- python manage.py migrate --settings=authors.settings.test
- coverage run --source=authors.apps ./manage.py test --settings=authors.settings.test
- coverage

Expand Down
Empty file.
53 changes: 53 additions & 0 deletions authors/apps/authentication/tests/test_models.py
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")
104 changes: 104 additions & 0 deletions authors/apps/authentication/tests/test_serializers.py
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)
74 changes: 74 additions & 0 deletions authors/apps/authentication/tests/test_views.py
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)
9 changes: 4 additions & 5 deletions authors/apps/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from .views import (LoginAPIView, RegistrationAPIView,
UserRetrieveUpdateAPIView)


app_name = "authentication"
app_name = 'authentication'

urlpatterns = [
path('user/', UserRetrieveUpdateAPIView.as_view()),
path('users/', RegistrationAPIView.as_view()),
path('users/login/', LoginAPIView.as_view()),
path('user/', UserRetrieveUpdateAPIView.as_view(), name='get users'),
path('users/', RegistrationAPIView.as_view(), name='register'),
path('users/login/', LoginAPIView.as_view(), name='login'),
]
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ djangorestframework==3.9.1
psycopg2-binary==2.7.7
PyJWT==1.4.2
python-decouple==3.1
pytz==2018.9
six==1.10.0

coverage==4.5.2

0 comments on commit 3d883a1

Please sign in to comment.