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