forked from pallets-eco/flask-principal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_principal.py
296 lines (221 loc) · 7.74 KB
/
test_principal.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from __future__ import with_statement
import unittest
from flask import Flask, Response
from flask_principal import Principal, Permission, Denial, RoleNeed, \
PermissionDenied, identity_changed, Identity, identity_loaded
anon_permission = Permission()
admin_permission = Permission(RoleNeed('admin'))
admin_or_editor = Permission(RoleNeed('admin'), RoleNeed('editor'))
editor_permission = Permission(RoleNeed('editor'))
admin_denied = Denial(RoleNeed('admin'))
def _on_principal_init(sender, identity):
if identity.id == 'ali':
identity.provides.add(RoleNeed('admin'))
class ReraiseException(Exception):
"""For checking reraising"""
def mkapp(with_factory=False):
app = Flask(__name__)
app.secret_key = 'notverysecret'
app.debug = True
if with_factory:
p = Principal()
p.init_app(app)
else:
p = Principal(app)
identity_loaded.connect(_on_principal_init)
@app.route('/')
def index():
with admin_permission.require():
pass
return Response('hello')
@app.route('/a')
@admin_permission.require()
def a():
return Response('hello')
@app.route('/b')
@anon_permission.require()
def b():
return Response('hello')
@app.route('/c')
def c():
with anon_permission.require():
raise ReraiseException
@app.route('/d')
@anon_permission.require()
def d():
raise ReraiseException
@app.route('/e')
def e():
i = mkadmin()
identity_changed.send(app, identity=i)
with admin_permission.require():
return Response('hello')
@app.route('/f')
def f():
i = mkadmin()
identity_changed.send(app, identity=i)
with admin_or_editor.require():
return Response('hello')
@app.route('/g')
@admin_permission.require()
@editor_permission.require()
def g():
return Response('hello')
@app.route('/h')
def h():
i = Identity('james')
identity_changed.send(app, identity=i)
with admin_permission.require():
with editor_permission.require():
pass
@app.route('/j')
def j():
i = Identity('james')
identity_changed.send(app, identity=i)
with admin_permission.require(403):
with editor_permission.require(403):
pass
@app.route('/k')
@admin_permission.require(403)
def k():
return Response('hello')
@app.route('/l')
def l():
s = []
if not admin_or_editor:
s.append("not admin")
i = Identity('ali')
identity_changed.send(app, identity=i)
if admin_or_editor:
s.append("now admin")
return Response('\n'.join(s))
@app.route("/m")
def m():
with admin_denied.require():
pass
return Response("OK")
@app.route("/n")
def n():
i = mkadmin()
identity_changed.send(app, identity=i)
with admin_denied.require():
pass
return Response("OK")
@app.route("/o")
def o():
admin_or_editor.test()
return Response("OK")
@app.route("/p")
def p():
admin_or_editor.test(404)
return Response("OK")
return app
def mkadmin():
i = Identity('ali')
return i
class PrincipalUnitTests(unittest.TestCase):
def test_permission_union(self):
p1 = Permission(('a', 'b'))
p2 = Permission(('a', 'c'))
p3 = p1.union(p2)
assert p1.issubset(p3)
assert p2.issubset(p3)
def test_permission_difference(self):
p1 = Permission(('a', 'b'), ('a', 'c'))
p2 = Permission(('a', 'c'), ('d', 'e'))
p3 = p1.difference(p2)
assert p3.needs == set([('a', 'b')])
p4 = p2.difference(p1)
assert p4.needs == set([('d', 'e')])
def test_permission_union_denial(self):
p1 = Permission(('a', 'b'))
p2 = Denial(('a', 'c'))
p3 = p1.union(p2)
assert p1.issubset(p3)
assert p2.issubset(p3)
def test_permission_difference_denial(self):
p1 = Denial(('a', 'b'), ('a', 'c'))
p2 = Denial(('a', 'c'), ('d', 'e'))
p3 = p1.difference(p2)
assert p3.excludes == set([('a', 'b')])
p4 = p2.difference(p1)
assert p4.excludes == set([('d', 'e')])
def test_reverse_permission(self):
p = Permission(('a', 'b'))
d = p.reverse()
assert ('a', 'b') in d.excludes
def test_permission_and(self):
p1 = Permission(RoleNeed('boss'))
p2 = Permission(RoleNeed('lackey'))
p3 = p1 & p2
p4 = p1.union(p2)
assert p3.needs == p4.needs
def test_permission_or(self):
p1 = Permission(RoleNeed('boss'), RoleNeed('lackey'))
p2 = Permission(RoleNeed('lackey'), RoleNeed('underling'))
p3 = p1 | p2
p4 = p1.difference(p2)
assert p3.needs == p4.needs
def test_contains(self):
p1 = Permission(RoleNeed('boss'), RoleNeed('lackey'))
p2 = Permission(RoleNeed('lackey'))
assert p2.issubset(p1)
assert p2 in p1
class PrincipalApplicationTests(unittest.TestCase):
def setUp(self):
self.client = mkapp().test_client()
def test_deny_with(self):
self.assertRaises(PermissionDenied, self.client.open, '/')
def test_deny_view(self):
self.assertRaises(PermissionDenied, self.client.open, '/a')
def test_allow_view(self):
assert self.client.open('/b').data == b'hello'
def test_reraise(self):
self.assertRaises(ReraiseException, self.client.open, '/c')
def test_error_view(self):
self.assertRaises(ReraiseException, self.client.open, '/d')
def test_identity_changed(self):
assert self.client.open('/e').data == b'hello'
def test_identity_load(self):
assert self.client.open('/e').data == b'hello'
assert self.client.open('/a').data == b'hello'
def test_or_permissions(self):
assert self.client.open('/e').data == b'hello'
assert self.client.open('/f').data == b'hello'
def test_and_permissions_view_denied(self):
self.assertRaises(PermissionDenied, self.client.open, '/g')
def test_and_permissions_view(self):
self.assertRaises(PermissionDenied, self.client.open, '/g')
def test_and_permissions_view_with_http_exc(self):
response = self.client.open("/j")
assert response.status_code == 403
def test_and_permissions_view_with_http_exc_decorated(self):
response = self.client.open("/k")
assert response.status_code == 403
def test_and_permissions_view_with_custom_errhandler(self):
app = mkapp()
@app.errorhandler(403)
def handle_permission_denied(error):
assert error.description == admin_permission
return Response("OK")
self.client = app.test_client()
response = self.client.open("/k")
assert response.status_code == 200
def test_permission_bool(self):
response = self.client.open('/l')
assert response.status_code == 200
assert b'not admin' in response.data
assert b'now admin' in response.data
def test_denied_passes(self):
response = self.client.open("/m")
assert response.status_code == 200
def test_denied_fails(self):
self.assertRaises(PermissionDenied, self.client.open, '/n')
def test_permission_test(self):
self.assertRaises(PermissionDenied, self.client.open, '/o')
def test_permission_test_with_http_exc(self):
response = self.client.open("/p")
assert response.status_code == 404
class FactoryMethodPrincipalApplicationTests(PrincipalApplicationTests):
def setUp(self):
self.client = mkapp(with_factory=True).test_client()