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
|
"""
test_context_processors
~~~~~~~~~~~~~~~~~~~~~~~
Context processor tests
"""
import pytest
from tests.test_two_factor import tf_authenticate
from tests.test_unified_signin import authenticate as us_authenticate
from tests.test_utils import authenticate, capture_reset_password_requests, logout
@pytest.mark.recoverable()
@pytest.mark.registerable()
@pytest.mark.confirmable()
@pytest.mark.changeable()
@pytest.mark.change_email()
@pytest.mark.username_recovery()
@pytest.mark.change_username()
@pytest.mark.settings(
login_without_confirmation=True,
change_email_template="custom_security/change_email.html",
change_password_template="custom_security/change_password.html",
login_user_template="custom_security/login_user.html",
reset_password_template="custom_security/reset_password.html",
forgot_password_template="custom_security/forgot_password.html",
send_confirmation_template="custom_security/send_confirmation.html",
register_user_template="custom_security/register_user.html",
verify_template="custom_security/verify.html",
username_recovery_template="custom_security/recover_username.html",
change_username_template="custom_security/change_username.html",
)
def test_context_processors(client, app):
@app.security.context_processor
def default_ctx_processor():
return {"global": "global"}
@app.security.forgot_password_context_processor
def forgot_password():
return {"foo": "bar-forgot"}
response = client.get("/reset")
assert b"global" in response.data
assert b"bar-forgot" in response.data
@app.security.login_context_processor
def login():
return {"foo": "bar-login"}
response = client.get("/login")
assert b"global" in response.data
assert b"bar-login" in response.data
@app.security.verify_context_processor
def verify():
return {"foo": "bar-verify"}
authenticate(client)
response = client.get("/verify")
assert b"CUSTOM VERIFY USER" in response.data
assert b"global" in response.data
assert b"bar-verify" in response.data
logout(client)
@app.security.register_context_processor
def register():
return {"foo": "bar-register"}
response = client.get("/register")
assert b"global" in response.data
assert b"bar-register" in response.data
@app.security.reset_password_context_processor
def reset_password():
return {"foo": "bar-reset"}
# /reset/token - need to generate a token
with capture_reset_password_requests() as requests:
response = client.post(
"/reset", data=dict(email="joe@lp.com"), follow_redirects=True
)
token = requests[0]["token"]
response = client.get(f"/reset/{token}")
assert b"global" in response.data
assert b"bar-reset" in response.data
@app.security.change_password_context_processor
def change_password():
return {"foo": "bar-change"}
authenticate(client)
response = client.get("/change")
assert b"global" in response.data
assert b"bar-change" in response.data
@app.security.send_confirmation_context_processor
def send_confirmation():
return {"foo": "bar-confirm"}
response = client.get("/confirm")
assert b"global" in response.data
assert b"bar-confirm" in response.data
@app.security.mail_context_processor
def mail():
return {"foo": "bar-mail"}
client.get("/logout")
client.post("/reset", data=dict(email="matt@lp.com"))
email = app.mail.outbox[1]
assert "global" in email.body
assert "bar-mail" in email.body
@app.security.change_email_context_processor
def change_email():
return {"foo": "bar-change-email"}
authenticate(client)
response = client.get("/change-email")
assert b"global" in response.data
assert b"bar-change-email" in response.data
@app.security.recover_username_context_processor
def recover_username():
return {"foo": "bar-recover-username"}
client.get("/logout")
response = client.get("/recover-username")
assert b"global" in response.data
assert b"bar-recover-username" in response.data
@app.security.change_username_context_processor
def change_username():
return {"foo": "bar-change-username"}
authenticate(client)
response = client.get("/change-username")
assert b"global" in response.data
assert b"bar-change-username" in response.data
@pytest.mark.passwordless()
@pytest.mark.settings(send_login_template="custom_security/send_login.html")
def test_passwordless_login_context_processor(app, client):
@app.security.send_login_context_processor
def send_login():
return {"foo": "bar-send-login"}
response = client.get("/login")
assert b"bar-send-login" in response.data
@pytest.mark.two_factor()
@pytest.mark.settings(
two_factor_required=True,
login_user_template="custom_security/login_user.html",
two_factor_setup_template="custom_security/tf_setup.html",
two_factor_verify_code_template="custom_security/tf_verify.html",
)
def test_two_factor_context_processors(client, app):
# Test two factor context processors
@app.security.context_processor
def default_ctx_processor():
return {"global": "global"}
@app.security.tf_setup_context_processor
def send_two_factor_setup():
return {"foo": "bar-tfsetup"}
# Note this just does initial login on a user that hasn't setup 2FA yet.
authenticate(client)
response = client.get("/tf-setup")
assert b"global" in response.data
assert b"bar-tfsetup" in response.data
logout(client)
@app.security.tf_token_validation_context_processor
def send_two_factor_token_validation():
return {"foo": "bar-tfvalidate"}
tf_authenticate(app, client, validate=False)
response = client.get("/tf-rescue")
assert b"global" in response.data
assert b"bar-tfvalidate" in response.data
logout(client)
@pytest.mark.unified_signin()
@pytest.mark.settings(
us_setup_template="custom_security/us_setup.html",
us_signin_template="custom_security/us_signin.html",
us_verify_template="custom_security/us_verify.html",
)
def test_unified_signin_context_processors(client, app):
@app.security.context_processor
def default_ctx_processor():
return {"global": "global"}
@app.security.us_signin_context_processor
def signin_ctx():
return {"foo": "signin"}
# signin template is used in 3 places (TODO test POST us-send-code)
response = client.get("/us-signin")
assert b"CUSTOM UNIFIED SIGN IN" in response.data
assert b"global" in response.data
assert b"signin" in response.data
response = client.post("/us-signin/send-code")
assert b"CUSTOM UNIFIED SIGN IN" in response.data
assert b"global" in response.data
assert b"signin" in response.data
@app.security.us_setup_context_processor
def setup_ctx():
return {"foo": "setup"}
us_authenticate(client)
response = client.get("us-setup")
assert b"CUSTOM UNIFIED SIGNIN SETUP" in response.data
assert b"global" in response.data
assert b"setup" in response.data
response = client.post("us-setup", data=dict(chosen_method="sms", phone="555-1212"))
assert b"CUSTOM UNIFIED SIGNIN SETUP" in response.data
assert b"global" in response.data
assert b"setup" in response.data
@app.security.us_verify_context_processor
def verify_ctx():
return {"foo": "setup"}
us_authenticate(client)
response = client.get("us-verify")
assert b"CUSTOM UNIFIED VERIFY" in response.data
assert b"global" in response.data
assert b"setup" in response.data
response = client.post(
"us-verify", data=dict(chosen_method="sms", phone="555-1212")
)
assert b"CUSTOM UNIFIED VERIFY" in response.data
assert b"global" in response.data
assert b"setup" in response.data
@pytest.mark.two_factor()
@pytest.mark.settings(
multi_factor_recovery_template="custom_security/mf_recovery.html",
multi_factor_recovery_codes_template="custom_security/mf_recovery_codes.html",
multi_factor_recovery_codes=True,
)
def test_mf_recovery_context_processors(client, app):
@app.security.context_processor
def default_ctx_processor():
return {"global": "global"}
@app.security.mf_recovery_codes_context_processor
def codes_ctx():
return {"foo": "codes"}
authenticate(client)
response = client.get("/mf-recovery-codes")
assert b"global" in response.data
assert b"codes" in response.data
logout(client)
@app.security.mf_recovery_context_processor
def code_ctx():
return {"foo": "code"}
authenticate(client, "gal@lp.com")
response = client.get("/mf-recovery")
assert b"global" in response.data
assert b"code" in response.data
|