Skip to content

Commit 54b3ca4

Browse files
refactor(auth tests): simplify auth test cases using BaseAuthTestCase.
- Introduce BaseAuthTestCase to handle common setup/teardown logic: - Predefine authentication-related URLs (signup, signin, protected, refresh) - Clean up User, BlacklistedToken and OutstandingToken records after tests - Remove redundant setUp()/tearDown() methods in individual test classes - Replace hardcoded reverse() calls with inherited URL attributes - Improve test maintainability through centralized configuration This refactoring follows DRY principles and makes test code more focused on actual test scenarios rather than boilerplate setup.
1 parent dd53e18 commit 54b3ca4

File tree

5 files changed

+71
-92
lines changed

5 files changed

+71
-92
lines changed

promo_code/user/tests/auth/base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import django.urls
2+
import rest_framework.test
3+
import rest_framework_simplejwt.token_blacklist.models as tb_models
4+
5+
import user.models
6+
7+
8+
class BaseAuthTestCase(rest_framework.test.APITestCase):
9+
@classmethod
10+
def setUpTestData(cls):
11+
super().setUpTestData()
12+
cls.client = rest_framework.test.APIClient()
13+
cls.protected_url = django.urls.reverse('api-core:protected')
14+
cls.refresh_url = django.urls.reverse('api-user:token_refresh')
15+
cls.signup_url = django.urls.reverse('api-user:sign-up')
16+
cls.signin_url = django.urls.reverse('api-user:sign-in')
17+
18+
def tearDown(self):
19+
user.models.User.objects.all().delete()
20+
tb_models.BlacklistedToken.objects.all().delete()
21+
tb_models.OutstandingToken.objects.all().delete()
22+
super().tearDown()

promo_code/user/tests/auth/test_authentication.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,10 @@
44
import rest_framework.test
55

66
import user.models
7+
import user.tests.auth.base
78

89

