Debian Codegraph @

tests/test_async.py

raw
1 """
2 test_async
3 ~~~~~~~~~~
4
5 Tests using Flask async.
6
7 Make sure our decorators allow for async views
8 Make sure signal receivers can be async
9
10 :copyright: (c) 2023-2023 by J. Christopher Wagner (jwag).
11 :license: MIT, see LICENSE for more details.
12
13 """
14
15 import asyncio
16 import base64
17
18 import pytest
19
20 from flask_principal import identity_changed
21
22 from flask_security import (
23 anonymous_user_required,
24 auth_token_required,
25 auth_required,
26 http_auth_required,
27 roles_required,
28 roles_accepted,
29 permissions_required,
30 permissions_accepted,
31 unauth_csrf,
32 )
33
34 from tests.test_utils import (
35 authenticate,
36 json_authenticate,
37 )
38
39 pytestmark = pytest.mark.flask_async()
40
41
42 def test_auth_required(app, client):
43 @app.route("/async_test")
44 @auth_required()
45 async def async_test():
46 await asyncio.sleep(0)
47 return "Access Granted"
48
49 authenticate(client)
50 response = client.get("/async_test")
51 assert b"Access Granted" in response.data
52
53
54 def test_auth_token_required(app, client):
55 @app.route("/async_test")
56 @auth_token_required
57 async def async_test():
58 await asyncio.sleep(0)
59 return "Access Granted"
60
61 @identity_changed.connect_via(app)
62 async def ic(myapp, identity, **extra_args):
63 await asyncio.sleep(0)
64
65 response = json_authenticate(client)
66 token = response.json["response"]["user"]["authentication_token"]
67 response = client.get("/async_test?auth_token=" + token)
68 assert b"Access Granted" in response.data
69
70
71 def test_auth_http_required(app, client):
72 @app.route("/async_test")
73 @http_auth_required
74 async def async_test():
75 await asyncio.sleep(0)
76 return "Access Granted"
77
78 response = client.get(
79 "/async_test",
80 headers={
81 "Authorization": "Basic %s"
82 % base64.b64encode(b"[email protected]:password").decode("utf-8")
83 },
84 )
85 assert b"Access Granted" in response.data
86
87
88 def test_roles_required(app, client):
89 @app.route("/async_test")
90 @roles_required("admin")
91 async def async_test():
92 await asyncio.sleep(0)
93 return "Access Granted"
94
95 authenticate(client)
96 response = client.get("/async_test")
97 assert b"Access Granted" in response.data
98
99
100 def test_roles_accepted(app, client):
101 @app.route("/async_test")
102 @roles_accepted("admin")
103 async def async_test():
104 await asyncio.sleep(0)
105 return "Access Granted"
106
107 authenticate(client)
108 response = client.get("/async_test")
109 assert b"Access Granted" in response.data
110
111
112 def test_permissions_required(app, client):
113 @app.route("/async_test")
114 @permissions_required("super")
115 async def async_test():
116 await asyncio.sleep(0)
117 return "Access Granted"
118
119 authenticate(client)
120 response = client.get("/async_test")
121 assert b"Access Granted" in response.data
122
123
124 def test_permissions_accepted(app, client):
125 @app.route("/async_test")
126 @permissions_accepted("super")
127 async def async_test():
128 await asyncio.sleep(0)
129 return "Access Granted"
130
131 authenticate(client)
132 response = client.get("/async_test")
133 assert b"Access Granted" in response.data
134
135
136 def test_anon(app, client):
137 @app.route("/async_test")
138 @anonymous_user_required
139 async def async_test():
140 await asyncio.sleep(0)
141 return "Access Granted"
142
143 response = client.get("/async_test")
144 assert b"Access Granted" in response.data
145
146
147 def test_unauth_csrf(app, client):
148 @app.route("/async_test")
149 @unauth_csrf()
150 async def async_test():
151 await asyncio.sleep(0)
152 return "Access Granted"
153
154 response = client.get("/async_test")
155 assert b"Access Granted" in response.data