Completed
Push — master ( 973ee3...d3e7d3 )
by Chris
01:05
created

test_update_valid()   A

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
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&amp;'
139
          'controls=0&amp;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_modules_count_and_inputs(monkeypatch, ctx, client):
210
    app, test = client
211
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
212
    view = get_json_config('inputs.json')
213
    readfunc = read(override=dict(view))
214
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
215
    res = test.get(url_for('jsondash.view', c_id=view['id']))
216 View Code Duplication
    dom = pq(res.data)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
217
    assert len(dom.find('.item')) == len(view['modules'])
218
    assert len(dom.find('.charts-input-icon')) == 1
219
220
221
def test_get_view_valid_modules_valid_dash_title(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)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
229
    assert dom.find('.dashboard-title').text() == view['name']
230
231
232
@pytest.mark.invalid_id_redirect
233
def test_get_view_invalid_id_redirect(monkeypatch, ctx, client):
234
    app, test = client
235
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
236
    res = test.get(url_for('jsondash.view', c_id='123'))
237
    assert REDIRECT_MSG in res.data
238
239
240
@pytest.mark.invalid_id_redirect
241
def test_clone_invalid_id_redirect(monkeypatch, ctx, client):
242
    app, test = client
243
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
244
    res = test.post(
245
        url_for('jsondash.clone', c_id='123'))
246
    assert REDIRECT_MSG in res.data
247
248
249
@pytest.mark.invalid_id_redirect
250
def test_delete_invalid_id_redirect(monkeypatch, ctx, client):
251
    app, test = client
252
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
253
    res = test.post(
254
        url_for('jsondash.delete', c_id='123'))
255
    assert REDIRECT_MSG in res.data
256
257
258
@pytest.mark.invalid_id_redirect
259
def test_update_invalid_id_redirect(monkeypatch, ctx, client):
260
    app, test = client
261
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
262
    res = test.post(
263
        url_for('jsondash.update', c_id='123'))
264
    assert REDIRECT_MSG in res.data
265
266
267
def test_update_valid(monkeypatch, ctx, client):
268
    app, test = client
269
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
270
    view = get_json_config('inputs.json')
271
    readfunc = read(override=dict(view))
272
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
273
    res = test.post(
274
        url_for('jsondash.update', c_id=view['id']),
275
        data=dict(name='newname'),
276
        follow_redirects=True)
277
    dom = pq(res.data)
278
    flash_msg = 'Updated view "{}"'.format(view['id'])
279
    assert dom.find('.alert-info').text() == flash_msg
280