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
|
"""
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.settings(
login_without_confirmation=True,
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",
)
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")
with app.mail.record_messages() as outbox:
client.post("/reset", data=dict(email="matt@lp.com"))
email = outbox[0]
assert "global" in email.html
assert "bar-mail" in email.html
@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.get("/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
|