aboutsummaryrefslogtreecommitdiff
path: root/tests/views/test_sent.py
blob: 644fc6f1be122fa79d4c7d05a2993feb5f85344f (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

import re
import uuid
from datetime import UTC, datetime, timedelta
from html import unescape
from http import HTTPStatus
from types import ModuleType
from unittest.mock import Mock
from urllib.parse import parse_qs, urlsplit

import pytest
import sqlalchemy as sa
from bs4 import BeautifulSoup, Tag
from flask import Flask
from flask.testing import FlaskClient

from webmentions_ssg import db
from webmentions_ssg.models.sources import (
    SentWebmention,
    SentWebmentionStatus,
    SentWebmentionStatusEvent,
    SourceDocument,
)
from webmentions_ssg.models.user import User

from .helpers import (
    create_outgoing,
    create_resendable_sent_webmention,
    create_sent_source,
    log_in,
    mock_admin_action_form,
)


def _href(tag: Tag) -> str:
    """Return an HTML link target, checking BeautifulSoup's attribute type."""
    href = tag.get("href")
    assert isinstance(href, str)
    return unescape(href)


@pytest.fixture
def sent_module(app: Flask) -> ModuleType:
    _ = app
    from webmentions_ssg.views import sent as module

    return module


@pytest.mark.parametrize("path", ["/sent", f"/sent/{uuid.uuid7()}"])
def test_sent_views_require_login(client: FlaskClient, path: str) -> None:
    response = client.get(path)

    assert response.status_code == HTTPStatus.FOUND
    assert "/login" in response.headers["Location"]


def test_sent_lists_source_documents(app: Flask, client: FlaskClient) -> None:
    log_in(app, client)
    identifier = create_sent_source(app)

    response = client.get("/sent")

    assert response.status_code == HTTPStatus.OK

    html = response.get_data(as_text=True)
    soup = BeautifulSoup(html, "html.parser")

    assert soup.h1 is not None
    assert soup.h1.get_text(strip=True) == "Sent Webmentions"
    assert "1 source document" in html
    assert soup.select_one('form[action="/sent"]') is not None
    assert "blog/example/index.html" in html
    assert "https://dennisfink.me/blog/example/" in html
    assert f"/sent/{identifier}" in html


def test_sent_source_documents_table_hides_secondary_columns_on_mobile(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    create_sent_source(app)

    response = client.get("/sent")

    assert response.status_code == HTTPStatus.OK
    html = BeautifulSoup(response.get_data(as_text=True), "html.parser")
    table = html.select_one(".table-responsive > table")
    assert table is not None
    headers = table.select("thead th")
    cells = table.select("tbody tr:first-child td")
    assert len(headers) == len(cells) == 8
    for index in (2, 4, 5, 6):  # URL, revision, created, last seen
        for cell in (headers[index], cells[index]):
            assert {"d-none", "d-md-table-cell"} <= set(
                cell.get_attribute_list("class")
            )
    for index in (0, 1, 3, 7):  # Status, path, counts, details stay visible
        assert "d-none" not in cells[index].get_attribute_list("class")
    assert "text-break" in cells[1].get_attribute_list("class")
    details = cells[7].select_one('a.btn[href^="/sent/"]')
    assert details is not None
    assert details.get_text(strip=True) == "Details"


def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None:
    log_in(app, client)
    identifier = create_sent_source(app)

    response = client.get(f"/sent/{identifier}")

    assert response.status_code == HTTPStatus.OK

    html = response.get_data(as_text=True)

    assert "blog/example/index.html" in html
    assert "https://dennisfink.me/blog/example/" in html
    assert "Sent Webmention Source" in html
    assert "Back to Sent Webmentions" in html

    first_target = "https://example.com/a"
    second_target = "https://example.com/b"

    assert first_target in html
    assert second_target in html
    assert html.index(first_target) < html.index(second_target)

    assert "Webmention endpoint returned HTTP 400" in html
    assert "https://example.com/webmention" in html


def test_sent_webmentions_table_keeps_status_and_actions_on_mobile(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    identifier = create_sent_source(app)

    response = client.get(f"/sent/{identifier}")

    assert response.status_code == HTTPStatus.OK
    html = BeautifulSoup(response.get_data(as_text=True), "html.parser")
    table = html.select_one(".table-responsive > table")
    assert table is not None
    headers = table.select("thead th")
    cells = table.select("tbody tr:first-child td")
    assert len(headers) == len(cells) == 7
    for index in (2, 3, 4, 5):  # Active, revisions, delivery, activity
        for cell in (headers[index], cells[index]):
            assert {"d-none", "d-md-table-cell"} <= set(
                cell.get_attribute_list("class")
            )
    for index in (0, 1, 6):  # Target, status, resend action stay visible
        assert "d-none" not in cells[index].get_attribute_list("class")
    assert cells[6].select_one("button[data-action]") is not None


def test_sent_source_returns_404_for_unknown_source(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    response = client.get(f"/sent/{uuid.uuid7()}")
    assert response.status_code == HTTPStatus.NOT_FOUND


def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None:
    response = client.post("/sent/rescan")

    assert response.status_code == HTTPStatus.FOUND
    assert "/login" in response.headers["Location"]


@pytest.mark.parametrize("valid", [False, True])
def test_rescan_sent_sources(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
    valid: bool,
) -> None:
    log_in(app, client)
    form = mock_admin_action_form(sent_module, monkeypatch, valid=valid)
    manual_scan_sources = Mock()
    monkeypatch.setattr(sent_module, "manual_scan_sources", manual_scan_sources)

    response = client.post("/sent/rescan")

    assert response.status_code == (
        HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST
    )
    form.validate_on_submit.assert_called_once_with()

    if valid:
        assert response.headers["Location"].endswith("/sent")
        manual_scan_sources.assert_called_once_with()
    else:
        manual_scan_sources.assert_not_called()


def test_resend_sent_webmention_requires_login(client: FlaskClient) -> None:
    response = client.post(f"/sent/{uuid.uuid7()}/{uuid.uuid7()}/resend")

    assert response.status_code == HTTPStatus.FOUND
    assert "/login" in response.headers["Location"]


def test_resend_sent_webmention_rejects_invalid_form(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    log_in(app, client)
    source_identifier, webmention_identifier = create_resendable_sent_webmention(app)
    mock_admin_action_form(sent_module, monkeypatch, valid=False)

    response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend")

    assert response.status_code == HTTPStatus.BAD_REQUEST


def test_resend_sent_webmention_returns_404_for_unknown_webmention(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    log_in(app, client)
    source_identifier = create_sent_source(app)
    mock_admin_action_form(sent_module, monkeypatch, valid=True)

    response = client.post(f"/sent/{source_identifier}/{uuid.uuid7()}/resend")

    assert response.status_code == HTTPStatus.NOT_FOUND


def test_resend_sent_webmention_rejects_wrong_source(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    log_in(app, client)
    _, webmention_identifier = create_resendable_sent_webmention(app)
    wrong_source_identifier = create_sent_source(app)
    mock_admin_action_form(sent_module, monkeypatch, valid=True)

    response = client.post(
        f"/sent/{wrong_source_identifier}/{webmention_identifier}/resend"
    )

    assert response.status_code == HTTPStatus.NOT_FOUND


def test_resend_sent_webmention_reopens_current_revision(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    log_in(app, client)
    source_identifier, webmention_identifier = create_resendable_sent_webmention(app)
    mock_admin_action_form(sent_module, monkeypatch, valid=True)
    send_webmention = Mock()
    monkeypatch.setattr(sent_module, "send_webmention", send_webmention)

    with app.app_context():
        webmention = db.session.get(SentWebmention, webmention_identifier)
        assert webmention is not None
        webmention.attempt_count = 6
        webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24)
        db.session.commit()

    response = client.post(f"/sent/{source_identifier}/{webmention_identifier}/resend")

    assert response.status_code == HTTPStatus.FOUND
    assert response.headers["Location"].endswith(f"/sent/{source_identifier}")

    with app.app_context():
        webmention = db.session.get(SentWebmention, webmention_identifier)
        assert webmention is not None
        assert webmention.desired_revision == 3
        assert webmention.processed_revision is None
        assert webmention.attempt_count == 0
        assert webmention.next_retry_at is None
        assert webmention.sent_revision == 2
        assert webmention.status is None
        assert webmention.failure_reason is None
        assert webmention.webmention_endpoint is None
        assert webmention.endpoint_response_status is None
        assert webmention.status_url is None
        events = db.session.scalars(
            sa.select(SentWebmentionStatusEvent).where(
                SentWebmentionStatusEvent.webmention_uuid == webmention_identifier
            )
        ).all()
        assert len(events) == 1
        assert events[0].status is None
        assert events[0].action == "Manually queued for resend"
        assert events[0].revision == 3

    send_webmention.assert_called_once_with(webmention_identifier)


@pytest.mark.parametrize(
    ("status_url", "safe"),
    [
        ("https://example.com/status", True),
        ("http://example.com/status", True),
        ("javascript:alert(1)", False),
        ("data:text/html,evil", False),
        ("ftp://example.com/status", False),
    ],
)
def test_admin_only_links_safe_existing_status_urls(
    app: Flask, client: FlaskClient, status_url: str, safe: bool
) -> None:
    source_identifier, _ = create_outgoing(app, status_url=status_url)
    with app.app_context():
        user = User(username="admin", password="test")
        db.session.add(user)
        db.session.commit()
        user_id = user.get_id()

    with client.session_transaction() as session:
        session["_user_id"] = str(user_id)
        session["_fresh"] = True

    response = client.get(f"/sent/{source_identifier}")
    assert response.status_code == HTTPStatus.OK
    html = response.get_data(as_text=True)
    assert (f'href="{status_url}"' in html) is safe
    assert status_url in html


def make_source(
    app: Flask,
    name: str,
    *,
    statuses: tuple[SentWebmentionStatus | None, ...] = (SentWebmentionStatus.SENT,),
    retrying: bool = False,
    processed: bool = True,
    deleted: bool = False,
    last_seen_at: datetime | None = None,
    revision: int = 1,
) -> uuid.UUID:
    """Create distinguishable sources with outgoing rows for list filtering."""
    now = last_seen_at or datetime(2026, 11, 12, 12, tzinfo=UTC)
    with app.app_context():
        source = SourceDocument(
            path=f"blog/{name}/index.html",
            url=f"https://source.example/{name}/",
            content_hash="a" * 32,
            revision=revision,
            last_seen_at=now,
            revised_at=now,
            deleted_at=now if deleted else None,
        )
        for number, status in enumerate(statuses):
            source.sent_webmentions.append(
                SentWebmention(
                    target=f"https://target.example/{name}/{number}",
                    linked=True,
                    desired_revision=revision,
                    processed_revision=revision if processed else None,
                    status=status,
                    attempt_count=2 if retrying else 0,
                    next_retry_at=now + timedelta(days=10) if retrying else None,
                )
            )
        db.session.add(source)
        db.session.commit()
        return source.uuid


@pytest.mark.parametrize(
    ("status", "expected"),
    [
        ("failing", {"failed", "retrying", "stale", "multiple"}),
        ("retrying", {"retrying"}),
        ("deleted", {"deleted"}),
    ],
)
def test_sent_filter_by_delivery_state_without_duplicate_rows(
    app: Flask, client: FlaskClient, status: str, expected: set[str]
) -> None:
    log_in(app, client)
    make_source(app, "failed", statuses=(SentWebmentionStatus.FAILED,))
    make_source(
        app,
        "retrying",
        statuses=(SentWebmentionStatus.FAILED,),
        retrying=True,
        processed=False,
    )
    make_source(app, "stale", statuses=(SentWebmentionStatus.FAILED,), retrying=True)
    make_source(
        app,
        "multiple",
        statuses=(SentWebmentionStatus.FAILED, SentWebmentionStatus.FAILED),
    )
    make_source(app, "deleted", deleted=True)
    make_source(app, "sent")
    make_source(app, "pending", statuses=(None,), processed=False)

    response = client.get("/sent", query_string={"status": status})
    assert response.status_code == HTTPStatus.OK
    soup = BeautifulSoup(response.get_data(as_text=True), "html.parser")
    paths = []
    for row in soup.select("tbody tr"):
        path = row.select_one("td:nth-child(2)")
        assert path is not None
        paths.append(path.get_text(strip=True))
    assert set(paths) == {f"blog/{name}/index.html" for name in expected}
    assert len(paths) == len(expected)
    assert (
        soup.select_one(f'select[name="status"] option[value="{status}"][selected]')
        is not None
    )


@pytest.mark.parametrize(
    ("q", "expected"),
    [
        ("blog/alpha", "alpha"),
        ("source.example/alpha", "alpha"),
        ("target.example/alpha", "alpha"),
        ("alpha/0", "alpha"),
        ("alpha%", None),
    ],
)
def test_sent_search_matches_path_source_url_and_sent_target(
    app: Flask, client: FlaskClient, q: str, expected: str | None
) -> None:
    log_in(app, client)
    make_source(app, "alpha")
    make_source(app, "beta")

    response = client.get("/sent", query_string={"q": f"  {q}  "})
    assert response.status_code == HTTPStatus.OK
    html = response.get_data(as_text=True)
    assert ("blog/alpha/index.html" in html) == (expected == "alpha")
    assert "blog/beta/index.html" not in html
    if expected is None:
        assert "No source documents match these filters." in html


def test_sent_filter_by_inclusive_last_seen_dates(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    make_source(app, "old", last_seen_at=datetime(2026, 11, 7, tzinfo=UTC))
    make_source(app, "included", last_seen_at=datetime(2026, 11, 10, 22, tzinfo=UTC))
    make_source(app, "new", last_seen_at=datetime(2026, 11, 15, tzinfo=UTC))
    response = client.get(
        "/sent", query_string={"date_from": "2026-11-08", "date_to": "2026-11-10"}
    )
    assert response.status_code == HTTPStatus.OK
    html = response.get_data(as_text=True)
    assert "blog/included/index.html" in html
    assert "blog/old/index.html" not in html
    assert "blog/new/index.html" not in html


@pytest.mark.parametrize(
    "sort", ["path", "url", "webmentions", "revision", "created", "last_seen", "status"]
)
def test_sent_sort_links_preserve_filters(
    app: Flask, client: FlaskClient, sort: str
) -> None:
    log_in(app, client)
    make_source(app, "alpha", statuses=(SentWebmentionStatus.FAILED,))
    response = client.get(
        "/sent",
        query_string={
            "status": "failing",
            "q": "alpha",
            "date_from": "2026-11-01",
            "sort": sort,
            "order": "asc",
            "per_page": 37,
        },
    )
    assert response.status_code == HTTPStatus.OK
    html = response.get_data(as_text=True)
    soup = BeautifulSoup(html, "html.parser")
    assert soup.select_one('th[aria-sort="ascending"]') is not None
    links = [
        parse_qs(urlsplit(_href(a)).query)
        for a in soup.select("th a[href]")
        if urlsplit(_href(a)).path == "/sent"
    ]
    assert len(links) == 7
    for link in links:
        assert link["status"] == ["failing"]
        assert link["q"] == ["alpha"]
        assert link["date_from"] == ["2026-11-01"]
        assert link["per_page"] == ["37"]
        assert link["sort"][0] in (
            "path",
            "url",
            "webmentions",
            "revision",
            "created",
            "last_seen",
            "status",
        )
    assert any(link["sort"] == [sort] and link["order"] == ["desc"] for link in links)


def test_sent_sort_orders_by_count_and_path(app: Flask, client: FlaskClient) -> None:
    log_in(app, client)
    make_source(app, "z", statuses=(SentWebmentionStatus.SENT,))
    make_source(
        app, "a", statuses=(SentWebmentionStatus.SENT, SentWebmentionStatus.FAILED)
    )
    for sort, order, expected_first in (
        ("webmentions", "desc", "a"),
        ("webmentions", "asc", "z"),
        ("path", "asc", "a"),
        ("path", "desc", "z"),
    ):
        response = client.get("/sent", query_string={"sort": sort, "order": order})
        assert response.status_code == HTTPStatus.OK
        soup = BeautifulSoup(response.get_data(as_text=True), "html.parser")
        path = soup.select_one("tbody tr td:nth-child(2)")
        assert path is not None
        assert path.get_text(strip=True) == f"blog/{expected_first}/index.html"


def test_sent_filter_pagination_and_navigation_preserve_parameters(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    for number in range(28):
        make_source(app, f"failed-{number:02}", statuses=(SentWebmentionStatus.FAILED,))
    filters = {
        "status": "failing",
        "q": "failed",
        "sort": "path",
        "order": "asc",
        "per_page": "10",
    }
    response = client.get("/sent", query_string={**filters, "page": "2"})
    assert response.status_code == HTTPStatus.OK
    html = response.get_data(as_text=True)
    assert "28 source documents" in html
    soup = BeautifulSoup(html, "html.parser")
    assert len(soup.select("tbody tr")) == 10
    urls = [_href(a) for a in soup.select("a[href]")]
    assert any(
        urlsplit(url).path == "/sent"
        and parse_qs(urlsplit(url).query)
        == {**{k: [v] for k, v in filters.items()}, "page": ["3"]}
        for url in urls
    )
    detail_urls = [
        url for url in urls if re.search(r"/sent/[0-9a-f-]{36}", urlsplit(url).path)
    ]
    assert detail_urls
    assert parse_qs(urlsplit(detail_urls[0]).query) == {
        **{k: [v] for k, v in filters.items()},
        "source_page": ["2"],
    }
    detail = client.get(detail_urls[0])
    assert detail.status_code == HTTPStatus.OK
    back = BeautifulSoup(detail.get_data(as_text=True), "html.parser").select_one(
        'a[href*="/sent?"]'
    )
    assert back is not None
    assert parse_qs(urlsplit(_href(back)).query) == {
        **{k: [v] for k, v in filters.items()},
        "page": ["2"],
    }


@pytest.mark.parametrize(
    "invalid",
    [
        "status=unknown",
        "sort=unsupported",
        "order=reverse",
        "per_page=0",
        "per_page=101",
        "per_page=abc",
        "date_from=2026-11-8",
        "date_to=bad",
        "date_from=2026-11-10&date_to=2026-11-01",
    ],
)
def test_sent_bad_filters_return_400(
    app: Flask, client: FlaskClient, invalid: str
) -> None:
    log_in(app, client)
    assert client.get(f"/sent?{invalid}").status_code == HTTPStatus.BAD_REQUEST


def test_rescan_sources_preserves_filter_query(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    log_in(app, client)
    mock_admin_action_form(sent_module, monkeypatch, valid=True)
    scan = Mock()
    monkeypatch.setattr(sent_module, "manual_scan_sources", scan)
    response = client.post(
        "/sent/rescan", query_string={"status": "retrying", "per_page": 37, "page": 2}
    )
    assert response.status_code == HTTPStatus.FOUND
    assert parse_qs(urlsplit(response.headers["Location"]).query) == {
        "status": ["retrying"],
        "per_page": ["37"],
        "page": ["2"],
    }
    scan.assert_called_once_with()


def test_sent_show_failed_and_retrying_counts_and_retry_deadline(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    identifier = make_source(
        app,
        "retry-badges",
        statuses=(SentWebmentionStatus.FAILED,),
        retrying=True,
        processed=False,
    )
    response = client.get("/sent", query_string={"status": "retrying"})
    assert response.status_code == HTTPStatus.OK
    html = response.get_data(as_text=True)
    assert "1 failed" in html
    assert "1 awaiting retry" in html

    response = client.get(f"/sent/{identifier}")
    assert response.status_code == HTTPStatus.OK
    assert "Retry scheduled after attempt 2:" in response.get_data(as_text=True)


def test_resend_preserves_source_filter_context(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    log_in(app, client)
    source_id, mention_id = create_resendable_sent_webmention(app)
    mock_admin_action_form(sent_module, monkeypatch, valid=True)
    send = Mock()
    monkeypatch.setattr(sent_module, "send_webmention", send)

    response = client.post(
        f"/sent/{source_id}/{mention_id}/resend",
        query_string={
            "status": "failing",
            "q": "resend",
            "sort": "path",
            "source_page": 2,
            "page": 3,
        },
    )
    assert response.status_code == HTTPStatus.FOUND
    assert urlsplit(response.headers["Location"]).path == f"/sent/{source_id}"
    assert parse_qs(urlsplit(response.headers["Location"]).query) == {
        "status": ["failing"],
        "q": ["resend"],
        "sort": ["path"],
        "source_page": ["2"],
        "page": ["3"],
    }
    send.assert_called_once_with(mention_id)


def test_sent_show_sql_statistics_with_zero_webmentions(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    make_source(
        app,
        "mixed-stats",
        statuses=(
            SentWebmentionStatus.SENT,
            SentWebmentionStatus.SENT,
            SentWebmentionStatus.FAILED,
            SentWebmentionStatus.NO_ENDPOINT,
        ),
    )
    make_source(app, "empty-stats", statuses=())

    response = client.get("/sent", query_string={"sort": "path", "order": "asc"})
    assert response.status_code == HTTPStatus.OK
    rows = BeautifulSoup(response.get_data(as_text=True), "html.parser").select(
        "tbody tr"
    )
    assert len(rows) == 2
    assert "0 Total" in rows[0].get_text(" ", strip=True)
    assert " sent" not in rows[0].get_text(" ", strip=True)
    mixed = rows[1].get_text(" ", strip=True)
    assert "4 Total" in mixed
    assert "2 sent" in mixed
    assert "1 failed" in mixed
    assert "awaiting retry" not in mixed


def test_sent_empty_filter_result_keeps_rescan_modal(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    response = client.get("/sent", query_string={"status": "failing"})
    assert response.status_code == HTTPStatus.OK
    html = response.get_data(as_text=True)
    assert "No source documents match these filters." in html
    assert 'id="confirmation-modal"' in html


def test_sent_list_and_source_use_confirmation_actions_and_detail_history(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    source_id, mention_id = create_resendable_sent_webmention(app)
    with app.app_context():
        mention = db.session.get(SentWebmention, mention_id)
        assert mention is not None
        mention.record_status_event("Delivery failed")
        db.session.commit()

    listing = BeautifulSoup(client.get("/sent").get_data(as_text=True), "html.parser")
    button = listing.select_one('button[data-action="/sent/failed/resend"]')
    assert button is not None
    assert button.get("data-bs-target") == "#confirmation-modal"
    details = listing.select_one('tbody a.btn[href^="/sent/"]')
    assert details is not None
    assert urlsplit(_href(details)).path == f"/sent/{source_id}"

    detail = BeautifulSoup(
        client.get(f"/sent/{source_id}").get_data(as_text=True), "html.parser"
    )
    assert detail.select_one("section.card #source-details-heading") is not None
    assert detail.select_one("section #sent-webmentions-heading") is not None
    assert detail.select_one("section #status-history-heading") is not None
    history = detail.select("#status-history-heading ~ ol li")
    assert len(history) == 1
    assert "Delivery failed" in history[0].get_text()
    assert "https://example.com/resend" in history[0].get_text()
    assert detail.select_one("section table tbody tr") is not None
    resend = detail.select_one('button[data-action*="resend-all"]')
    assert resend is not None
    assert resend.get_text(strip=True) == "Resend all"


@pytest.mark.parametrize(
    "path", ["/sent/failed/resend", f"/sent/{uuid.uuid7()}/resend-all"]
)
def test_bulk_resend_requires_login(client: FlaskClient, path: str) -> None:
    response = client.post(path)
    assert response.status_code == HTTPStatus.FOUND
    assert "/login" in response.headers["Location"]


@pytest.mark.parametrize("valid", [False, True])
def test_global_resend_all_failed_ignores_filters_and_preserves_other_statuses(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
    valid: bool,
) -> None:
    log_in(app, client)
    first = make_source(
        app, "first", statuses=(SentWebmentionStatus.FAILED, SentWebmentionStatus.SENT)
    )
    second = make_source(
        app,
        "second",
        statuses=(SentWebmentionStatus.FAILED,),
        retrying=True,
        processed=False,
    )
    make_source(app, "third", statuses=(SentWebmentionStatus.NO_ENDPOINT, None))
    mock_admin_action_form(sent_module, monkeypatch, valid=valid)
    send = Mock()
    monkeypatch.setattr(sent_module, "send_webmention", send)

    response = client.post(
        "/sent/failed/resend",
        query_string={"status": "deleted", "q": "no-match", "page": 2},
    )
    assert response.status_code == (
        HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST
    )
    if valid:
        assert parse_qs(urlsplit(response.headers["Location"]).query) == {
            "status": ["deleted"],
            "q": ["no-match"],
            "page": ["2"],
        }
    with app.app_context():
        all_mentions = db.session.scalars(sa.select(SentWebmention)).all()
        affected = [m for m in all_mentions if m.source_uuid in (first, second)]
        failed = [m for m in affected if m.target.endswith("/0")]
        untouched = [m for m in all_mentions if m not in failed]
        assert len(failed) == 2
        for mention in failed:
            assert (mention.status is None) is valid
            if valid:
                assert mention.processed_revision is None
                assert mention.attempt_count == 0
                assert mention.next_retry_at is None
        assert any(m.status == SentWebmentionStatus.SENT for m in untouched)
        assert any(m.status == SentWebmentionStatus.NO_ENDPOINT for m in untouched)
        events = db.session.scalars(sa.select(SentWebmentionStatusEvent)).all()
        assert len(events) == (2 if valid else 0)
        assert {event.webmention_uuid for event in events} == (
            {m.uuid for m in failed} if valid else set()
        )
    assert {call.args[0] for call in send.call_args_list} == (
        {m.uuid for m in failed} if valid else set()
    )
    assert send.call_count == (2 if valid else 0)


def test_source_resend_all_includes_successful_and_pending_but_only_this_source(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    log_in(app, client)
    source_id = make_source(
        app,
        "bulk",
        statuses=(
            SentWebmentionStatus.SENT,
            SentWebmentionStatus.FAILED,
            SentWebmentionStatus.NO_ENDPOINT,
            None,
        ),
    )
    other_source = make_source(app, "other", statuses=(SentWebmentionStatus.FAILED,))
    mock_admin_action_form(sent_module, monkeypatch, valid=True)
    send = Mock()
    monkeypatch.setattr(sent_module, "send_webmention", send)

    response = client.post(
        f"/sent/{source_id}/resend-all",
        query_string={
            "status": "failing",
            "source_page": 2,
            "page": 3,
            "history_page": 4,
        },
    )
    assert response.status_code == HTTPStatus.FOUND
    assert urlsplit(response.headers["Location"]).path == f"/sent/{source_id}"
    assert parse_qs(urlsplit(response.headers["Location"]).query) == {
        "status": ["failing"],
        "source_page": ["2"],
        "page": ["3"],
        "history_page": ["4"],
    }
    with app.app_context():
        mentions = db.session.scalars(
            sa.select(SentWebmention).where(SentWebmention.source_uuid == source_id)
        ).all()
        assert len(mentions) == 4
        assert all(m.status is None and m.processed_revision is None for m in mentions)
        assert all(m.attempt_count == 0 for m in mentions)
        assert all(m.sent_revision is None for m in mentions)
        other = db.session.scalar(
            sa.select(SentWebmention).where(SentWebmention.source_uuid == other_source)
        )
        assert other is not None and other.status == SentWebmentionStatus.FAILED
        history = db.session.scalars(sa.select(SentWebmentionStatusEvent)).all()
        assert {event.webmention_uuid for event in history} == {
            m.uuid for m in mentions
        }
    assert {call.args[0] for call in send.call_args_list} == {m.uuid for m in mentions}
    assert send.call_count == 4


def test_source_resend_all_rejects_unknown_source_and_invalid_form(
    app: Flask,
    client: FlaskClient,
    sent_module: ModuleType,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    log_in(app, client)
    source_id = make_source(app, "protected", statuses=(SentWebmentionStatus.FAILED,))
    form = mock_admin_action_form(sent_module, monkeypatch, valid=False)
    send = Mock()
    monkeypatch.setattr(sent_module, "send_webmention", send)
    assert (
        client.post(f"/sent/{source_id}/resend-all").status_code
        == HTTPStatus.BAD_REQUEST
    )
    form.validate_on_submit.assert_called_once_with()
    mock_admin_action_form(sent_module, monkeypatch, valid=True)
    assert (
        client.post(f"/sent/{uuid.uuid7()}/resend-all").status_code
        == HTTPStatus.NOT_FOUND
    )
    with app.app_context():
        mention = db.session.scalar(sa.select(SentWebmention))
        assert mention is not None and mention.status == SentWebmentionStatus.FAILED
    send.assert_not_called()


def test_sent_source_history_is_paginated_independently_of_webmentions(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    source_id, mention_id = create_resendable_sent_webmention(app)
    with app.app_context():
        mention = db.session.get(SentWebmention, mention_id)
        assert mention is not None
        for n in range(27):
            mention.record_status_event(f"Historical delivery {n}")
        db.session.commit()

    first_page = client.get(f"/sent/{source_id}")
    assert first_page.status_code == HTTPStatus.OK
    soup = BeautifulSoup(first_page.get_data(as_text=True), "html.parser")
    history = soup.select("#status-history-heading ~ ol li")
    assert len(history) == 25
    assert "Historical delivery 26" in history[0].get_text()
    assert "Historical delivery 0" not in first_page.get_data(as_text=True)
    links = [_href(link) for link in soup.select("#status-history-heading ~ div a")]
    assert any("history_page=2" in link for link in links)

    second_page = client.get(f"/sent/{source_id}?history_page=2")
    assert second_page.status_code == HTTPStatus.OK
    soup = BeautifulSoup(second_page.get_data(as_text=True), "html.parser")
    history = soup.select("#status-history-heading ~ ol li")
    assert len(history) == 2
    assert "Historical delivery 0" in history[-1].get_text()
    assert len(soup.select("section table tbody tr")) == 1


def test_sent_source_history_links_keep_webmentions_page(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    source_id, mention_id = create_resendable_sent_webmention(app)
    with app.app_context():
        source = db.session.get(SourceDocument, source_id)
        mention = db.session.get(SentWebmention, mention_id)
        assert source is not None and mention is not None
        source.sent_webmentions.extend(
            SentWebmention(
                target=f"https://example.org/extra/{number}",
                linked=True,
                desired_revision=3,
            )
            for number in range(25)
        )
        for number in range(27):
            mention.record_status_event(f"History event {number}")
        db.session.commit()

    response = client.get(f"/sent/{source_id}?page=2&source_page=3")
    assert response.status_code == HTTPStatus.OK
    soup = BeautifulSoup(response.get_data(as_text=True), "html.parser")
    history_links = [
        (_href(link), parse_qs(urlsplit(_href(link)).query))
        for link in soup.select("#status-history-heading ~ div a")
        if urlsplit(_href(link)).path == f"/sent/{source_id}"
    ]
    next_history = next(
        href
        for href, query in history_links
        if query.get("history_page") == ["2"]
    )
    query = parse_qs(urlsplit(next_history).query)
    assert query["page"] == ["2"]
    assert query["source_page"] == ["3"]

    following = client.get(next_history)
    assert following.status_code == HTTPStatus.OK
    next_soup = BeautifulSoup(following.get_data(as_text=True), "html.parser")
    assert len(next_soup.select("#status-history-heading ~ ol li")) == 2
    assert len(next_soup.select("section table tbody tr")) == 1


def test_sent_source_explains_current_failed_delivery(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    identifier = create_sent_source(app)

    response = client.get(f"/sent/{identifier}")

    assert response.status_code == HTTPStatus.OK
    soup = BeautifulSoup(response.get_data(as_text=True), "html.parser")
    assert "Webmention endpoint rejected the submission" in soup.get_text()
    assert "Check that your published source page links" in soup.get_text()
    assert "Suggested next step:" in soup.get_text()
    reason = soup.find("details")
    assert reason is not None
    assert "Webmention endpoint returned HTTP 400" in reason.get_text()
    # Keep the existing target table and its resend action.
    assert soup.select_one(".table-responsive table tbody tr button[data-action]")


def test_sent_source_explains_historical_failures_and_escapes_raw_reason(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    identifier = create_sent_source(app)
    raw_reason = "Unexpected transport failure <script>alert('x')</script>"
    with app.app_context():
        mention = db.session.scalar(
            sa.select(SentWebmention).where(SentWebmention.source_uuid == identifier)
        )
        assert mention is not None
        db.session.add_all(
            [
                SentWebmentionStatusEvent(
                    webmention_uuid=mention.uuid,
                    status=SentWebmentionStatus.FAILED,
                    action="Delivery failed; automatic retry scheduled",
                    revision=2,
                    attempt_count=1,
                    failure_reason="Webmention endpoint returned HTTP 503",
                    endpoint_response_status=503,
                    occurred_at=datetime(2026, 11, 12, tzinfo=UTC),
                ),
                SentWebmentionStatusEvent(
                    webmention_uuid=mention.uuid,
                    status=SentWebmentionStatus.FAILED,
                    action="Delivery failed",
                    revision=2,
                    attempt_count=2,
                    failure_reason=raw_reason,
                    occurred_at=datetime(2026, 11, 13, tzinfo=UTC),
                ),
            ]
        )
        db.session.commit()

    response = client.get(f"/sent/{identifier}")

    assert response.status_code == HTTPStatus.OK
    html = response.get_data(as_text=True)
    soup = BeautifulSoup(html, "html.parser")
    history = soup.select_one("#status-history-heading")
    assert history is not None
    assert "Webmention endpoint returned a server error" in soup.get_text()
    assert "Automatic retries run while attempts remain" in soup.get_text()
    assert raw_reason in soup.get_text()
    assert "Check the application logs for details before resending." in soup.get_text()
    assert "<script>" not in html
    assert "&lt;script&gt;" in html


def test_sent_source_explains_no_endpoint_without_suggesting_remote_edits(
    app: Flask, client: FlaskClient
) -> None:
    log_in(app, client)
    identifier = create_sent_source(app)
    with app.app_context():
        mention = db.session.scalar(
            sa.select(SentWebmention).where(SentWebmention.source_uuid == identifier)
        )
        assert mention is not None
        mention.status = SentWebmentionStatus.NO_ENDPOINT
        mention.failure_reason = "No Webmention endpoint discovered"
        db.session.commit()

    response = client.get(f"/sent/{identifier}")

    assert response.status_code == HTTPStatus.OK
    soup = BeautifulSoup(response.get_data(as_text=True), "html.parser")
    assert "does not advertise a usable Webmention endpoint" in soup.get_text()
    assert "No Webmention endpoint discovered" in soup.get_text()
    # The unrelated failed mention may have guidance; the no-endpoint row must not.
    no_endpoint_row = next(
        row
        for row in soup.select(".table-responsive table tbody tr")
        if "No Webmention endpoint discovered" in row.get_text()
    )
    assert "Suggested next step:" not in no_endpoint_row.get_text()