# SPDX-FileCopyrightText: 2026 Dennis Fink # # SPDX-License-Identifier: BSD-3-Clause import pytest from flask import Flask from werkzeug.datastructures import MultiDict from webmentions_ssg.forms.user import ChangePasswordForm, LoginForm @pytest.mark.parametrize( ("form_data", "invalid_field"), [ pytest.param({"password": "secret"}, "username", id="username-required"), pytest.param({"username": "admin"}, "password", id="password-required"), ], ) def test_login_form_requires_credentials( app: Flask, form_data: dict[str, str], invalid_field: str ) -> None: with app.test_request_context("/login", method="POST"): form = LoginForm(formdata=MultiDict(form_data), meta={"csrf": False}) assert not form.validate() assert "This field is required." in form.errors[invalid_field] def test_login_form_accepts_credentials(app: Flask) -> None: with app.test_request_context("/login", method="POST"): form = LoginForm( formdata=MultiDict({"username": "admin", "password": "secret"}), 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()