-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_admin.py
404 lines (326 loc) · 14.8 KB
/
test_admin.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
# encoding: utf-8
import pytest
from bs4 import BeautifulSoup
import ckan.model as model
import ckan.tests.factories as factories
import ckan.tests.helpers as helpers
from ckan.common import config
from ckan.lib.helpers import url_for
from ckan.model.system_info import get_system_info
submit_and_follow = helpers.submit_and_follow
webtest_submit = helpers.webtest_submit
def _get_admin_config_page(app):
user = factories.Sysadmin()
env = {"REMOTE_USER": user["name"].encode("ascii")}
response = app.get(
url=url_for(controller="admin", action="config"), extra_environ=env
)
return env, response
def _reset_config(app):
"""Reset config via action"""
user = factories.Sysadmin()
env = {"REMOTE_USER": user["name"].encode("ascii")}
app.post(url=url_for("admin.reset_config"), extra_environ=env)
class TestConfig(object):
"""View tests to go along with 'Customizing look and feel' docs."""
@pytest.mark.usefixtures("clean_db")
def test_form_renders(self, app):
"""admin-config-form in the response"""
env, response = _get_admin_config_page(app)
assert "admin-config-form" in response.forms
@pytest.mark.usefixtures("clean_db")
def test_site_title(self, app):
"""Configure the site title"""
# current site title
index_response = app.get("/")
assert "Welcome - CKAN" in index_response
# change site title
env, config_response = _get_admin_config_page(app)
config_form = config_response.forms["admin-config-form"]
config_form["ckan.site_title"] = "Test Site Title"
webtest_submit(config_form, "save", status=302, extra_environ=env)
# new site title
new_index_response = app.get("/")
assert "Welcome - Test Site Title" in new_index_response
# reset config value
_reset_config(app)
reset_index_response = app.get("/")
assert "Welcome - CKAN" in reset_index_response
@pytest.mark.usefixtures("clean_db")
def test_main_css_list(self, app):
"""Style list contains pre-configured styles"""
STYLE_NAMES = ["Default", "Red", "Green", "Maroon", "Fuchsia"]
env, config_response = _get_admin_config_page(app)
config_response_html = BeautifulSoup(config_response.body)
style_select_options = config_response_html.select(
"#field-ckan-main-css option"
)
for option in style_select_options:
assert option.string in STYLE_NAMES
@pytest.mark.usefixtures("clean_db")
def test_main_css(self, app):
"""Select a colour style"""
# current style
index_response = app.get("/")
assert "main.css" in index_response or "main.min.css" in index_response
# set new style css
env, config_response = _get_admin_config_page(app)
config_form = config_response.forms["admin-config-form"]
config_form["ckan.main_css"] = "/base/css/red.css"
webtest_submit(config_form, "save", status=302, extra_environ=env)
# new style
new_index_response = app.get("/")
assert (
"red.css" in new_index_response
or "red.min.css" in new_index_response
)
assert "main.css" not in new_index_response
assert "main.min.css" not in new_index_response
# reset config value
_reset_config(app)
reset_index_response = app.get("/")
assert (
"main.css" in reset_index_response
or "main.min.css" in reset_index_response
)
@pytest.mark.usefixtures("clean_db")
def test_tag_line(self, app):
"""Add a tag line (only when no logo)"""
# current tagline
index_response = app.get("/")
assert "Special Tagline" not in index_response
# set new tagline css
env, config_response = _get_admin_config_page(app)
config_form = config_response.forms["admin-config-form"]
config_form["ckan.site_description"] = "Special Tagline"
webtest_submit(config_form, "save", status=302, extra_environ=env)
# new tagline not visible yet
new_index_response = app.get("/")
assert "Special Tagline" not in new_index_response
# remove logo
env, config_response = _get_admin_config_page(app)
config_form = config_response.forms["admin-config-form"]
config_form["ckan.site_logo"] = ""
webtest_submit(config_form, "save", status=302, extra_environ=env)
# new tagline
new_index_response = app.get("/")
assert "Special Tagline" in new_index_response
# reset config value
_reset_config(app)
reset_index_response = app.get("/")
assert "Special Tagline" not in reset_index_response
@pytest.mark.usefixtures("clean_db")
def test_about(self, app):
"""Add some About tag text"""
# current about
about_response = app.get("/about")
assert "My special about text" not in about_response
# set new about
env, config_response = _get_admin_config_page(app)
config_form = config_response.forms["admin-config-form"]
config_form["ckan.site_about"] = "My special about text"
webtest_submit(config_form, "save", status=302, extra_environ=env)
# new about
new_about_response = app.get("/about")
assert "My special about text" in new_about_response
# reset config value
_reset_config(app)
reset_about_response = app.get("/about")
assert "My special about text" not in reset_about_response
@pytest.mark.usefixtures("clean_db")
def test_intro(self, app):
"""Add some Intro tag text"""
# current intro
intro_response = app.get("/")
assert "My special intro text" not in intro_response
# set new intro
env, config_response = _get_admin_config_page(app)
config_form = config_response.forms["admin-config-form"]
config_form["ckan.site_intro_text"] = "My special intro text"
webtest_submit(config_form, "save", status=302, extra_environ=env)
# new intro
new_intro_response = app.get("/")
assert "My special intro text" in new_intro_response
# reset config value
_reset_config(app)
reset_intro_response = app.get("/")
assert "My special intro text" not in reset_intro_response
@pytest.mark.usefixtures("clean_db")
def test_custom_css(self, app):
"""Add some custom css to the head element"""
# current tagline
intro_response_html = BeautifulSoup(app.get("/").body)
style_tag = intro_response_html.select("head style")
assert len(style_tag) == 0
# set new tagline css
env, config_response = _get_admin_config_page(app)
config_form = config_response.forms["admin-config-form"]
config_form["ckan.site_custom_css"] = "body {background-color:red}"
webtest_submit(config_form, "save", status=302, extra_environ=env)
# new tagline not visible yet
new_intro_response_html = BeautifulSoup(app.get("/").body)
style_tag = new_intro_response_html.select("head style")
assert len(style_tag) == 1
assert style_tag[0].text.strip() == "body {background-color:red}"
# reset config value
_reset_config(app)
reset_intro_response_html = BeautifulSoup(app.get("/").body)
style_tag = reset_intro_response_html.select("head style")
assert len(style_tag) == 0
@pytest.mark.ckan_config("debug", True)
@pytest.mark.usefixtures("clean_db")
def test_homepage_style(self, app):
"""Select a homepage style"""
# current style
index_response = app.get("/")
assert "<!-- Snippet home/layout1.html start -->" in index_response
# set new style css
env, config_response = _get_admin_config_page(app)
config_form = config_response.forms["admin-config-form"]
config_form["ckan.homepage_style"] = "2"
webtest_submit(config_form, "save", status=302, extra_environ=env)
# new style
new_index_response = app.get("/")
assert (
"<!-- Snippet home/layout1.html start -->"
not in new_index_response
)
assert "<!-- Snippet home/layout2.html start -->" in new_index_response
# reset config value
_reset_config(app)
reset_index_response = app.get("/")
assert (
"<!-- Snippet home/layout1.html start -->" in reset_index_response
)
class TestTrashView(object):
"""View tests for permanently deleting datasets with Admin Trash."""
@pytest.mark.ckan_config("debug", True)
def test_trash_view_anon_user(self, app):
"""An anon user shouldn't be able to access trash view."""
trash_url = url_for(controller="admin", action="trash")
trash_response = app.get(trash_url, status=403)
@pytest.mark.usefixtures("clean_db")
def test_trash_view_normal_user(self, app):
"""A normal logged in user shouldn't be able to access trash view."""
user = factories.User()
env = {"REMOTE_USER": user["name"].encode("ascii")}
trash_url = url_for(controller="admin", action="trash")
trash_response = app.get(trash_url, extra_environ=env, status=403)
assert (
"Need to be system administrator to administer" in trash_response
)
@pytest.mark.usefixtures("clean_db")
def test_trash_view_sysadmin(self, app):
"""A sysadmin should be able to access trash view."""
user = factories.Sysadmin()
env = {"REMOTE_USER": user["name"].encode("ascii")}
trash_url = url_for(controller="admin", action="trash")
trash_response = app.get(trash_url, extra_environ=env, status=200)
# On the purge page
assert "form-purge-packages" in trash_response
@pytest.mark.usefixtures("clean_db")
def test_trash_no_datasets(self, app):
"""Getting the trash view with no 'deleted' datasets should list no
datasets."""
factories.Dataset()
user = factories.Sysadmin()
env = {"REMOTE_USER": user["name"].encode("ascii")}
trash_url = url_for(controller="admin", action="trash")
trash_response = app.get(trash_url, extra_environ=env, status=200)
trash_response_html = BeautifulSoup(trash_response.body)
# it's called a 'user list' for some reason
trash_pkg_list = trash_response_html.select("ul.user-list li")
# no packages available to purge
assert len(trash_pkg_list) == 0
@pytest.mark.usefixtures("clean_db")
def test_trash_with_deleted_datasets(self, app):
"""Getting the trash view with 'deleted' datasets should list the
datasets."""
user = factories.Sysadmin()
factories.Dataset(state="deleted")
factories.Dataset(state="deleted")
factories.Dataset()
env = {"REMOTE_USER": user["name"].encode("ascii")}
trash_url = url_for(controller="admin", action="trash")
trash_response = app.get(trash_url, extra_environ=env, status=200)
trash_response_html = BeautifulSoup(trash_response.body)
# it's called a 'user list' for some reason
trash_pkg_list = trash_response_html.select("ul.user-list li")
# Two packages in the list to purge
assert len(trash_pkg_list) == 2
@pytest.mark.usefixtures("clean_db")
def test_trash_purge_deleted_datasets(self, app):
"""Posting the trash view with 'deleted' datasets, purges the
datasets."""
user = factories.Sysadmin()
factories.Dataset(state="deleted")
factories.Dataset(state="deleted")
factories.Dataset()
# how many datasets before purge
pkgs_before_purge = model.Session.query(model.Package).count()
assert pkgs_before_purge == 3
env = {"REMOTE_USER": user["name"].encode("ascii")}
trash_url = url_for(controller="admin", action="trash")
trash_response = app.get(trash_url, extra_environ=env, status=200)
# submit the purge form
purge_form = trash_response.forms["form-purge-packages"]
purge_response = webtest_submit(
purge_form, "purge-packages", status=302, extra_environ=env
)
purge_response = purge_response.follow(extra_environ=env)
# redirected back to trash page
assert "Purge complete" in purge_response
# how many datasets after purge
pkgs_after_purge = model.Session.query(model.Package).count()
assert pkgs_after_purge == 1
class TestAdminConfigUpdate(object):
def _update_config_option(self, app):
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin["name"].encode("ascii")}
url = url_for(controller="admin", action="config")
response = app.get(url=url, extra_environ=env)
form = response.forms[1]
form["ckan.site_title"] = "My Updated Site Title"
webtest_submit(form, "save", status=302, extra_environ=env)
@pytest.mark.usefixtures("clean_db")
def test_admin_config_update(self, app):
"""Changing a config option using the admin interface appropriately
updates value returned by config_option_show,
system_info.get_system_info and in the title tag in templates."""
# test value before update
# config_option_show returns default value
before_update = helpers.call_action(
"config_option_show", key="ckan.site_title"
)
assert before_update == "CKAN"
# system_info.get_system_info returns None, or default
# test value before update
before_update = get_system_info("ckan.site_title")
assert before_update is None
# test value before update with default
before_update_default = get_system_info(
"ckan.site_title", config["ckan.site_title"]
)
assert before_update_default == "CKAN"
# title tag contains default value
# app = make_app()
home_page_before = app.get("/", status=200)
assert "Welcome - CKAN" in home_page_before
# update the option
self._update_config_option(app)
# test config_option_show returns new value after update
after_update = helpers.call_action(
"config_option_show", key="ckan.site_title"
)
assert after_update == "My Updated Site Title"
# system_info.get_system_info returns new value
after_update = get_system_info("ckan.site_title")
assert after_update == "My Updated Site Title"
# test value after update with default
after_update_default = get_system_info(
"ckan.site_title", config["ckan.site_title"]
)
assert after_update_default == "My Updated Site Title"
# title tag contains new value
home_page_after = app.get("/", status=200)
assert "Welcome - My Updated Site Title" in home_page_after