aboutsummaryrefslogtreecommitdiff
path: root/tests/models/test_user.py
blob: a3f95aeec51c29d54dfbfec0ed58afe9bdf864ee (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
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

import pytest

from webmentions_ssg.models.user import User


def test_user_repr() -> None:
    user = User(username="admin", password="secret")
    assert repr(user) == "<User admin>"


def test_user_password_is_write_only() -> None:
    user = User(username="admin", password="secret")
    with pytest.raises(AttributeError, match="Password is write-only"):
        _ = user.password


def test_user_password_is_hashed_and_can_be_checked() -> None:
    user = User(username="admin", password="secret")
    assert user.password_hash != "secret"
    assert user.check_password("secret")
    assert not user.check_password("wrong")


def test_user_session_id_changes_when_password_hash_changes() -> None:
    user = User(username="admin", password="a strong passphrase", id=42)
    original_hash = user.password_hash
    original_id = user.get_id()

    assert original_id.startswith("42:")
    assert original_hash not in original_id
    assert len(original_id.split(":", 1)[1]) == 64

    # Even a reset to the same password rehashes with a new salt, revoking
    # any old session token.
    user.password = "a strong passphrase"
    assert user.password_hash != original_hash
    assert user.get_id() != original_id