Completed
Push — master ( a3a24e...108d36 )
by Chris
01:27
created

test_routes()   B

Complexity

Conditions 7

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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