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
|
|
|
@pytest.mark.invalid_id_redirect |
253
|
|
|
def test_clone_invalid_id_redirect(monkeypatch, ctx, client): |
254
|
|
|
app, test = client |
255
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
256
|
|
|
res = test.post( |
257
|
|
|
url_for('jsondash.clone', c_id='123')) |
258
|
|
|
assert REDIRECT_MSG in res.data |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
@pytest.mark.invalid_id_redirect |
262
|
|
|
def test_delete_invalid_id_redirect(monkeypatch, ctx, client): |
263
|
|
|
app, test = client |
264
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
265
|
|
|
res = test.post( |
266
|
|
|
url_for('jsondash.delete', c_id='123')) |
267
|
|
|
assert REDIRECT_MSG in res.data |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
@pytest.mark.invalid_id_redirect |
271
|
|
|
def test_update_invalid_id_redirect(monkeypatch, ctx, client): |
272
|
|
|
app, test = client |
273
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
274
|
|
|
res = test.post( |
275
|
|
|
url_for('jsondash.update', c_id='123')) |
276
|
|
|
assert REDIRECT_MSG in res.data |
277
|
|
|
|
278
|
|
|
|
279
|
|
View Code Duplication |
def test_update_invalid_config(monkeypatch, ctx, client): |
|
|
|
|
280
|
|
|
app, test = client |
281
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
282
|
|
|
view = get_json_config('inputs.json') |
283
|
|
|
readfunc = read(override=dict(view)) |
284
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
285
|
|
|
res = test.post( |
286
|
|
|
url_for('jsondash.update', c_id=view['id']), |
287
|
|
|
data={'edit-raw': 'on'}, |
288
|
|
|
follow_redirects=True) |
289
|
|
|
dom = pq(res.data) |
290
|
|
|
assert dom.find('.alert-danger').text() == 'Error: Invalid JSON config.' |
291
|
|
|
|
292
|
|
|
|
293
|
|
View Code Duplication |
def test_update_valid(monkeypatch, ctx, client): |
|
|
|
|
294
|
|
|
app, test = client |
295
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_ok) |
296
|
|
|
view = get_json_config('inputs.json') |
297
|
|
|
readfunc = read(override=dict(view)) |
298
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
299
|
|
|
res = test.post( |
300
|
|
|
url_for('jsondash.update', c_id=view['id']), |
301
|
|
|
data=dict(name='newname'), |
302
|
|
|
follow_redirects=True) |
303
|
|
|
dom = pq(res.data) |
304
|
|
|
flash_msg = 'Updated view "{}"'.format(view['id']) |
305
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
306
|
|
|
|