| 1 |
""" |
| 2 |
test_recoverable |
| 3 |
~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Recoverable (password, usernanme) functionality tests |
| 6 |
|
| 7 |
:copyright: (c) 2019-2026 by J. Christopher Wagner (jwag). |
| 8 |
:license: MIT, see LICENSE for more details. |
| 9 |
""" |
| 10 |
|
| 11 |
from datetime import date, timedelta |
| 12 |
import re |
| 13 |
from urllib.parse import parse_qsl, urlsplit |
| 14 |
|
| 15 |
import pytest |
| 16 |
from flask import Flask |
| 17 |
from freezegun import freeze_time |
| 18 |
from wtforms.fields import StringField |
| 19 |
from wtforms.validators import Length |
| 20 |
from tests.test_utils import ( |
| 21 |
authenticate, |
| 22 |
capture_flashes, |
| 23 |
capture_reset_password_requests, |
| 24 |
check_location, |
| 25 |
check_signals, |
| 26 |
get_form_input_value, |
| 27 |
is_authenticated, |
| 28 |
logout, |
| 29 |
populate_data, |
| 30 |
) |
| 31 |
|
| 32 |
from flask_security.core import Security, UserMixin |
| 33 |
from flask_security.forms import ForgotPasswordForm, LoginForm |
| 34 |
from flask_security.signals import ( |
| 35 |
password_reset, |
| 36 |
reset_password_instructions_sent, |
| 37 |
) |
| 38 |
|
| 39 |
pytestmark = pytest.mark.recoverable() |
| 40 |
|
| 41 |
|
| 42 |
def test_recoverable_flag(app, clients, get_message, outbox): |
| 43 |
recorded_resets = [] |
| 44 |
recorded_instructions_sent = [] |
| 45 |
|
| 46 |
@password_reset.connect_via(app) |
| 47 |
def on_password_reset(app, user): |
| 48 |
recorded_resets.append(user) |
| 49 |
|
| 50 |
@reset_password_instructions_sent.connect_via(app) |
| 51 |
def on_instructions_sent(app, **kwargs): |
| 52 |
assert isinstance(app, Flask) |
| 53 |
assert isinstance(kwargs["user"], UserMixin) |
| 54 |
assert isinstance(kwargs["token"], str) |
| 55 |
recorded_instructions_sent.append(kwargs["user"]) |
| 56 |
|
| 57 |
# Test the reset view |
| 58 |
response = clients.get("/reset") |
| 59 |
assert b"<h1>Send password reset instructions</h1>" in response.data |
| 60 |
assert re.search(b'<input[^>]*type="email"[^>]*>', response.data) |
| 61 |
|
| 62 |
# Test submitting email to reset password creates a token and sends email |
| 63 |
with capture_reset_password_requests() as requests: |
| 64 |
response = clients.post( |
| 65 |
"/reset", data=dict(email="[email protected]"), follow_redirects=True |
| 66 |
) |
| 67 |
|
| 68 |
assert len(recorded_instructions_sent) == 1 |
| 69 |
assert len(outbox) == 1 |
| 70 |
assert response.status_code == 200 |
| 71 |
assert get_message("PASSWORD_RESET_REQUEST", email="[email protected]") in response.data |
| 72 |
token = requests[0]["token"] |
| 73 |
|
| 74 |
# Test view for reset token |
| 75 |
response = clients.get("/reset/" + token) |
| 76 |
assert b"<h1>Reset password</h1>" in response.data |
| 77 |
|
| 78 |
# Test submitting a new password but leave out confirm |
| 79 |
response = clients.post( |
| 80 |
"/reset/" + token, data={"password": "newpassword"}, follow_redirects=True |
| 81 |
) |
| 82 |
assert get_message("PASSWORD_NOT_PROVIDED") in response.data |
| 83 |
assert len(recorded_resets) == 0 |
| 84 |
|
| 85 |
# Test submitting a new password |
| 86 |
response = clients.post( |
| 87 |
"/reset/" + token, |
| 88 |
data={"password": "awesome sunset", "password_confirm": "awesome sunset"}, |
| 89 |
follow_redirects=True, |
| 90 |
) |
| 91 |
|
| 92 |
assert get_message("PASSWORD_RESET_NO_LOGIN") in response.data |
| 93 |
assert len(recorded_resets) == 1 |
| 94 |
|
| 95 |
logout(clients) |
| 96 |
|
| 97 |
# Test logging in with the new password |
| 98 |
response = authenticate( |
| 99 |
clients, "[email protected]", "awesome sunset", follow_redirects=True |
| 100 |
) |
| 101 |
assert b"Welcome [email protected]" in response.data |
| 102 |
|
| 103 |
logout(clients) |
| 104 |
|
| 105 |
# Test invalid email |
| 106 |
response = clients.post( |
| 107 |
"/reset", data=dict(email="[email protected]"), follow_redirects=True |
| 108 |
) |
| 109 |
assert get_message("USER_DOES_NOT_EXIST") in response.data |
| 110 |
|
| 111 |
logout(clients) |
| 112 |
|
| 113 |
# Test invalid token |
| 114 |
response = clients.post( |
| 115 |
"/reset/bogus", |
| 116 |
data={"password": "awesome sunset", "password_confirm": "awesome sunset"}, |
| 117 |
follow_redirects=True, |
| 118 |
) |
| 119 |
assert get_message("INVALID_RESET_PASSWORD_TOKEN") in response.data |
| 120 |
|
| 121 |
# Test mangled token |
| 122 |
token = ( |
| 123 |
"WyIxNjQ2MzYiLCIxMzQ1YzBlZmVhM2VhZjYwODgwMDhhZGU2YzU0MzZjMiJd." |
| 124 |
"BZEw_Q.lQyo3npdPZtcJ_sNHVHP103syjM" |
| 125 |
"&url_id=fbb89a8328e58c181ea7d064c2987874bc54a23d" |
| 126 |
) |
| 127 |
response = clients.post( |
| 128 |
"/reset/" + token, |
| 129 |
data={"password": "newpassword", "password_confirm": "newpassword"}, |
| 130 |
follow_redirects=True, |
| 131 |
) |
| 132 |
assert get_message("INVALID_RESET_PASSWORD_TOKEN") in response.data |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.confirmable() |
| 136 |
@pytest.mark.settings(requires_confirmation_error_view="/confirm") |
| 137 |
def test_requires_confirmation_error_redirect(app, clients): |
| 138 |
response = clients.post( |
| 139 |
"/reset", data=dict(email="[email protected]"), follow_redirects=True |
| 140 |
) |
| 141 |
assert b"send_confirmation_form" in response.data |
| 142 |
assert b"[email protected]" in response.data |
| 143 |
|
| 144 |
|
| 145 |
@pytest.mark.settings() |
| 146 |
def test_recoverable_json(app, client, get_message, outbox): |
| 147 |
recorded_resets = [] |
| 148 |
recorded_instructions_sent = [] |
| 149 |
|
| 150 |
@password_reset.connect_via(app) |
| 151 |
def on_password_reset(app, user): |
| 152 |
recorded_resets.append(user) |
| 153 |
|
| 154 |
@reset_password_instructions_sent.connect_via(app) |
| 155 |
def on_instructions_sent(app, **kwargs): |
| 156 |
recorded_instructions_sent.append(kwargs["user"]) |
| 157 |
|
| 158 |
with capture_flashes() as flashes: |
| 159 |
# Test reset password creates a token and sends email |
| 160 |
with capture_reset_password_requests() as requests: |
| 161 |
response = client.post( |
| 162 |
"/reset", |
| 163 |
json=dict(email="[email protected]"), |
| 164 |
headers={"Content-Type": "application/json"}, |
| 165 |
) |
| 166 |
assert response.headers["Content-Type"] == "application/json" |
| 167 |
|
| 168 |
assert len(recorded_instructions_sent) == 1 |
| 169 |
assert len(outbox) == 1 |
| 170 |
assert response.status_code == 200 |
| 171 |
token = requests[0]["token"] |
| 172 |
|
| 173 |
# Test invalid email |
| 174 |
response = client.post( |
| 175 |
"/reset", |
| 176 |
json=dict(email="[email protected]"), |
| 177 |
) |
| 178 |
assert response.status_code == 400 |
| 179 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 180 |
"USER_DOES_NOT_EXIST" |
| 181 |
) |
| 182 |
|
| 183 |
# Test submitting a new password but leave out 'confirm' |
| 184 |
response = client.post( |
| 185 |
"/reset/" + token, |
| 186 |
json=dict(password="newpassword"), |
| 187 |
headers={"Content-Type": "application/json"}, |
| 188 |
) |
| 189 |
assert response.status_code == 400 |
| 190 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 191 |
"PASSWORD_NOT_PROVIDED" |
| 192 |
) |
| 193 |
|
| 194 |
# Test submitting a new password |
| 195 |
response = client.post( |
| 196 |
"/reset/" + token + "?include_auth_token", |
| 197 |
json=dict(password="awesome sunset", password_confirm="awesome sunset"), |
| 198 |
) |
| 199 |
assert not response.json["response"] |
| 200 |
assert len(recorded_resets) == 1 |
| 201 |
|
| 202 |
# reset automatically logs user in |
| 203 |
logout(client) |
| 204 |
|
| 205 |
# Test logging in with the new password |
| 206 |
response = client.post( |
| 207 |
"/login?include_auth_token", |
| 208 |
json=dict(email="[email protected]", password="awesome sunset"), |
| 209 |
headers={"Content-Type": "application/json"}, |
| 210 |
) |
| 211 |
assert all( |
| 212 |
k in response.json["response"]["user"] |
| 213 |
for k in ["email", "authentication_token"] |
| 214 |
) |
| 215 |
|
| 216 |
logout(client) |
| 217 |
|
| 218 |
# Use token again - should fail since already have set new password. |
| 219 |
response = client.post( |
| 220 |
"/reset/" + token, |
| 221 |
json=dict(password="newpassword", password_confirm="newpassword"), |
| 222 |
headers={"Content-Type": "application/json"}, |
| 223 |
) |
| 224 |
assert response.status_code == 400 |
| 225 |
assert len(recorded_resets) == 1 |
| 226 |
|
| 227 |
# Test invalid token |
| 228 |
response = client.post( |
| 229 |
"/reset/bogus", |
| 230 |
json=dict(password="newpassword", password_confirm="newpassword"), |
| 231 |
headers={"Content-Type": "application/json"}, |
| 232 |
) |
| 233 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 234 |
"INVALID_RESET_PASSWORD_TOKEN" |
| 235 |
) |
| 236 |
assert len(flashes) == 0 |
| 237 |
|
| 238 |
|
| 239 |
def test_recoverable_template(app, client, get_message, outbox): |
| 240 |
# Check contents of email template - this uses a test template |
| 241 |
# in order to check all context vars since the default template |
| 242 |
# doesn't have all of them. |
| 243 |
with capture_reset_password_requests() as resets: |
| 244 |
response = client.post( |
| 245 |
"/reset", data=dict(email="[email protected]"), follow_redirects=True |
| 246 |
) |
| 247 |
assert len(outbox) == 1 |
| 248 |
matcher = re.findall(r"\w+:.*", outbox[0].body, re.IGNORECASE) |
| 249 |
# should be 4 - link, email, token, config item |
| 250 |
assert matcher[1].split(":")[1] == "[email protected]" |
| 251 |
assert matcher[2].split(":")[1] == resets[0]["reset_token"] |
| 252 |
assert matcher[3].split(":")[1] == "True" # register_blueprint |
| 253 |
assert matcher[4].split(":")[1] == "/reset" # SECURITY_RESET_URL |
| 254 |
|
| 255 |
# check link |
| 256 |
link = matcher[0].split(":", 1)[1] |
| 257 |
response = client.get(link, follow_redirects=True) |
| 258 |
assert b"Reset Password" in response.data |
| 259 |
|
| 260 |
|
| 261 |
def test_recover_invalidates_session(app, client): |
| 262 |
# Make sure that if we reset our password - prior sessions are invalidated. |
| 263 |
|
| 264 |
other_client = app.test_client() |
| 265 |
authenticate(other_client) |
| 266 |
response = other_client.get("/profile", follow_redirects=True) |
| 267 |
assert b"Profile Page" in response.data |
| 268 |
|
| 269 |
# use normal client to reset password |
| 270 |
with capture_reset_password_requests() as requests: |
| 271 |
response = client.post( |
| 272 |
"/reset", |
| 273 |
json=dict(email="[email protected]"), |
| 274 |
) |
| 275 |
assert response.headers["Content-Type"] == "application/json" |
| 276 |
|
| 277 |
assert response.status_code == 200 |
| 278 |
token = requests[0]["token"] |
| 279 |
|
| 280 |
# Test submitting a new password |
| 281 |
response = client.post( |
| 282 |
"/reset/" + token + "?include_auth_token", |
| 283 |
json=dict(password="awesome sunset", password_confirm="awesome sunset"), |
| 284 |
) |
| 285 |
assert response.status_code == 200 |
| 286 |
|
| 287 |
# try to access protected endpoint with old session - shouldn't work |
| 288 |
response = other_client.get("/profile") |
| 289 |
assert response.status_code in [302, 303] |
| 290 |
assert response.location == "/login?next=/profile" |
| 291 |
|
| 292 |
|
| 293 |
def test_login_form_description(app, sqlalchemy_datastore): |
| 294 |
app.security = Security(app, datastore=sqlalchemy_datastore) |
| 295 |
with app.test_request_context("/login"): |
| 296 |
login_form = LoginForm() |
| 297 |
expected = '<a href="/reset">Forgot password?</a>' |
| 298 |
assert login_form.password.description == expected |
| 299 |
|
| 300 |
|
| 301 |
@pytest.mark.settings(reset_password_within="1 milliseconds") |
| 302 |
def test_expired_reset_token(client, get_message): |
| 303 |
# Note that we need relatively new-ish date since session cookies also expire. |
| 304 |
with freeze_time(date.today() + timedelta(days=-1)): |
| 305 |
with capture_reset_password_requests() as requests: |
| 306 |
client.post("/reset", data=dict(email="[email protected]"), follow_redirects=True) |
| 307 |
|
| 308 |
user = requests[0]["user"] |
| 309 |
token = requests[0]["token"] |
| 310 |
|
| 311 |
with capture_flashes() as flashes: |
| 312 |
msg = get_message( |
| 313 |
"PASSWORD_RESET_EXPIRED", within="1 milliseconds", email=user.email |
| 314 |
) |
| 315 |
|
| 316 |
# Test getting reset form with expired token |
| 317 |
response = client.get("/reset/" + token, follow_redirects=True) |
| 318 |
assert msg in response.data |
| 319 |
|
| 320 |
# Test trying to reset password with expired token |
| 321 |
response = client.post( |
| 322 |
"/reset/" + token, |
| 323 |
data={"password": "newpassword", "password_confirm": "newpassword"}, |
| 324 |
follow_redirects=True, |
| 325 |
) |
| 326 |
|
| 327 |
assert msg in response.data |
| 328 |
assert len(flashes) == 2 |
| 329 |
|
| 330 |
|
| 331 |
def test_bad_reset_token(client, get_message): |
| 332 |
# Test invalid token - get form |
| 333 |
response = client.get("/reset/bogus", follow_redirects=True) |
| 334 |
assert get_message("INVALID_RESET_PASSWORD_TOKEN") in response.data |
| 335 |
|
| 336 |
# Test invalid token - reset password |
| 337 |
response = client.post( |
| 338 |
"/reset/bogus", |
| 339 |
data={"password": "newpassword", "password_confirm": "newpassword"}, |
| 340 |
follow_redirects=True, |
| 341 |
) |
| 342 |
assert get_message("INVALID_RESET_PASSWORD_TOKEN") in response.data |
| 343 |
|
| 344 |
# Test mangled token |
| 345 |
token = ( |
| 346 |
"WyIxNjQ2MzYiLCIxMzQ1YzBlZmVhM2VhZjYwODgwMDhhZGU2YzU0MzZjMiJd." |
| 347 |
"BZEw_Q.lQyo3npdPZtcJ_sNHVHP103syjM" |
| 348 |
"&url_id=fbb89a8328e58c181ea7d064c2987874bc54a23d" |
| 349 |
) |
| 350 |
response = client.post( |
| 351 |
"/reset/" + token, |
| 352 |
data={"password": "newpassword", "password_confirm": "newpassword"}, |
| 353 |
follow_redirects=True, |
| 354 |
) |
| 355 |
assert get_message("INVALID_RESET_PASSWORD_TOKEN") in response.data |
| 356 |
|
| 357 |
|
| 358 |
def test_reset_token_deleted_user(app, client, get_message): |
| 359 |
with capture_reset_password_requests() as requests: |
| 360 |
client.post("/reset", data=dict(email="[email protected]"), follow_redirects=True) |
| 361 |
|
| 362 |
token = requests[0]["token"] |
| 363 |
|
| 364 |
# Delete user |
| 365 |
with app.app_context(): |
| 366 |
# load user (and role) to get into session so cascade delete works. |
| 367 |
user = app.security.datastore.find_user(email="[email protected]") |
| 368 |
app.security.datastore.delete(user) |
| 369 |
app.security.datastore.commit() |
| 370 |
|
| 371 |
response = client.post( |
| 372 |
"/reset/" + token, |
| 373 |
data={"password": "newpassword", "password_confirm": "newpassword"}, |
| 374 |
follow_redirects=True, |
| 375 |
) |
| 376 |
|
| 377 |
msg = get_message("INVALID_RESET_PASSWORD_TOKEN") |
| 378 |
assert msg in response.data |
| 379 |
|
| 380 |
|
| 381 |
def test_used_reset_token(client, get_message): |
| 382 |
with capture_reset_password_requests() as requests: |
| 383 |
client.post("/reset", data=dict(email="[email protected]"), follow_redirects=True) |
| 384 |
|
| 385 |
token = requests[0]["token"] |
| 386 |
|
| 387 |
# use the token |
| 388 |
response = client.post( |
| 389 |
"/reset/" + token, |
| 390 |
data={"password": "awesome sunset", "password_confirm": "awesome sunset"}, |
| 391 |
follow_redirects=True, |
| 392 |
) |
| 393 |
|
| 394 |
assert get_message("PASSWORD_RESET_NO_LOGIN") in response.data |
| 395 |
|
| 396 |
logout(client) |
| 397 |
|
| 398 |
# attempt to use it a second time |
| 399 |
response2 = client.post( |
| 400 |
"/reset/" + token, |
| 401 |
data={"password": "otherpassword", "password_confirm": "otherpassword"}, |
| 402 |
follow_redirects=True, |
| 403 |
) |
| 404 |
|
| 405 |
msg = get_message("INVALID_RESET_PASSWORD_TOKEN") |
| 406 |
assert msg in response2.data |
| 407 |
|
| 408 |
|
| 409 |
def test_reset_passwordless_user(client, get_message): |
| 410 |
with capture_reset_password_requests() as requests: |
| 411 |
client.post("/reset", data=dict(email="[email protected]"), follow_redirects=True) |
| 412 |
|
| 413 |
token = requests[0]["token"] |
| 414 |
|
| 415 |
# use the token |
| 416 |
response = client.post( |
| 417 |
"/reset/" + token, |
| 418 |
data={"password": "awesome sunset", "password_confirm": "awesome sunset"}, |
| 419 |
follow_redirects=True, |
| 420 |
) |
| 421 |
|
| 422 |
assert get_message("PASSWORD_RESET_NO_LOGIN") in response.data |
| 423 |
|
| 424 |
|
| 425 |
@pytest.mark.settings(reset_url="/custom_reset") |
| 426 |
def test_custom_reset_url(client): |
| 427 |
response = client.get("/custom_reset") |
| 428 |
assert response.status_code == 200 |
| 429 |
|
| 430 |
|
| 431 |
@pytest.mark.settings( |
| 432 |
reset_password_template="custom_security/reset_password.html", |
| 433 |
forgot_password_template="custom_security/forgot_password.html", |
| 434 |
) |
| 435 |
def test_custom_reset_templates(client): |
| 436 |
response = client.get("/reset") |
| 437 |
assert b"CUSTOM FORGOT PASSWORD" in response.data |
| 438 |
|
| 439 |
with capture_reset_password_requests() as requests: |
| 440 |
client.post("/reset", data=dict(email="[email protected]"), follow_redirects=True) |
| 441 |
token = requests[0]["token"] |
| 442 |
|
| 443 |
response = client.get("/reset/" + token) |
| 444 |
assert b"CUSTOM RESET PASSWORD" in response.data |
| 445 |
|
| 446 |
|
| 447 |
@pytest.mark.settings( |
| 448 |
redirect_host="myui.com:8090", |
| 449 |
redirect_behavior="spa", |
| 450 |
reset_view="/reset-redirect", |
| 451 |
) |
| 452 |
def test_spa_get(app, client): |
| 453 |
""" |
| 454 |
Test 'single-page-application' style redirects |
| 455 |
This uses json only. |
| 456 |
""" |
| 457 |
with capture_reset_password_requests() as requests: |
| 458 |
response = client.post( |
| 459 |
"/reset", |
| 460 |
json=dict(email="[email protected]"), |
| 461 |
) |
| 462 |
assert response.headers["Content-Type"] == "application/json" |
| 463 |
assert "user" not in response.json["response"] |
| 464 |
token = requests[0]["token"] |
| 465 |
|
| 466 |
response = client.get("/reset/" + token) |
| 467 |
assert response.status_code in [302, 303] |
| 468 |
split = urlsplit(response.headers["Location"]) |
| 469 |
assert "myui.com:8090" == split.netloc |
| 470 |
assert "/reset-redirect" == split.path |
| 471 |
qparams = dict(parse_qsl(split.query)) |
| 472 |
# we shouldn't be showing PII |
| 473 |
assert "email" not in qparams |
| 474 |
assert qparams["token"] == token |
| 475 |
|
| 476 |
|
| 477 |
@pytest.mark.settings( |
| 478 |
reset_password_within="1 milliseconds", |
| 479 |
redirect_host="localhost:8081", |
| 480 |
redirect_behavior="spa", |
| 481 |
reset_error_view="/reset-error", |
| 482 |
) |
| 483 |
def test_spa_get_bad_token(app, client, get_message): |
| 484 |
"""Test expired and invalid token""" |
| 485 |
with capture_flashes() as flashes: |
| 486 |
# Note that we need relatively new-ish date since session cookies also expire. |
| 487 |
with freeze_time(date.today() + timedelta(days=-1)): |
| 488 |
with capture_reset_password_requests() as requests: |
| 489 |
response = client.post( |
| 490 |
"/reset", |
| 491 |
json=dict(email="[email protected]"), |
| 492 |
headers={"Content-Type": "application/json"}, |
| 493 |
) |
| 494 |
assert response.headers["Content-Type"] == "application/json" |
| 495 |
assert "user" not in response.json["response"] |
| 496 |
token = requests[0]["token"] |
| 497 |
|
| 498 |
response = client.get("/reset/" + token) |
| 499 |
assert response.status_code in [302, 303] |
| 500 |
split = urlsplit(response.headers["Location"]) |
| 501 |
assert "localhost:8081" == split.netloc |
| 502 |
assert "/reset-error" == split.path |
| 503 |
qparams = dict(parse_qsl(split.query)) |
| 504 |
# on error - no PII should be returned. |
| 505 |
assert "error" in qparams |
| 506 |
assert "identity" not in qparams |
| 507 |
assert "email" not in qparams |
| 508 |
|
| 509 |
msg = get_message( |
| 510 |
"PASSWORD_RESET_EXPIRED", within="1 milliseconds", email="[email protected]" |
| 511 |
) |
| 512 |
assert msg == qparams["error"].encode("utf-8") |
| 513 |
|
| 514 |
# Test mangled token |
| 515 |
token = ( |
| 516 |
"WyIxNjQ2MzYiLCIxMzQ1YzBlZmVhM2VhZjYwODgwMDhhZGU2YzU0MzZjMiJd." |
| 517 |
"BZEw_Q.lQyo3npdPZtcJ_sNHVHP103syjM" |
| 518 |
"&url_id=fbb89a8328e58c181ea7d064c2987874bc54a23d" |
| 519 |
) |
| 520 |
response = client.get("/reset/" + token) |
| 521 |
assert response.status_code in [302, 303] |
| 522 |
split = urlsplit(response.headers["Location"]) |
| 523 |
assert "localhost:8081" == split.netloc |
| 524 |
assert "/reset-error" == split.path |
| 525 |
qparams = dict(parse_qsl(split.query)) |
| 526 |
assert len(qparams) == 1 |
| 527 |
assert all(k in qparams for k in ["error"]) |
| 528 |
|
| 529 |
msg = get_message("INVALID_RESET_PASSWORD_TOKEN") |
| 530 |
assert msg == qparams["error"].encode("utf-8") |
| 531 |
assert len(flashes) == 0 |
| 532 |
|
| 533 |
|
| 534 |
@pytest.mark.settings(password_complexity_checker="zxcvbn") |
| 535 |
def test_easy_password(client, get_message): |
| 536 |
with capture_reset_password_requests() as requests: |
| 537 |
client.post("/reset", data=dict(email="[email protected]"), follow_redirects=True) |
| 538 |
|
| 539 |
token = requests[0]["token"] |
| 540 |
|
| 541 |
# use the token |
| 542 |
response = client.post( |
| 543 |
"/reset/" + token, |
| 544 |
data={"password": "mypassword", "password_confirm": "mypassword"}, |
| 545 |
follow_redirects=True, |
| 546 |
) |
| 547 |
|
| 548 |
assert b"This is a very common password" in response.data |
| 549 |
|
| 550 |
|
| 551 |
def test_reset_inactive(client, get_message): |
| 552 |
response = client.post( |
| 553 |
"/reset", data=dict(email="[email protected]"), follow_redirects=True |
| 554 |
) |
| 555 |
assert get_message("DISABLED_ACCOUNT") in response.data |
| 556 |
|
| 557 |
response = client.post( |
| 558 |
"/reset", |
| 559 |
json=dict(email="[email protected]"), |
| 560 |
headers={"Content-Type": "application/json"}, |
| 561 |
) |
| 562 |
assert response.status_code == 400 |
| 563 |
|
| 564 |
|
| 565 |
def test_email_normalization(client, get_message): |
| 566 |
response = client.post( |
| 567 |
"/reset", data=dict(email="[email protected]"), follow_redirects=True |
| 568 |
) |
| 569 |
assert response.status_code == 200 |
| 570 |
assert get_message("PASSWORD_RESET_REQUEST", email="[email protected]") in response.data |
| 571 |
|
| 572 |
|
| 573 |
def test_password_normalization(app, client, get_message): |
| 574 |
with capture_reset_password_requests() as requests: |
| 575 |
response = client.post( |
| 576 |
"/reset", |
| 577 |
json=dict(email="[email protected]"), |
| 578 |
) |
| 579 |
assert response.status_code == 200 |
| 580 |
token = requests[0]["token"] |
| 581 |
|
| 582 |
response = client.post( |
| 583 |
"/reset/" + token, |
| 584 |
json=dict(password="HöheHöhe", password_confirm="HöheHöhe"), |
| 585 |
) |
| 586 |
assert response.status_code == 200 |
| 587 |
logout(client) |
| 588 |
|
| 589 |
# make sure can log in with new password both normalized or not |
| 590 |
response = client.post( |
| 591 |
"/login", |
| 592 |
json=dict(email="[email protected]", password="HöheHöhe"), |
| 593 |
) |
| 594 |
assert response.status_code == 200 |
| 595 |
# verify actually logged in |
| 596 |
response = client.get("/profile", follow_redirects=False) |
| 597 |
assert response.status_code == 200 |
| 598 |
logout(client) |
| 599 |
|
| 600 |
response = client.post( |
| 601 |
"/login", |
| 602 |
json=dict(email="[email protected]", password="Ho\u0308heHo\u0308he"), |
| 603 |
) |
| 604 |
assert response.status_code == 200 |
| 605 |
# verify actually logged in |
| 606 |
response = client.get("/profile", follow_redirects=False) |
| 607 |
assert response.status_code == 200 |
| 608 |
|
| 609 |
|
| 610 |
@pytest.mark.settings(return_generic_responses=True) |
| 611 |
def test_generic_response(app, client, get_message): |
| 612 |
# try unknown user |
| 613 |
response = client.post("/reset", data=dict(email="[email protected]")) |
| 614 |
assert ( |
| 615 |
get_message("PASSWORD_RESET_REQUEST", email="[email protected]") in response.data |
| 616 |
) |
| 617 |
|
| 618 |
response = client.post("/reset", json=dict(email="[email protected]")) |
| 619 |
assert response.status_code == 200 |
| 620 |
assert not any(e in response.json["response"].keys() for e in ["error", "errors"]) |
| 621 |
|
| 622 |
|
| 623 |
def test_generic_with_extra(app, sqlalchemy_datastore): |
| 624 |
# If application adds a field, make sure we properly return errors |
| 625 |
# even if 'RETURN_GENERIC_RESPONSES' is set. |
| 626 |
class MyForgotPasswordForm(ForgotPasswordForm): |
| 627 |
recaptcha = StringField("Recaptcha", validators=[Length(min=5)]) |
| 628 |
|
| 629 |
app.config["SECURITY_RETURN_GENERIC_RESPONSES"] = True |
| 630 |
app.config["SECURITY_FORGOT_PASSWORD_TEMPLATE"] = "generic_reset.html" |
| 631 |
app.security = Security( |
| 632 |
app, |
| 633 |
datastore=sqlalchemy_datastore, |
| 634 |
forgot_password_form=MyForgotPasswordForm, |
| 635 |
) |
| 636 |
|
| 637 |
populate_data(app) |
| 638 |
client = app.test_client() |
| 639 |
|
| 640 |
# Test valid user but invalid additional form field |
| 641 |
# We should get a form error for the extra (invalid) field, no flash |
| 642 |
bad_data = dict(email="[email protected]", recaptcha="1234") |
| 643 |
good_data = dict(email="[email protected]", recaptcha="123456") |
| 644 |
|
| 645 |
with capture_flashes() as flashes: |
| 646 |
response = client.post("/reset", data=bad_data) |
| 647 |
assert b"Field must be at least 5" in response.data |
| 648 |
assert len(flashes) == 0 |
| 649 |
with capture_flashes() as flashes: |
| 650 |
response = client.post("/reset", data=good_data) |
| 651 |
assert len(flashes) == 1 |
| 652 |
|
| 653 |
# JSON |
| 654 |
with capture_flashes() as flashes: |
| 655 |
response = client.post("/reset", json=bad_data) |
| 656 |
assert response.status_code == 400 |
| 657 |
assert ( |
| 658 |
"Field must be at least 5" |
| 659 |
in response.json["response"]["field_errors"]["recaptcha"][0] |
| 660 |
) |
| 661 |
assert len(flashes) == 0 |
| 662 |
with capture_flashes() as flashes: |
| 663 |
response = client.post("/reset", json=good_data) |
| 664 |
assert response.status_code == 200 |
| 665 |
assert len(flashes) == 0 |
| 666 |
|
| 667 |
# Try bad email AND bad recaptcha |
| 668 |
bad_data = dict(email="joe44-lp.com", recaptcha="1234") |
| 669 |
with capture_flashes() as flashes: |
| 670 |
response = client.post("/reset", data=bad_data) |
| 671 |
assert b"Field must be at least 5" in response.data |
| 672 |
assert len(flashes) == 0 |
| 673 |
with capture_flashes() as flashes: |
| 674 |
response = client.post("/reset", json=bad_data) |
| 675 |
assert response.status_code == 400 |
| 676 |
assert ( |
| 677 |
"Field must be at least 5" |
| 678 |
in response.json["response"]["field_errors"]["recaptcha"][0] |
| 679 |
) |
| 680 |
assert len(response.json["response"]["errors"]) == 1 |
| 681 |
assert len(flashes) == 0 |
| 682 |
|
| 683 |
|
| 684 |
def _locked(self, form_error): |
| 685 |
if self.email == "[email protected]": |
| 686 |
form_error.append("You are not allowed to do that") |
| 687 |
return True |
| 688 |
return False |
| 689 |
|
| 690 |
|
| 691 |
@pytest.mark.app_settings(TESTING_USER_INJECT=dict(is_locked=_locked)) |
| 692 |
def test_override_user_locked(app, client, get_message): |
| 693 |
response = client.post("/reset", json=dict(email="[email protected]")) |
| 694 |
assert response.status_code == 400 |
| 695 |
assert response.json["response"]["errors"] == ["You are not allowed to do that"] |
| 696 |
assert response.json["response"]["field_errors"]["email"] == [ |
| 697 |
"You are not allowed to do that" |
| 698 |
] |
| 699 |
|
| 700 |
|
| 701 |
@pytest.mark.filterwarnings("ignore") |
| 702 |
@pytest.mark.settings(auto_login_after_reset=True, post_reset_view="/post_reset") |
| 703 |
def test_auto_login(client, get_message): |
| 704 |
# test backwards compat flag (not OWASP recommended) |
| 705 |
with capture_reset_password_requests() as requests: |
| 706 |
response = client.post( |
| 707 |
"/reset", data=dict(email="[email protected]"), follow_redirects=True |
| 708 |
) |
| 709 |
assert response.status_code == 200 |
| 710 |
token = requests[0]["token"] |
| 711 |
|
| 712 |
# Test submitting a new password |
| 713 |
with capture_flashes() as flashes: |
| 714 |
response = client.post( |
| 715 |
"/reset/" + token, |
| 716 |
data=dict(password="awesome sunset", password_confirm="awesome sunset"), |
| 717 |
follow_redirects=True, |
| 718 |
) |
| 719 |
assert b"Post Reset" in response.data |
| 720 |
assert len(flashes) == 1 |
| 721 |
assert get_message("PASSWORD_RESET") == flashes[0]["message"].encode("utf-8") |
| 722 |
|
| 723 |
# verify actually logged in |
| 724 |
response = client.get("/profile", follow_redirects=False) |
| 725 |
assert response.status_code == 200 |
| 726 |
|
| 727 |
|
| 728 |
@pytest.mark.filterwarnings("ignore") |
| 729 |
@pytest.mark.settings(auto_login_after_reset=True) |
| 730 |
def test_auto_login_json(client, get_message): |
| 731 |
# test backwards compat flag (not OWASP recommended) |
| 732 |
with capture_reset_password_requests() as requests: |
| 733 |
response = client.post( |
| 734 |
"/reset", |
| 735 |
json=dict(email="[email protected]"), |
| 736 |
) |
| 737 |
assert response.headers["Content-Type"] == "application/json" |
| 738 |
|
| 739 |
assert response.status_code == 200 |
| 740 |
token = requests[0]["token"] |
| 741 |
|
| 742 |
# Test submitting a new password |
| 743 |
response = client.post( |
| 744 |
"/reset/" + token + "?include_auth_token", |
| 745 |
json=dict(password="awesome sunset", password_confirm="awesome sunset"), |
| 746 |
) |
| 747 |
assert all( |
| 748 |
k in response.json["response"]["user"] |
| 749 |
for k in ["email", "authentication_token"] |
| 750 |
) |
| 751 |
# verify actually logged in |
| 752 |
response = client.get("/profile", follow_redirects=False) |
| 753 |
assert response.status_code == 200 |
| 754 |
|
| 755 |
|
| 756 |
@pytest.mark.flask_async() |
| 757 |
@pytest.mark.settings() |
| 758 |
def test_recoverable_json_async(app, client, get_message): |
| 759 |
recorded_resets = [] |
| 760 |
recorded_instructions_sent = [] |
| 761 |
|
| 762 |
@password_reset.connect_via(app) |
| 763 |
async def on_password_reset(myapp, user): |
| 764 |
recorded_resets.append(user) |
| 765 |
|
| 766 |
@reset_password_instructions_sent.connect_via(app) |
| 767 |
async def on_instructions_sent(myapp, **kwargs): |
| 768 |
recorded_instructions_sent.append(kwargs["user"]) |
| 769 |
|
| 770 |
# Test reset password creates a token and sends email |
| 771 |
with capture_reset_password_requests() as requests: |
| 772 |
response = client.post( |
| 773 |
"/reset", |
| 774 |
json=dict(email="[email protected]"), |
| 775 |
headers={"Content-Type": "application/json"}, |
| 776 |
) |
| 777 |
|
| 778 |
assert len(recorded_instructions_sent) == 1 |
| 779 |
assert response.status_code == 200 |
| 780 |
token = requests[0]["token"] |
| 781 |
|
| 782 |
# Test submitting a new password |
| 783 |
response = client.post( |
| 784 |
"/reset/" + token + "?include_auth_token", |
| 785 |
json=dict(password="awesome sunset", password_confirm="awesome sunset"), |
| 786 |
) |
| 787 |
assert not response.json["response"] |
| 788 |
assert len(recorded_resets) == 1 |
| 789 |
|
| 790 |
|
| 791 |
@pytest.mark.csrf() |
| 792 |
@pytest.mark.settings(post_reset_view="/post_reset_view") |
| 793 |
def test_csrf(app, client, get_message): |
| 794 |
response = client.get("/reset") |
| 795 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 796 |
with capture_reset_password_requests() as requests: |
| 797 |
client.post( |
| 798 |
"/reset", |
| 799 |
data=dict(email="[email protected]", csrf_token=csrf_token), |
| 800 |
follow_redirects=True, |
| 801 |
) |
| 802 |
token = requests[0]["token"] |
| 803 |
|
| 804 |
# use the token - no CSRF so shouldn't work |
| 805 |
data = {"password": "mypassword", "password_confirm": "mypassword"} |
| 806 |
response = client.post( |
| 807 |
"/reset/" + token, |
| 808 |
data=data, |
| 809 |
) |
| 810 |
assert b"The CSRF token is missing" in response.data |
| 811 |
|
| 812 |
data["csrf_token"] = csrf_token |
| 813 |
response = client.post(f"/reset/{token}", data=data) |
| 814 |
assert check_location(app, response.location, "/post_reset_view") |
| 815 |
|
| 816 |
|
| 817 |
@pytest.mark.csrf(ignore_unauth=True) |
| 818 |
@pytest.mark.settings(post_reset_view="/post_reset_view") |
| 819 |
def test_auth_csrf(app, client, get_message): |
| 820 |
# Test reset when authenticated and unauth CSRF is off |
| 821 |
authenticate(client) |
| 822 |
response = client.get("/reset") |
| 823 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 824 |
assert "[email protected]" == get_form_input_value(response, "email") |
| 825 |
with capture_reset_password_requests() as requests: |
| 826 |
client.post( |
| 827 |
"/reset", |
| 828 |
data=dict(email="[email protected]", csrf_token=csrf_token), |
| 829 |
follow_redirects=True, |
| 830 |
) |
| 831 |
token = requests[0]["token"] |
| 832 |
|
| 833 |
# use the token - no CSRF so shouldn't work |
| 834 |
data = {"password": "mypassword", "password_confirm": "mypassword"} |
| 835 |
response = client.post( |
| 836 |
"/reset/" + token, |
| 837 |
data=data, |
| 838 |
) |
| 839 |
assert b"The CSRF token is missing" in response.data |
| 840 |
|
| 841 |
data["csrf_token"] = csrf_token |
| 842 |
response = client.post(f"/reset/{token}", data=data) |
| 843 |
assert check_location(app, response.location, "/post_reset_view") |
| 844 |
assert not is_authenticated(client, get_message) |
| 845 |
|
| 846 |
|
| 847 |
def test_recoverable_auth_json(app, client, get_message, outbox): |
| 848 |
recorded_resets = [] |
| 849 |
recorded_instructions_sent = [] |
| 850 |
|
| 851 |
@password_reset.connect_via(app) |
| 852 |
def on_password_reset(app, user): |
| 853 |
recorded_resets.append(user) |
| 854 |
|
| 855 |
@reset_password_instructions_sent.connect_via(app) |
| 856 |
def on_instructions_sent(app, **kwargs): |
| 857 |
recorded_instructions_sent.append(kwargs["user"]) |
| 858 |
|
| 859 |
authenticate(client, "[email protected]", password="password") |
| 860 |
with capture_flashes() as flashes: |
| 861 |
# Test reset password creates a token and sends email |
| 862 |
with capture_reset_password_requests() as requests: |
| 863 |
response = client.post( |
| 864 |
"/reset", |
| 865 |
json=dict(email="[email protected]"), |
| 866 |
) |
| 867 |
|
| 868 |
assert len(recorded_instructions_sent) == 1 |
| 869 |
assert len(outbox) == 1 |
| 870 |
assert response.status_code == 200 |
| 871 |
token = requests[0]["token"] |
| 872 |
|
| 873 |
# Test invalid email |
| 874 |
response = client.post( |
| 875 |
"/reset", |
| 876 |
json=dict(email="[email protected]"), |
| 877 |
) |
| 878 |
assert response.status_code == 400 |
| 879 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 880 |
"USER_DOES_NOT_EXIST" |
| 881 |
) |
| 882 |
|
| 883 |
# Test submitting a new password |
| 884 |
response = client.post( |
| 885 |
"/reset/" + token + "?include_auth_token", |
| 886 |
json=dict(password="awesome sunset", password_confirm="awesome sunset"), |
| 887 |
) |
| 888 |
assert not response.json["response"] |
| 889 |
assert len(recorded_resets) == 1 |
| 890 |
assert not is_authenticated(client, get_message) |
| 891 |
|
| 892 |
# Test logging in with the new password |
| 893 |
response = client.post( |
| 894 |
"/login?include_auth_token", |
| 895 |
json=dict(email="[email protected]", password="awesome sunset"), |
| 896 |
) |
| 897 |
assert all( |
| 898 |
k in response.json["response"]["user"] |
| 899 |
for k in ["email", "authentication_token"] |
| 900 |
) |
| 901 |
logout(client) |
| 902 |
|
| 903 |
# Use token again - should fail since already have set new password. |
| 904 |
response = client.post( |
| 905 |
"/reset/" + token, |
| 906 |
json=dict(password="newpassword", password_confirm="newpassword"), |
| 907 |
headers={"Content-Type": "application/json"}, |
| 908 |
) |
| 909 |
assert response.status_code == 400 |
| 910 |
assert len(recorded_resets) == 1 |
| 911 |
|
| 912 |
# Test invalid token |
| 913 |
response = client.post( |
| 914 |
"/reset/bogus", |
| 915 |
json=dict(password="newpassword", password_confirm="newpassword"), |
| 916 |
headers={"Content-Type": "application/json"}, |
| 917 |
) |
| 918 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 919 |
"INVALID_RESET_PASSWORD_TOKEN" |
| 920 |
) |
| 921 |
assert len(flashes) == 0 |
| 922 |
|
| 923 |
|
| 924 |
@pytest.mark.username_recovery() |
| 925 |
def test_username_recovery_valid_email(app, clients, get_message, outbox, signals): |
| 926 |
# Test the username recovery view |
| 927 |
response = clients.get("/recover-username") |
| 928 |
assert b"<h1>Username Recovery</h1>" in response.data |
| 929 |
|
| 930 |
response = clients.post( |
| 931 |
"/recover-username", data=dict(email="[email protected]"), follow_redirects=True |
| 932 |
) |
| 933 |
|
| 934 |
check_signals(signals, "username_recovery_email_sent") |
| 935 |
assert signals["username_recovery_email_sent"][0]["user_email"] == "[email protected]" |
| 936 |
assert len(outbox) == 1 |
| 937 |
assert response.status_code == 200 |
| 938 |
|
| 939 |
with capture_flashes() as flashes: |
| 940 |
response = clients.post( |
| 941 |
"/recover-username", |
| 942 |
data=dict(email="[email protected]"), |
| 943 |
follow_redirects=True, |
| 944 |
) |
| 945 |
assert len(flashes) == 1 |
| 946 |
assert get_message("USERNAME_RECOVERY_REQUEST") == flashes[0]["message"].encode( |
| 947 |
"utf-8" |
| 948 |
) |
| 949 |
|
| 950 |
# Validate the emailed username |
| 951 |
email = outbox[1] |
| 952 |
assert "Your username is: joe" in email.body |
| 953 |
|
| 954 |
# Test JSON responses |
| 955 |
response = clients.post( |
| 956 |
"/recover-username", |
| 957 |
json=dict(email="[email protected]"), |
| 958 |
headers={"Content-Type": "application/json"}, |
| 959 |
) |
| 960 |
assert response.status_code == 200 |
| 961 |
assert response.headers["Content-Type"] == "application/json" |
| 962 |
|
| 963 |
|
| 964 |
@pytest.mark.username_recovery() |
| 965 |
def test_username_recovery_invalid_email(app, client, outbox): |
| 966 |
response = client.post( |
| 967 |
"/recover-username", data=dict(email="[email protected]"), follow_redirects=True |
| 968 |
) |
| 969 |
|
| 970 |
assert len(outbox) == 0 |
| 971 |
assert response.status_code == 200 |
| 972 |
|
| 973 |
# Test JSON responses |
| 974 |
response = client.post( |
| 975 |
"/recover-username", |
| 976 |
json=dict(email="[email protected]"), |
| 977 |
) |
| 978 |
assert response.status_code == 400 |
| 979 |
assert response.headers["Content-Type"] == "application/json" |
| 980 |
assert len(response.json["response"]["errors"]) == 1 |
| 981 |
assert ( |
| 982 |
"Specified user does not exist" |
| 983 |
in response.json["response"]["field_errors"]["email"][0] |
| 984 |
) |
| 985 |
|
| 986 |
|
| 987 |
@pytest.mark.username_recovery() |
| 988 |
@pytest.mark.settings(return_generic_responses=True) |
| 989 |
def test_username_recovery_generic_responses( |
| 990 |
app, clients, get_message, outbox, signals |
| 991 |
): |
| 992 |
# Test with valid email |
| 993 |
with capture_flashes() as flashes: |
| 994 |
response = clients.post( |
| 995 |
"/recover-username", |
| 996 |
data=dict(email="[email protected]"), |
| 997 |
follow_redirects=True, |
| 998 |
) |
| 999 |
assert len(flashes) == 1 |
| 1000 |
assert get_message("USERNAME_RECOVERY_REQUEST") == flashes[0]["message"].encode( |
| 1001 |
"utf-8" |
| 1002 |
) |
| 1003 |
check_signals(signals, "username_recovery_email_sent") |
| 1004 |
assert signals["username_recovery_email_sent"][0]["user_email"] == "[email protected]" |
| 1005 |
assert len(outbox) == 1 |
| 1006 |
assert response.status_code == 200 |
| 1007 |
|
| 1008 |
# Test with non-existant email (should still return 200) |
| 1009 |
with capture_flashes() as flashes: |
| 1010 |
response = clients.post( |
| 1011 |
"/recover-username", |
| 1012 |
data=dict(email="[email protected]"), |
| 1013 |
follow_redirects=True, |
| 1014 |
) |
| 1015 |
assert len(flashes) == 1 |
| 1016 |
assert get_message("USERNAME_RECOVERY_REQUEST") == flashes[0]["message"].encode( |
| 1017 |
"utf-8" |
| 1018 |
) |
| 1019 |
# Validate no email was sent (there should only be one from the previous test) |
| 1020 |
check_signals(signals, "username_recovery_email_sent") |
| 1021 |
assert len(outbox) == 1 |
| 1022 |
assert response.status_code == 200 |
| 1023 |
|
| 1024 |
# Test JSON responses - valid email |
| 1025 |
response = clients.post("/recover-username", json=dict(email="[email protected]")) |
| 1026 |
assert response.status_code == 200 |
| 1027 |
assert response.headers["Content-Type"] == "application/json" |
| 1028 |
|
| 1029 |
# Test JSON responses - invalid email |
| 1030 |
response = clients.post("/recover-username", json=dict(email="[email protected]")) |
| 1031 |
assert response.status_code == 200 |
| 1032 |
assert response.headers["Content-Type"] == "application/json" |
| 1033 |
assert not any(e in response.json["response"].keys() for e in ["error", "errors"]) |
| 1034 |
|
| 1035 |
|
| 1036 |
@pytest.mark.username_recovery() |
| 1037 |
def test_ur_inactive(client, get_message): |
| 1038 |
response = client.post( |
| 1039 |
"/recover-username", data=dict(email="[email protected]"), follow_redirects=True |
| 1040 |
) |
| 1041 |
assert get_message("DISABLED_ACCOUNT") in response.data |
| 1042 |
|
| 1043 |
|
| 1044 |
@pytest.mark.username_recovery() |
| 1045 |
@pytest.mark.confirmable() |
| 1046 |
def test_ur_confirm_needed(app, client, get_message, outbox): |
| 1047 |
response = client.post("/recover-username", json=dict(email="[email protected]")) |
| 1048 |
assert response.status_code == 400 |
| 1049 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 1050 |
"CONFIRMATION_REQUIRED" |
| 1051 |
) |
| 1052 |
assert len(outbox) == 0 |
| 1053 |
|
| 1054 |
|
| 1055 |
@pytest.mark.username_recovery() |
| 1056 |
@pytest.mark.confirmable() |
| 1057 |
@pytest.mark.settings(requires_confirmation_error_view="/confirm") |
| 1058 |
def test_ur_confirmation_error_redirect(app, client): |
| 1059 |
response = client.post( |
| 1060 |
"/recover-username", data=dict(email="[email protected]"), follow_redirects=True |
| 1061 |
) |
| 1062 |
assert b"send_confirmation_form" in response.data |
| 1063 |
assert b"[email protected]" in response.data |
| 1064 |
|
| 1065 |
|
| 1066 |
@pytest.mark.username_recovery() |
| 1067 |
@pytest.mark.app_settings(TESTING_USER_INJECT=dict(is_locked=_locked)) |
| 1068 |
def test_ur_override_user_locked(app, client, get_message): |
| 1069 |
response = client.post("/recover-username", json=dict(email="[email protected]")) |
| 1070 |
assert response.status_code == 400 |
| 1071 |
assert response.json["response"]["errors"] == ["You are not allowed to do that"] |
| 1072 |
assert response.json["response"]["field_errors"]["email"] == [ |
| 1073 |
"You are not allowed to do that" |
| 1074 |
] |