|
1
|
|
|
from flask import url_for |
|
2
|
|
|
|
|
3
|
|
|
from pyquery import PyQuery as pq |
|
4
|
|
|
|
|
5
|
|
|
import pytest |
|
6
|
|
|
|
|
7
|
|
|
from conftest import ( |
|
8
|
|
|
get_json_config, |
|
9
|
|
|
auth_valid, |
|
10
|
|
|
read, |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
from flask_jsondash import charts_builder |
|
14
|
|
|
from flask_jsondash import settings |
|
15
|
|
|
|
|
16
|
|
|
REDIRECT_MSG = 'You should be redirected automatically' |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def test_app_redirects(ctx, client): |
|
20
|
|
|
app, test = client |
|
21
|
|
|
res = test.get('/charts') |
|
22
|
|
|
assert REDIRECT_MSG in str(res.data) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def test_routes(ctx, client): |
|
26
|
|
|
assert url_for('jsondash.dashboard') == '/charts/' |
|
27
|
|
|
assert url_for('jsondash.view', c_id='foo') == '/charts/foo' |
|
28
|
|
|
assert url_for('jsondash.update', c_id='foo') == '/charts/foo/update' |
|
29
|
|
|
assert url_for('jsondash.clone', c_id='foo') == '/charts/foo/clone' |
|
30
|
|
|
assert url_for('jsondash.delete', c_id='foo') == '/charts/foo/delete' |
|
31
|
|
|
assert url_for('jsondash.create') == '/charts/create' |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def test_get_dashboard_contains_all_chart_types_list(monkeypatch, ctx, client): |
|
35
|
|
|
app, test = client |
|
36
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
37
|
|
|
res = test.get(url_for('jsondash.dashboard')) |
|
38
|
|
|
for family, config in settings.CHARTS_CONFIG.items(): |
|
39
|
|
|
for chart in config['charts']: |
|
40
|
|
|
_, label = chart |
|
41
|
|
|
assert label in str(res.data) |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def test_get_dashboard_contains_no_chart_msg(monkeypatch, ctx, client): |
|
45
|
|
|
app, test = client |
|
46
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
47
|
|
|
res = str(test.get(url_for('jsondash.dashboard')).data) |
|
48
|
|
|
assert 'No dashboards exist. Create one below to get started.' in res |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
def test_dashboards_override_perpage_pagination(monkeypatch, ctx, client): |
|
|
|
|
|
|
52
|
|
|
app, test = client |
|
53
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
54
|
|
|
for i in range(10): |
|
55
|
|
|
data = dict(name=i, modules=[]) |
|
56
|
|
|
res = test.post(url_for('jsondash.create'), data=data) |
|
57
|
|
|
res = test.get(url_for('jsondash.dashboard') + '?per_page=2') |
|
58
|
|
|
# Ensure 10 exist, but only 5 are shown |
|
59
|
|
|
assert len(read()) == 10 |
|
60
|
|
|
dom = pq(res.data) |
|
61
|
|
|
assert len(dom.find('.pagination').find('li:not(.active)')) == 5 |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
def test_create_dashboards_check_index_count(monkeypatch, ctx, client): |
|
|
|
|
|
|
65
|
|
|
app, test = client |
|
66
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
67
|
|
|
for i in range(10): |
|
68
|
|
|
data = dict(name=i, modules=[]) |
|
69
|
|
|
res = test.post(url_for('jsondash.create'), data=data) |
|
70
|
|
|
res = test.get(url_for('jsondash.dashboard')) |
|
71
|
|
|
assert len(read()) == 10 |
|
72
|
|
|
heading = pq(res.data).find('h1.lead').text() |
|
73
|
|
|
assert 'Showing 10 dashboards with 0 charts' in heading |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
View Code Duplication |
def test_get_view_valid_id_invalid_config(monkeypatch, ctx, client): |
|
|
|
|
|
|
77
|
|
|
app, test = client |
|
78
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
79
|
|
|
view = dict(modules=[dict(foo='bar')]) |
|
80
|
|
|
readfunc = read(override=dict(view)) |
|
81
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
82
|
|
|
with pytest.raises(ValueError): |
|
83
|
|
|
res = test.get(url_for('jsondash.view', c_id='123')) |
|
84
|
|
|
assert 'Invalid config!' in str(res.data) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
def test_get_view_valid_id_invalid_modules(monkeypatch, ctx, client): |
|
88
|
|
|
app, test = client |
|
89
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
90
|
|
|
view = dict(id='123') |
|
91
|
|
|
readfunc = read(override=dict(view)) |
|
92
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
93
|
|
|
res = test.get( |
|
94
|
|
|
url_for('jsondash.view', c_id='123'), |
|
95
|
|
|
follow_redirects=True) |
|
96
|
|
|
assert 'Invalid configuration - missing modules.' in str(res.data) |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
def test_get_view_valid_id_ensure__id_popped(monkeypatch, ctx, client): |
|
100
|
|
|
app, test = client |
|
101
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
102
|
|
|
view = get_json_config('inputs.json') |
|
103
|
|
|
view = dict(view) |
|
104
|
|
|
view.update(_id='foo') |
|
105
|
|
|
readfunc = read(override=view) |
|
106
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
107
|
|
|
res = test.get(url_for('jsondash.view', c_id=view['id'])) |
|
108
|
|
|
dom = pq(res.data) |
|
109
|
|
|
assert len(dom.find('.item')) == len(view['modules']) |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
def test_view_valid_dashboard_count_and_inputs(monkeypatch, ctx, client): |
|
113
|
|
|
app, test = client |
|
114
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
115
|
|
|
view = get_json_config('inputs.json') |
|
116
|
|
|
readfunc = read(override=dict(view)) |
|
117
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
118
|
|
|
res = test.get(url_for('jsondash.view', c_id=view['id'])) |
|
119
|
|
|
dom = pq(res.data) |
|
120
|
|
|
assert len(dom.find('.item')) == len(view['modules']) |
|
121
|
|
|
assert len(dom.find('.charts-input-icon')) == 1 |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
def test_view_valid_dashboard_inputs_form(monkeypatch, ctx, client): |
|
125
|
|
|
app, test = client |
|
126
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
127
|
|
|
view = get_json_config('inputs.json') |
|
128
|
|
|
readfunc = read(override=dict(view)) |
|
129
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
130
|
|
|
res = test.get(url_for('jsondash.view', c_id=view['id'])) |
|
131
|
|
|
dom = pq(res.data) |
|
132
|
|
|
charts_with_inputs = [m for m in view['modules'] if 'inputs' in m] |
|
133
|
|
|
num_config_inputs = len(charts_with_inputs[0]['inputs']['options']) |
|
134
|
|
|
assert num_config_inputs == 5 # Sanity check |
|
135
|
|
|
assert len(charts_with_inputs) == 1 |
|
136
|
|
|
assert len(dom.find('.chart-inputs')) == 1 |
|
137
|
|
|
assert dom.find('.chart-inputs').hasClass('collapse') |
|
138
|
|
|
# There are 7 input fields generated for this particular json file. |
|
139
|
|
|
assert len(dom.find('.chart-inputs form input')) == 7 |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
def test_view_valid_dashboard_inputs_form_counts(monkeypatch, ctx, client): |
|
143
|
|
|
app, test = client |
|
144
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
145
|
|
|
view = get_json_config('inputs.json') |
|
146
|
|
|
readfunc = read(override=dict(view)) |
|
147
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
148
|
|
|
res = test.get(url_for('jsondash.view', c_id=view['id'])) |
|
149
|
|
|
dom = pq(res.data) |
|
150
|
|
|
charts_with_inputs = [m for m in view['modules'] if 'inputs' in m] |
|
151
|
|
|
input_options = charts_with_inputs[0]['inputs']['options'] |
|
152
|
|
|
radio_opts = [o for o in input_options if o['type'] == 'radio'][0] |
|
153
|
|
|
radio_opts = radio_opts['options'] |
|
154
|
|
|
assert len(dom.find('.chart-inputs form .input-radio')) == len(radio_opts) |
|
155
|
|
|
select = [o for o in input_options if o['type'] == 'select'] |
|
156
|
|
|
assert len(dom.find('.chart-inputs form select')) == len(select) |
|
157
|
|
|
options = select[0]['options'] |
|
158
|
|
|
assert len(dom.find('.chart-inputs form select option')) == len(options) |
|
159
|
|
|
numbers = [inp for inp in input_options if inp['type'] == 'number'] |
|
160
|
|
|
assert len(dom.find('.chart-inputs [type="number"]')) == len(numbers) |
|
161
|
|
|
text = [inp for inp in input_options if inp['type'] == 'text'] |
|
162
|
|
|
assert len(dom.find('.chart-inputs [type="text"]')) == len(text) |
|
163
|
|
|
checkbox = [inp for inp in input_options if inp['type'] == 'checkbox'] |
|
164
|
|
|
assert len(dom.find('.chart-inputs [type="checkbox"]')) == len(checkbox) |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
def test_get_view_valid_modules_valid_dash_title(monkeypatch, ctx, client): |
|
168
|
|
|
app, test = client |
|
169
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
170
|
|
|
view = get_json_config('inputs.json') |
|
171
|
|
|
readfunc = read(override=dict(view)) |
|
172
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
173
|
|
|
res = test.get(url_for('jsondash.view', c_id=view['id'])) |
|
174
|
|
|
dom = pq(res.data) |
|
175
|
|
|
assert dom.find('.dashboard-title').text() == view['name'] |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
def test_create_valid(monkeypatch, ctx, client): |
|
179
|
|
|
app, test = client |
|
180
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
181
|
|
|
res = test.post( |
|
182
|
|
|
url_for('jsondash.create'), |
|
183
|
|
|
data=dict(name='mydash-valid'), |
|
184
|
|
|
follow_redirects=True) |
|
185
|
|
|
dom = pq(res.data) |
|
186
|
|
|
flash_msg = 'Created new dashboard "mydash-valid"' |
|
187
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
@pytest.mark.invalid_id_redirect |
|
191
|
|
|
def test_get_view_invalid_id_redirect(monkeypatch, ctx, client): |
|
192
|
|
|
app, test = client |
|
193
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
194
|
|
|
res = test.get(url_for('jsondash.view', c_id='123')) |
|
195
|
|
|
assert REDIRECT_MSG in str(res.data) |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
@pytest.mark.invalid_id_redirect |
|
199
|
|
|
def test_clone_invalid_id_redirect(monkeypatch, ctx, client): |
|
200
|
|
|
app, test = client |
|
201
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
202
|
|
|
res = test.post(url_for('jsondash.clone', c_id='123')) |
|
203
|
|
|
assert REDIRECT_MSG in str(res.data) |
|
204
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
@pytest.mark.invalid_id_redirect |
|
207
|
|
|
def test_delete_invalid_id_redirect(monkeypatch, ctx, client): |
|
208
|
|
|
app, test = client |
|
209
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
210
|
|
|
res = test.post( |
|
211
|
|
|
url_for('jsondash.delete', c_id='123')) |
|
212
|
|
|
assert REDIRECT_MSG in str(res.data) |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
@pytest.mark.invalid_id_redirect |
|
216
|
|
|
def test_update_invalid_id_redirect(monkeypatch, ctx, client): |
|
217
|
|
|
app, test = client |
|
218
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
219
|
|
|
res = test.post(url_for('jsondash.update', c_id='123')) |
|
220
|
|
|
assert REDIRECT_MSG in str(res.data) |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
def test_clone_valid(monkeypatch, ctx, client): |
|
224
|
|
|
app, test = client |
|
225
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
226
|
|
|
assert len(read()) == 0 |
|
227
|
|
|
res = test.post(url_for('jsondash.create'), |
|
228
|
|
|
data=dict(name='mydash', modules=[]), |
|
229
|
|
|
follow_redirects=True) |
|
230
|
|
|
dom = pq(res.data) |
|
231
|
|
|
new_id = read()[0]['id'] |
|
232
|
|
|
assert read()[0]['name'] == 'mydash' |
|
233
|
|
|
flash_msg = 'Created new dashboard "mydash"' |
|
234
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
235
|
|
|
assert len(read()) == 1 |
|
236
|
|
|
assert read()[0]['name'] == 'mydash' |
|
237
|
|
|
res = test.post( |
|
238
|
|
|
url_for('jsondash.clone', c_id=new_id), |
|
239
|
|
|
follow_redirects=True) |
|
240
|
|
|
dom = pq(res.data) |
|
241
|
|
|
flash_msg = 'Created new dashboard clone "Clone of mydash"' |
|
242
|
|
|
assert flash_msg in dom.find('.alert').text() |
|
243
|
|
|
assert len(read()) == 2 |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
def test_delete_valid(monkeypatch, ctx, client): |
|
247
|
|
|
app, test = client |
|
248
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
249
|
|
|
view = dict(name='mydash', modules=[]) |
|
250
|
|
|
readfunc = read(override=dict(view)) |
|
251
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
252
|
|
|
assert not read() |
|
253
|
|
|
# Create first one. |
|
254
|
|
|
res = test.post(url_for('jsondash.create'), |
|
255
|
|
|
data=view, |
|
256
|
|
|
follow_redirects=True) |
|
257
|
|
|
assert len(read()) == 1 |
|
258
|
|
|
view_id = read()[0]['id'] |
|
259
|
|
|
dom = pq(res.data) |
|
260
|
|
|
flash_msg = 'Created new dashboard "mydash"' |
|
261
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
262
|
|
|
assert len(read()) == 1 |
|
263
|
|
|
res = test.post(url_for('jsondash.delete', c_id=view_id), |
|
264
|
|
|
follow_redirects=True) |
|
265
|
|
|
dom = pq(res.data) |
|
266
|
|
|
flash_msg = 'Deleted dashboard "{}"'.format(view_id) |
|
267
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
268
|
|
|
assert len(read()) == 0 |
|
269
|
|
|
|
|
270
|
|
|
|
|
271
|
|
View Code Duplication |
def test_update_invalid_config(monkeypatch, ctx, client): |
|
|
|
|
|
|
272
|
|
|
app, test = client |
|
273
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
274
|
|
|
view = get_json_config('inputs.json') |
|
275
|
|
|
readfunc = read(override=dict(view)) |
|
276
|
|
|
monkeypatch.setattr(charts_builder.adapter, 'read', readfunc) |
|
277
|
|
|
res = test.post( |
|
278
|
|
|
url_for('jsondash.update', c_id=view['id']), |
|
279
|
|
|
data={'edit-raw': 'on'}, |
|
280
|
|
|
follow_redirects=True) |
|
281
|
|
|
dom = pq(res.data) |
|
282
|
|
|
assert dom.find('.alert-danger').text() == 'Error: Invalid JSON config.' |
|
283
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
def test_update_valid(monkeypatch, ctx, client): |
|
286
|
|
|
app, test = client |
|
287
|
|
|
monkeypatch.setattr(charts_builder, 'auth', auth_valid) |
|
288
|
|
|
assert not read() |
|
289
|
|
|
res = test.post( |
|
290
|
|
|
url_for('jsondash.create'), |
|
291
|
|
|
data=dict(name='newname', modules=[]), |
|
292
|
|
|
follow_redirects=True) |
|
293
|
|
|
assert len(read()) == 1 |
|
294
|
|
|
view_id = read()[0]['id'] |
|
295
|
|
|
res = test.post( |
|
296
|
|
|
url_for('jsondash.update', c_id=view_id), |
|
297
|
|
|
data=dict(name='newname'), |
|
298
|
|
|
follow_redirects=True) |
|
299
|
|
|
dom = pq(res.data) |
|
300
|
|
|
flash_msg = 'Updated view "{}"'.format(view_id) |
|
301
|
|
|
assert dom.find('.alert-info').text() == flash_msg |
|
302
|
|
|
assert len(read()) == 1 |
|
303
|
|
|
|