Debian Codegraph @

tests/test_context_processors.py

raw
1 """
2 test_context_processors
3 ~~~~~~~~~~~~~~~~~~~~~~~
4
5 Context processor tests
6 """
7
8 import pytest
9 from tests.test_two_factor import tf_authenticate
10 from tests.test_unified_signin import authenticate as us_authenticate
11 from tests.test_utils import authenticate, capture_reset_password_requests, logout
12
13
14 @pytest.mark.recoverable()
15 @pytest.mark.registerable()
16 @pytest.mark.confirmable()
17 @pytest.mark.changeable()
18 @pytest.mark.change_email()
19 @pytest.mark.username_recovery()
20 @pytest.mark.change_username()
21 @pytest.mark.settings(
22 login_without_confirmation=True,
23 change_email_template="custom_security/change_email.html",
24 change_password_template="custom_security/change_password.html",
25 login_user_template="custom_security/login_user.html",
26 reset_password_template="custom_security/reset_password.html",
27 forgot_password_template="custom_security/forgot_password.html",
28 send_confirmation_template="custom_security/send_confirmation.html",
29 register_user_template="custom_security/register_user.html",
30 verify_template="custom_security/verify.html",
31 username_recovery_template="custom_security/recover_username.html",
32 change_username_template="custom_security/change_username.html",
33 email_template_password_reset="security/email/reset_instructions_test",
34 email_html=False,
35 )
36 def test_context_processors(client, app, outbox):
37 @app.security.context_processor
38 def default_ctx_processor():
39 return {"global": "global"}
40
41 @app.security.forgot_password_context_processor
42 def forgot_password():
43 return {"foo": "bar-forgot"}
44
45 response = client.get("/reset")
46 assert b"global" in response.data
47 assert b"bar-forgot" in response.data
48
49 @app.security.login_context_processor
50 def login():
51 return {"foo": "bar-login"}
52
53 response = client.get("/login")
54 assert b"global" in response.data
55 assert b"bar-login" in response.data
56
57 @app.security.verify_context_processor
58 def verify():
59 return {"foo": "bar-verify"}
60
61 authenticate(client)
62 response = client.get("/verify")
63 assert b"CUSTOM VERIFY USER" in response.data
64 assert b"global" in response.data
65 assert b"bar-verify" in response.data
66 logout(client)
67
68 @app.security.register_context_processor
69 def register():
70 return {"foo": "bar-register"}
71
72 response = client.get("/register")
73 assert b"global" in response.data
74 assert b"bar-register" in response.data
75
76 @app.security.reset_password_context_processor
77 def reset_password():
78 return {"foo": "bar-reset"}
79
80 # /reset/token - need to generate a token
81 with capture_reset_password_requests() as requests:
82 response = client.post(
83 "/reset", data=dict(email="[email protected]"), follow_redirects=True
84 )
85 token = requests[0]["token"]
86 response = client.get(f"/reset/{token}")
87 assert b"global" in response.data
88 assert b"bar-reset" in response.data
89
90 @app.security.change_password_context_processor
91 def change_password():
92 return {"foo": "bar-change"}
93
94 authenticate(client)
95 response = client.get("/change")
96 assert b"global" in response.data
97 assert b"bar-change" in response.data
98
99 @app.security.send_confirmation_context_processor
100 def send_confirmation():
101 return {"foo": "bar-confirm"}
102
103 response = client.get("/confirm")
104 assert b"global" in response.data
105 assert b"bar-confirm" in response.data
106
107 @app.security.mail_context_processor
108 def mail():
109 return {"foo": "bar-mail"}
110
111 client.post("/logout")
112
113 client.post("/reset", data=dict(email="[email protected]"))
114
115 email = outbox[1]
116 assert "global" in email.body
117 assert "bar-mail" in email.body
118
119 @app.security.change_email_context_processor
120 def change_email():
121 return {"foo": "bar-change-email"}
122
123 authenticate(client)
124 response = client.get("/change-email")
125 assert b"global" in response.data
126 assert b"bar-change-email" in response.data
127
128 @app.security.recover_username_context_processor
129 def recover_username():
130 return {"foo": "bar-recover-username"}
131
132 client.post("/logout")
133 response = client.get("/recover-username")
134 assert b"global" in response.data
135 assert b"bar-recover-username" in response.data
136
137 @app.security.change_username_context_processor
138 def change_username():
139 return {"foo": "bar-change-username"}
140
141 authenticate(client)
142 response = client.get("/change-username")
143 assert b"global" in response.data
144 assert b"bar-change-username" in response.data
145
146
147 @pytest.mark.passwordless()
148 @pytest.mark.settings(send_login_template="custom_security/send_login.html")
149 def test_passwordless_login_context_processor(app, client):
150 @app.security.send_login_context_processor
151 def send_login():
152 return {"foo": "bar-send-login"}
153
154 response = client.get("/login")
155 assert b"bar-send-login" in response.data
156
157
158 @pytest.mark.two_factor()
159 @pytest.mark.settings(
160 two_factor_required=True,
161 login_user_template="custom_security/login_user.html",
162 two_factor_setup_template="custom_security/tf_setup.html",
163 two_factor_verify_code_template="custom_security/tf_verify.html",
164 )
165 def test_two_factor_context_processors(client, app):
166 # Test two factor context processors
167 @app.security.context_processor
168 def default_ctx_processor():
169 return {"global": "global"}
170
171 @app.security.tf_setup_context_processor
172 def send_two_factor_setup():
173 return {"foo": "bar-tfsetup"}
174
175 # Note this just does initial login on a user that hasn't setup 2FA yet.
176 authenticate(client)
177 response = client.get("/tf-setup")
178 assert b"global" in response.data
179 assert b"bar-tfsetup" in response.data
180 logout(client)
181
182 @app.security.tf_token_validation_context_processor
183 def send_two_factor_token_validation():
184 return {"foo": "bar-tfvalidate"}
185
186 tf_authenticate(app, client, validate=False)
187 response = client.get("/tf-rescue")
188 assert b"global" in response.data
189 assert b"bar-tfvalidate" in response.data
190 logout(client)
191
192
193 @pytest.mark.unified_signin()
194 @pytest.mark.settings(
195 us_setup_template="custom_security/us_setup.html",
196 us_signin_template="custom_security/us_signin.html",
197 us_verify_template="custom_security/us_verify.html",
198 )
199 def test_unified_signin_context_processors(client, app):
200 @app.security.context_processor
201 def default_ctx_processor():
202 return {"global": "global"}
203
204 @app.security.us_signin_context_processor
205 def signin_ctx():
206 return {"foo": "signin"}
207
208 # signin template is used in 3 places (TODO test POST us-send-code)
209 response = client.get("/us-signin")
210 assert b"CUSTOM UNIFIED SIGN IN" in response.data
211 assert b"global" in response.data
212 assert b"signin" in response.data
213
214 response = client.post("/us-signin/send-code")
215 assert b"CUSTOM UNIFIED SIGN IN" in response.data
216 assert b"global" in response.data
217 assert b"signin" in response.data
218
219 @app.security.us_setup_context_processor
220 def setup_ctx():
221 return {"foo": "setup"}
222
223 us_authenticate(client)
224 response = client.get("us-setup")
225 assert b"CUSTOM UNIFIED SIGNIN SETUP" in response.data
226 assert b"global" in response.data
227 assert b"setup" in response.data
228
229 response = client.post("us-setup", data=dict(chosen_method="sms", phone="555-1212"))
230 assert b"CUSTOM UNIFIED SIGNIN SETUP" in response.data
231 assert b"global" in response.data
232 assert b"setup" in response.data
233
234 @app.security.us_verify_context_processor
235 def verify_ctx():
236 return {"foo": "setup"}
237
238 us_authenticate(client)
239 response = client.get("us-verify")
240 assert b"CUSTOM UNIFIED VERIFY" in response.data
241 assert b"global" in response.data
242 assert b"setup" in response.data
243
244 response = client.post(
245 "us-verify", data=dict(chosen_method="sms", phone="555-1212")
246 )
247 assert b"CUSTOM UNIFIED VERIFY" in response.data
248 assert b"global" in response.data
249 assert b"setup" in response.data
250
251
252 @pytest.mark.two_factor()
253 @pytest.mark.settings(
254 multi_factor_recovery_template="custom_security/mf_recovery.html",
255 multi_factor_recovery_codes_template="custom_security/mf_recovery_codes.html",
256 multi_factor_recovery_codes=True,
257 )
258 def test_mf_recovery_context_processors(client, app):
259 @app.security.context_processor
260 def default_ctx_processor():
261 return {"global": "global"}
262
263 @app.security.mf_recovery_codes_context_processor
264 def codes_ctx():
265 return {"foo": "codes"}
266
267 authenticate(client)
268 response = client.get("/mf-recovery-codes")
269 assert b"global" in response.data
270 assert b"codes" in response.data
271 logout(client)
272
273 @app.security.mf_recovery_context_processor
274 def code_ctx():
275 return {"foo": "code"}
276
277 authenticate(client, "[email protected]")
278 response = client.get("/mf-recovery")
279 assert b"global" in response.data
280 assert b"code" in response.data
281
282
283 @pytest.mark.settings(
284 logout_user_template="custom_security/logout_user.html", logout_csrf=True
285 )
286 @pytest.mark.csrf(ignore_unauth=True)
287 def test_logout_context_processor(app, client):
288 @app.security.context_processor
289 def default_ctx_processor():
290 return {"global": "global"}
291
292 @app.security.logout_context_processor
293 def logout_ctx():
294 return {"foo": "bar-logout"}
295
296 authenticate(client)
297 response = client.post("/logout")
298
299 assert b"global" in response.data
300 assert b"bar-logout" in response.data