diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/forms/test_user.py | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/tests/forms/test_user.py b/tests/forms/test_user.py index 4b35a9a..216b0dc 100644 --- a/tests/forms/test_user.py +++ b/tests/forms/test_user.py @@ -6,7 +6,7 @@ import pytest from flask import Flask from werkzeug.datastructures import MultiDict -from webmentions_ssg.forms.user import LoginForm +from webmentions_ssg.forms.user import ChangePasswordForm, LoginForm @pytest.mark.parametrize( @@ -33,3 +33,84 @@ def test_login_form_accepts_credentials(app: Flask) -> None: meta={"csrf": False}, ) assert form.validate() + + +@pytest.mark.parametrize( + ("password", "error"), + [ + pytest.param("short", "at least 8 characters", id="too-short"), + pytest.param("valid\x00password", "control characters", id="control-character"), + ], +) +def test_change_password_form_uses_shared_password_policy( + app: Flask, password: str, error: str +) -> None: + with app.test_request_context("/user", method="POST"): + form = ChangePasswordForm( + formdata=MultiDict( + { + "current_password": "old password", + "new_password": password, + "confirm_password": password, + } + ), + meta={"csrf": False}, + ) + + assert not form.validate() + assert any(error in message for message in form.errors["new_password"]) + + +def test_change_password_form_rejects_mismatched_confirmation(app: Flask) -> None: + with app.test_request_context("/user", method="POST"): + form = ChangePasswordForm( + formdata=MultiDict( + { + "current_password": "old password", + "new_password": "new password", + "confirm_password": "different password", + } + ), + meta={"csrf": False}, + ) + + assert not form.validate() + assert "Passwords must match." in form.errors["confirm_password"] + + +@pytest.mark.parametrize( + "missing_field", ["current_password", "new_password", "confirm_password"] +) +def test_change_password_form_requires_all_fields( + app: Flask, missing_field: str +) -> None: + form_data = { + "current_password": "old password", + "new_password": "new password", + "confirm_password": "new password", + } + del form_data[missing_field] + with app.test_request_context("/user", method="POST"): + form = ChangePasswordForm(formdata=MultiDict(form_data), meta={"csrf": False}) + + assert not form.validate() + assert "This field is required." in form.errors[missing_field] + + +@pytest.mark.parametrize("password", [" ", "pāssword🐈"]) +def test_change_password_form_accepts_valid_unicode_and_whitespace_passwords( + app: Flask, password: str +) -> None: + with app.test_request_context("/user", method="POST"): + form = ChangePasswordForm( + formdata=MultiDict( + { + "current_password": "old password", + "new_password": password, + "confirm_password": password, + } + ), + meta={"csrf": False}, + ) + + assert form.validate() |
