Skip to content

Commit

Permalink
test(authentication app): Write unit tests for the remaining current …
Browse files Browse the repository at this point in the history
…functionality

   - test authentication model exceptions
   - test creation of a superuser
   - test login serialiser exceptions
  • Loading branch information
Colline Waitire authored and Colline Waitire committed Sep 4, 2018
1 parent 7f2ef9e commit 848ca6f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
5 changes: 4 additions & 1 deletion authors/apps/authentication/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This module contains data used by other test modules
"""


class BaseTest():
"""
This class contains data required for testing by test classes.
Expand All @@ -11,6 +12,9 @@ def __init__(self):
self.user_name = "iroq"
self.user_email = "iroq@sims.andela"
self.password = "teamiroq"
self.superuser_name = "iroquois"
self.superuser_email = "iroq@sims1.andela"
self.superuserpassword = "teamiroq"
self.user_data = {"user": {"username": self.user_name, "email": self.user_email,
"password": self.password,
}
Expand All @@ -26,4 +30,3 @@ def __init__(self):
"password": self.password,
}
}

13 changes: 13 additions & 0 deletions authors/apps/authentication/tests/test_login_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ def test_invalid_registration(self):
format="json")
self.assertEqual(self.response.status_code,
status.HTTP_400_BAD_REQUEST)

def test_wrong_email_on_login(self):
"""Test an error is returned when missing an email address in login credentials."""
self.response = self.client.post(
"/api/users/login/",
{"user": {
"email": 'kake@gmail.com',
"password": "fakemail",
}
},
format="json")
self.assertEqual('A user with this email and password was not found.',
self.response.json()['errors']['error'][0])
39 changes: 38 additions & 1 deletion authors/apps/authentication/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,51 @@ def setUp(self):
"""Define the test client and other test variables."""
BaseTest.__init__(self)
self.user = User.objects.create_user(self.user_name, self.user_email)
self.superuser = User.objects.create_superuser(
self.superuser_name, self.superuser_email, self.superuserpassword)

def test_model_can_create_a_user(self):
"""Test the user model can create a user."""
self.assertEqual(self.user.username, "iroq")
self.assertEqual(self.user.email, "iroq@sims.andela")
self.assertEqual(1, User.objects.count())
self.assertEqual(self.user_email, str(
User.objects.get(username="iroq")))

def test_getting_short_name_full_name(self):
"""Test the return of a short name and a full name."""
self.assertEqual("iroq", self.user.get_full_name)
self.assertEqual("iroq", self.user.get_short_name())

def test_missing_username(self):
"""Test that an error is returned when a username is not provided."""

try:
User.objects.create_user(None, self.user.email)
except TypeError as error:
self.assertEqual(str(error), "Users must have a username.")

def test_missing_email_address(self):
"""Test that an error is return when an email address is not provided."""

try:
User.objects.create_user(self.user_name, None)
except TypeError as error:
self.assertEqual(str(error), "Users must have an email address.")

def test_superuser_missing_password(self):
"""
Test that an error is return when a password is missing on creating
a superuser.
"""

try:
User.objects.create_superuser(
self.user_name, self.user_email, None)
except TypeError as error:
self.assertEqual(str(error), "Superusers must have a password.")

def test_model_can_create_a_superuser(self):
"""Test the user model can create a user."""

self.assertEqual(self.superuser_email, str(
User.objects.get(username='iroquois')))

0 comments on commit 848ca6f

Please sign in to comment.