aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks/test_target_matching.py
blob: 31523af71ca8c59cef06bba134bfed1a6652e7d4 (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
165
166
167
168
169
170
171
172
173
174
175
176
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

from types import ModuleType
from unittest.mock import Mock, patch

import pytest
from flask import Flask

SOURCE_URL = "https://source.example/article"
TARGET_URL = "https://dennisfink.me/blog/example/"


@pytest.fixture
def receiver(app: Flask) -> ModuleType:
    _ = app
    from webmentions_ssg.tasks import target_matching

    return target_matching


@pytest.mark.parametrize(
    ("body", "expected"),
    [
        pytest.param(f'<a href="{TARGET_URL}">Reply</a>', True, id="a-href"),
        pytest.param(f'<area href="{TARGET_URL}" alt="Target">', True, id="area-href"),
        pytest.param(
            f'<link href="{TARGET_URL}" rel="alternate">', True, id="link-href"
        ),
        pytest.param(f'<img src="{TARGET_URL}" alt="">', True, id="img-src"),
        pytest.param(f'<audio src="{TARGET_URL}"></audio>', True, id="audio-src"),
        pytest.param(f'<video src="{TARGET_URL}"></video>', True, id="video-src"),
        pytest.param(
            f'<audio><source src="{TARGET_URL}"></audio>', True, id="audio-source-src"
        ),
        pytest.param(
            f'<video><source src="{TARGET_URL}"></video>', True, id="video-source-src"
        ),
        pytest.param(f'<iframe src="{TARGET_URL}"></iframe>', True, id="iframe-src"),
        pytest.param(f'<embed src="{TARGET_URL}">', True, id="embed-src"),
        pytest.param(f'<script src="{TARGET_URL}"></script>', True, id="script-src"),
        pytest.param(
            f'<video><track src="{TARGET_URL}"></video>', True, id="track-src"
        ),
        pytest.param(
            f'<input type="image" src="{TARGET_URL}" alt="">',
            True,
            id="image-input-src",
        ),
        pytest.param(
            f'<input type="IMAGE" src="{TARGET_URL}" alt="">',
            True,
            id="image-input-case-insensitive",
        ),
        pytest.param(
            f'<blockquote cite="{TARGET_URL}">Quotation</blockquote>',
            True,
            id="blockquote-cite",
        ),
        pytest.param(f'<q cite="{TARGET_URL}">Quotation</q>', True, id="q-cite"),
        pytest.param(f'<ins cite="{TARGET_URL}">Addition</ins>', True, id="ins-cite"),
        pytest.param(f'<del cite="{TARGET_URL}">Removal</del>', True, id="del-cite"),
        pytest.param(
            '<base href="https://dennisfink.me/blog/"><a href="example/">Reply</a>',
            True,
            id="base-url",
        ),
        pytest.param(f'<img cite="{TARGET_URL}" alt="">', False, id="img-cite-invalid"),
        pytest.param(
            f'<blockquote src="{TARGET_URL}">Quote</blockquote>',
            False,
            id="blockquote-src-invalid",
        ),
        pytest.param(f'<a src="{TARGET_URL}">Reply</a>', False, id="a-src-invalid"),
        pytest.param(f'<div href="{TARGET_URL}"></div>', False, id="div-href-invalid"),
        pytest.param(f'<link src="{TARGET_URL}">', False, id="link-src-invalid"),
        pytest.param(
            f'<input type="text" src="{TARGET_URL}">',
            False,
            id="text-input-src-invalid",
        ),
        pytest.param(
            f'<picture><source src="{TARGET_URL}"></picture>',
            False,
            id="picture-source-src-invalid",
        ),
        pytest.param(f'<base href="{TARGET_URL}">', False, id="base-is-not-mention"),
        pytest.param(
            f'<a href="{TARGET_URL}more">Different page</a>', False, id="longer-url"
        ),
        pytest.param(
            f'<img cite="{TARGET_URL}" src="https://example.com/image.jpg" alt="">',
            False,
            id="invalid-cite-does-not-override-valid-src",
        ),
        pytest.param(f"<p>{TARGET_URL}</p>", False, id="text-content"),
        pytest.param(
            '<a href="https://example.com/">Other site</a>', False, id="missing-target"
        ),
    ],
)
def test_html_mentions_target(receiver: ModuleType, body: str, expected: bool) -> None:
    assert (
        receiver.html_mentions_target(body.encode(), SOURCE_URL, TARGET_URL) is expected
    )


def test_html_mentions_relative_target(receiver: ModuleType) -> None:
    assert receiver.html_mentions_target(
        b'<a href="../target/">Reply</a>',
        "https://source.example/posts/article/",
        "https://source.example/posts/target/",
    )


@pytest.mark.parametrize(
    ("body", "target_url", "expected"),
    [
        (TARGET_URL, TARGET_URL, True),
        (f"This post replies to {TARGET_URL}", TARGET_URL, True),
        (
            f"https://example.com/first {TARGET_URL} https://example.com/last",
            TARGET_URL,
            True,
        ),
        pytest.param(
            f"See {TARGET_URL}, thanks", TARGET_URL, True, id="trailing-comma"
        ),
        pytest.param(f"({TARGET_URL})", TARGET_URL, True, id="parenthesized-url"),
        pytest.param(
            f"See ({TARGET_URL}), thanks!", TARGET_URL, True, id="mixed-punctuation"
        ),
        pytest.param(f"See {TARGET_URL}. Next", TARGET_URL, True, id="trailing-period"),
        pytest.param(
            "https://example.com/wiki/Foo_(bar)",
            "https://example.com/wiki/Foo_(bar)",
            True,
            id="balanced-parentheses-in-url",
        ),
        pytest.param(
            "https://example.com/wiki/Foo_(bar)",
            "https://example.com/wiki/Foo_(bar",
            False,
            id="balanced-parentheses-not-trimmed",
        ),
        (f"{TARGET_URL}more", TARGET_URL, False),
        pytest.param(
            f"{TARGET_URL},another-page",
            TARGET_URL,
            False,
            id="comma-within-longer-url",
        ),
        ("https://dennisfink.me/blog/other/", TARGET_URL, False),
        ("/blog/example/", TARGET_URL, False),
        ("There are no links here.", TARGET_URL, False),
    ],
)
def test_text_mentions_target(
    receiver: ModuleType, body: str, target_url: str, expected: bool
) -> None:
    assert receiver.text_mentions_target(body, target_url) is expected


@patch("webmentions_ssg.tasks.target_matching.BeautifulSoup")
def test_html_mentions_target_ignores_non_string_attribute(
    beautiful_soup: Mock, receiver: ModuleType
) -> None:
    document = beautiful_soup.return_value
    document.select_one.return_value = None

    element = Mock()
    element.get.return_value = ["not", "a", "string"]
    document.select.return_value = [element]

    assert not receiver.html_mentions_target(b"<html></html>", SOURCE_URL, TARGET_URL)