aboutsummaryrefslogtreecommitdiff
path: root/tests/forms/test_user.py
blob: 216b0dce69c2c3b139d8344686316c8eac203b5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# 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()