From cb126f963a87e8b4f00b7be5d5df72cf76886ab8 Mon Sep 17 00:00:00 2001 From: Marek Date: Mon, 16 Oct 2023 00:31:34 +0200 Subject: [PATCH] Update unit tests to reflect changes and add tests for utils --- user_api/tests/tests_serializers.py | 20 +++++++++-------- user_api/tests/tests_utils.py | 28 +++++++++++++++++++++++ user_api/tests/tests_views.py | 35 ++++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 user_api/tests/tests_utils.py diff --git a/user_api/tests/tests_serializers.py b/user_api/tests/tests_serializers.py index fbb24f7..58ea2ce 100644 --- a/user_api/tests/tests_serializers.py +++ b/user_api/tests/tests_serializers.py @@ -15,7 +15,8 @@ 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) @@ -23,10 +24,9 @@ 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): @@ -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): @@ -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', @@ -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 = { diff --git a/user_api/tests/tests_utils.py b/user_api/tests/tests_utils.py new file mode 100644 index 0000000..658fd1d --- /dev/null +++ b/user_api/tests/tests_utils.py @@ -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) diff --git a/user_api/tests/tests_views.py b/user_api/tests/tests_views.py index 50eb456..5f233b4 100644 --- a/user_api/tests/tests_views.py +++ b/user_api/tests/tests_views.py @@ -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): """ @@ -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() @@ -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() @@ -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() @@ -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()