Skip to content

Commit 6d1a999

Browse files
Merge pull request #10 from RandomProgramm3r/develop
test: Add tests for the business application. - Add tests for registration, authentication, data validation, and jwt tokens - Change some names in the user application tests
2 parents 3d9295d + 0b8b083 commit 6d1a999

File tree

12 files changed

+309
-28
lines changed

12 files changed

+309
-28
lines changed

promo_code/business/tests/__init__.py

Whitespace-only changes.

promo_code/business/tests/auth/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import business.models
2+
import django.urls
3+
import rest_framework
4+
import rest_framework.status
5+
import rest_framework.test
6+
7+
8+
class BaseBusinessAuthTestCase(rest_framework.test.APITestCase):
9+
@classmethod
10+
def setUpTestData(cls):
11+
super().setUpTestData()
12+
cls.client = rest_framework.test.APIClient()
13+
cls.signup_url = django.urls.reverse('api-business:company-sign-up')
14+
cls.signin_url = django.urls.reverse('api-business:company-sign-in')
15+
cls.protected_url = django.urls.reverse('api-core:protected')
16+
cls.valid_data = {
17+
'name': 'Digital Marketing Solutions Inc.',
18+
'email': 'testcompany@example.com',
19+
'password': 'SecurePass123!',
20+
}
21+
22+
def tearDown(self):
23+
business.models.Company.objects.all().delete()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import business.models
2+
import business.tests.auth.base
3+
import rest_framework.status
4+
import rest_framework.test
5+
6+
7+
class AuthenticationTests(business.tests.auth.base.BaseBusinessAuthTestCase):
8+
def test_signin_success(self):
9+
registration_data = {**self.valid_data, 'email': 'unique@company.com'}
10+
business.models.Company.objects.create_company(
11+
name=registration_data['name'],
12+
email=registration_data['email'],
13+
password=registration_data['password'],
14+
)
15+
self.assertTrue(
16+
business.models.Company.objects.filter(
17+
email=registration_data['email'],
18+
).exists(),
19+
)
20+
21+
response = self.client.post(
22+
self.signin_url,
23+
{
24+
'email': registration_data['email'],
25+
'password': registration_data['password'],
26+
},
27+
format='json',
28+
)
29+
self.assertEqual(
30+
response.status_code,
31+
rest_framework.status.HTTP_200_OK,
32+
)
33+
self.assertIn('access', response.data)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import business.models
2+
import business.tests.auth.base
3+
import rest_framework.status
4+
import rest_framework.test
5+
6+
7+
class TestCompanyRegistration(
8+
business.tests.auth.base.BaseBusinessAuthTestCase,
9+
):
10+
def test_registration_success(self):
11+
registration_data = {**self.valid_data, 'email': 'unique@company.com'}
12+
response = self.client.post(
13+
self.signup_url,
14+
registration_data,
15+
format='json',
16+
)
17+
self.assertTrue(
18+
business.models.Company.objects.filter(
19+
email=registration_data['email'],
20+
).exists(),
21+
)
22+
self.assertEqual(
23+
response.status_code,
24+
rest_framework.status.HTTP_200_OK,
25+
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import business.models
2+
import business.tests.auth.base
3+
import rest_framework.status
4+
import rest_framework.test
5+
6+
7+
class JWTTests(business.tests.auth.base.BaseBusinessAuthTestCase):
8+
def setUp(self):
9+
super().setUp()
10+
business.models.Company.objects.create_company(
11+
name='Digital Marketing Solutions Inc.',
12+
email='testcompany@example.com',
13+
password='SuperStrongPassword2000!',
14+
)
15+
16+
self.user_data = {
17+
'email': 'testcompany@example.com',
18+
'password': 'SuperStrongPassword2000!',
19+
}
20+
21+
def test_access_protected_view_with_valid_token(self):
22+
response = self.client.post(
23+
self.signin_url,
24+
self.user_data,
25+
format='json',
26+
)
27+
28+
token = response.data['access']
29+
30+
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
31+
response = self.client.get(self.protected_url)
32+
self.assertEqual(
33+
response.status_code,
34+
rest_framework.status.HTTP_200_OK,
35+
)
36+
self.assertEqual(response.data['status'], 'request was permitted')
37+
38+
def test_registration_token_invalid_after_login(self):
39+
data = {
40+
'email': 'test@example.com',
41+
'password': 'StrongPass123!cd',
42+
'name': 'Digital Marketing Solutions Inc.',
43+
}
44+
response = self.client.post(
45+
self.signup_url,
46+
data,
47+
format='json',
48+
)
49+
reg_access_token = response.data['access']
50+
51+
self.client.credentials(
52+
HTTP_AUTHORIZATION=f'Bearer {reg_access_token}',
53+
)
54+
response = self.client.get(self.protected_url)
55+
self.assertEqual(
56+
response.status_code,
57+
rest_framework.status.HTTP_200_OK,
58+
)
59+
60+
login_data = {'email': data['email'], 'password': data['password']}
61+
response = self.client.post(
62+
self.signin_url,
63+
login_data,
64+
format='json',
65+
)
66+
login_access_token = response.data['access']
67+
68+
self.client.credentials(
69+
HTTP_AUTHORIZATION=f'Bearer {reg_access_token}',
70+
)
71+
response = self.client.get(self.protected_url)
72+
self.assertEqual(
73+
response.status_code,
74+
rest_framework.status.HTTP_401_UNAUTHORIZED,
75+
)
76+
77+
self.client.credentials(
78+
HTTP_AUTHORIZATION=f'Bearer {login_access_token}',
79+
)
80+
response = self.client.get(self.protected_url)
81+
self.assertEqual(
82+
response.status_code,
83+
rest_framework.status.HTTP_200_OK,
84+
)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import business.models
2+
import business.tests.auth.base
3+
import parameterized
4+
import rest_framework.status
5+
import rest_framework.test
6+
7+
8+
class InvalidCompanyRegistrationTestCase(
9+
business.tests.auth.base.BaseBusinessAuthTestCase,
10+
):
11+
def test_duplicate_email_registration(self):
12+
business.models.Company.objects.create_company(
13+
name=self.valid_data['name'],
14+
email=self.valid_data['email'],
15+
password=self.valid_data['password'],
16+
)
17+
self.assertTrue(
18+
business.models.Company.objects.filter(
19+
email=self.valid_data['email'],
20+
).exists(),
21+
)
22+
23+
response = self.client.post(
24+
self.signup_url,
25+
self.valid_data,
26+
format='json',
27+
)
28+
self.assertEqual(
29+
response.status_code,
30+
rest_framework.status.HTTP_409_CONFLICT,
31+
)
32+
33+
@parameterized.parameterized.expand(
34+
[
35+
('short_password_1', 'easypwd'),
36+
('short_password_2', 'Ar1@!$'),
37+
('no_digits', 'PasswordWithoutDigits'),
38+
('no_special_chars', 'PasswordWithoutSpecial1'),
39+
('common_phrase', 'whereismymoney777'),
40+
('missing_uppercase', 'lowercase123$'),
41+
('missing_lowercase', 'UPPERCASE123$'),
42+
('non_ascii', 'Päss123$!AAd'),
43+
('emoji', '😎werY!!*Dj3sd'),
44+
],
45+
)
46+
def test_invalid_password_cases(self, _, invalid_password):
47+
test_data = {**self.valid_data, 'password': invalid_password}
48+
response = self.client.post(self.signup_url, test_data, format='json')
49+
self.assertEqual(
50+
response.status_code,
51+
rest_framework.status.HTTP_400_BAD_REQUEST,
52+
f'Failed for password: {invalid_password}',
53+
)
54+
55+
@parameterized.parameterized.expand(
56+
[
57+
('domain_missing_dot', 'a@b'),
58+
('domain_missing_dot', 'test@dom'),
59+
('missing_local_part', '@domain.com'),
60+
('missing_at_symbol', 'missing.at.sign'),
61+
('multiple_at_symbols', 'double@@at.com'),
62+
],
63+
)
64+
def test_invalid_email_cases(self, _, invalid_email):
65+
test_data = {**self.valid_data, 'email': invalid_email}
66+
response = self.client.post(self.signup_url, test_data, format='json')
67+
self.assertEqual(
68+
response.status_code,
69+
rest_framework.status.HTTP_400_BAD_REQUEST,
70+
f'Failed for email: {invalid_email}',
71+
)
72+
73+
def test_short_company_name(self):
74+
test_data = {**self.valid_data, 'name': 'A'}
75+
response = self.client.post(self.signup_url, test_data, format='json')
76+
self.assertEqual(
77+
response.status_code,
78+
rest_framework.status.HTTP_400_BAD_REQUEST,
79+
)
80+
81+
82+
class InvalidCompanyAuthenticationTestCase(
83+
business.tests.auth.base.BaseBusinessAuthTestCase,
84+
):
85+
@parameterized.parameterized.expand(
86+
[
87+
('missing_password', {'email': 'valid@example.com'}, 'password'),
88+
('missing_email', {'password': 'any'}, 'email'),
89+
('empty_data', {}, ['email', 'password']),
90+
],
91+
)
92+
def test_missing_required_fields(self, case_name, data, expected_fields):
93+
response = self.client.post(self.signin_url, data, format='json')
94+
self.assertEqual(
95+
response.status_code,
96+
rest_framework.status.HTTP_400_BAD_REQUEST,
97+
)
98+
99+
def test_signin_invalid_password(self):
100+
business.models.Company.objects.create_company(
101+
email=self.valid_data['email'],
102+
name=self.valid_data['name'],
103+
password=self.valid_data['password'],
104+
)
105+
106+
data = {
107+
'email': self.valid_data['email'],
108+
'password': 'SuperInvalidPassword2000!',
109+
}
110+
response = self.client.post(
111+
self.signin_url,
112+
data,
113+
format='json',
114+
)
115+
self.assertEqual(
116+
response.status_code,
117+
rest_framework.status.HTTP_401_UNAUTHORIZED,
118+
)

promo_code/user/tests/auth/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import user.models
66

77

8-
class BaseAuthTestCase(rest_framework.test.APITestCase):
8+
class BaseUserAuthTestCase(rest_framework.test.APITestCase):
99
@classmethod
1010
def setUpTestData(cls):
1111
super().setUpTestData()

promo_code/user/tests/auth/test_authentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import user.tests.auth.base
66

77

8-
class AuthenticationTests(user.tests.auth.base.BaseAuthTestCase):
8+
class UserAuthenticationTests(user.tests.auth.base.BaseUserAuthTestCase):
99
def test_signin_success(self):
1010
user.models.User.objects.create_user(
1111
email='minecraft.digger@gmail.com',

promo_code/user/tests/auth/test_registration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import user.tests.auth.base
66

77

8-
class RegistrationTests(user.tests.auth.base.BaseAuthTestCase):
8+
class UserRegistrationTests(user.tests.auth.base.BaseUserAuthTestCase):
99
def test_registration_success(self):
1010
valid_data = {
1111
'name': 'Emma',

0 commit comments

Comments
 (0)