|
1
|
|
|
import json |
|
2
|
|
|
import os |
|
3
|
|
|
from datetime import datetime as dt |
|
4
|
|
|
|
|
5
|
|
|
from flask import ( |
|
6
|
|
|
Flask, |
|
7
|
|
|
current_app, |
|
8
|
|
|
url_for, |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
from pyquery import PyQuery as pq |
|
12
|
|
|
|
|
13
|
|
|
import pytest |
|
14
|
|
|
|
|
15
|
|
|
from conftest import ( |
|
16
|
|
|
URL_BASE, |
|
17
|
|
|
auth_ok, |
|
18
|
|
|
read, |
|
19
|
|
|
update, |
|
20
|
|
|
) |
|
21
|
|
|
|
|
22
|
|
|
from flask_jsondash import charts_builder |
|
23
|
|
|
from flask_jsondash import settings |
|
24
|
|
|
|
|
25
|
|
|
REDIRECT_MSG = 'You should be redirected automatically' |
|
26
|
|
|
cwd = os.getcwd() |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def get_json_config(name): |
|
30
|
|
|
parent = cwd.replace('tests/', '') |
|
31
|
|
|
path = '{0}/example_app/examples/config/{1}'.format(parent, name) |
|
32
|
|
|
view = json.load(open(path, 'rb+')) |
|
33
|
|
|
return view |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
@pytest.mark.globalflag |
|
37
|
|
|
def test_no_config_sanity_test(ctx, client): |
|
38
|
|
|
app, test = client |
|
39
|
|
|
assert not app.config.get('JSONDASH_GLOBALDASH') |
|
40
|
|
|
assert not app.config.get('JSONDASH_FILTERUSERS') |
|
41
|
|
|
assert app.config.get('JSONDASH_GLOBAL_USER') == 'global-test' |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
@pytest.mark.globalflag |
|
45
|
|
|
def test_setting(ctx, client): |
|
46
|
|
|
app, test = client |
|
47
|
|
|
_get = charts_builder.setting |
|
48
|
|
|
assert not _get('JSONDASH_GLOBALDASH') |
|
49
|
|
|
assert not _get('JSONDASH_FILTERUSERS') |
|
50
|
|
|
assert _get('JSONDASH_GLOBAL_USER') == 'global-test' |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
@pytest.mark.globalflag |
|
54
|
|
|
def test_is_global_dashboard_true(ctx, client): |
|
55
|
|
|
app, test = client |
|
56
|
|
|
app.config.update(JSONDASH_GLOBALDASH=True) |
|
57
|
|
|
assert charts_builder.is_global_dashboard( |
|
58
|
|
|
dict(created_by='global-test')) |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
@pytest.mark.globalflag |
|
62
|
|
|
def test_is_global_dashboard_false(ctx, client): |
|
63
|
|
|
app, test = client |
|
64
|
|
|
is_global = charts_builder.is_global_dashboard |
|
65
|
|
|
assert not is_global(dict(created_by='foo')) |
|
66
|
|
|
assert not is_global(dict(created_by='Username')) |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
@pytest.mark.auth |
|
70
|
|
|
def test_auth_false_realauth(ctx, client): |
|
71
|
|
|
app, test = client |
|
72
|
|
|
assert not charts_builder.auth(authtype='create') |
|
73
|
|
|
assert not charts_builder.auth(authtype='view') |
|
74
|
|
|
assert not charts_builder.auth(authtype='delete') |
|
75
|
|
|
assert not charts_builder.auth(authtype='clone') |
|
76
|
|
|
assert not charts_builder.auth(authtype='edit_global') |
|
77
|
|
|
assert not charts_builder.auth(authtype='edit_others') |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
@pytest.mark.auth |
|
81
|
|
|
def test_auth_true_realauth(ctx, client): |
|
82
|
|
|
app, test = client |
|
83
|
|
|
def authfunc(*args): |
|
84
|
|
|
return True |
|
85
|
|
|
|
|
86
|
|
|
app.config['JSONDASH']['auth'] = dict( |
|
87
|
|
|
clone=authfunc, |
|
88
|
|
|
edit_global=authfunc, |
|
89
|
|
|
create=authfunc, |
|
90
|
|
|
delete=authfunc, |
|
91
|
|
|
view=authfunc, |
|
92
|
|
|
) |
|
93
|
|
|
assert charts_builder.auth(authtype='create') |
|
94
|
|
|
assert charts_builder.auth(authtype='view') |
|
95
|
|
|
assert charts_builder.auth(authtype='delete') |
|
96
|
|
|
assert charts_builder.auth(authtype='clone') |
|
97
|
|
|
assert charts_builder.auth(authtype='edit_global') |
|
98
|
|
|
assert charts_builder.auth(authtype='edit_others') |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
@pytest.mark.auth |
|
102
|
|
|
def test_auth_true_fakeauth(ctx, client): |
|
103
|
|
|
app, test = client |
|
104
|
|
|
assert charts_builder.auth(authtype=None) |
|
105
|
|
|
assert charts_builder.auth(authtype='foo') |
|
106
|
|
|
assert charts_builder.metadata(key='foo') is None |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
def test_metadata(ctx, client): |
|
110
|
|
|
app, test = client |
|
111
|
|
|
assert charts_builder.metadata() == dict( |
|
112
|
|
|
username='Username', |
|
113
|
|
|
created_by='Username', |
|
114
|
|
|
) |
|
115
|
|
|
assert charts_builder.metadata(key='username') == 'Username' |
|
116
|
|
|
assert charts_builder.metadata(key='created_by') == 'Username' |
|
117
|
|
|
assert charts_builder.metadata(exclude='created_by') == dict( |
|
118
|
|
|
username='Username' |
|
119
|
|
|
) |
|
120
|
|
|
assert charts_builder.metadata(exclude='username') == dict( |
|
121
|
|
|
created_by='Username' |
|
122
|
|
|
) |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
@pytest.mark.filters |
|
126
|
|
|
def test_getdims_normal(ctx, client): |
|
127
|
|
|
app, test = client |
|
128
|
|
|
data = dict(width=100, height=100, type='foo') |
|
129
|
|
|
expected = dict(width=100, height=100) |
|
130
|
|
|
assert charts_builder.get_dims(object, data) == expected |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
@pytest.mark.filters |
|
134
|
|
|
def test_getdims_youtube(ctx, client): |
|
135
|
|
|
app, test = client |
|
136
|
|
|
yt = ('<iframe width="650" height="366" ' |
|
137
|
|
|
'src="https://www.youtube.com/embed/' |
|
138
|
|
|
'_hI0qMtdfng?list=RD_hI0qMtdfng&' |
|
139
|
|
|
'controls=0&showinfo=0" frameborder="0"' |
|
140
|
|
|
' allowfullscreen></iframe>') |
|
141
|
|
|
data = dict(type='youtube', dataSource=yt, width=100, height=100) |
|
142
|
|
|
expected = dict(width=650 + 20, height=366 + 60) |
|
143
|
|
|
assert charts_builder.get_dims(object, data) == expected |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
@pytest.mark.filters |
|
147
|
|
|
def test_jsonstring(ctx, client): |
|
148
|
|
|
app, test = client |
|
149
|
|
|
now = dt.now() |
|
150
|
|
|
data = dict(date=now, foo='bar') |
|
151
|
|
|
res = charts_builder.jsonstring(object, data) |
|
152
|
|
|
assert 'foo' in res |
|
153
|
|
|
assert isinstance(res, str) |
|
154
|
|
|
d = json.loads(res) |
|
155
|
|
|
assert isinstance(d['date'], unicode) |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
@pytest.mark.utils |
|
159
|
|
|
def test_order_sort(): |
|
160
|
|
|
item = dict() |
|
161
|
|
|
assert charts_builder.order_sort(item) == item |
|
162
|
|
|
item = dict(order=1) |
|
163
|
|
|
assert charts_builder.order_sort(item) == 1 |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
def test_app_redirects(ctx, client): |
|
167
|
|
|
app, test = client |
|
168
|
|
|
res = test.get('/charts') |
|
169
|
|
|
assert REDIRECT_MSG in res.data |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
def test_routes(ctx, client): |
|
173
|
|
|
assert url_for('jsondash.dashboard') == '/charts/' |
|
174
|
|
|
assert url_for('jsondash.view', c_id='foo') == '/charts/foo' |
|
175
|
|
|
assert url_for('jsondash.update', c_id='foo') == '/charts/foo/update' |
|
176
|
|
|
assert url_for('jsondash.clone', c_id='foo') == '/charts/foo/clone' |
|
177
|
|
|
assert url_for('jsondash.delete', c_id='foo') == '/charts/foo/delete' |
|
178
|
|
|
assert url_for('jsondash.create') == '/charts/create' |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
def test_get_dashboard_contains_all_chart_types_list(monkeypatch, ctx, client): |
|
182
|
|
|
app, test = client |
|
183
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
184
|
|
|
res = test.get(url_for('jsondash.dashboard')) |
|
185
|
|
|
for family, config in settings.CHARTS_CONFIG.items(): |
|
186
|
|
|
for chart in config['charts']: |
|
187
|
|
|
_, label = chart |
|
188
|
|
|
assert label in res.data |
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
def test_get_dashboard_contains_no_chart_msg(monkeypatch, ctx, client): |
|
192
|
|
|
app, test = client |
|
193
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
194
|
|
|
res = test.get(url_for('jsondash.dashboard')) |
|
195
|
|
|
assert 'No dashboards exist. Create one below to get started.' in res.data |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
def test_get_view_valid_id_invalid_config(monkeypatch, ctx, client): |
|
199
|
|
|
app, test = client |
|
200
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
201
|
|
|
view = dict(modules=[dict(foo='bar')]) |
|
202
|
|
|
readfunc = read(override=dict(view)) |
|
203
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
204
|
|
|
with pytest.raises(ValueError): |
|
205
|
|
|
res = test.get(url_for('jsondash.view', c_id='123')) |
|
206
|
|
|
assert 'Invalid config!' in res.data |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
def test_get_view_valid_id_invalid_modules(monkeypatch, ctx, client): |
|
210
|
|
|
app, test = client |
|
211
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
212
|
|
|
view = dict(id='123') |
|
213
|
|
|
readfunc = read(override=dict(view)) |
|
214
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
215
|
|
|
res = test.get( |
|
216
|
|
View Code Duplication |
url_for('jsondash.view', c_id='123'), |
|
|
|
|
|
|
217
|
|
|
follow_redirects=True) |
|
218
|
|
|
assert 'Invalid configuration - missing modules.' in res.data |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
def test_get_view_valid_modules_count_and_inputs(monkeypatch, ctx, client): |
|
222
|
|
|
app, test = client |
|
223
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
224
|
|
|
view = get_json_config('inputs.json') |
|
225
|
|
|
readfunc = read(override=dict(view)) |
|
226
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
227
|
|
|
res = test.get(url_for('jsondash.view', c_id=view['id'])) |
|
228
|
|
View Code Duplication |
dom = pq(res.data) |
|
|
|
|
|
|
229
|
|
|
assert len(dom.find('.item')) == len(view['modules']) |
|
230
|
|
|
assert len(dom.find('.charts-input-icon')) == 1 |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
def test_get_view_valid_modules_valid_dash_title(monkeypatch, ctx, client): |
|
234
|
|
|
app, test = client |
|
235
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
236
|
|
|
view = get_json_config('inputs.json') |
|
237
|
|
|
readfunc = read(override=dict(view)) |
|
238
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
239
|
|
|
res = test.get(url_for('jsondash.view', c_id=view['id'])) |
|
240
|
|
|
dom = pq(res.data) |
|
241
|
|
|
assert dom.find('.dashboard-title').text() == view['name'] |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
@pytest.mark.invalid_id_redirect |
|
245
|
|
|
def test_get_view_invalid_id_redirect(monkeypatch, ctx, client): |
|
246
|
|
|
app, test = client |
|
247
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
248
|
|
|
res = test.get(url_for('jsondash.view', c_id='123')) |
|
249
|
|
|
assert REDIRECT_MSG in res.data |
|
250
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
def test_create_valid(monkeypatch, ctx, client): |
|
253
|
|
|
app, test = client |
|
254
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
255
|
|
|
res = test.post( |
|
256
|
|
|
url_for('jsondash.create'), |
|
257
|
|
|
data=dict(name='mydash-valid'), |
|
258
|
|
|
follow_redirects=True) |
|
259
|
|
|
dom = pq(res.data) |
|
260
|
|
|
flash_msg = 'Created new dashboard "mydash-valid"' |
|
261
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
@pytest.mark.invalid_id_redirect |
|
265
|
|
|
def test_clone_invalid_id_redirect(monkeypatch, ctx, client): |
|
266
|
|
|
app, test = client |
|
267
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
268
|
|
|
res = test.post(url_for('jsondash.clone', c_id='123')) |
|
269
|
|
|
assert REDIRECT_MSG in res.data |
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
def test_clone_valid(monkeypatch, ctx, client): |
|
273
|
|
|
app, test = client |
|
274
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
275
|
|
|
assert len(read()) == 0 |
|
276
|
|
|
res = test.post(url_for('jsondash.create'), |
|
277
|
|
|
data=dict(name='mydash', modules=[]), |
|
278
|
|
|
follow_redirects=True) |
|
279
|
|
|
dom = pq(res.data) |
|
280
|
|
|
new_id = read()[0]['id'] |
|
281
|
|
|
assert read()[0]['name'] == 'mydash' |
|
282
|
|
|
flash_msg = 'Created new dashboard "mydash"' |
|
283
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
284
|
|
|
assert len(read()) == 1 |
|
285
|
|
|
assert read()[0]['name'] == 'mydash' |
|
286
|
|
|
res = test.post( |
|
287
|
|
|
url_for('jsondash.clone', c_id=new_id), |
|
288
|
|
|
follow_redirects=True) |
|
289
|
|
|
dom = pq(res.data) |
|
290
|
|
|
flash_msg = 'Created new dashboard clone "Clone of mydash"' |
|
291
|
|
|
assert flash_msg in dom.find('.alert').text() |
|
292
|
|
|
assert len(read()) == 2 |
|
293
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
@pytest.mark.invalid_id_redirect |
|
296
|
|
|
def test_delete_invalid_id_redirect(monkeypatch, ctx, client): |
|
297
|
|
|
app, test = client |
|
298
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
299
|
|
|
res = test.post( |
|
300
|
|
|
url_for('jsondash.delete', c_id='123')) |
|
301
|
|
|
assert REDIRECT_MSG in res.data |
|
302
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
def test_delete_valid(monkeypatch, ctx, client): |
|
305
|
|
|
app, test = client |
|
306
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
307
|
|
|
view = dict(name='mydash', modules=[]) |
|
308
|
|
|
readfunc = read(override=dict(view)) |
|
309
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
310
|
|
|
assert not read() |
|
311
|
|
|
# Create first one. |
|
312
|
|
|
res = test.post(url_for('jsondash.create'), |
|
313
|
|
|
data=view, |
|
314
|
|
|
follow_redirects=True) |
|
315
|
|
|
assert len(read()) == 1 |
|
316
|
|
|
view_id = read()[0]['id'] |
|
317
|
|
|
dom = pq(res.data) |
|
318
|
|
|
flash_msg = 'Created new dashboard "mydash"' |
|
319
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
320
|
|
|
assert len(read()) == 1 |
|
321
|
|
|
res = test.post(url_for('jsondash.delete', c_id=view_id), |
|
322
|
|
|
follow_redirects=True) |
|
323
|
|
|
dom = pq(res.data) |
|
324
|
|
|
flash_msg = 'Deleted dashboard "{}"'.format(view_id) |
|
325
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
326
|
|
|
assert len(read()) == 0 |
|
327
|
|
|
|
|
328
|
|
|
|
|
329
|
|
|
@pytest.mark.invalid_id_redirect |
|
330
|
|
|
def test_update_invalid_id_redirect(monkeypatch, ctx, client): |
|
331
|
|
|
app, test = client |
|
332
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
333
|
|
|
res = test.post(url_for('jsondash.update', c_id='123')) |
|
334
|
|
|
assert REDIRECT_MSG in res.data |
|
335
|
|
|
|
|
336
|
|
|
|
|
337
|
|
|
def test_update_invalid_config(monkeypatch, ctx, client): |
|
338
|
|
|
app, test = client |
|
339
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
340
|
|
|
view = get_json_config('inputs.json') |
|
341
|
|
|
readfunc = read(override=dict(view)) |
|
342
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
343
|
|
|
res = test.post( |
|
344
|
|
|
url_for('jsondash.update', c_id=view['id']), |
|
345
|
|
|
data={'edit-raw': 'on'}, |
|
346
|
|
|
follow_redirects=True) |
|
347
|
|
|
dom = pq(res.data) |
|
348
|
|
|
assert dom.find('.alert-danger').text() == 'Error: Invalid JSON config.' |
|
349
|
|
|
|
|
350
|
|
|
|
|
351
|
|
|
def test_update_valid(monkeypatch, ctx, client): |
|
352
|
|
|
app, test = client |
|
353
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
|
354
|
|
|
assert not read() |
|
355
|
|
|
res = test.post( |
|
356
|
|
|
url_for('jsondash.create'), |
|
357
|
|
|
data=dict(name='newname', modules=[]), |
|
358
|
|
|
follow_redirects=True) |
|
359
|
|
|
assert len(read()) == 1 |
|
360
|
|
|
view_id = read()[0]['id'] |
|
361
|
|
|
res = test.post( |
|
362
|
|
|
url_for('jsondash.update', c_id=view_id), |
|
363
|
|
|
data=dict(name='newname'), |
|
364
|
|
|
follow_redirects=True) |
|
365
|
|
|
dom = pq(res.data) |
|
366
|
|
|
flash_msg = 'Updated view "{}"'.format(view_id) |
|
367
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
368
|
|
|
assert len(read()) == 1 |
|
369
|
|
|
|