Completed
Push — master ( 0ff6d7...2aa92f )
by Chris
01:05
created

test_routes()   D

Complexity

Conditions 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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