9-
class AuthenticationTests(rest_framework.test.APITestCase):
10-
def setUp(self):
11-
self.client = rest_framework.test.APIClient()
12-
super().setUp()
13-
14-
def tearDown(self):
15-
user.models.User.objects.all().delete()
16-
super().tearDown()
17-
18-
def test_valid_registration(self):
19-
data = {
20-
'name': 'Steve',
21-
'surname': 'Jobs',
22-
'email': '[email protected]',
23-
'password': 'SuperStrongPassword2000!',
24-
'other': {'age': 23, 'country': 'gb'},
25-
}
26-
response = self.client.post(
27-
django.urls.reverse('api-user:sign-up'),
28-
data,
29-
format='json',
30-
)
31-
self.assertEqual(
32-
response.status_code,
33-
rest_framework.status.HTTP_200_OK,
34-
)
35-
self.assertIn('access', response.data)
36-
self.assertTrue(
37-
user.models.User.objects.filter(
38-
39-
).exists(),
40-
)
41-
10+
class AuthenticationTests(user.tests.auth.base.BaseAuthTestCase):
4211
def test_signin_success(self):
4312
user.models.User.objects.create_user(
4413

promo_code/user/tests/auth/test_registration.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import django.urls
21
import rest_framework.status
32
import rest_framework.test
43

54
import user.models
5+
import user.tests.auth.base
66

77

8-
class RegistrationTests(rest_framework.test.APITestCase):
9-
def setUp(self):
10-
self.client = rest_framework.test.APIClient()
11-
super().setUp()
12-
13-
def tearDown(self):
14-
user.models.User.objects.all().delete()
15-
super().tearDown()
16-
8+
class RegistrationTests(user.tests.auth.base.BaseAuthTestCase):
179
def test_registration_success(self):
1810
valid_data = {
1911
'name': 'Emma',
@@ -23,7 +15,7 @@ def test_registration_success(self):
2315
'other': {'age': 23, 'country': 'us'},
2416
}
2517
response = self.client.post(
26-
django.urls.reverse('api-user:sign-up'),
18+
self.signup_url,
2719
valid_data,
2820
format='json',
2921
)

promo_code/user/tests/auth/test_tokens.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
1-
import django.test
2-
import django.urls
31
import rest_framework.status
42
import rest_framework.test
53
import rest_framework_simplejwt.token_blacklist.models as tb_models
64

75
import user.models
6+
import user.tests.auth.base
87

98

10-
class JWTTests(rest_framework.test.APITestCase):
9+
class JWTTests(user.tests.auth.base.BaseAuthTestCase):
1110
def setUp(self):
12-
self.signup_url = django.urls.reverse('api-user:sign-up')
13-
self.signin_url = django.urls.reverse('api-user:sign-in')
14-
self.protected_url = django.urls.reverse('api-core:protected')
15-
self.refresh_url = django.urls.reverse('api-user:token_refresh')
11+
super().setUp()
1612
user.models.User.objects.create_user(
1713
name='John',
1814
surname='Doe',
1915
2016
password='SuperStrongPassword2000!',
2117
other={'age': 25, 'country': 'us'},
2218
)
19+
2320
self.user_data = {
2421
'email': '[email protected]',
2522
'password': 'SuperStrongPassword2000!',
2623
}
2724

28-
super(JWTTests, self).setUp()
29-
30-
def tearDown(self):
31-
user.models.User.objects.all().delete()
32-
33-
super(JWTTests, self).tearDown()
34-
3525
def test_access_protected_view_with_valid_token(self):
3626
response = self.client.post(
3727
self.signin_url,

promo_code/user/tests/auth/test_validation.py

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
import django.test
2-
import django.urls
31
import parameterized
42
import rest_framework.status
53
import rest_framework.test
64

75
import user.models
6+
import user.tests.auth.base
87

98

10-
class RegistrationTestCase(rest_framework.test.APITestCase):
11-
def setUp(self):
12-
self.client = rest_framework.test.APIClient()
13-
super().setUp()
14-
15-
def tearDown(self):
16-
user.models.User.objects.all().delete()
17-
super().tearDown()
18-
9+
class RegistrationTestCase(user.tests.auth.base.BaseAuthTestCase):
1910
def test_email_duplication(self):
2011
valid_data = {
2112
'name': 'Emma',
@@ -25,7 +16,7 @@ def test_email_duplication(self):
2516
'other': {'age': 23, 'country': 'us'},
2617
}
2718
response = self.client.post(
28-
django.urls.reverse('api-user:sign-up'),
19+
self.signup_url,
2920
valid_data,
3021
format='json',
3122
)
@@ -42,7 +33,7 @@ def test_email_duplication(self):
4233
'other': {'age': 14, 'country': 'fr'},
4334
}
4435
response = self.client.post(
45-
django.urls.reverse('api-user:sign-up'),
36+
self.signup_url,
4637
duplicate_data,
4738
format='json',
4839
)
@@ -60,7 +51,7 @@ def test_invalid_email_format(self):
6051
'other': {'age': 23, 'country': 'us'},
6152
}
6253
response = self.client.post(
63-
django.urls.reverse('api-user:sign-up'),
54+
self.signup_url,
6455
data,
6556
format='json',
6657
)
@@ -91,7 +82,7 @@ def test_weak_password_cases(self, case_name, password):
9182
}
9283

9384
response = self.client.post(
94-
django.urls.reverse('api-user:sign-up'),
85+
self.signup_url,
9586
data,
9687
format='json',
9788
)
@@ -127,7 +118,7 @@ def test_invalid_avatar_urls(self, name, url, email):
127118
}
128119

129120
response = self.client.post(
130-
django.urls.reverse('api-user:sign-up'),
121+
self.signup_url,
131122
data,
132123
format='json',
133124
)
@@ -147,7 +138,7 @@ def test_missing_country_field(self):
147138
'other': {'age': 23},
148139
}
149140
response = self.client.post(
150-
django.urls.reverse('api-user:sign-up'),
141+
self.signup_url,
151142
data,
152143
format='json',
153144
)
@@ -156,6 +147,30 @@ def test_missing_country_field(self):
156147
rest_framework.status.HTTP_400_BAD_REQUEST,
157148
)
158149

