Completed
Push — master ( 7d2d7b...a4e4e0 )
by Chris
01:05
created

test_auth_false_realauth()   C

Complexity

Conditions 8

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
c 2
b 1
f 0
dl 0
loc 8
rs 6.6666
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
def test_app_redirects(client):
161
    res = client.get('/charts')
162
    assert 'You should be redirected automatically' in res.data
163
164
165
def test_routes(client):
166
    app.config['SERVER_NAME'] = '127.0.0.1:80'
167
    with app.app_context():
168
        # Index
169
        url = '{}/charts/'.format(URL_BASE)
170
        assert url_for('jsondash.dashboard') == url
171
        # View
172
        url = '{}/charts/foo'.format(URL_BASE)
173
        assert url_for('jsondash.view', c_id='foo') == url
174
        # Update
175
        url = '{}/charts/foo/update'.format(URL_BASE)
176
        assert url_for('jsondash.update', c_id='foo') == url
177
        # Clone
178
        url = '{}/charts/foo/clone'.format(URL_BASE)
179
        assert url_for('jsondash.clone', c_id='foo') == url
180
        # Delete
181
        url = '{}/charts/foo/delete'.format(URL_BASE)
182
        assert url_for('jsondash.delete', c_id='foo') == url
183
        # Create
184
        url = '{}/charts/create'.format(URL_BASE)
185
        assert url_for('jsondash.create') == url
186