forked from maxcountryman/flask-seasurf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_seasurf.py
138 lines (100 loc) · 3.61 KB
/
test_seasurf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import unittest
from flask import Flask
from flask_seasurf import SeaSurf
class SeaSurfTestCase(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = 'hunter2'
self.app = app
csrf = SeaSurf(app)
csrf._csrf_disable = False
self.csrf = csrf
@csrf.exempt
@app.route('/foo', methods=['POST'])
def foo():
return 'bar'
@app.route('/bar', methods=['POST'])
def bar():
return 'foo'
def test_generate_token(self):
self.assertIsNotNone(self.csrf._generate_token())
def test_unique_generation(self):
token_a = self.csrf._generate_token()
token_b = self.csrf._generate_token()
self.assertNotEqual(token_a, token_b)
def test_token_is_string(self):
token = self.csrf._generate_token()
self.assertEqual(type(token), str)
def test_exempt_view(self):
rv = self.app.test_client().post('/foo')
self.assertIn('bar', rv.data)
def test_token_validation(self):
# should produce a logger warning
rv = self.app.test_client().post('/bar')
self.assertIn('403 Forbidden', rv.data)
# Methods for backwards compatibility with python 2.5 & 2.6
def assertIn(self, value, container):
self.assertTrue(value in container)
def assertIsNotNone(self, value):
self.assertNotEqual(value, None)
class SeaSurfTestCaseExemptViews(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = 'hunter2'
app.config['SEASURF_INCLUDE_OR_EXEMPT_VIEWS'] = 'exempt'
self.app = app
csrf = SeaSurf(app)
csrf._csrf_disable = False
self.csrf = csrf
@csrf.exempt
@app.route('/foo', methods=['POST'])
def foo():
return 'bar'
@app.route('/bar', methods=['POST'])
def bar():
return 'foo'
def test_exempt_view(self):
rv = self.app.test_client().post('/foo')
self.assertIn('bar', rv.data)
def test_token_validation(self):
# should produce a logger warning
rv = self.app.test_client().post('/bar')
self.assertIn('403 Forbidden', rv.data)
def assertIn(self, value, container):
self.assertTrue(value in container)
class SeaSurfTestCaseIncludeViews(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = 'hunter2'
app.config['SEASURF_INCLUDE_OR_EXEMPT_VIEWS'] = 'include'
self.app = app
csrf = SeaSurf(app)
csrf._csrf_disable = False
self.csrf = csrf
@csrf.include
@app.route('/foo', methods=['POST'])
def foo():
return 'bar'
@app.route('/bar', methods=['POST'])
def bar():
return 'foo'
def test_include_view(self):
rv = self.app.test_client().post('/foo')
self.assertIn('403 Forbidden', rv.data)
def test_token_validation(self):
# should produce a logger warning
rv = self.app.test_client().post('/bar')
self.assertIn('foo', rv.data)
def assertIn(self, value, container):
self.assertTrue(value in container)
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SeaSurfTestCase))
suite.addTest(unittest.makeSuite(SeaSurfTestCaseExemptViews))
suite.addTest(unittest.makeSuite(SeaSurfTestCaseIncludeViews))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')