aboutsummaryrefslogtreecommitdiff
path: root/tests/test_models.py
blob: 392b2463ae16df070073893db372997a1a43ac35 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

import uuid
from collections.abc import Callable
from datetime import UTC, datetime

import pytest

from webmentions_ssg.models import (
    ReceivedWebmention,
    SentWebmention,
    Source,
    User,
    uuid7_to_datetime,
)


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")


@pytest.mark.parametrize(
    ("status", "expected"),
    [
        ("received", False),
        ("verifying", False),
        ("verified", True),
        ("failed", False),
        ("deleted", False),
    ],
)
def test_received_webmention_verified(status: str, expected: bool) -> None:
    webmention = ReceivedWebmention(
        uuid=uuid.uuid7(),
        source="https://source.example/post",
        target="https://dennisfink.me/blog/example/",
        status=status,
    )

    assert webmention.verified is expected


@pytest.mark.parametrize(
    ("desired_revision", "processed_revision", "expected"),
    [(1, None, True), (1, 0, True), (2, 1, True), (1, 1, False), (1, 2, False)],
)
def test_sent_webmention_pending(
    desired_revision: int, processed_revision: int | None, expected: bool
) -> None:
    webmention = SentWebmention(
        target="https://example.com/post",
        desired_revision=desired_revision,
        processed_revision=processed_revision,
    )
    assert webmention.pending is expected


def test_uuid7_to_datetime() -> None:
    identifier = uuid.uuid7()
    timestamp = uuid7_to_datetime(identifier)

    assert timestamp.tzinfo is UTC
    assert timestamp.timestamp() == pytest.approx(identifier.time / 1000)


@pytest.mark.parametrize(
    "factory",
    [
        pytest.param(
            lambda identifier: ReceivedWebmention(
                uuid=identifier,
                source="https://source.example/post",
                target="https://dennisfink.me/blog/example/",
            ),
            id="received-webmention",
        ),
        pytest.param(
            lambda identifier: Source(
                uuid=identifier,
                path="example/index.html",
                url="https://dennisfink.me/blog/example/",
                content_hash="0" * 32,
                revision=1,
                last_seen_at=datetime.now(UTC),
                revised_at=datetime.now(UTC),
            ),
            id="source",
        ),
        pytest.param(
            lambda identifier: SentWebmention(
                uuid=identifier, target="https://example.com/post", desired_revision=1
            ),
            id="sent-webmention",
        ),
    ],
)
def test_created_at_comes_from_uuid7_timestamp(
    factory: Callable[[uuid.UUID], ReceivedWebmention | Source | SentWebmention],
) -> None:
    identifier = uuid.uuid7()
    model = factory(identifier)
    assert model.created_at == uuid7_to_datetime(identifier)


def test_received_webmention_repr() -> None:
    webmention = ReceivedWebmention(
        uuid=uuid.uuid7(),
        source="https://source.example/post",
        target="https://dennisfink.me/blog/example/",
    )

    assert repr(webmention) == (
        "<ReceivedWebmention 'https://source.example/post' -> "
        "'https://dennisfink.me/blog/example/'>"
    )


def test_source_repr() -> None:
    source = Source(
        path="example/index.html",
        url="https://dennisfink.me/blog/example/",
        content_hash="0" * 32,
        revision=1,
        last_seen_at=datetime.now(UTC),
        revised_at=datetime.now(UTC),
    )
    assert repr(source) == "<Source 'https://dennisfink.me/blog/example/'>"


def test_sent_webmention_repr() -> None:
    webmention = SentWebmention(target="https://example.com/post", desired_revision=1)
    assert repr(webmention) == "<SentWebmention 'https://example.com/post'>"


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