150+
def test_invalid_country_code(self):
151+
invalid_data = {
152+
'name': 'Emma',
153+
'surname': 'Thompson',
154+
'email': '[email protected]',
155+
'password': 'SuperStrongPassword2000!',
156+
'other': {
157+
'age': 23,
158+
'country': 'XX',
159+
},
160+
}
161+
162+
response = self.client.post(
163+
self.signup_url,
164+
invalid_data,
165+
format='json',
166+
)
167+
168+
self.assertEqual(
169+
response.status_code,
170+
rest_framework.status.HTTP_400_BAD_REQUEST,
171+
'Invalid country code should trigger validation error',
172+
)
173+
159174
def test_invalid_age_type(self):
160175
data = {
161176
'name': 'Emma',
@@ -165,7 +180,7 @@ def test_invalid_age_type(self):
165180
'other': {'age': '23aaaaaa', 'country': 'us'},
166181
}
167182
response = self.client.post(
168-
django.urls.reverse('api-user:sign-up'),
183+
self.signup_url,
169184
data,
170185
format='json',
171186
)
@@ -183,7 +198,7 @@ def test_missing_age_field(self):
183198
'other': {'country': 'us'},
184199
}
185200
response = self.client.post(
186-
django.urls.reverse('api-user:sign-up'),
201+
self.signup_url,
187202
data,
188203
format='json',
189204
)
@@ -201,7 +216,7 @@ def test_negative_age_value(self):
201216
'other': {'age': -20, 'country': 'us'},
202217
}
203218
response = self.client.post(
204-
django.urls.reverse('api-user:sign-up'),
219+
self.signup_url,
205220
data,
206221
format='json',
207222
)
@@ -228,7 +243,7 @@ def test_invalid_email_formats(self, name, email):
228243
}
229244

230245
response = self.client.post(
231-
django.urls.reverse('api-user:sign-up'),
246+
self.signup_url,
232247
data,
233248
format='json',
234249
)
@@ -248,7 +263,7 @@ def test_empty_name_field(self):
248263
'other': {'age': 23, 'country': 'us'},
249264
}
250265
response = self.client.post(
251-
django.urls.reverse('api-user:sign-up'),
266+
self.signup_url,
252267
data,
253268
format='json',
254269
)
@@ -267,7 +282,7 @@ def test_empty_surname_field(self):
267282
'other': {'age': 23, 'country': 'us'},
268283
}
269284
response = self.client.post(
270-
django.urls.reverse('api-user:sign-up'),
285+
self.signup_url,
271286
data,
272287
format='json',
273288
)
@@ -277,16 +292,7 @@ def test_empty_surname_field(self):
277292
)
278293

279294

280-
class AuthenticationTestCase(rest_framework.test.APITestCase):
281-
def setUp(self):
282-
self.client = rest_framework.test.APIClient()
283-
self.signin_url = django.urls.reverse('api-user:sign-in')
284-
super().setUp()
285-
286-
def tearDown(self):
287-
user.models.User.objects.all().delete()
288-
super().tearDown()
289-
295+
class AuthenticationTestCase(user.tests.auth.base.BaseAuthTestCase):
290296
@parameterized.parameterized.expand(
291297
[
292298
('missing_password', {'email': '[email protected]'}, 'password'),
@@ -321,7 +327,7 @@ def test_signin_invalid_password(self):
321327
'password': 'SuperInvalidPassword2000!',
322328
}
323329
response = self.client.post(
324-
django.urls.reverse('api-user:sign-in'),
330+
self.signin_url,
325331
data,
326332
format='json',
327333
)

0 commit comments

Comments
 (0)