1
- import django .test
2
- import django .urls
3
1
import parameterized
4
2
import rest_framework .status
5
3
import rest_framework .test
6
4
7
5
import user .models
6
+ import user .tests .auth .base
8
7
9
8
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 ):
19
10
def test_email_duplication (self ):
20
11
valid_data = {
21
12
'name' : 'Emma' ,
@@ -25,7 +16,7 @@ def test_email_duplication(self):
25
16
'other' : {'age' : 23 , 'country' : 'us' },
26
17
}
27
18
response = self .client .post (
28
- django . urls . reverse ( 'api-user:sign-up' ) ,
19
+ self . signup_url ,
29
20
valid_data ,
30
21
format = 'json' ,
31
22
)
@@ -42,7 +33,7 @@ def test_email_duplication(self):
42
33
'other' : {'age' : 14 , 'country' : 'fr' },
43
34
}
44
35
response = self .client .post (
45
- django . urls . reverse ( 'api-user:sign-up' ) ,
36
+ self . signup_url ,
46
37
duplicate_data ,
47
38
format = 'json' ,
48
39
)
@@ -60,7 +51,7 @@ def test_invalid_email_format(self):
60
51
'other' : {'age' : 23 , 'country' : 'us' },
61
52
}
62
53
response = self .client .post (
63
- django . urls . reverse ( 'api-user:sign-up' ) ,
54
+ self . signup_url ,
64
55
data ,
65
56
format = 'json' ,
66
57
)
@@ -91,7 +82,7 @@ def test_weak_password_cases(self, case_name, password):
91
82
}
92
83
93
84
response = self .client .post (
94
- django . urls . reverse ( 'api-user:sign-up' ) ,
85
+ self . signup_url ,
95
86
data ,
96
87
format = 'json' ,
97
88
)
@@ -127,7 +118,7 @@ def test_invalid_avatar_urls(self, name, url, email):
127
118
}
128
119
129
120
response = self .client .post (
130
- django . urls . reverse ( 'api-user:sign-up' ) ,
121
+ self . signup_url ,
131
122
data ,
132
123
format = 'json' ,
133
124
)
@@ -147,7 +138,7 @@ def test_missing_country_field(self):
147
138
'other' : {'age' : 23 },
148
139
}
149
140
response = self .client .post (
150
- django . urls . reverse ( 'api-user:sign-up' ) ,
141
+ self . signup_url ,
151
142
data ,
152
143
format = 'json' ,
153
144
)
@@ -156,6 +147,30 @@ def test_missing_country_field(self):
156
147
rest_framework .status .HTTP_400_BAD_REQUEST ,
157
148
)
158
149
150
+ def test_invalid_country_code (self ):
151
+ invalid_data = {
152
+ 'name' : 'Emma' ,
153
+ 'surname' : 'Thompson' ,
154
+
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
+
159
174
def test_invalid_age_type (self ):
160
175
data = {
161
176
'name' : 'Emma' ,
@@ -165,7 +180,7 @@ def test_invalid_age_type(self):
165
180
'other' : {'age' : '23aaaaaa' , 'country' : 'us' },
166
181
}
167
182
response = self .client .post (
168
- django . urls . reverse ( 'api-user:sign-up' ) ,
183
+ self . signup_url ,
169
184
data ,
170
185
format = 'json' ,
171
186
)
@@ -183,7 +198,7 @@ def test_missing_age_field(self):
183
198
'other' : {'country' : 'us' },
184
199
}
185
200
response = self .client .post (
186
- django . urls . reverse ( 'api-user:sign-up' ) ,
201
+ self . signup_url ,
187
202
data ,
188
203
format = 'json' ,
189
204
)
@@ -201,7 +216,7 @@ def test_negative_age_value(self):
201
216
'other' : {'age' : - 20 , 'country' : 'us' },
202
217
}
203
218
response = self .client .post (
204
- django . urls . reverse ( 'api-user:sign-up' ) ,
219
+ self . signup_url ,
205
220
data ,
206
221
format = 'json' ,
207
222
)
@@ -228,7 +243,7 @@ def test_invalid_email_formats(self, name, email):
228
243
}
229
244
230
245
response = self .client .post (
231
- django . urls . reverse ( 'api-user:sign-up' ) ,
246
+ self . signup_url ,
232
247
data ,
233
248
format = 'json' ,
234
249
)
@@ -248,7 +263,7 @@ def test_empty_name_field(self):
248
263
'other' : {'age' : 23 , 'country' : 'us' },
249
264
}
250
265
response = self .client .post (
251
- django . urls . reverse ( 'api-user:sign-up' ) ,
266
+ self . signup_url ,
252
267
data ,
253
268
format = 'json' ,
254
269
)
@@ -267,7 +282,7 @@ def test_empty_surname_field(self):
267
282
'other' : {'age' : 23 , 'country' : 'us' },
268
283
}
269
284
response = self .client .post (
270
- django . urls . reverse ( 'api-user:sign-up' ) ,
285
+ self . signup_url ,
271
286
data ,
272
287
format = 'json' ,
273
288
)
@@ -277,16 +292,7 @@ def test_empty_surname_field(self):
277
292
)
278
293
279
294
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 ):
290
296
@parameterized .parameterized .expand (
291
297
[
292
298
('missing_password' , {'email' : '[email protected] ' }, 'password' ),
@@ -321,7 +327,7 @@ def test_signin_invalid_password(self):
321
327
'password' : 'SuperInvalidPassword2000!' ,
322
328
}
323
329
response = self .client .post (
324
- django . urls . reverse ( 'api-user:sign-in' ) ,
330
+ self . signin_url ,
325
331
data ,
326
332
format = 'json' ,
327
333
)
0 commit comments