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