| 1 |
""" |
| 2 |
test_change_email |
| 3 |
~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Change email functionality tests |
| 6 |
|
| 7 |
:copyright: (c) 2024-2026 by J. Christopher Wagner (jwag). |
| 8 |
:license: MIT, see LICENSE for more details. |
| 9 |
""" |
| 10 |
|
| 11 |
from contextlib import contextmanager |
| 12 |
from datetime import date, timedelta |
| 13 |
import re |
| 14 |
from urllib.parse import urlsplit |
| 15 |
|
| 16 |
import pytest |
| 17 |
from freezegun import freeze_time |
| 18 |
from tests.test_utils import ( |
| 19 |
authenticate, |
| 20 |
capture_flashes, |
| 21 |
is_authenticated, |
| 22 |
json_authenticate, |
| 23 |
logout, |
| 24 |
) |
| 25 |
from flask_security import hash_password |
| 26 |
from flask_security.signals import ( |
| 27 |
change_email_instructions_sent, |
| 28 |
change_email_confirmed, |
| 29 |
) |
| 30 |
|
| 31 |
pytestmark = pytest.mark.change_email() |
| 32 |
|
| 33 |
|
| 34 |
@contextmanager |
| 35 |
def capture_change_email_requests(): |
| 36 |
change_email_requests = [] |
| 37 |
|
| 38 |
def _on(app, **data): |
| 39 |
change_email_requests.append(data) |
| 40 |
|
| 41 |
change_email_instructions_sent.connect(_on) |
| 42 |
|
| 43 |
try: |
| 44 |
yield change_email_requests |
| 45 |
finally: |
| 46 |
change_email_instructions_sent.disconnect(_on) |
| 47 |
|
| 48 |
|
| 49 |
@pytest.mark.settings(change_email_error_view="/change-email") |
| 50 |
def test_ce(app, clients, get_message, outbox): |
| 51 |
@change_email_confirmed.connect_via(app) |
| 52 |
def _on(app, **kwargs): |
| 53 |
assert kwargs["old_email"] == "[email protected]" |
| 54 |
assert kwargs["user"].email == "[email protected]" |
| 55 |
|
| 56 |
authenticate(clients, email="[email protected]") |
| 57 |
|
| 58 |
with capture_change_email_requests() as ce_requests: |
| 59 |
response = clients.post("/change-email", data={"email": "<EMAIL>"}) |
| 60 |
assert get_message("INVALID_EMAIL_ADDRESS") in response.data |
| 61 |
assert len(outbox) == 0 |
| 62 |
|
| 63 |
response = clients.post("/change-email", data=dict(email="[email protected]")) |
| 64 |
msg = get_message("CHANGE_EMAIL_SENT", email="[email protected]") |
| 65 |
assert msg in response.data |
| 66 |
assert "[email protected]" == ce_requests[0]["new_email"] |
| 67 |
token = ce_requests[0]["token"] |
| 68 |
assert len(outbox) == 1 |
| 69 |
assert app.config["SECURITY_CHANGE_EMAIL_WITHIN"] in outbox[0].body |
| 70 |
|
| 71 |
response = clients.get("/change-email/" + token, follow_redirects=True) |
| 72 |
assert get_message("CHANGE_EMAIL_CONFIRMED") in response.data |
| 73 |
assert is_authenticated(clients, get_message) |
| 74 |
|
| 75 |
logout(clients) |
| 76 |
authenticate(clients, email="[email protected]") |
| 77 |
assert is_authenticated(clients, get_message) |
| 78 |
|
| 79 |
# try using link again - should fail |
| 80 |
with capture_flashes() as flashes: |
| 81 |
clients.get("/change-email/" + token, follow_redirects=True) |
| 82 |
assert flashes[0]["message"].encode("utf-8") == get_message("API_ERROR") |
| 83 |
|
| 84 |
|
| 85 |
def test_ce_json(app, client, get_message, outbox): |
| 86 |
headers = {"Accept": "application/json", "Content-Type": "application/json"} |
| 87 |
|
| 88 |
@change_email_confirmed.connect_via(app) |
| 89 |
def _on(app, **kwargs): |
| 90 |
assert kwargs["old_email"] == "[email protected]" |
| 91 |
assert kwargs["user"].email == "[email protected]" |
| 92 |
|
| 93 |
json_authenticate(client, email="[email protected]") |
| 94 |
|
| 95 |
with capture_change_email_requests() as ce_requests: |
| 96 |
response = client.post("/change-email", json={"email": "<EMAIL>"}) |
| 97 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 98 |
"INVALID_EMAIL_ADDRESS" |
| 99 |
) |
| 100 |
assert len(outbox) == 0 |
| 101 |
|
| 102 |
response = client.post("/change-email", json=dict(email="[email protected]")) |
| 103 |
assert response.status_code == 200 |
| 104 |
assert response.json["response"]["current_email"] == "[email protected]" |
| 105 |
assert "[email protected]" == ce_requests[0]["new_email"] |
| 106 |
token = ce_requests[0]["token"] |
| 107 |
assert len(outbox) == 1 |
| 108 |
|
| 109 |
response = client.get( |
| 110 |
"/change-email/" + token, headers=headers, follow_redirects=True |
| 111 |
) |
| 112 |
assert get_message("CHANGE_EMAIL_CONFIRMED") in response.data |
| 113 |
assert is_authenticated(client, get_message) |
| 114 |
|
| 115 |
logout(client) |
| 116 |
authenticate(client, email="[email protected]") |
| 117 |
assert is_authenticated(client, get_message) |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.settings( |
| 121 |
change_email_within="1 milliseconds", change_email_error_view="/change-email" |
| 122 |
) |
| 123 |
def test_expired_token(client, get_message): |
| 124 |
# Note that we need relatively new-ish date since session cookies also expire. |
| 125 |
with freeze_time(date.today() + timedelta(days=-1)): |
| 126 |
authenticate(client, email="[email protected]") |
| 127 |
with capture_change_email_requests() as ce_requests: |
| 128 |
client.post("/change-email", data=dict(email="[email protected]")) |
| 129 |
|
| 130 |
assert "[email protected]" == ce_requests[0]["new_email"] |
| 131 |
token = ce_requests[0]["token"] |
| 132 |
|
| 133 |
response = client.get("/change-email/" + token, follow_redirects=True) |
| 134 |
msg = get_message("CHANGE_EMAIL_EXPIRED", within="1 milliseconds") |
| 135 |
assert msg in response.data |
| 136 |
|
| 137 |
|
| 138 |
def test_template(app, client, get_message, outbox): |
| 139 |
# Check contents of email template - this uses a test template |
| 140 |
# to check all context vars since the default template |
| 141 |
# doesn't have all of them. |
| 142 |
|
| 143 |
authenticate(client, email="[email protected]") |
| 144 |
with capture_change_email_requests() as ce_requests: |
| 145 |
client.post("/change-email", data=dict(email="[email protected]")) |
| 146 |
# check email |
| 147 |
assert outbox[0].recipients[0] == "[email protected]" |
| 148 |
matcher = re.findall(r"\w+:.*", outbox[0].body, re.IGNORECASE) |
| 149 |
# should be 5 - link, email, token, config item, within |
| 150 |
assert matcher[1].split(":")[1] == "[email protected]" |
| 151 |
assert matcher[2].split(":")[1] == ce_requests[0]["token"] |
| 152 |
assert matcher[3].split(":")[1] == "True" # register_blueprint |
| 153 |
assert matcher[4].split(":")[1] == "2 hours" |
| 154 |
|
| 155 |
# check link |
| 156 |
_, link = matcher[0].split(":", 1) |
| 157 |
response = client.get(link, follow_redirects=True) |
| 158 |
assert get_message("CHANGE_EMAIL_CONFIRMED") in response.data |
| 159 |
|
| 160 |
|
| 161 |
@pytest.mark.settings(return_generic_responses=True) |
| 162 |
def test_generic_response(app, client, get_message, outbox): |
| 163 |
authenticate(client, email="[email protected]") |
| 164 |
with capture_change_email_requests(): |
| 165 |
# first try bad formatted email - should get detailed error |
| 166 |
response = client.post("/change-email", json={"email": "<EMAIL>"}) |
| 167 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 168 |
"INVALID_EMAIL_ADDRESS" |
| 169 |
) |
| 170 |
|
| 171 |
# try existing email - should get same response as if it 'worked' |
| 172 |
response = client.post("/change-email", data=dict(email="[email protected]")) |
| 173 |
msg = get_message("CHANGE_EMAIL_SENT", email="[email protected]") |
| 174 |
assert msg in response.data |
| 175 |
# but no email was actually sent |
| 176 |
assert len(outbox) == 0 |
| 177 |
|
| 178 |
|
| 179 |
@pytest.mark.settings( |
| 180 |
redirect_host="myui.com:8090", |
| 181 |
redirect_behavior="spa", |
| 182 |
post_change_email_view="/change-email-redirect", |
| 183 |
change_email_error_view="/change-email-error", |
| 184 |
) |
| 185 |
def test_spa_get(app, client, get_message): |
| 186 |
json_authenticate(client, email="[email protected]") |
| 187 |
|
| 188 |
with capture_change_email_requests() as ce_requests: |
| 189 |
response = client.post("/change-email", data=dict(email="[email protected]")) |
| 190 |
msg = get_message("CHANGE_EMAIL_SENT", email="[email protected]") |
| 191 |
assert msg in response.data |
| 192 |
assert "[email protected]" == ce_requests[0]["new_email"] |
| 193 |
token = ce_requests[0]["token"] |
| 194 |
response = client.get("/change-email/" + token, follow_redirects=False) |
| 195 |
assert response.status_code in [302, 303] |
| 196 |
split = urlsplit(response.headers["Location"]) |
| 197 |
assert "myui.com:8090" == split.netloc |
| 198 |
assert "/change-email-redirect" == split.path |
| 199 |
|
| 200 |
# again - should be an error |
| 201 |
response = client.get("/change-email/" + token, follow_redirects=False) |
| 202 |
assert response.status_code in [302, 303] |
| 203 |
split = urlsplit(response.headers["Location"]) |
| 204 |
assert "myui.com:8090" == split.netloc |
| 205 |
assert "/change-email-error" == split.path |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.settings(change_email_error_view="/change-email") |
| 209 |
def test_ce_race(app, client, get_message): |
| 210 |
# test that if an email is taken between the link being sent and |
| 211 |
# the user confirming - they get an error |
| 212 |
|
| 213 |
authenticate(client, email="[email protected]") |
| 214 |
|
| 215 |
with capture_change_email_requests() as ce_requests: |
| 216 |
client.post("/change-email", data=dict(email="[email protected]")) |
| 217 |
token = ce_requests[0]["token"] |
| 218 |
|
| 219 |
with app.app_context(): |
| 220 |
app.security.datastore.create_user( |
| 221 |
email="[email protected]", |
| 222 |
password=hash_password("password"), |
| 223 |
) |
| 224 |
app.security.datastore.commit() |
| 225 |
with capture_flashes() as flashes: |
| 226 |
client.get("/change-email/" + token, follow_redirects=True) |
| 227 |
assert flashes[0]["message"].encode("utf-8") == get_message("API_ERROR") |