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