1- import django .test
2- import django .urls
31import parameterized
42import rest_framework .status
53import rest_framework .test
64
75import 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' : 'test.invalid.country@example.com' ,
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' : 'valid@example.com' }, '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