Completed
Push — master ( a4e4e0...7063c4 )
by Chris
01:01
created

test_jsonstring()   B

Complexity

Conditions 5

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 10
rs 8.5454
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
        assert not charts_builder.auth(authtype='edit_others')
94
95
96
def test_auth_true_realauth():
97
    with app.app_context():
98
        def authfunc(*args):
99
            return True
100
101
        app.config['JSONDASH']['auth'] = dict(
102
            clone=authfunc,
103
            edit_global=authfunc,
104
            create=authfunc,
105
            delete=authfunc,
106
            view=authfunc,
107
        )
108
        assert charts_builder.auth(authtype='create')
109
        assert charts_builder.auth(authtype='view')
110
        assert charts_builder.auth(authtype='delete')
111
        assert charts_builder.auth(authtype='clone')
112
        assert charts_builder.auth(authtype='edit_global')
113
        assert charts_builder.auth(authtype='edit_others')
114
115
116
def test_auth_true_fakeauth():
117
    with app.app_context():
118
        assert charts_builder.auth(authtype=None)
119
        assert charts_builder.auth(authtype='foo')
120
        assert charts_builder.metadata(key='foo') is None
121
122
123
def test_metadata():
124
    with app.app_context():
125
        assert charts_builder.metadata() == dict(
126
            username='Username',
127
            created_by='Username',
128
        )
129
        assert charts_builder.metadata(key='username') == 'Username'
130
        assert charts_builder.metadata(key='created_by') == 'Username'
131
        assert charts_builder.metadata(exclude='created_by') == dict(
132
            username='Username'
133
        )
134
        assert charts_builder.metadata(exclude='username') == dict(
135
            created_by='Username'
136
        )
137
138
139
@pytest.mark.filters
140
def test_getdims_normal():
141
    with app.app_context():
142
        data = dict(width=100, height=100, type='foo')
143
        expected = dict(width=100, height=100)
144
        assert charts_builder.get_dims(object, data) == expected
145
146
147
@pytest.mark.filters
148
def test_getdims_youtube():
149
    with app.app_context():
150
        yt = ('<iframe width="650" height="366" '
151
              'src="https://www.youtube.com/embed/'
152
              '_hI0qMtdfng?list=RD_hI0qMtdfng&amp;'
153
              'controls=0&amp;showinfo=0" frameborder="0"'
154
              ' allowfullscreen></iframe>')
155
        data = dict(type='youtube', dataSource=yt, width=100, height=100)
156
        expected = dict(width=650 + 20, height=366 + 60)
157
        assert charts_builder.get_dims(object, data) == expected
158
159
160
@pytest.mark.filters
161
def test_jsonstring():
162
    with app.app_context():
163
        now = dt.now()
164
        data = dict(date=now, foo='bar')
165
        res = charts_builder.jsonstring(object, data)
166
        assert 'foo' in res
167
        assert isinstance(res, str)
168
        d = json.loads(res)
169
        assert isinstance(d['date'], unicode)
170
171
172
def test_app_redirects(client):
173
    res = client.get('/charts')
174
    assert 'You should be redirected automatically' in res.data
175
176
177
def test_routes(client):
178
    app.config['SERVER_NAME'] = '127.0.0.1:80'
179
    with app.app_context():
180
        # Index
181
        url = '{}/charts/'.format(URL_BASE)
182
        assert url_for('jsondash.dashboard') == url
183
        # View
184
        url = '{}/charts/foo'.format(URL_BASE)
185
        assert url_for('jsondash.view', c_id='foo') == url
186
        # Update
187
        url = '{}/charts/foo/update'.format(URL_BASE)
188
        assert url_for('jsondash.update', c_id='foo') == url
189
        # Clone
190
        url = '{}/charts/foo/clone'.format(URL_BASE)
191
        assert url_for('jsondash.clone', c_id='foo') == url
192
        # Delete
193
        url = '{}/charts/foo/delete'.format(URL_BASE)
194
        assert url_for('jsondash.delete', c_id='foo') == url
195
        # Create
196
        url = '{}/charts/create'.format(URL_BASE)
197
        assert url_for('jsondash.create') == url
198