-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_helpers.py
452 lines (333 loc) · 17 KB
/
test_helpers.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# encoding: utf-8
import nose
import pytz
import tzlocal
from babel import Locale
from pylons import config
import ckan.lib.helpers as h
import ckan.plugins as p
import ckan.exceptions
from ckan.tests import helpers
import ckan.lib.base as base
ok_ = nose.tools.ok_
eq_ = nose.tools.eq_
raises = nose.tools.raises
CkanUrlException = ckan.exceptions.CkanUrlException
class BaseUrlFor(object):
@classmethod
def setup_class(cls):
# Make a copy of the Pylons config, so we can restore it in teardown.
cls._original_config = dict(config)
config['ckan.site_url'] = 'http://example.com'
cls.app = helpers._get_test_app()
def setup(self):
self.request_context = self.app.flask_app.test_request_context()
self.request_context.push()
def teardown(self):
self.request_context.pop()
@classmethod
def teardown_class(cls):
# Restore the config to its original values
config.clear()
config.update(cls._original_config)
class TestHelpersUrlForStatic(BaseUrlFor):
def test_url_for_static(self):
url = '/assets/ckan.jpg'
eq_(h.url_for_static(url), url)
def test_url_for_static_adds_starting_slash_if_url_doesnt_have_it(self):
slashless_url = 'ckan.jpg'
url = '/' + slashless_url
eq_(h.url_for_static(slashless_url), url)
def test_url_for_static_converts_unicode_strings_to_regular_strings(self):
url = u'/ckan.jpg'
assert isinstance(h.url_for_static(url), str)
def test_url_for_static_raises_when_called_with_external_urls(self):
url = 'http://assets.ckan.org/ckan.jpg'
nose.tools.assert_raises(CkanUrlException, h.url_for_static, url)
def test_url_for_static_raises_when_called_with_protocol_relative(self):
url = '//assets.ckan.org/ckan.jpg'
nose.tools.assert_raises(CkanUrlException, h.url_for_static, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_static_with_root_path(self):
url = '/my/custom/path/foo/my-asset/file.txt'
generated_url = h.url_for_static('/my-asset/file.txt')
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_static_qualified_with_root_path(self):
url = 'http://example.com/my/custom/path/foo/my-asset/file.txt'
generated_url = h.url_for_static('/my-asset/file.txt',
qualified=True)
eq_(generated_url, url)
@helpers.set_extra_environ('SCRIPT_NAME', '/my/custom/path')
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_static_with_root_path_and_script_name_env(self):
url = 'http://example.com/my/custom/path/foo/my-asset/file.txt'
generated_url = h.url_for_static('/my-asset/file.txt',
qualified=True)
eq_(generated_url, url)
class TestHelpersUrlForStaticOrExternal(BaseUrlFor):
def test_url_for_static_or_external(self):
url = '/assets/ckan.jpg'
eq_(h.url_for_static_or_external(url), url)
def test_url_for_static_or_external_works_with_external_urls(self):
url = 'http://assets.ckan.org/ckan.jpg'
eq_(h.url_for_static_or_external(url), url)
def test_url_for_static_or_external_converts_unicode_to_strings(self):
url = u'/ckan.jpg'
assert isinstance(h.url_for_static_or_external(url), str)
def test_url_for_static_or_external_adds_starting_slash_if_needed(self):
slashless_url = 'ckan.jpg'
url = '/' + slashless_url
eq_(h.url_for_static_or_external(slashless_url), url)
def test_url_for_static_or_external_works_with_protocol_relative_url(self):
url = '//assets.ckan.org/ckan.jpg'
eq_(h.url_for_static_or_external(url), url)
class TestHelpersUrlFor(BaseUrlFor):
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_default(self):
url = '/dataset/my_dataset'
generated_url = h.url_for(controller='package', action='read',
id='my_dataset')
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_with_locale(self):
url = '/de/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
locale='de')
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/foo/{{LANG}}')
def test_url_for_with_locale_object(self):
url = '/foo/de/dataset/my_dataset'
generated_url = h.url_for('/dataset/my_dataset',
locale=Locale('de'))
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_not_qualified(self):
url = '/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=False)
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_qualified(self):
url = 'http://example.com/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True)
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/prefix')
def test_url_for_qualified_with_root_path(self):
url = 'http://example.com/my/prefix/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True)
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_qualified_with_locale(self):
url = 'http://example.com/de/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_qualified_with_root_path_and_locale(self):
url = 'http://example.com/my/custom/path/de/foo/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
eq_(generated_url, url)
@helpers.set_extra_environ('SCRIPT_NAME', '/my/custom/path')
@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test_url_for_qualified_with_root_path_locale_and_script_name_env(self):
url = 'http://example.com/my/custom/path/de/foo/dataset/my_dataset'
generated_url = h.url_for(controller='package',
action='read',
id='my_dataset',
qualified=True,
locale='de')
eq_(generated_url, url)
class TestHelpersUrlForFlaskandPylons(BaseUrlFor):
def test_url_for_flask_route_new_syntax(self):
url = '/api/3/action/status_show'
generated_url = h.url_for('api.action', logic_function='status_show', ver=3)
eq_(generated_url, url)
def test_url_for_flask_route_old_syntax(self):
url = '/api/3/action/status_show'
generated_url = h.url_for(controller='api', action='action', logic_function='status_show', ver=3)
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_flask_route_new_syntax_external(self):
url = 'http://example.com/api/3/action/status_show'
generated_url = h.url_for('api.action', logic_function='status_show', ver=3, _external=True)
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_flask_route_old_syntax_external(self):
url = 'http://example.com/api/3/action/status_show'
generated_url = h.url_for(controller='api', action='action', logic_function='status_show', ver=3, _external=True)
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_flask_route_old_syntax_qualified(self):
url = 'http://example.com/api/3/action/status_show'
generated_url = h.url_for(controller='api', action='action', logic_function='status_show', ver=3, qualified=True)
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_flask_route_new_syntax_site_url(self):
url = '/api/3/action/status_show'
generated_url = h.url_for('api.action', logic_function='status_show', ver=3)
eq_(generated_url, url)
@helpers.change_config('ckan.site_url', 'http://example.com')
def test_url_for_flask_route_old_syntax_site_url(self):
url = '/api/3/action/status_show'
generated_url = h.url_for(controller='api', action='action', logic_function='status_show', ver=3)
eq_(generated_url, url)
def test_url_for_flask_route_new_syntax_request_context(self):
with self.app.flask_app.test_request_context():
url = '/api/3/action/status_show'
generated_url = h.url_for('api.action', logic_function='status_show', ver=3)
eq_(generated_url, url)
class TestHelpersRenderMarkdown(object):
def test_render_markdown_allow_html(self):
data = '<h1>moo</h1>'
eq_(h.render_markdown(data, allow_html=True), data)
def test_render_markdown_not_allow_html(self):
data = '<h1>moo</h1>'
output = '<p>moo</p>'
eq_(h.render_markdown(data), output)
def test_render_markdown_auto_link_without_path(self):
data = 'http://example.com'
output = '<p><a href="http://example.com" target="_blank" rel="nofollow">http://example.com</a></p>'
eq_(h.render_markdown(data), output)
def test_render_markdown_auto_link(self):
data = 'https://example.com/page.html'
output = '<p><a href="https://example.com/page.html" target="_blank" rel="nofollow">https://example.com/page.html</a></p>'
eq_(h.render_markdown(data), output)
def test_render_markdown_auto_link_ignoring_trailing_punctuation(self):
data = 'My link: http://example.com/page.html.'
output = '<p>My link: <a href="http://example.com/page.html" target="_blank" rel="nofollow">http://example.com/page.html</a>.</p>'
eq_(h.render_markdown(data), output)
def test_render_naughty_markdown(self):
data = u'* [Foo (http://foo.bar) * Bar] (http://foo.bar)'
output = u'<ul>\n<li>[Foo (<a href="http://foo.bar" target="_blank" rel="nofollow">http://foo.bar</a>) * Bar] (<a href="http://foo.bar" target="_blank" rel="nofollow">http://foo.bar</a>)</li>\n</ul>'
eq_(h.render_markdown(data), output)
class TestHelpersRemoveLineBreaks(object):
def test_remove_linebreaks_removes_linebreaks(self):
test_string = 'foo\nbar\nbaz'
result = h.remove_linebreaks(test_string)
assert result.find('\n') == -1,\
'"remove_linebreaks" should remove line breaks'
def test_remove_linebreaks_casts_into_unicode(self):
class UnicodeLike(unicode):
pass
test_string = UnicodeLike('foo')
result = h.remove_linebreaks(test_string)
strType = u''.__class__
assert result.__class__ == strType,\
'"remove_linebreaks" casts into unicode()'
class TestLicenseOptions(object):
def test_includes_existing_license(self):
licenses = h.license_options('some-old-license')
eq_(dict(licenses)['some-old-license'], 'some-old-license')
# and it is first on the list
eq_(licenses[0][0], 'some-old-license')
class TestUnifiedResourceFormat(object):
def test_unified_resource_format_by_extension(self):
eq_(h.unified_resource_format('xls'), 'XLS')
def test_unified_resource_format_by_description(self):
eq_(h.unified_resource_format('Excel document'), 'XLS')
def test_unified_resource_format_by_primary_mimetype(self):
eq_(h.unified_resource_format('application/vnd.ms-excel'), 'XLS')
def test_unified_resource_format_by_alternative_description(self):
eq_(h.unified_resource_format('application/msexcel'), 'XLS')
def test_unified_resource_format_by_alternative_description2(self):
eq_(h.unified_resource_format('Excel'), 'XLS')
def test_autodetect_tsv(self):
eq_(h.unified_resource_format('tsv'), 'TSV')
eq_(h.unified_resource_format('text/tab-separated-values'), 'TSV')
eq_(h.unified_resource_format('text/tsv'), 'TSV')
class TestGetDisplayTimezone(object):
@helpers.change_config('ckan.display_timezone', '')
def test_missing_config(self):
eq_(h.get_display_timezone(), pytz.timezone('utc'))
@helpers.change_config('ckan.display_timezone', 'server')
def test_server_timezone(self):
eq_(h.get_display_timezone(), tzlocal.get_localzone())
@helpers.change_config('ckan.display_timezone', 'America/New_York')
def test_named_timezone(self):
eq_(h.get_display_timezone(), pytz.timezone('America/New_York'))
class TestHelperException(helpers.FunctionalTestBase):
@raises(ckan.exceptions.HelperError)
def test_helper_exception_non_existing_helper_as_attribute(self):
'''Calling a non-existing helper on `h` raises a HelperException.'''
if not p.plugin_loaded('test_helpers_plugin'):
p.load('test_helpers_plugin')
app = self._get_test_app()
app.get('/broken_helper_as_attribute')
p.unload('test_helpers_plugin')
@raises(ckan.exceptions.HelperError)
def test_helper_exception_non_existing_helper_as_item(self):
'''Calling a non-existing helper on `h` raises a HelperException.'''
if not p.plugin_loaded('test_helpers_plugin'):
p.load('test_helpers_plugin')
app = self._get_test_app()
app.get('/broken_helper_as_item')
p.unload('test_helpers_plugin')
def test_helper_existing_helper_as_attribute(self):
'''Calling an existing helper on `h` doesn't raises a
HelperException.'''
if not p.plugin_loaded('test_helpers_plugin'):
p.load('test_helpers_plugin')
app = self._get_test_app()
res = app.get('/helper_as_attribute')
ok_('My lang is: en' in res.body)
p.unload('test_helpers_plugin')
def test_helper_existing_helper_as_item(self):
'''Calling an existing helper on `h` doesn't raises a
HelperException.'''
if not p.plugin_loaded('test_helpers_plugin'):
p.load('test_helpers_plugin')
app = self._get_test_app()
res = app.get('/helper_as_item')
ok_('My lang is: en' in res.body)
p.unload('test_helpers_plugin')
class TestHelpersPlugin(p.SingletonPlugin):
p.implements(p.IRoutes, inherit=True)
controller = 'ckan.tests.lib.test_helpers:TestHelperController'
def after_map(self, _map):
_map.connect('/broken_helper_as_attribute',
controller=self.controller,
action='broken_helper_as_attribute')
_map.connect('/broken_helper_as_item',
controller=self.controller,
action='broken_helper_as_item')
_map.connect('/helper_as_attribute',
controller=self.controller,
action='helper_as_attribute')
_map.connect('/helper_as_item',
controller=self.controller,
action='helper_as_item')
return _map
class TestHelperController(p.toolkit.BaseController):
def broken_helper_as_attribute(self):
return base.render('tests/broken_helper_as_attribute.html')
def broken_helper_as_item(self):
return base.render('tests/broken_helper_as_item.html')
def helper_as_attribute(self):
return base.render('tests/helper_as_attribute.html')
def helper_as_item(self):
return base.render('tests/helper_as_item.html')