1
|
|
|
"""Test jinja filters.""" |
2
|
|
|
|
3
|
|
|
from flask import Flask |
4
|
|
|
import pytest |
5
|
|
|
|
6
|
|
|
from flask_extras import FlaskExtras |
7
|
|
|
from flask_extras import decorators |
8
|
|
|
|
9
|
|
|
app = Flask('test_flask_jsondash') |
10
|
|
|
app.debug = True |
11
|
|
|
app.secret_key = 'Foo' |
12
|
|
|
FlaskExtras(app) |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
@app.route('/xhr-custom') |
16
|
|
|
@decorators.xhr_only(status_code=400) |
17
|
|
|
def foo_xhr_custom(): |
18
|
|
|
return '' |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
@app.route('/xhr') |
22
|
|
|
@decorators.xhr_only() |
23
|
|
|
def foo_xhr(): |
24
|
|
|
return '' |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
@app.route('/args') |
28
|
|
|
@decorators.require_args(params=['foo', 'bar']) |
29
|
|
|
def foo_args(): |
30
|
|
|
return '' |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
@app.route('/cookies') |
34
|
|
|
@decorators.require_cookies(['foo']) |
35
|
|
|
def foo_cookies(): |
36
|
|
|
return '' |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
@app.route('/headers') |
40
|
|
|
@decorators.require_headers(headers=['X-Foo']) |
41
|
|
|
def foo_headers(): |
42
|
|
|
return '' |
43
|
|
|
|
44
|
|
|
client = app.test_client() |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
class TestXhr: |
48
|
|
|
"""Test class for function.""" |
49
|
|
|
|
50
|
|
|
def test_invalid_xhr_decorator(self): |
51
|
|
|
"""Test expected failure of function.""" |
52
|
|
|
with app.app_context(): |
53
|
|
|
res = client.get('/xhr') |
54
|
|
|
assert res.status_code == 415 |
55
|
|
|
|
56
|
|
|
def test_invalid_xhr_custom_decorator(self): |
57
|
|
|
"""Test expected success of function.""" |
58
|
|
|
with app.app_context(): |
59
|
|
|
res = client.get('/xhr-custom') |
60
|
|
|
assert res.status_code == 400 |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
class TestRequireArgs: |
64
|
|
|
"""Test class for function.""" |
65
|
|
|
|
66
|
|
|
def test_invalid_require_args_decorator(self): |
67
|
|
|
"""Test expected failure of function.""" |
68
|
|
|
with pytest.raises(ValueError): |
69
|
|
|
with app.app_context(): |
70
|
|
|
client.get('/args') |
71
|
|
|
|
72
|
|
|
def test_valid_require_args_decorator(self): |
73
|
|
|
"""Test expected success of function.""" |
74
|
|
|
with app.app_context(): |
75
|
|
|
res = client.get('/args?foo=1&bar=1') |
76
|
|
|
assert res.status_code == 200 |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
class TestRequireCookies: |
80
|
|
|
"""Test class for function.""" |
81
|
|
|
|
82
|
|
|
def test_invalid_require_cookies_decorator(self): |
83
|
|
|
"""Test expected failure of function.""" |
84
|
|
|
with pytest.raises(ValueError): |
85
|
|
|
with app.app_context(): |
86
|
|
|
client.get('/cookies') |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
class TestRequireHeaders: |
90
|
|
|
"""Test class for function.""" |
91
|
|
|
|
92
|
|
|
def test_invalid_require_headers_decorator(self): |
93
|
|
|
"""Test expected failure of function.""" |
94
|
|
|
with pytest.raises(ValueError): |
95
|
|
|
with app.app_context(): |
96
|
|
|
client.get('/headers') |
97
|
|
|
|
98
|
|
|
def test_valid_require_headers_decorator(self): |
99
|
|
|
"""Test expected success of function.""" |
100
|
|
|
with app.app_context(): |
101
|
|
|
res = client.get('/headers', headers={'X-Foo': 'Foo'}) |
102
|
|
|
assert res.status_code == 200 |
103
|
|
|
|