-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_group.py
437 lines (343 loc) · 16.6 KB
/
test_group.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
from bs4 import BeautifulSoup
from nose.tools import assert_equal, assert_true
from routes import url_for
import ckan.tests.helpers as helpers
import ckan.model as model
from ckan.tests import factories
assert_in = helpers.assert_in
webtest_submit = helpers.webtest_submit
submit_and_follow = helpers.submit_and_follow
class TestGroupController(helpers.FunctionalTestBase):
def setup(self):
model.repo.rebuild_db()
def test_bulk_process_throws_404_for_nonexistent_org(self):
app = self._get_test_app()
bulk_process_url = url_for(controller='organization',
action='bulk_process', id='does-not-exist')
app.get(url=bulk_process_url, status=404)
def test_page_thru_list_of_orgs(self):
orgs = [factories.Organization() for i in range(35)]
app = self._get_test_app()
org_url = url_for(controller='organization', action='index')
response = app.get(url=org_url)
assert orgs[0]['name'] in response
assert orgs[-1]['name'] not in response
response2 = response.click('2')
assert orgs[0]['name'] not in response2
assert orgs[-1]['name'] in response2
def _get_group_new_page(app):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
response = app.get(
url=url_for(controller='group', action='new'),
extra_environ=env,
)
return env, response
class TestGroupControllerNew(helpers.FunctionalTestBase):
def test_not_logged_in(self):
app = self._get_test_app()
app.get(url=url_for(controller='group', action='new'),
status=302)
def test_form_renders(self):
app = self._get_test_app()
env, response = _get_group_new_page(app)
assert_in('group-edit', response.forms)
def test_name_required(self):
app = self._get_test_app()
env, response = _get_group_new_page(app)
form = response.forms['group-edit']
response = webtest_submit(form, 'save', status=200, extra_environ=env)
assert_true('group-edit' in response.forms)
assert_true('Name: Missing value' in response)
def test_saved(self):
app = self._get_test_app()
env, response = _get_group_new_page(app)
form = response.forms['group-edit']
form['name'] = u'saved'
response = submit_and_follow(app, form, env, 'save')
group = model.Group.by_name(u'saved')
assert_equal(group.title, u'')
assert_equal(group.type, 'group')
assert_equal(group.state, 'active')
def test_all_fields_saved(self):
app = self._get_test_app()
env, response = _get_group_new_page(app)
form = response.forms['group-edit']
form['name'] = u'all-fields-saved'
form['title'] = 'Science'
form['description'] = 'Sciencey datasets'
form['image_url'] = 'http://example.com/image.png'
response = submit_and_follow(app, form, env, 'save')
group = model.Group.by_name(u'all-fields-saved')
assert_equal(group.title, u'Science')
assert_equal(group.description, 'Sciencey datasets')
def _get_group_edit_page(app, group_name=None):
user = factories.User()
if group_name is None:
group = factories.Group(user=user)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for(controller='group',
action='edit',
id=group_name)
response = app.get(url=url, extra_environ=env)
return env, response, group_name
class TestGroupControllerEdit(helpers.FunctionalTestBase):
def test_not_logged_in(self):
app = self._get_test_app()
app.get(url=url_for(controller='group', action='new'),
status=302)
def test_group_doesnt_exist(self):
app = self._get_test_app()
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for(controller='group',
action='edit',
id='doesnt_exist')
app.get(url=url, extra_environ=env,
status=404)
def test_form_renders(self):
app = self._get_test_app()
env, response, group_name = _get_group_edit_page(app)
assert_in('group-edit', response.forms)
def test_saved(self):
app = self._get_test_app()
env, response, group_name = _get_group_edit_page(app)
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')
def test_all_fields_saved(self):
app = self._get_test_app()
env, response, group_name = _get_group_edit_page(app)
form = response.forms['group-edit']
form['name'] = u'all-fields-edited'
form['title'] = 'Science'
form['description'] = 'Sciencey datasets'
form['image_url'] = 'http://example.com/image.png'
response = submit_and_follow(app, form, env, 'save')
group = model.Group.by_name(u'all-fields-edited')
assert_equal(group.title, u'Science')
assert_equal(group.description, 'Sciencey datasets')
assert_equal(group.image_url, 'http://example.com/image.png')
class TestGroupRead(helpers.FunctionalTestBase):
def setup(self):
super(TestGroupRead, self).setup()
self.app = helpers._get_test_app()
self.user = factories.User()
self.user_env = {'REMOTE_USER': self.user['name'].encode('ascii')}
self.group = factories.Group(user=self.user)
def test_group_read(self):
response = self.app.get(url=url_for(controller='group',
action='read',
id=self.group['id']),
status=200,
extra_environ=self.user_env)
assert_in(self.group['title'], response)
assert_in(self.group['description'], response)
class TestGroupFollow(helpers.FunctionalTestBase):
def test_group_follow(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group()
env = {'REMOTE_USER': user['name'].encode('ascii')}
follow_url = url_for(controller='group',
action='follow',
id=group['id'])
response = app.post(follow_url, extra_environ=env, status=302)
response = response.follow()
assert_true('You are now following {0}'
.format(group['display_name'])
in response)
def test_group_follow_not_exist(self):
'''Pass an id for a group that doesn't exist'''
app = self._get_test_app()
user_one = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
follow_url = url_for(controller='group',
action='follow',
id='not-here')
response = app.post(follow_url, extra_environ=env, status=404)
assert_true('Group not found' in response)
def test_group_unfollow(self):
app = self._get_test_app()
user_one = factories.User()
group = factories.Group()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
follow_url = url_for(controller='group',
action='follow',
id=group['id'])
app.post(follow_url, extra_environ=env, status=302)
unfollow_url = url_for(controller='group', action='unfollow',
id=group['id'])
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow()
assert_true('You are no longer following {0}'
.format(group['display_name'])
in unfollow_response)
def test_group_unfollow_not_following(self):
'''Unfollow a group not currently following'''
app = self._get_test_app()
user_one = factories.User()
group = factories.Group()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
unfollow_url = url_for(controller='group', action='unfollow',
id=group['id'])
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=302)
unfollow_response = unfollow_response.follow()
assert_true('You are not following {0}'.format(group['id'])
in unfollow_response)
def test_group_unfollow_not_exist(self):
'''Unfollow a group that doesn't exist.'''
app = self._get_test_app()
user_one = factories.User()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
unfollow_url = url_for(controller='group', action='unfollow',
id='not-here')
unfollow_response = app.post(unfollow_url, extra_environ=env,
status=404)
assert_true('Group not found' in unfollow_response)
def test_group_follower_list(self):
'''Following users appear on followers list page.'''
app = self._get_test_app()
user_one = factories.Sysadmin()
group = factories.Group()
env = {'REMOTE_USER': user_one['name'].encode('ascii')}
follow_url = url_for(controller='group',
action='follow',
id=group['id'])
app.post(follow_url, extra_environ=env, status=302)
followers_url = url_for(controller='group', action='followers',
id=group['id'])
# Only sysadmins can view the followers list pages
followers_response = app.get(followers_url, extra_environ=env,
status=200)
assert_true(user_one['display_name'] in followers_response)
class TestGroupSearch(helpers.FunctionalTestBase):
'''Test searching for groups.'''
def setup(self):
super(TestGroupSearch, self).setup()
self.app = self._get_test_app()
factories.Group(name='grp-one', title='AGrp One')
factories.Group(name='grp-two', title='AGrp Two')
factories.Group(name='grp-three', title='Grp Three')
self.search_url = url_for(controller='group', action='index')
def test_group_search(self):
'''Requesting group search (index) returns list of groups and search
form.'''
index_response = self.app.get(self.search_url)
index_response_html = BeautifulSoup(index_response.body)
grp_names = index_response_html.select('ul.media-grid '
'li.media-item '
'h3.media-heading')
grp_names = [n.string for n in grp_names]
assert_equal(len(grp_names), 3)
assert_true('AGrp One' in grp_names)
assert_true('AGrp Two' in grp_names)
assert_true('Grp Three' in grp_names)
def test_group_search_results(self):
'''Searching via group search form returns list of expected groups.'''
index_response = self.app.get(self.search_url)
search_form = index_response.forms['group-search-form']
search_form['q'] = 'AGrp'
search_response = webtest_submit(search_form)
search_response_html = BeautifulSoup(search_response.body)
grp_names = search_response_html.select('ul.media-grid '
'li.media-item '
'h3.media-heading')
grp_names = [n.string for n in grp_names]
assert_equal(len(grp_names), 2)
assert_true('AGrp One' in grp_names)
assert_true('AGrp Two' in grp_names)
assert_true('Grp Three' not in grp_names)
def test_group_search_no_results(self):
'''Searching with a term that doesn't apply returns no results.'''
index_response = self.app.get(self.search_url)
search_form = index_response.forms['group-search-form']
search_form['q'] = 'No Results Here'
search_response = webtest_submit(search_form)
search_response_html = BeautifulSoup(search_response.body)
grp_names = search_response_html.select('ul.media-grid '
'li.media-item '
'h3.media-heading')
grp_names = [n.string for n in grp_names]
assert_equal(len(grp_names), 0)
assert_true("No groups found for "No Results Here""
in search_response)
class TestGroupInnerSearch(helpers.FunctionalTestBase):
'''Test searching within an group.'''
def test_group_search_within_org(self):
'''Group read page request returns list of datasets owned by group.'''
app = self._get_test_app()
grp = factories.Group()
factories.Dataset(name="ds-one", title="Dataset One",
groups=[{'id': grp['id']}])
factories.Dataset(name="ds-two", title="Dataset Two",
groups=[{'id': grp['id']}])
factories.Dataset(name="ds-three", title="Dataset Three",
groups=[{'id': grp['id']}])
grp_url = url_for(controller='group', action='read',
id=grp['id'])
grp_response = app.get(grp_url)
grp_response_html = BeautifulSoup(grp_response.body)
ds_titles = grp_response_html.select('.dataset-list '
'.dataset-item '
'.dataset-heading a')
ds_titles = [t.string for t in ds_titles]
assert_true('3 datasets found' in grp_response)
assert_equal(len(ds_titles), 3)
assert_true('Dataset One' in ds_titles)
assert_true('Dataset Two' in ds_titles)
assert_true('Dataset Three' in ds_titles)
def test_group_search_within_org_results(self):
'''Searching within an group returns expected dataset results.'''
app = self._get_test_app()
grp = factories.Group()
factories.Dataset(name="ds-one", title="Dataset One",
groups=[{'id': grp['id']}])
factories.Dataset(name="ds-two", title="Dataset Two",
groups=[{'id': grp['id']}])
factories.Dataset(name="ds-three", title="Dataset Three",
groups=[{'id': grp['id']}])
grp_url = url_for(controller='group', action='read',
id=grp['id'])
grp_response = app.get(grp_url)
search_form = grp_response.forms['group-datasets-search-form']
search_form['q'] = 'One'
search_response = webtest_submit(search_form)
assert_true('1 dataset found for "One"' in search_response)
search_response_html = BeautifulSoup(search_response.body)
ds_titles = search_response_html.select('.dataset-list '
'.dataset-item '
'.dataset-heading a')
ds_titles = [t.string for t in ds_titles]
assert_equal(len(ds_titles), 1)
assert_true('Dataset One' in ds_titles)
assert_true('Dataset Two' not in ds_titles)
assert_true('Dataset Three' not in ds_titles)
def test_group_search_within_org_no_results(self):
'''Searching for non-returning phrase within an group returns no
results.'''
app = self._get_test_app()
grp = factories.Group()
factories.Dataset(name="ds-one", title="Dataset One",
groups=[{'id': grp['id']}])
factories.Dataset(name="ds-two", title="Dataset Two",
groups=[{'id': grp['id']}])
factories.Dataset(name="ds-three", title="Dataset Three",
groups=[{'id': grp['id']}])
grp_url = url_for(controller='group', action='read',
id=grp['id'])
grp_response = app.get(grp_url)
search_form = grp_response.forms['group-datasets-search-form']
search_form['q'] = 'Nout'
search_response = webtest_submit(search_form)
assert_true('No datasets found for "Nout"' in search_response)
search_response_html = BeautifulSoup(search_response.body)
ds_titles = search_response_html.select('.dataset-list '
'.dataset-item '
'.dataset-heading a')
ds_titles = [t.string for t in ds_titles]
assert_equal(len(ds_titles), 0)