-
Notifications
You must be signed in to change notification settings - Fork 2k
/
helpers.py
188 lines (138 loc) · 6.55 KB
/
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
'''This is a collection of helper functions for use in tests.
We want to avoid sharing test helper functions between test modules as
much as possible, and we definitely don't want to share test fixtures between
test modules, or to introduce a complex hierarchy of test class subclasses,
etc.
We want to reduce the amount of "travel" that a reader needs to undertake to
understand a test method -- reducing the number of other files they need to go
and read to understand what the test code does. And we want to avoid tightly
coupling test modules to each other by having them share code.
But some test helper functions just increase the readability of tests so much
and make writing tests so much easier, that it's worth having them despite the
potential drawbacks.
This module is reserved for these very useful functions.
'''
import webtest
from pylons import config
import nose.tools
import ckan.config.middleware
import ckan.model as model
import ckan.logic as logic
import ckan.new_authz as new_authz
def reset_db():
'''Reset CKAN's database.
If a test class uses the database, then it should call this function in its
``setup()`` method to make sure that it has a clean database to start with
(nothing left over from other test classes or from previous test runs).
If a test class doesn't use the database (and most test classes shouldn't
need to) then it doesn't need to call this function.
:returns: ``None``
'''
# Close any database connections that have been left open.
# This prevents CKAN from hanging waiting for some unclosed connection.
model.Session.close_all()
model.repo.rebuild_db()
def call_action(action_name, context=None, **kwargs):
'''Call the named ``ckan.logic.action`` function and return the result.
This is just a nicer way for user code to call action functions, nicer than
either calling the action function directly or via
:py:func:`ckan.logic.get_action`.
For example::
user_dict = call_action('user_create', name='seanh',
email='seanh@seanh.com', password='pass')
Any keyword arguments given will be wrapped in a dict and passed to the
action function as its ``data_dict`` argument.
Note: this skips authorization! It passes 'ignore_auth': True to action
functions in their ``context`` dicts, so the corresponding authorization
functions will not be run.
This is because ckan.new_tests.logic.action tests only the actions, the
authorization functions are tested separately in
ckan.new_tests.logic.auth.
See the :doc:`testing guidelines </contributing/testing>` for more info.
This function should eventually be moved to
:py:func:`ckan.logic.call_action` and the current
:py:func:`ckan.logic.get_action` function should be
deprecated. The tests may still need their own wrapper function for
:py:func:`ckan.logic.call_action`, e.g. to insert ``'ignore_auth': True``
into the ``context`` dict.
:param action_name: the name of the action function to call, e.g.
``'user_update'``
:type action_name: string
:param context: the context dict to pass to the action function
(optional, if no context is given a default one will be supplied)
:type context: dict
:returns: the dict or other value that the action function returns
'''
if context is None:
context = {}
context.setdefault('user', '127.0.0.1')
context.setdefault('ignore_auth', True)
return logic.get_action(action_name)(context=context, data_dict=kwargs)
def call_auth(auth_name, context, **kwargs):
'''Call the named ``ckan.logic.auth`` function and return the result.
This is just a convenience function for tests in
:py:mod:`ckan.new_tests.logic.auth` to use.
Usage::
result = helpers.call_auth('user_update', context=context,
id='some_user_id',
name='updated_user_name')
:param auth_name: the name of the auth function to call, e.g.
``'user_update'``
:type auth_name: string
:param context: the context dict to pass to the auth function, must
contain ``'user'`` and ``'model'`` keys,
e.g. ``{'user': 'fred', 'model': my_mock_model_object}``
:type context: dict
:returns: the dict that the auth function returns, e.g.
``{'success': True}`` or ``{'success': False, msg: '...'}``
or just ``{'success': False}``
:rtype: dict
'''
assert 'user' in context, ('Test methods must put a user name in the '
'context dict')
assert 'model' in context, ('Test methods must put a model in the '
'context dict')
return logic.check_access(auth_name, context, data_dict=kwargs)
def _get_test_app():
'''Return a webtest.TestApp for CKAN, with legacy templates disabled.
For functional tests that need to request CKAN pages or post to the API.
Unit tests shouldn't need this.
'''
config['ckan.legacy_templates'] = False
app = ckan.config.middleware.make_app(config['global_conf'], **config)
app = webtest.TestApp(app)
return app
try:
from nose.tools import assert_in, assert_not_in
except ImportError:
# Python 2.6 doesn't have these, so define them here
def assert_in(a, b, msg=None):
assert a in b, msg or '%r was not in %r' % (a, b)
def assert_not_in(a, b, msg=None):
assert a not in b, msg or '%r was in %r' % (a, b)
def change_config(key, value):
'''Decorator to temporarily changes Pylons' config to a new value
This allows you to easily create tests that need specific config values to
be set, making sure it'll be reverted to what it was originally, after your
test is run.
Usage::
@helpers.change_config('ckan.site_title', 'My Test CKAN')
def test_ckan_site_title(self):
assert pylons.config['ckan.site_title'] == 'My Test CKAN'
:param key: the config key to be changed, e.g. ``'ckan.site_title'``
:type key: string
:param value: the new config key's value, e.g. ``'My Test CKAN'``
:type value: string
'''
def decorator(func):
def wrapper(*args, **kwargs):
_original_config = config.copy()
config[key] = value
new_authz.clear_auth_functions_cache()
return_value = func(*args, **kwargs)
config.clear()
config.update(_original_config)
new_authz.clear_auth_functions_cache()
return return_value
return nose.tools.make_decorator(func)(wrapper)
return decorator