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_metadata(client): |
66
|
|
|
with app.app_context(): |
67
|
|
|
assert charts_builder.metadata(key='created_by') == 'Username' |
68
|
|
|
assert charts_builder.metadata(key='username') == 'Username' |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_metadata_bad(client): |
72
|
|
|
with app.app_context(): |
73
|
|
|
assert charts_builder.metadata(key='foo') is None |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_app_redirect(client): |
77
|
|
|
resp = client.get('/charts') |
78
|
|
|
assert 'You should be redirected automatically' in resp.data |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def test_is_global_dashboard_true(client): |
82
|
|
|
with app.app_context(): |
83
|
|
|
app.config.update(JSONDASH_GLOBALDASH=True) |
84
|
|
|
assert charts_builder.is_global_dashboard( |
85
|
|
|
dict(created_by='global-test')) |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
def test_is_global_dashboard_false(client): |
89
|
|
|
with app.app_context(): |
90
|
|
|
is_global = charts_builder.is_global_dashboard |
91
|
|
|
assert not is_global(dict(created_by='foo')) |
92
|
|
|
assert not is_global(dict(created_by='Username')) |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_auth_false_realauth(): |
96
|
|
|
with app.app_context(): |
97
|
|
|
assert not charts_builder.auth(authtype='create') |
98
|
|
|
assert not charts_builder.auth(authtype='view') |
99
|
|
|
assert not charts_builder.auth(authtype='delete') |
100
|
|
|
assert not charts_builder.auth(authtype='clone') |
101
|
|
|
assert not charts_builder.auth(authtype='edit_global') |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def test_auth_true_realauth(): |
105
|
|
|
with app.app_context(): |
106
|
|
|
def authfunc(*args): |
107
|
|
|
return True |
108
|
|
|
|
109
|
|
|
app.config['JSONDASH']['auth'] = dict( |
110
|
|
|
clone=authfunc, |
111
|
|
|
edit_global=authfunc, |
112
|
|
|
create=authfunc, |
113
|
|
|
delete=authfunc, |
114
|
|
|
view=authfunc, |
115
|
|
|
) |
116
|
|
|
assert charts_builder.auth(authtype='create') |
117
|
|
|
assert charts_builder.auth(authtype='view') |
118
|
|
|
assert charts_builder.auth(authtype='delete') |
119
|
|
|
assert charts_builder.auth(authtype='clone') |
120
|
|
|
assert charts_builder.auth(authtype='edit_global') |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
def test_auth_true_fakeauth(): |
124
|
|
|
with app.app_context(): |
125
|
|
|
assert charts_builder.auth(authtype=None) |
126
|
|
|
assert charts_builder.auth(authtype='foo') |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
def test_metadata(): |
130
|
|
|
with app.app_context(): |
131
|
|
|
assert charts_builder.metadata() == dict( |
132
|
|
|
username='Username', |
133
|
|
|
created_by='Username', |
134
|
|
|
) |
135
|
|
|
assert charts_builder.metadata(key='username') == 'Username' |
136
|
|
|
assert charts_builder.metadata(key='created_by') == 'Username' |
137
|
|
|
assert charts_builder.metadata(exclude='created_by') == dict( |
138
|
|
|
username='Username' |
139
|
|
|
) |
140
|
|
|
assert charts_builder.metadata(exclude='username') == dict( |
141
|
|
|
created_by='Username' |
142
|
|
|
) |
143
|
|
|
|