| 1 |
""" |
| 2 |
test_changeable |
| 3 |
~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Changeable tests |
| 6 |
|
| 7 |
:copyright: (c) 2019-2026 by J. Christopher Wagner (jwag). |
| 8 |
:license: MIT, see LICENSE for more details. |
| 9 |
""" |
| 10 |
|
| 11 |
import base64 |
| 12 |
|
| 13 |
import pytest |
| 14 |
from flask import Flask |
| 15 |
import markupsafe |
| 16 |
|
| 17 |
from flask_security import PasswordUtil, UserMixin, password_changed, user_authenticated |
| 18 |
from flask_security.forms import _default_field_labels |
| 19 |
from flask_security.utils import localize_callback |
| 20 |
from tests.test_utils import ( |
| 21 |
authenticate, |
| 22 |
check_location, |
| 23 |
check_xlation, |
| 24 |
get_form_input_value, |
| 25 |
get_session, |
| 26 |
hash_password, |
| 27 |
init_app_with_options, |
| 28 |
json_authenticate, |
| 29 |
logout, |
| 30 |
) |
| 31 |
|
| 32 |
pytestmark = pytest.mark.changeable() |
| 33 |
|
| 34 |
|
| 35 |
def test_changeable_flag(app, clients, get_message, outbox): |
| 36 |
recorded = [] |
| 37 |
|
| 38 |
@password_changed.connect_via(app) |
| 39 |
def on_password_changed(app, user): |
| 40 |
assert isinstance(app, Flask) |
| 41 |
assert isinstance(user, UserMixin) |
| 42 |
recorded.append(user) |
| 43 |
|
| 44 |
authenticate(clients) |
| 45 |
|
| 46 |
# Test change view |
| 47 |
response = clients.get("/change", follow_redirects=True) |
| 48 |
assert b"Change Password" in response.data |
| 49 |
|
| 50 |
# Test wrong original password |
| 51 |
response = clients.post( |
| 52 |
"/change", |
| 53 |
data={ |
| 54 |
"password": "notpassword", |
| 55 |
"new_password": "newpassword", |
| 56 |
"new_password_confirm": "newpassword", |
| 57 |
}, |
| 58 |
follow_redirects=True, |
| 59 |
) |
| 60 |
assert get_message("INVALID_PASSWORD") in response.data |
| 61 |
|
| 62 |
# Test mismatch |
| 63 |
response = clients.post( |
| 64 |
"/change", |
| 65 |
data={ |
| 66 |
"password": "password", |
| 67 |
"new_password": "newpassword", |
| 68 |
"new_password_confirm": "notnewpassword", |
| 69 |
}, |
| 70 |
follow_redirects=True, |
| 71 |
) |
| 72 |
assert get_message("RETYPE_PASSWORD_MISMATCH") in response.data |
| 73 |
|
| 74 |
# Test missing password |
| 75 |
response = clients.post( |
| 76 |
"/change", |
| 77 |
data={"password": " ", "new_password": "", "new_password_confirm": ""}, |
| 78 |
follow_redirects=True, |
| 79 |
) |
| 80 |
assert get_message("PASSWORD_NOT_PROVIDED") in response.data |
| 81 |
response = clients.post( |
| 82 |
"/change", |
| 83 |
data={ |
| 84 |
"password": " ", |
| 85 |
"new_password": "awesome password", |
| 86 |
"new_password_confirm": "awesome password", |
| 87 |
}, |
| 88 |
follow_redirects=True, |
| 89 |
) |
| 90 |
assert get_message("PASSWORD_NOT_PROVIDED") in response.data |
| 91 |
|
| 92 |
# Test bad password |
| 93 |
response = clients.post( |
| 94 |
"/change", |
| 95 |
data={"password": "password", "new_password": "a", "new_password_confirm": "a"}, |
| 96 |
follow_redirects=True, |
| 97 |
) |
| 98 |
assert get_message("PASSWORD_INVALID_LENGTH", length=8) in response.data |
| 99 |
|
| 100 |
# Test same as previous |
| 101 |
response = clients.post( |
| 102 |
"/change", |
| 103 |
data={ |
| 104 |
"password": "password", |
| 105 |
"new_password": "password", |
| 106 |
"new_password_confirm": "password", |
| 107 |
}, |
| 108 |
follow_redirects=True, |
| 109 |
) |
| 110 |
assert get_message("PASSWORD_IS_THE_SAME") in response.data |
| 111 |
|
| 112 |
# Test successful submit sends email notification |
| 113 |
response = clients.post( |
| 114 |
"/change", |
| 115 |
data={ |
| 116 |
"password": "password", |
| 117 |
"new_password": "new strong password", |
| 118 |
"new_password_confirm": "new strong password", |
| 119 |
}, |
| 120 |
follow_redirects=True, |
| 121 |
) |
| 122 |
|
| 123 |
assert get_message("PASSWORD_CHANGE") in response.data |
| 124 |
assert b"Home Page" in response.data |
| 125 |
assert len(recorded) == 1 |
| 126 |
assert len(outbox) == 1 |
| 127 |
assert "Your password has been changed" in outbox[0].body |
| 128 |
|
| 129 |
# Test leading and trailing whitespace not stripped |
| 130 |
response = clients.post( |
| 131 |
"/change", |
| 132 |
data={ |
| 133 |
"password": "new strong password", |
| 134 |
"new_password": " new strong password ", |
| 135 |
"new_password_confirm": " new strong password ", |
| 136 |
}, |
| 137 |
follow_redirects=True, |
| 138 |
) |
| 139 |
assert get_message("PASSWORD_CHANGE") in response.data |
| 140 |
|
| 141 |
# Test JSON |
| 142 |
data = ( |
| 143 |
'{"password": " new strong password ", ' |
| 144 |
'"new_password": "new stronger password2", ' |
| 145 |
'"new_password_confirm": "new stronger password2"}' |
| 146 |
) |
| 147 |
response = clients.post( |
| 148 |
"/change", data=data, headers={"Content-Type": "application/json"} |
| 149 |
) |
| 150 |
assert response.status_code == 200 |
| 151 |
assert response.headers["Content-Type"] == "application/json" |
| 152 |
|
| 153 |
# Test JSON errors |
| 154 |
data = '{"password": "newpassword"}' |
| 155 |
response = clients.post( |
| 156 |
"/change", data=data, headers={"Content-Type": "application/json"} |
| 157 |
) |
| 158 |
assert response.status_code == 400 |
| 159 |
assert response.json["response"]["field_errors"]["new_password"] == [ |
| 160 |
"Password not provided" |
| 161 |
] |
| 162 |
|
| 163 |
|
| 164 |
def test_change_invalidates_session(app, client): |
| 165 |
# Make sure that if we change our password - prior sessions are invalidated. |
| 166 |
|
| 167 |
# changing password effectively re-logs in user - verify the signal |
| 168 |
auths = [] |
| 169 |
|
| 170 |
@user_authenticated.connect_via(app) |
| 171 |
def authned(myapp, user, **extra_args): |
| 172 |
auths.append((user.email, extra_args["authn_via"])) |
| 173 |
|
| 174 |
# No remember cookie since that also be reset and auto-login. |
| 175 |
data = dict(email="[email protected]", password="password", remember="") |
| 176 |
response = client.post("/login", data=data) |
| 177 |
sess = get_session(response) |
| 178 |
cur_user_id = sess.get("_user_id", sess.get("user_id")) |
| 179 |
|
| 180 |
response = client.post( |
| 181 |
"/change", |
| 182 |
data={ |
| 183 |
"password": "password", |
| 184 |
"new_password": "new strong password", |
| 185 |
"new_password_confirm": "new strong password", |
| 186 |
}, |
| 187 |
follow_redirects=True, |
| 188 |
) |
| 189 |
# First auth was the initial login above - second should be from /change |
| 190 |
assert auths[1][0] == "[email protected]" |
| 191 |
assert "change" in auths[1][1] |
| 192 |
|
| 193 |
# Should have received a new session cookie - so should still be logged in |
| 194 |
response = client.get("/profile", follow_redirects=True) |
| 195 |
assert b"Profile Page" in response.data |
| 196 |
|
| 197 |
# Now use old session - shouldn't work. |
| 198 |
with client.session_transaction() as oldsess: |
| 199 |
oldsess["_user_id"] = cur_user_id |
| 200 |
oldsess["user_id"] = cur_user_id |
| 201 |
|
| 202 |
# try to access protected endpoint - shouldn't work |
| 203 |
response = client.get("/profile") |
| 204 |
assert response.status_code in [302, 303] |
| 205 |
assert response.location == "/login?next=/profile" |
| 206 |
|
| 207 |
|
| 208 |
def test_change_updates_remember(app, client): |
| 209 |
# Test that on change password - remember cookie updated |
| 210 |
authenticate(client, remember=True) |
| 211 |
|
| 212 |
response = client.post( |
| 213 |
"/change", |
| 214 |
data={ |
| 215 |
"password": "password", |
| 216 |
"new_password": "new strong password", |
| 217 |
"new_password_confirm": "new strong password", |
| 218 |
}, |
| 219 |
follow_redirects=True, |
| 220 |
) |
| 221 |
|
| 222 |
# Should have received a new session cookie - so should still be logged in |
| 223 |
response = client.get("/profile", follow_redirects=True) |
| 224 |
assert b"Profile Page" in response.data |
| 225 |
|
| 226 |
assert client.get_cookie("remember_token") |
| 227 |
client.delete_cookie("session") |
| 228 |
response = client.get("/profile", follow_redirects=True) |
| 229 |
assert b"Profile Page" in response.data |
| 230 |
|
| 231 |
|
| 232 |
def test_change_invalidates_auth_token(app, client): |
| 233 |
# if change password, by default that should invalidate auth tokens |
| 234 |
response = json_authenticate(client) |
| 235 |
token = response.json["response"]["user"]["authentication_token"] |
| 236 |
headers = {"Authentication-Token": token} |
| 237 |
# make sure can access restricted page |
| 238 |
response = client.get("/token", headers=headers) |
| 239 |
assert b"Token Authentication" in response.data |
| 240 |
|
| 241 |
response = client.post( |
| 242 |
"/change", |
| 243 |
data={ |
| 244 |
"password": "password", |
| 245 |
"new_password": "new strong password", |
| 246 |
"new_password_confirm": "new strong password", |
| 247 |
}, |
| 248 |
follow_redirects=True, |
| 249 |
) |
| 250 |
assert response.status_code == 200 |
| 251 |
|
| 252 |
# authtoken should now be invalid |
| 253 |
response = client.get("/token", headers=headers) |
| 254 |
assert response.status_code in [302, 303] |
| 255 |
assert response.location == "/login?next=/token" |
| 256 |
|
| 257 |
|
| 258 |
def test_auth_uniquifier(app): |
| 259 |
pytest.importorskip("sqlalchemy") |
| 260 |
pytest.importorskip("flask_sqlalchemy") |
| 261 |
# If add fs_token_uniquifier to user model - change password shouldn't invalidate |
| 262 |
# auth tokens. |
| 263 |
from sqlalchemy import Column, String |
| 264 |
from flask_sqlalchemy import SQLAlchemy |
| 265 |
from flask_security.models import fsqla_v2 as fsqla |
| 266 |
from flask_security import Security, SQLAlchemyUserDatastore |
| 267 |
|
| 268 |
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" |
| 269 |
db = SQLAlchemy(app) |
| 270 |
|
| 271 |
fsqla.FsModels.set_db_info(db) |
| 272 |
|
| 273 |
class Role(db.Model, fsqla.FsRoleMixin): |
| 274 |
pass |
| 275 |
|
| 276 |
class User(db.Model, fsqla.FsUserMixin): |
| 277 |
fs_token_uniquifier = Column(String(64), unique=True, nullable=False) |
| 278 |
|
| 279 |
with app.app_context(): |
| 280 |
db.create_all() |
| 281 |
|
| 282 |
ds = SQLAlchemyUserDatastore(db, User, Role) |
| 283 |
app.security = Security(app, datastore=ds) |
| 284 |
|
| 285 |
with app.app_context(): |
| 286 |
ds.create_user( |
| 287 |
email="[email protected]", |
| 288 |
password=hash_password("password"), |
| 289 |
) |
| 290 |
ds.commit() |
| 291 |
|
| 292 |
tcl = app.test_client() |
| 293 |
|
| 294 |
# standard login with auth token |
| 295 |
response = json_authenticate(tcl) |
| 296 |
token = response.json["response"]["user"]["authentication_token"] |
| 297 |
headers = {"Authentication-Token": token} |
| 298 |
# make sure can access restricted page |
| 299 |
response = tcl.get("/token", headers=headers) |
| 300 |
assert b"Token Authentication" in response.data |
| 301 |
|
| 302 |
# change password |
| 303 |
response = tcl.post( |
| 304 |
"/change", |
| 305 |
data={ |
| 306 |
"password": "password", |
| 307 |
"new_password": "new strong password", |
| 308 |
"new_password_confirm": "new strong password", |
| 309 |
}, |
| 310 |
follow_redirects=True, |
| 311 |
) |
| 312 |
assert response.status_code == 200 |
| 313 |
|
| 314 |
# authtoken should still be valid |
| 315 |
response = tcl.get("/token", headers=headers) |
| 316 |
assert response.status_code == 200 |
| 317 |
|
| 318 |
with app.app_context(): |
| 319 |
db.engine.dispose() |
| 320 |
|
| 321 |
|
| 322 |
@pytest.mark.app_settings(babel_default_locale="fr_FR") |
| 323 |
@pytest.mark.babel() |
| 324 |
def test_xlation(app, client, get_message_local, outbox): |
| 325 |
# Test form and email translation |
| 326 |
assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog" |
| 327 |
|
| 328 |
authenticate(client) |
| 329 |
|
| 330 |
response = client.get("/change", follow_redirects=True) |
| 331 |
with app.test_request_context(): |
| 332 |
# Check header |
| 333 |
assert ( |
| 334 |
f'<h1>{localize_callback("Change Password")}</h1>'.encode() in response.data |
| 335 |
) |
| 336 |
submit = localize_callback(_default_field_labels["change_password"]) |
| 337 |
assert f'value="{submit}"'.encode() in response.data |
| 338 |
|
| 339 |
response = client.post( |
| 340 |
"/change", |
| 341 |
data={ |
| 342 |
"password": "password", |
| 343 |
"new_password": "new strong password", |
| 344 |
"new_password_confirm": "new strong password", |
| 345 |
}, |
| 346 |
follow_redirects=True, |
| 347 |
) |
| 348 |
|
| 349 |
with app.test_request_context(): |
| 350 |
assert get_message_local("PASSWORD_CHANGE").encode("utf-8") in response.data |
| 351 |
assert b"Home Page" in response.data |
| 352 |
assert len(outbox) == 1 |
| 353 |
assert ( |
| 354 |
localize_callback( |
| 355 |
app.config["SECURITY_EMAIL_SUBJECT_PASSWORD_CHANGE_NOTICE"] |
| 356 |
) |
| 357 |
in outbox[0].subject |
| 358 |
) |
| 359 |
assert ( |
| 360 |
str(markupsafe.escape(localize_callback("Your password has been changed."))) |
| 361 |
in outbox[0].alts["html"] |
| 362 |
) |
| 363 |
assert localize_callback("Your password has been changed") in outbox[0].body |
| 364 |
|
| 365 |
|
| 366 |
@pytest.mark.settings(change_url="/custom_change") |
| 367 |
def test_custom_change_url(client): |
| 368 |
authenticate(client) |
| 369 |
response = client.get("/custom_change") |
| 370 |
assert response.status_code == 200 |
| 371 |
|
| 372 |
|
| 373 |
@pytest.mark.settings(change_password_template="custom_security/change_password.html") |
| 374 |
def test_custom_change_template(client): |
| 375 |
authenticate(client) |
| 376 |
response = client.get("/change") |
| 377 |
assert b"CUSTOM CHANGE PASSWORD" in response.data |
| 378 |
|
| 379 |
|
| 380 |
@pytest.mark.settings(send_password_change_email=False) |
| 381 |
def test_disable_change_emails(app, client, outbox): |
| 382 |
client.post( |
| 383 |
"/change", |
| 384 |
data={ |
| 385 |
"password": "password", |
| 386 |
"new_password": "newpassword", |
| 387 |
"new_password_confirm": "newpassword", |
| 388 |
}, |
| 389 |
follow_redirects=True, |
| 390 |
) |
| 391 |
assert len(outbox) == 0 |
| 392 |
|
| 393 |
|
| 394 |
@pytest.mark.settings(post_change_view="/profile") |
| 395 |
def test_custom_post_change_view(client): |
| 396 |
authenticate(client) |
| 397 |
response = client.post( |
| 398 |
"/change", |
| 399 |
data={ |
| 400 |
"password": "password", |
| 401 |
"new_password": "new strong password", |
| 402 |
"new_password_confirm": "new strong password", |
| 403 |
}, |
| 404 |
follow_redirects=True, |
| 405 |
) |
| 406 |
|
| 407 |
assert b"Profile Page" in response.data |
| 408 |
|
| 409 |
|
| 410 |
def test_token_change(app, client_nc): |
| 411 |
# Verify can change password using token auth only |
| 412 |
login_response = json_authenticate(client_nc) |
| 413 |
token = login_response.json["response"]["user"]["authentication_token"] |
| 414 |
|
| 415 |
data = dict( |
| 416 |
password="password", |
| 417 |
new_password="new strong password", |
| 418 |
new_password_confirm="new strong password", |
| 419 |
) |
| 420 |
response = client_nc.post( |
| 421 |
"/change?include_auth_token=1", |
| 422 |
json=data, |
| 423 |
headers={"Content-Type": "application/json", "Authentication-Token": token}, |
| 424 |
) |
| 425 |
assert response.status_code == 200 |
| 426 |
assert "authentication_token" in response.json["response"]["user"] |
| 427 |
|
| 428 |
|
| 429 |
@pytest.mark.settings(api_enabled_methods=["basic"]) |
| 430 |
def test_basic_change(app, client_nc, get_message): |
| 431 |
# Verify can change password using basic auth |
| 432 |
data = dict( |
| 433 |
password="password", |
| 434 |
new_password="new strong password", |
| 435 |
new_password_confirm="new strong password", |
| 436 |
) |
| 437 |
response = client_nc.post("/change", data=data) |
| 438 |
assert get_message("UNAUTHENTICATED") in response.data |
| 439 |
assert "WWW-Authenticate" in response.headers |
| 440 |
|
| 441 |
response = client_nc.post( |
| 442 |
"/change", |
| 443 |
data=data, |
| 444 |
headers={ |
| 445 |
"Authorization": "Basic %s" |
| 446 |
% base64.b64encode(b"[email protected]:password").decode("utf-8") |
| 447 |
}, |
| 448 |
follow_redirects=True, |
| 449 |
) |
| 450 |
assert response.status_code == 200 |
| 451 |
# No session so no flashing |
| 452 |
assert b"Home Page" in response.data |
| 453 |
|
| 454 |
|
| 455 |
def __test_easy_password(client): |
| 456 |
authenticate(client) |
| 457 |
|
| 458 |
data = ( |
| 459 |
'{"password": "password", ' |
| 460 |
'"new_password": "mattmatt2", ' |
| 461 |
'"new_password_confirm": "mattmatt2"}' |
| 462 |
) |
| 463 |
response = client.post( |
| 464 |
"/change", data=data, headers={"Content-Type": "application/json"} |
| 465 |
) |
| 466 |
assert response.headers["Content-Type"] == "application/json" |
| 467 |
return response |
| 468 |
|
| 469 |
|
| 470 |
@pytest.mark.settings(password_complexity_checker="zxcvbn") |
| 471 |
def test_easy_password(app, client): |
| 472 |
response = __test_easy_password(client) |
| 473 |
assert response.status_code == 400 |
| 474 |
# Response from zxcvbn |
| 475 |
assert "Repeats like" in response.json["response"]["errors"][0] |
| 476 |
|
| 477 |
|
| 478 |
@pytest.mark.settings(password_complexity_checker="zxcvbn", zxcvbn_minimum_score=0) |
| 479 |
def test_easy_password_ok(app, client): |
| 480 |
response = __test_easy_password(client) |
| 481 |
assert response.status_code == 200 |
| 482 |
|
| 483 |
|
| 484 |
def test_my_validator(app, sqlalchemy_datastore): |
| 485 |
class MyPwUtil(PasswordUtil): |
| 486 |
def validate(self, password, is_register, **kwargs): |
| 487 |
user = kwargs["user"] |
| 488 |
# This is setup in createusers for matt. |
| 489 |
assert user.security_number == 123456 |
| 490 |
return ["Are you crazy?"], password |
| 491 |
|
| 492 |
init_app_with_options( |
| 493 |
app, sqlalchemy_datastore, **{"security_args": {"password_util_cls": MyPwUtil}} |
| 494 |
) |
| 495 |
tcl = app.test_client() |
| 496 |
|
| 497 |
authenticate(tcl) |
| 498 |
|
| 499 |
data = ( |
| 500 |
'{"password": "password", ' |
| 501 |
'"new_password": "mattmatt2", ' |
| 502 |
'"new_password_confirm": "mattmatt2"}' |
| 503 |
) |
| 504 |
response = tcl.post( |
| 505 |
"/change", data=data, headers={"Content-Type": "application/json"} |
| 506 |
) |
| 507 |
assert response.headers["Content-Type"] == "application/json" |
| 508 |
assert response.status_code == 400 |
| 509 |
assert "Are you crazy" in response.json["response"]["errors"][0] |
| 510 |
|
| 511 |
|
| 512 |
@pytest.mark.settings(password_length_min=12) |
| 513 |
def test_override_length(app, client, get_message): |
| 514 |
authenticate(client) |
| 515 |
response = client.post( |
| 516 |
"/change", |
| 517 |
data={ |
| 518 |
"password": "password", |
| 519 |
"new_password": "01234567890", |
| 520 |
"new_password_confirm": "01234567890", |
| 521 |
}, |
| 522 |
follow_redirects=True, |
| 523 |
) |
| 524 |
assert get_message("PASSWORD_INVALID_LENGTH", length=12) in response.data |
| 525 |
|
| 526 |
|
| 527 |
def test_unicode_length(app, client, get_message): |
| 528 |
# From NIST and OWASP - each unicode code point should count as a character. |
| 529 |
authenticate(client) |
| 530 |
|
| 531 |
# Emoji's are 4 bytes in utf-8 |
| 532 |
data = dict( |
| 533 |
password="password", |
| 534 |
new_password="\N{CYCLONE}\N{SUNRISE}\N{FOGGY}" |
| 535 |
"\N{VOLCANO}\N{CRESCENT MOON}\N{MILKY WAY}" |
| 536 |
"\N{FOG}\N{THERMOMETER}\N{ROSE}", |
| 537 |
new_password_confirm="\N{CYCLONE}\N{SUNRISE}\N{FOGGY}" |
| 538 |
"\N{VOLCANO}\N{CRESCENT MOON}\N{MILKY WAY}" |
| 539 |
"\N{FOG}\N{THERMOMETER}\N{ROSE}", |
| 540 |
) |
| 541 |
response = client.post( |
| 542 |
"/change", json=data, headers={"Content-Type": "application/json"} |
| 543 |
) |
| 544 |
assert response.headers["Content-Type"] == "application/json" |
| 545 |
assert response.status_code == 200 |
| 546 |
|
| 547 |
|
| 548 |
def test_unicode_invalid_length(app, client, get_message): |
| 549 |
# From NIST and OWASP - each unicode code point should count as a character. |
| 550 |
authenticate(client) |
| 551 |
|
| 552 |
# Emoji's are 4 bytes in utf-8 |
| 553 |
data = dict( |
| 554 |
password="password", |
| 555 |
new_password="\N{CYCLONE}\N{CYCLONE}\N{FOGGY}\N{FOGGY}", |
| 556 |
new_password_confirm="\N{CYCLONE}\N{CYCLONE}\N{FOGGY}\N{FOGGY}", |
| 557 |
) |
| 558 |
response = client.post( |
| 559 |
"/change", json=data, headers={"Content-Type": "application/json"} |
| 560 |
) |
| 561 |
assert response.headers["Content-Type"] == "application/json" |
| 562 |
assert response.status_code == 400 |
| 563 |
assert get_message("PASSWORD_INVALID_LENGTH", length=8) in response.data |
| 564 |
|
| 565 |
|
| 566 |
def test_pwd_normalize(app, client): |
| 567 |
"""Verify that can log in with both original and normalized pwd""" |
| 568 |
authenticate(client) |
| 569 |
|
| 570 |
data = dict( |
| 571 |
password="password", |
| 572 |
new_password="new strong password\N{ROMAN NUMERAL ONE}", |
| 573 |
new_password_confirm="new strong password\N{ROMAN NUMERAL ONE}", |
| 574 |
) |
| 575 |
response = client.post( |
| 576 |
"/change", |
| 577 |
json=data, |
| 578 |
headers={"Content-Type": "application/json"}, |
| 579 |
) |
| 580 |
assert response.status_code == 200 |
| 581 |
logout(client) |
| 582 |
|
| 583 |
# use original typed-in pwd |
| 584 |
response = client.post( |
| 585 |
"/login", |
| 586 |
json=dict( |
| 587 |
email="[email protected]", password="new strong password\N{ROMAN NUMERAL ONE}" |
| 588 |
), |
| 589 |
headers={"Content-Type": "application/json"}, |
| 590 |
) |
| 591 |
assert response.status_code == 200 |
| 592 |
logout(client) |
| 593 |
|
| 594 |
# try with normalized password |
| 595 |
response = client.post( |
| 596 |
"/login", |
| 597 |
json=dict( |
| 598 |
email="[email protected]", |
| 599 |
password="new strong password\N{LATIN CAPITAL LETTER I}", |
| 600 |
), |
| 601 |
headers={"Content-Type": "application/json"}, |
| 602 |
) |
| 603 |
assert response.status_code == 200 |
| 604 |
|
| 605 |
# Verify can change password using original password |
| 606 |
data = dict( |
| 607 |
password="new strong password\N{ROMAN NUMERAL ONE}", |
| 608 |
new_password="new strong password\N{ROMAN NUMERAL TWO}", |
| 609 |
new_password_confirm="new strong password\N{ROMAN NUMERAL TWO}", |
| 610 |
) |
| 611 |
response = client.post( |
| 612 |
"/change", |
| 613 |
json=data, |
| 614 |
headers={"Content-Type": "application/json"}, |
| 615 |
) |
| 616 |
assert response.status_code == 200 |
| 617 |
|
| 618 |
|
| 619 |
@pytest.mark.settings(password_normalize_form=None) |
| 620 |
def test_pwd_no_normalize(app, client): |
| 621 |
"""Verify that can log in with original but not normalized if have |
| 622 |
disabled normalization |
| 623 |
""" |
| 624 |
authenticate(client) |
| 625 |
|
| 626 |
data = dict( |
| 627 |
password="password", |
| 628 |
new_password="new strong password\N{ROMAN NUMERAL ONE}", |
| 629 |
new_password_confirm="new strong password\N{ROMAN NUMERAL ONE}", |
| 630 |
) |
| 631 |
response = client.post( |
| 632 |
"/change", |
| 633 |
json=data, |
| 634 |
headers={"Content-Type": "application/json"}, |
| 635 |
) |
| 636 |
assert response.status_code == 200 |
| 637 |
logout(client) |
| 638 |
|
| 639 |
# try with normalized password - should fail |
| 640 |
response = client.post( |
| 641 |
"/login", |
| 642 |
json=dict( |
| 643 |
email="[email protected]", |
| 644 |
password="new strong password\N{LATIN CAPITAL LETTER I}", |
| 645 |
), |
| 646 |
headers={"Content-Type": "application/json"}, |
| 647 |
) |
| 648 |
assert response.status_code == 400 |
| 649 |
|
| 650 |
# use original typed-in pwd |
| 651 |
response = client.post( |
| 652 |
"/login", |
| 653 |
json=dict( |
| 654 |
email="[email protected]", password="new strong password\N{ROMAN NUMERAL ONE}" |
| 655 |
), |
| 656 |
headers={"Content-Type": "application/json"}, |
| 657 |
) |
| 658 |
assert response.status_code == 200 |
| 659 |
|
| 660 |
# Verify can change password using original password |
| 661 |
data = dict( |
| 662 |
password="new strong password\N{ROMAN NUMERAL ONE}", |
| 663 |
new_password="new strong password\N{ROMAN NUMERAL TWO}", |
| 664 |
new_password_confirm="new strong password\N{ROMAN NUMERAL TWO}", |
| 665 |
) |
| 666 |
response = client.post( |
| 667 |
"/change", |
| 668 |
json=data, |
| 669 |
headers={"Content-Type": "application/json"}, |
| 670 |
) |
| 671 |
assert response.status_code == 200 |
| 672 |
|
| 673 |
|
| 674 |
@pytest.mark.csrf(ignore_unauth=True) |
| 675 |
@pytest.mark.settings(post_change_view="/post_change_view") |
| 676 |
def test_csrf(app, client): |
| 677 |
# enable CSRF, make sure template shows CSRF errors. |
| 678 |
authenticate(client) |
| 679 |
data = { |
| 680 |
"password": "password", |
| 681 |
"new_password": "new strong password", |
| 682 |
"new_password_confirm": "new strong password", |
| 683 |
} |
| 684 |
response = client.post("/change", data=data) |
| 685 |
assert b"The CSRF token is missing" in response.data |
| 686 |
# Note that we get a CSRF token EVEN for errors - this seems odd |
| 687 |
# but can't find anything that says its a security issue |
| 688 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 689 |
|
| 690 |
data["csrf_token"] = csrf_token |
| 691 |
response = client.post("/change", data=data) |
| 692 |
assert check_location(app, response.location, "/post_change_view") |
| 693 |
|
| 694 |
|
| 695 |
@pytest.mark.csrf(ignore_unauth=True, csrfprotect=True) |
| 696 |
def test_csrf_json(app, client): |
| 697 |
# This tests the handle_csrf code path - especially the JSON code path |
| 698 |
# that should return a JSON response! |
| 699 |
authenticate(client) |
| 700 |
data = { |
| 701 |
"password": "password", |
| 702 |
"new_password": "new strong password", |
| 703 |
"new_password_confirm": "new strong password", |
| 704 |
} |
| 705 |
response = client.post("/change", json=data) |
| 706 |
assert response.status_code == 400 |
| 707 |
assert response.json["response"]["errors"][0] == "The CSRF token is missing." |
| 708 |
|
| 709 |
# check form path also |
| 710 |
response = client.post("/change", data=data) |
| 711 |
assert response.status_code == 400 |
| 712 |
assert b"The CSRF token is missing." in response.data |
| 713 |
|
| 714 |
response = client.get("/change", content_type="application/json") |
| 715 |
csrf_token = response.json["response"]["csrf_token"] |
| 716 |
response = client.post("/change", json=data, headers={"X-CSRF-Token": csrf_token}) |
| 717 |
assert response.status_code == 200 |