-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_validators.py
399 lines (319 loc) · 14 KB
/
test_validators.py
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding: utf-8 -*-
'''Unit tests for ckan/logic/validators.py.
'''
import copy
import mock
import nose.tools
import ckan.new_tests.factories as factories
def returns_arg(message):
'''A decorator that tests that the decorated function returns the argument
that it is called with, unmodified.
:param message: the message that will be printed if the function doesn't
return the same argument that it was called with and the assert fails
:type message: string
Usage:
@returns_arg('user_name_validator() should return the same arg that '
'it is called with, when called with a valid arg')
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors)
'''
def decorator(function):
def call_and_assert(arg, context=None):
if context is None:
context = {}
result = function(arg, context=context)
assert result == arg, message
return result
return call_and_assert
return decorator
def returns_None(message):
'''A decorator that asserts that the decorated function returns None.
:param message: the message that will be printed if the function doesn't
return None and the assert fails
:type message: string
Usage:
@returns_None('user_name_validator() should return None when given '
'valid input')
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors)
'''
def decorator(function):
def call_and_assert(*args, **kwargs):
result = function(*args, **kwargs)
assert result is None, message
return result
return call_and_assert
return decorator
def raises_Invalid(function):
'''A decorator that asserts that the decorated function raises
dictization_functions.Invalid.
Usage:
@raises_Invalid
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors)
'''
def call_and_assert(*args, **kwargs):
import ckan.lib.navl.dictization_functions as df
nose.tools.assert_raises(df.Invalid, function, *args, **kwargs)
return call_and_assert
def does_not_modify_data_dict(message):
'''A decorator that asserts that the decorated validator doesn't modify
its `data` dict param.
:param message: the message that will be printed if the function does
modify the data dict and the assert fails
:type message: string
Usage:
@does_not_modify_data_dict('user_name_validator() should not modify '
'the data dict')
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors)
'''
def decorator(validator):
def call_and_assert(key, data, errors, context=None):
if context is None:
context = {}
# Make a copy of the data dict so we can assert against it later.
original_data_dict = copy.deepcopy(data)
result = validator(key, data, errors, context=context)
assert data == original_data_dict, message
return result
return call_and_assert
return decorator
def does_not_modify_errors_dict(message):
'''A decorator that asserts that the decorated validator doesn't modify its
`errors` dict param.
:param message: the message that will be printed if the function does
modify the errors dict and the assert fails
:type message: string
Usage:
@does_not_modify_errors_dict('user_name_validator() should not modify '
'the errors dict')
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors)
'''
def decorator(validator):
def call_and_assert(key, data, errors, context=None):
if context is None:
context = {}
# Make a copy of the errors dict so we can assert against it later.
original_errors_dict = copy.deepcopy(errors)
result = validator(key, data, errors, context=context)
assert errors == original_errors_dict, message
return result
return call_and_assert
return decorator
def does_not_modify_other_keys_in_errors_dict(message):
'''A decorator that asserts that the decorated validator doesn't add,
modify the value of, or remove any other keys from its `errors` dict param.
The function *may* modify its own errors `key`.
:param message: the message that will be printed if the function does
modify another key in the errors dict and the assert fails
:type message: string
Usage:
@does_not_modify_other_keys_in_errors_dict('user_name_validator() '
'should not modify other keys in the errors dict')
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors)
'''
def decorator(validator):
def call_and_assert(key, data, errors, context=None):
if context is None:
context = {}
# Make a copy of the errors dict so we can assert against it later.
original_errors_dict = copy.deepcopy(errors)
result = validator(key, data, errors, context=context)
# Copy the errors dict because we don't want to modify it.
errors = copy.deepcopy(errors)
errors[key] = []
assert errors == original_errors_dict, message
return result
return call_and_assert
return decorator
def adds_message_to_errors_dict(error_message, message):
'''A decorator that asserts the the decorated validator adds a given
error message to the `errors` dict.
:param error_message: the error message that the validator is expected to
add to the `errors` dict
:type error_message: string
:param message: the message that will be printed if the function doesn't
add the right error message to the errors dict, and the assert fails
:type message: string
Usage:
@adds_message_to_errors_dict('That login name is not available.',
'user_name_validator() should add to the errors dict when called '
'with a user name with already exists')
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors)
'''
def decorator(validator):
def call_and_assert(key, data, errors, context):
result = validator(key, data, errors, context)
assert errors[key] == [error_message], message
return result
return call_and_assert
return decorator
class TestValidators(object):
def test_name_validator_with_invalid_value(self):
'''If given an invalid value name_validator() should do raise Invalid.
'''
import ckan.logic.validators as validators
import ckan.model as model
invalid_values = [
# Non-string names aren't allowed as names.
13,
23.7,
100L,
1.0j,
None,
True,
False,
('a', 2, False),
[13, None, True],
{'foo': 'bar'},
lambda x: x**2,
# Certain reserved strings aren't allowed as names.
'new',
'edit',
'search',
# Strings < 2 characters long aren't allowed as names.
'',
'a',
'2',
# Strings > PACKAGE_NAME_MAX_LENGTH long aren't allowed as names.
'a' * (model.PACKAGE_NAME_MAX_LENGTH + 1),
# Strings containing non-ascii characters aren't allowed as names.
u"fred_❤%'\"Ußabc@fred.com",
# Strings containing upper-case characters aren't allowed as names.
'seanH',
# Strings containing spaces aren't allowed as names.
'sean h',
# Strings containing punctuation aren't allowed as names.
'seanh!',
]
for invalid_value in invalid_values:
@raises_Invalid
def call_validator(*args, **kwargs):
return validators.name_validator(*args, **kwargs)
call_validator(invalid_value, context={})
def test_name_validator_with_valid_value(self):
'''If given a valid string name_validator() should do nothing and
return the string.
'''
import ckan.logic.validators as validators
import ckan.model as model
valid_names = [
'fred',
'fred-flintstone',
'fred_flintstone',
'fred_flintstone-9',
'f' * model.PACKAGE_NAME_MAX_LENGTH,
'-' * model.PACKAGE_NAME_MAX_LENGTH,
'_' * model.PACKAGE_NAME_MAX_LENGTH,
'9' * model.PACKAGE_NAME_MAX_LENGTH,
'99',
'--',
'__',
u'fred-flintstone_9',
]
for valid_name in valid_names:
@returns_arg('If given a valid string name_validator() should '
'return the string unmodified')
def call_validator(*args, **kwargs):
return validators.name_validator(*args, **kwargs)
call_validator(valid_name)
def test_user_name_validator_with_non_string_value(self):
'''user_name_validator() should raise Invalid if given a non-string
value.
'''
import ckan.logic.validators as validators
non_string_values = [
13,
23.7,
100L,
1.0j,
None,
True,
False,
('a', 2, False),
[13, None, True],
{'foo': 'bar'},
lambda x: x**2,
]
# Mock ckan.model.
mock_model = mock.MagicMock()
# model.User.get(some_user_id) needs to return None for this test.
mock_model.User.get.return_value = None
key = ('name',)
for non_string_value in non_string_values:
data = factories.validator_data_dict()
data[key] = non_string_value
errors = factories.validator_errors_dict()
errors[key] = []
@does_not_modify_errors_dict('user_name_validator() should not '
'modify the errors dict')
@does_not_modify_data_dict('user_name_validator() should not '
'modify the data dict')
@raises_Invalid
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors, context={'model': mock_model})
def test_user_name_validator_with_a_name_that_already_exists(self):
'''user_name_validator() should add to the errors dict if given a
user name that already exists.
'''
import ckan.logic.validators as validators
# Mock ckan.model. model.User.get('user_name') will return another mock
# object rather than None, which will simulate an existing user with
# the same user name in the database.
mock_model = mock.MagicMock()
data = factories.validator_data_dict()
key = ('name',)
data[key] = 'user_name'
errors = factories.validator_errors_dict()
errors[key] = []
@does_not_modify_other_keys_in_errors_dict('user_name_validator() '
'should not modify other '
'keys in the errors dict')
@does_not_modify_data_dict('user_name_validator() should not modify '
'the data dict')
@returns_None('user_name_validator() should return None if called '
'with a user name that already exists')
@adds_message_to_errors_dict('That login name is not available.',
'user_name_validator() should add to the '
'errors dict when called with the name '
'of a user that already exists')
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors, context={'model': mock_model})
def test_user_name_validator_successful(self):
'''user_name_validator() should do nothing if given a valid name.'''
import ckan.logic.validators as validators
data = factories.validator_data_dict()
key = ('name',)
data[key] = 'new_user_name'
errors = factories.validator_errors_dict()
errors[key] = []
# Mock ckan.model.
mock_model = mock.MagicMock()
# model.User.get(user_name) should return None, to simulate that no
# user with that name exists in the database.
mock_model.User.get.return_value = None
@does_not_modify_errors_dict('user_name_validator() should not '
'modify the errors dict when given '
'valid input')
@does_not_modify_data_dict('user_name_validator() should not modify '
'the data dict when given valid input')
@returns_None('user_name_validator() should return None when given '
'valid input')
def call_validator(*args, **kwargs):
return validators.user_name_validator(*args, **kwargs)
call_validator(key, data, errors, context={'model': mock_model})
# TODO: Test user_name_validator()'s behavior when there's a 'user_obj' in
# the context dict.