1
|
|
|
import json |
2
|
|
|
from datetime import datetime as dt |
3
|
|
|
|
4
|
|
|
from flask import ( |
5
|
|
|
Flask, |
6
|
|
|
current_app, |
7
|
|
|
url_for, |
8
|
|
|
) |
9
|
|
|
import pytest |
10
|
|
|
|
11
|
|
|
from flask_jsondash import settings |
12
|
|
|
from flask_jsondash import charts_builder |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
URL_BASE = 'http://127.0.0.1:80' |
16
|
|
|
app = Flask('test_flask_jsondash') |
17
|
|
|
app.secret_key = '123' |
18
|
|
|
app.debug = True |
19
|
|
|
app.register_blueprint(charts_builder.charts) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def _username(): |
23
|
|
|
return 'Username' |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def _authtest(**kwargs): |
27
|
|
|
return False |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
@pytest.fixture() |
31
|
|
|
def client(): |
32
|
|
|
app.config.update( |
33
|
|
|
JSONDASH_GLOBALDASH=False, |
34
|
|
|
JSONDASH_FILTERUSERS=False, |
35
|
|
|
JSONDASH_GLOBAL_USER='global-test', |
36
|
|
|
) |
37
|
|
|
app.config['JSONDASH'] = dict( |
38
|
|
|
metadata=dict( |
39
|
|
|
created_by=_username, |
40
|
|
|
username=_username, |
41
|
|
|
), |
42
|
|
|
static=dict( |
43
|
|
|
js_path='js/vendor/', |
44
|
|
|
css_path='css/vendor/', |
45
|
|
|
), |
46
|
|
|
auth=dict( |
47
|
|
|
edit_others=_authtest, |
48
|
|
|
edit_global=_authtest, |
49
|
|
|
create=_authtest, |
50
|
|
|
view=_authtest, |
51
|
|
|
clone=_authtest, |
52
|
|
|
delete=_authtest, |
53
|
|
|
) |
54
|
|
|
) |
55
|
|
|
return app.test_client() |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def test_no_config_sanity_test(client): |
59
|
|
|
assert not app.config.get('JSONDASH_GLOBALDASH') |
60
|
|
|
assert not app.config.get('JSONDASH_FILTERUSERS') |
61
|
|
|
assert app.config.get('JSONDASH_GLOBAL_USER') == 'global-test' |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def test_setting(client): |
65
|
|
|
with app.app_context(): |
66
|
|
|
_get = charts_builder.setting |
67
|
|
|
assert not _get('JSONDASH_GLOBALDASH') |
68
|
|
|
assert not _get('JSONDASH_FILTERUSERS') |
69
|
|
|
assert _get('JSONDASH_GLOBAL_USER') == 'global-test' |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def test_is_global_dashboard_true(client): |
73
|
|
|
with app.app_context(): |
74
|
|
|
app.config.update(JSONDASH_GLOBALDASH=True) |
75
|
|
|
assert charts_builder.is_global_dashboard( |
76
|
|
|
dict(created_by='global-test')) |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def test_is_global_dashboard_false(client): |
80
|
|
|
with app.app_context(): |
81
|
|
|
is_global = charts_builder.is_global_dashboard |
82
|
|
|
assert not is_global(dict(created_by='foo')) |
83
|
|
|
assert not is_global(dict(created_by='Username')) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def test_auth_false_realauth(): |
87
|
|
|
with app.app_context(): |
88
|
|
|
assert not charts_builder.auth(authtype='create') |
89
|
|
|
assert not charts_builder.auth(authtype='view') |
90
|
|
|
assert not charts_builder.auth(authtype='delete') |
91
|
|
|
assert not charts_builder.auth(authtype='clone') |
92
|
|
|
assert not charts_builder.auth(authtype='edit_global') |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_auth_true_realauth(): |
96
|
|
|
with app.app_context(): |
97
|
|
|
def authfunc(*args): |
98
|
|
|
return True |
99
|
|
|
|
100
|
|
|
app.config['JSONDASH']['auth'] = dict( |
101
|
|
|
clone=authfunc, |
102
|
|
|
edit_global=authfunc, |
103
|
|
|
create=authfunc, |
104
|
|
|
delete=authfunc, |
105
|
|
|
view=authfunc, |
106
|
|
|
) |
107
|
|
|
assert charts_builder.auth(authtype='create') |
108
|
|
|
assert charts_builder.auth(authtype='view') |
109
|
|
|
assert charts_builder.auth(authtype='delete') |
110
|
|
|
assert charts_builder.auth(authtype='clone') |
111
|
|
|
assert charts_builder.auth(authtype='edit_global') |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
def test_auth_true_fakeauth(): |
115
|
|
|
with app.app_context(): |
116
|
|
|
assert charts_builder.auth(authtype=None) |
117
|
|
|
assert charts_builder.auth(authtype='foo') |
118
|
|
|
assert charts_builder.metadata(key='foo') is None |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
def test_metadata(): |
122
|
|
|
with app.app_context(): |
123
|
|
|
assert charts_builder.metadata() == dict( |
124
|
|
|
username='Username', |
125
|
|
|
created_by='Username', |
126
|
|
|
) |
127
|
|
|
assert charts_builder.metadata(key='username') == 'Username' |
128
|
|
|
assert charts_builder.metadata(key='created_by') == 'Username' |
129
|
|
|
assert charts_builder.metadata(exclude='created_by') == dict( |
130
|
|
|
username='Username' |
131
|
|
|
) |
132
|
|
|
assert charts_builder.metadata(exclude='username') == dict( |
133
|
|
|
created_by='Username' |
134
|
|
|
) |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
@pytest.mark.filters |
138
|
|
|
def test_getdims_normal(): |
139
|
|
|
with app.app_context(): |
140
|
|
|
data = dict(width=100, height=100, type='foo') |
141
|
|
|
expected = dict(width=100, height=100) |
142
|
|
|
assert charts_builder.get_dims(object, data) == expected |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
@pytest.mark.filters |
146
|
|
|
def test_getdims_youtube(): |
147
|
|
|
with app.app_context(): |
148
|
|
|
yt = ('<iframe width="650" height="366" ' |
149
|
|
|
'src="https://www.youtube.com/embed/' |
150
|
|
|
'_hI0qMtdfng?list=RD_hI0qMtdfng&' |
151
|
|
|
'controls=0&showinfo=0" frameborder="0"' |
152
|
|
|
' allowfullscreen></iframe>') |
153
|
|
|
data = dict(type='youtube', dataSource=yt, width=100, height=100) |
154
|
|
|
expected = dict(width=650 + 20, height=366 + 60) |
155
|
|
|
assert charts_builder.get_dims(object, data) == expected |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
def test_app_redirects(client): |
159
|
|
|
resp = client.get('/charts') |
160
|
|
|
assert 'You should be redirected automatically' in resp.data |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
def test_routes(client): |
164
|
|
|
app.config['SERVER_NAME'] = '127.0.0.1:80' |
165
|
|
|
with app.app_context(): |
166
|
|
|
# Index |
167
|
|
|
url = '{}/charts/'.format(URL_BASE) |
168
|
|
|
assert url_for('jsondash.dashboard') == url |
169
|
|
|
# View |
170
|
|
|
url = '{}/charts/foo'.format(URL_BASE) |
171
|
|
|
assert url_for('jsondash.view', c_id='foo') == url |
172
|
|
|
# Update |
173
|
|
|
url = '{}/charts/foo/update'.format(URL_BASE) |
174
|
|
|
assert url_for('jsondash.update', c_id='foo') == url |
175
|
|
|
# Clone |
176
|
|
|
url = '{}/charts/foo/clone'.format(URL_BASE) |
177
|
|
|
assert url_for('jsondash.clone', c_id='foo') == url |
178
|
|
|
# Delete |
179
|
|
|
url = '{}/charts/foo/delete'.format(URL_BASE) |
180
|
|
|
assert url_for('jsondash.delete', c_id='foo') == url |
181
|
|
|
# Create |
182
|
|
|
url = '{}/charts/create'.format(URL_BASE) |
183
|
|
|
assert url_for('jsondash.create') == url |
184
|
|
|
|