Skip to content

Commit

Permalink
[ckan#2936] move extra_environ change to decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Apr 4, 2016
1 parent 9cf0369 commit 2195975
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
33 changes: 33 additions & 0 deletions ckan/tests/helpers.py
Expand Up @@ -379,3 +379,36 @@ def side_effect(called_action_name):

return nose.tools.make_decorator(func)(wrapper)
return decorator


def set_extra_environ(key, value):
'''Decorator to temporarily changes a single request environemnt value
Create a new test app and use the a side effect of making a request
to set an extra_environ value. Reset the value to '' after the test.
Usage::
@helpers.extra_environ('SCRIPT_NAME', '/myscript')
def test_ckan_thing_affected_by_script_name(self):
# ...
:param key: the extra_environ key to be changed, e.g. ``'SCRIPT_NAME'``
:type key: string
:param value: the new extra_environ key's value, e.g. ``'/myscript'``
:type value: string
'''
def decorator(func):
def wrapper(*args, **kwargs):
app = _get_test_app()
app.get('/', extra_environ={key: value})

try:
return_value = func(*args, **kwargs)
finally:
app.get('/', extra_environ={key: ''})

return return_value
return nose.tools.make_decorator(func)(wrapper)
return decorator
59 changes: 16 additions & 43 deletions ckan/tests/lib/test_helpers.py
Expand Up @@ -2,7 +2,6 @@
import pytz
import tzlocal
from babel import Locale
from pylons import config

import ckan.lib.helpers as h
import ckan.exceptions
Expand Down Expand Up @@ -49,26 +48,13 @@ def test_url_for_static_qualified_with_root_path(self):
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):

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test():
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)

app = helpers._get_test_app()
app.get('/', extra_environ={
'SCRIPT_NAME': '/my/custom/path'
})

try:
test()
finally:
app.get('/', extra_environ={
'SCRIPT_NAME': ''
})
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(object):
Expand Down Expand Up @@ -169,30 +155,17 @@ def test_url_for_qualified_with_root_path_and_locale(self):
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):

@helpers.change_config('ckan.site_url', 'http://example.com')
@helpers.change_config('ckan.root_path', '/my/custom/path/{{LANG}}/foo')
def test():
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)

app = helpers._get_test_app()
app.get('/', extra_environ={
'SCRIPT_NAME': '/my/custom/path'
})

try:
test()
finally:
app.get('/', extra_environ={
'SCRIPT_NAME': ''
})
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 TestHelpersRenderMarkdown(object):
Expand Down

0 comments on commit 2195975

Please sign in to comment.