-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_controllers.py
280 lines (230 loc) · 9.89 KB
/
test_controllers.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
# encoding: utf-8
from nose.tools import assert_equal, assert_in
from routes import url_for
import ckan.plugins as plugins
import ckan.tests.helpers as helpers
import ckan.model as model
from ckan.tests import factories
webtest_submit = helpers.webtest_submit
submit_and_follow = helpers.submit_and_follow
custom_group_type = u'grup'
group_type = u'group'
def _get_group_new_page(app, group_type):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
response = app.get(
url_for('%s_new' % group_type),
extra_environ=env,
)
return env, response
class TestGroupController(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestGroupController, cls).setup_class()
plugins.load('example_igroupform')
@classmethod
def teardown_class(cls):
plugins.unload('example_igroupform')
super(TestGroupController, cls).teardown_class()
def test_about(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_about' % custom_group_type,
id=group_name)
response = app.get(url=url, extra_environ=env)
response.mustcontain(group_name)
def test_bulk_process(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_bulk_process' % custom_group_type,
id=group_name)
try:
response = app.get(url=url, extra_environ=env)
except Exception as e:
assert (e.args == ('Must be an organization', ))
else:
raise Exception("Response should have raised an exception")
def test_delete(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_action' % custom_group_type, action='delete',
id=group_name)
response = app.get(url=url, extra_environ=env)
class TestOrganizationController(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestOrganizationController, cls).setup_class()
plugins.load('example_igroupform_organization')
@classmethod
def teardown_class(cls):
plugins.unload('example_igroupform_organization')
super(TestOrganizationController, cls).teardown_class()
def test_about(self):
app = self._get_test_app()
user = factories.User()
group = factories.Organization(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_about' % custom_group_type,
id=group_name)
response = app.get(url=url, extra_environ=env)
response.mustcontain(group_name)
def test_bulk_process(self):
app = self._get_test_app()
user = factories.User()
group = factories.Organization(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_bulk_process' % custom_group_type,
id=group_name)
response = app.get(url=url, extra_environ=env)
def test_delete(self):
app = self._get_test_app()
user = factories.User()
group = factories.Organization(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_action' % custom_group_type, action='delete',
id=group_name)
response = app.get(url=url, extra_environ=env)
class TestGroupControllerNew(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestGroupControllerNew, cls).setup_class()
plugins.load('example_igroupform')
@classmethod
def teardown_class(cls):
plugins.unload('example_igroupform')
super(TestGroupControllerNew, cls).teardown_class()
def test_save(self):
app = self._get_test_app()
env, response = _get_group_new_page(app, custom_group_type)
form = response.forms['group-edit']
form['name'] = u'saved'
response = submit_and_follow(app, form, env, 'save')
# check correct redirect
assert_equal(response.req.url,
'http://test.ckan.net/%s/saved' % custom_group_type)
# check saved ok
group = model.Group.by_name(u'saved')
assert_equal(group.title, u'')
assert_equal(group.type, custom_group_type)
assert_equal(group.state, 'active')
def test_custom_group_form(self):
'''Our custom group form is being used for new groups.'''
app = self._get_test_app()
env, response = _get_group_new_page(app, custom_group_type)
assert_in('My Custom Group Form!', response,
msg="Custom group form not being used.")
class TestGroupControllerNew_DefaultGroupType(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestGroupControllerNew_DefaultGroupType, cls).setup_class()
plugins.load('example_igroupform_default_group_type')
@classmethod
def teardown_class(cls):
plugins.unload('example_igroupform_default_group_type')
super(TestGroupControllerNew_DefaultGroupType, cls).teardown_class()
def test_save(self):
app = self._get_test_app()
env, response = _get_group_new_page(app, group_type)
form = response.forms['group-edit']
form['name'] = u'saved'
response = submit_and_follow(app, form, env, 'save')
# check correct redirect
assert_equal(response.req.url,
'http://test.ckan.net/%s/saved' % group_type)
# check saved ok
group = model.Group.by_name(u'saved')
assert_equal(group.title, u'')
assert_equal(group.type, group_type)
assert_equal(group.state, 'active')
def test_custom_group_form(self):
'''Our custom group form is being used for new groups.'''
app = self._get_test_app()
env, response = _get_group_new_page(app, group_type)
assert_in('My Custom Group Form!', response,
msg="Custom group form not being used.")
def _get_group_edit_page(app, group_type, group_name=None):
user = factories.User()
if group_name is None:
group = factories.Group(user=user, type=group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_edit' % group_type,
id=group_name)
response = app.get(url=url, extra_environ=env)
return env, response, group_name
class TestGroupControllerEdit(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestGroupControllerEdit, cls).setup_class()
plugins.load('example_igroupform')
@classmethod
def teardown_class(cls):
plugins.unload('example_igroupform')
super(TestGroupControllerEdit, cls).teardown_class()
def test_group_doesnt_exist(self):
app = self._get_test_app()
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_edit' % custom_group_type,
id='doesnt_exist')
app.get(url=url, extra_environ=env,
status=404)
def test_save(self):
app = self._get_test_app()
env, response, group_name = \
_get_group_edit_page(app, custom_group_type)
form = response.forms['group-edit']
response = submit_and_follow(app, form, env, 'save')
group = model.Group.by_name(group_name)
assert_equal(group.state, 'active')
assert_equal(group.type, custom_group_type)
def test_custom_group_form(self):
'''Our custom group form is being used to edit groups.'''
app = self._get_test_app()
env, response, group_name = \
_get_group_edit_page(app, custom_group_type)
assert_in('My Custom Group Form!', response,
msg="Custom group form not being used.")
class TestGroupControllerEdit_DefaultGroupType(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestGroupControllerEdit_DefaultGroupType, cls).setup_class()
plugins.load('example_igroupform_default_group_type')
@classmethod
def teardown_class(cls):
plugins.unload('example_igroupform_default_group_type')
super(TestGroupControllerEdit_DefaultGroupType, cls).teardown_class()
def test_group_doesnt_exist(self):
app = self._get_test_app()
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_edit' % group_type,
id='doesnt_exist')
app.get(url=url, extra_environ=env,
status=404)
def test_save(self):
app = self._get_test_app()
env, response, group_name = _get_group_edit_page(app, group_type)
form = response.forms['group-edit']
response = submit_and_follow(app, form, env, 'save')
group = model.Group.by_name(group_name)
assert_equal(group.state, 'active')
assert_equal(group.type, group_type)
def test_custom_group_form(self):
'''Our custom group form is being used to edit groups.'''
app = self._get_test_app()
env, response, group_name = _get_group_edit_page(app, group_type)
assert_in('My Custom Group Form!', response,
msg="Custom group form not being used.")