-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_validation.py
More file actions
322 lines (294 loc) · 9.22 KB
/
test_validation.py
File metadata and controls
322 lines (294 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import parameterized
import rest_framework.status
import rest_framework.test
import user.models
import user.tests.auth.base
class InvalidUserRegistrationTestCase(
user.tests.auth.base.BaseUserAuthTestCase,
):
def test_email_duplication(self):
valid_data = {
'name': 'Emma',
'surname': 'Thompson',
'email': 'emmat1@gmail.com',
'password': 'SuperStrongPassword2000!',
'other': {'age': 23, 'country': 'us'},
}
response = self.client.post(
self.signup_url,
valid_data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_200_OK,
)
duplicate_data = {
'name': 'Lui',
'surname': 'Jomalone',
'email': 'emmat1@gmail.com',
'password': 'SuperStrongPassword2000!',
'other': {'age': 14, 'country': 'fr'},
}
response = self.client.post(
self.signup_url,
duplicate_data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_409_CONFLICT,
)
@parameterized.parameterized.expand(
[
('common_phrase', 'whereismymoney777'),
('missing_special_char', 'fioejifojfieoAAAA9299'),
('too_short', 'Aa7$b!'),
('missing_uppercase', 'lowercase123$'),
('missing_lowercase', 'UPPERCASE123$'),
('missing_digits', 'PasswordSpecial$'),
('non_ascii', 'Päss123$!AAd'),
('emoji', '😎werY!!*Dj3sd'),
],
)
def test_weak_password_cases(self, case_name, password):
data = {
'name': 'Emma',
'surname': 'Thompson',
'email': f'test.user+{case_name}@example.com',
'password': password,
'other': {'age': 23, 'country': 'us'},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
f'Failed for case: {case_name}. Response: {response.data}',
)
def generate_test_cases():
invalid_urls = [
'itsnotalink',
'itsnotalinkjpeg',
'https://',
'grpc://',
'',
]
return [
(f'url_{i}', url, f'{i}dota.for.fan@gmail.com')
for i, url in enumerate(invalid_urls)
]
@parameterized.parameterized.expand(generate_test_cases())
def test_invalid_avatar_urls(self, name, url, email):
data = {
'name': 'Emma',
'surname': 'Thompson',
'email': email,
'password': 'SuperStrongPassword2000!',
'avatar_url': url,
'other': {'age': 23, 'country': 'us'},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
msg=f'Failed for URL: {url}',
)
def test_missing_country_field(self):
data = {
'name': 'Emma',
'surname': 'Thompson',
'email': 'dota.for.fan@gmail.com',
'password': 'SuperStrongPassword2000!',
'other': {'age': 23},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
)
def test_invalid_country_code(self):
invalid_data = {
'name': 'Emma',
'surname': 'Thompson',
'email': 'test.invalid.country@example.com',
'password': 'SuperStrongPassword2000!',
'other': {
'age': 23,
'country': 'XX',
},
}
response = self.client.post(
self.signup_url,
invalid_data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
'Invalid country code should trigger validation error',
)
def test_invalid_age_type(self):
data = {
'name': 'Emma',
'surname': 'Thompson',
'email': 'dota.for.fan@gmail.com',
'password': 'SuperStrongPassword2000!',
'other': {'age': '23aaaaaa', 'country': 'us'},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
)
def test_missing_age_field(self):
data = {
'name': 'Emma',
'surname': 'Thompson',
'email': 'dota.for.fan@gmail.com',
'password': 'SuperStrongPassword2000!',
'other': {'country': 'us'},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
)
def test_negative_age_value(self):
data = {
'name': 'Emma',
'surname': 'Thompson',
'email': 'dota.for.fan@gmail.com',
'password': 'SuperStrongPassword2000!',
'other': {'age': -20, 'country': 'us'},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
)
@parameterized.parameterized.expand(
[
('empty_domain', '@'),
('no_at_symbol', 'dota'),
('missing_username', '@gmail.com'),
('missing_domain_part', 'gmail.com'),
],
)
def test_invalid_email_formats(self, name, email):
data = {
'name': 'Emma',
'surname': 'Thompson',
'email': email,
'password': 'SuperStrongPassword2000!',
'other': {'age': 23, 'country': 'us'},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
msg=f'Failed for email: {email}',
)
def test_empty_name_field(self):
data = {
'name': '',
'surname': 'Thompson',
'email': 'gogogonow@gmail.com',
'password': 'SuperStrongPassword2000!',
'other': {'age': 23, 'country': 'us'},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
)
def test_empty_surname_field(self):
data = {
'name': 'Emma',
'surname': '',
'email': 'gogogonow@gmail.com',
'password': 'SuperStrongPassword2000!',
'other': {'age': 23, 'country': 'us'},
}
response = self.client.post(
self.signup_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
)
class InvalidUserAuthenticationTestCase(
user.tests.auth.base.BaseUserAuthTestCase,
):
@parameterized.parameterized.expand(
[
('missing_password', {'email': 'valid@example.com'}, 'password'),
('missing_email', {'password': 'any'}, 'email'),
('empty_data', {}, ['email', 'password']),
],
)
def test_missing_required_fields(self, case_name, data, expected_fields):
response = self.client.post(self.signin_url, data, format='json')
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_400_BAD_REQUEST,
)
if isinstance(expected_fields, list):
for field in expected_fields:
self.assertIn(field, response.data)
else:
self.assertIn(expected_fields, response.data)
def test_signin_invalid_password(self):
user.models.User.objects.create_user(
email='minecraft.digger@gmail.com',
name='Steve',
surname='Jobs',
password='SuperStrongPassword2000!',
other={'age': 23, 'country': 'gb'},
)
data = {
'email': 'minecraft.digger@gmail.com',
'password': 'SuperInvalidPassword2000!',
}
response = self.client.post(
self.signin_url,
data,
format='json',
)
self.assertEqual(
response.status_code,
rest_framework.status.HTTP_401_UNAUTHORIZED,
)