Completed
Push — master ( a4d3c4...6088ec )
by Chris
01:09
created

get_json_config()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 5
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_view_invalid_id_redirect(monkeypatch, ctx, client):
182
    app, test = client
183
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
184
    res = test.get(url_for('jsondash.view', c_id='123'))
185
    assert REDIRECT_MSG in res.data
186
187
188
def test_get_dashboard_contains_all_chart_types_list(monkeypatch, ctx, client):
189
    app, test = client
190
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
191
    res = test.get(url_for('jsondash.dashboard'))
192
    for family, config in settings.CHARTS_CONFIG.items():
193
        for chart in config['charts']:
194
            _, label = chart
195
            assert label in res.data
196
197
198
def test_get_dashboard_contains_no_chart_msg(monkeypatch, ctx, client):
199
    app, test = client
200
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
201
    res = test.get(url_for('jsondash.dashboard'))
202
    assert 'No dashboards exist. Create one below to get started.' in res.data
203
204
205
def test_get_view_valid_id_invalid_config(monkeypatch, ctx, client):
206
    app, test = client
207
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
208
    view = dict(modules=[dict(foo='bar')])
209
    readfunc = read(override=dict(view))
210
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
211
    with pytest.raises(ValueError):
212
        res = test.get(url_for('jsondash.view', c_id='123'))
213
        assert 'Invalid config!' in res.data
214
215
216
def test_get_view_valid_modules_count(monkeypatch, ctx, client):
217
    app, test = client
218
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
219
    view = get_json_config('inputs.json')
220
    readfunc = read(override=dict(view))
221
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
222
    res = test.get(url_for('jsondash.view', c_id=view['id']))
223
    dom = pq(res.data)
224
    assert len(dom.find('.item')) == len(view['modules'])
225