Completed
Push — master ( 636c5d...905128 )
by Chris
01:05
created

test_update_invalid_config()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

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