Skip to content

Commit

Permalink
Update unit tests to reflect changes and add tests for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
UserMarekDrag committed Oct 15, 2023
1 parent e23daab commit cb126f9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
20 changes: 11 additions & 9 deletions user_api/tests/tests_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ def setUp(self):
self.user = AppUser.objects.create_user(
email='testuser@example.com',
username='testuser',
password='password123'
password='password123',
is_active=True
)
self.user_serializer = UserSerializer(instance=self.user)

def test_user_serializer(self):
"""
Test the UserSerializer.
"""
data = self.user_serializer.data
self.assertEqual(set(data.keys()), set(['email', 'username']))
self.assertEqual(data['email'], self.user.email)
self.assertEqual(data['username'], self.user.username)
serialized_user = self.user_serializer.data
self.assertEqual(serialized_user['email'], self.user.email)
self.assertEqual(serialized_user['username'], self.user.username)


class UserRegisterSerializerTestCase(TestCase):
Expand All @@ -51,8 +51,8 @@ def test_user_register_serializer(self):
"""
self.assertTrue(self.serializer.is_valid(), self.serializer.errors)
user = self.serializer.save()
self.assertEqual(user['email'], self.user_data['email'])
self.assertEqual(user['username'], self.user_data['username'])
self.assertEqual(user.email, self.user_data['email'])
self.assertEqual(user.username, self.user_data['username'])


class UserLoginSerializerTestCase(TestCase):
Expand All @@ -66,7 +66,8 @@ def setUp(self):
self.user = AppUser.objects.create_user(
email='testuser3@example.com',
username='testuser3',
password='password123'
password='password123',
is_active=True
)
self.login_data = {
'email': 'testuser3@example.com',
Expand Down Expand Up @@ -95,7 +96,8 @@ def setUp(self):
self.user = AppUser.objects.create_user(
email='testuser4@example.com',
username='testuser4',
password='password123'
password='password123',
is_active=True
)
self.factory = RequestFactory()
self.context = {
Expand Down
28 changes: 28 additions & 0 deletions user_api/tests/tests_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import uuid
from unittest.mock import patch
from django.test import TestCase
from decouple import config
from user_api.utils import generate_verification_url


class UtilsTestCase(TestCase):
"""
Test case for generate varification url.
"""

def test_generate_verification_url(self):
"""
Test generate varification url.
"""
# Given
token = uuid.uuid4()
test_base_url = config('FRONTEND_BASE_URL')

# Mock the config method to return our test_base_url for 'FRONTEND_BASE_URL'
with patch('user_api.utils.config', return_value=test_base_url):
# When
verification_url = generate_verification_url(token)

# Then
expected_url = f"{test_base_url}/verify-email/{token}/"
self.assertEqual(verification_url, expected_url)
35 changes: 30 additions & 5 deletions user_api/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ def setUp(self):
Set up the test by creating an instance of the APIClient and a user instance.
"""
self.client = APIClient()
self.user = User.objects.create_user(username='test', email='test@example.com', password='test1234')
self.user = User.objects.create_user(
username='test',
email='test@example.com',
password='test1234',
is_active=True
)

def test_user_login_view(self):
"""
Expand All @@ -61,7 +66,12 @@ def setUp(self):
Set up the test by creating an instance of the APIClient, a user instance, and authenticating the client.
"""
self.client = APIClient()
self.user = User.objects.create_user(username='test', email='test@example.com', password='test1234')
self.user = User.objects.create_user(
username='test',
email='test@example.com',
password='test1234',
is_active=True
)
self.token = Token.objects.create(user=self.user)
self.api_authentication()

Expand Down Expand Up @@ -89,7 +99,12 @@ def setUp(self):
Set up the test by creating an instance of the APIClient, a user instance, and authenticating the client.
"""
self.client = APIClient()
self.user = User.objects.create_user(username='test', email='test@example.com', password='test1234')
self.user = User.objects.create_user(
username='test',
email='test@example.com',
password='test1234',
is_active=True
)
self.token = Token.objects.create(user=self.user)
self.api_authentication()

Expand Down Expand Up @@ -117,7 +132,12 @@ def setUp(self):
Set up the test by creating an instance of the APIClient, a user instance, and authenticating the client.
"""
self.client = APIClient()
self.user = User.objects.create_user(username='test', email='test@example.com', password='Test1234!')
self.user = User.objects.create_user(
username='test',
email='test@example.com',
password='Test1234!',
is_active=True
)
self.token = Token.objects.create(user=self.user)
self.api_authentication()

Expand Down Expand Up @@ -149,7 +169,12 @@ def setUp(self):
Set up the test by creating an instance of the APIClient, a user instance, and authenticating the client.
"""
self.client = APIClient()
self.user = User.objects.create_user(username='test', email='test@example.com', password='Test1234!')
self.user = User.objects.create_user(
username='test',
email='test@example.com',
password='Test1234!',
is_active=True
)
self.token = Token.objects.create(user=self.user)
self.api_authentication()

Expand Down

0 comments on commit cb126f9

Please sign in to comment.