"""Tests for link extraction, link safety and the trash actions (fake IMAP server).""" from email.message import EmailMessage import pytest import app as unsub def _mail(html: str, subject: str = "Newsletter") -> bytes: msg = EmailMessage() msg["Subject"] = subject msg["From"] = "news@example.org" msg.set_content("plain") msg.add_alternative(html, subtype="html") return msg.as_bytes() def test_finds_anchor_and_plain_links_without_duplicates(): html = ( 'Abmelden Home' " https://ex.org/unsubscribe/2 https://ex.org/unsubscribe/2" ) assert unsub.find_unsubscribe_links(html) == ["https://ex.org/u?id=1", "https://ex.org/unsubscribe/2"] def test_escaped_ampersand_in_an_href_does_not_give_a_second_mangled_link(): html = ('Abmelden' ' Von allen abmelden') assert unsub.find_unsubscribe_links(html) == ["https://ex.org/unsubscribe?u=1&t=abc", "https://ex.org/unsubscribe-all?u=1"] def test_plain_text_link_keeps_its_place_behind_an_escaped_duplicate(): html = ('Abmelden' ' oder hier: https://ex.org/unsubscribe/2.') assert unsub.find_unsubscribe_links(html) == ["https://ex.org/unsubscribe?u=1&t=abc", "https://ex.org/unsubscribe/2"] @pytest.mark.parametrize("tail", [".", ",", ";", ":", "!", "?", ")", "”", "’", "»", ").", "!”"]) def test_punctuation_after_a_plain_text_link_is_not_part_of_it(tail): text = "Abmelden (siehe https://ex.org/unsubscribe/2" + tail + " danach)" assert unsub.find_unsubscribe_links(text) == ["https://ex.org/unsubscribe/2"] @pytest.mark.parametrize("href", ["javascript:alert(1)", "data:text/html,", "JavaScript:x"]) def test_dangerous_links_are_dropped(href): assert unsub.find_unsubscribe_links(f'Unsubscribe') == [] def test_encoded_subject_is_decoded(): msg = EmailMessage() msg["Subject"] = "Grüße vom Newsletter" parsed = unsub.email.message_from_bytes(msg.as_bytes()) assert unsub.get_subject(parsed) == "Grüße vom Newsletter" class FakeIMAP: def __init__(self, mails: dict[bytes, bytes]): self.mails = mails self.calls = [] def uid(self, command, *args): self.calls.append((command, args)) if command == "search": return "OK", [b" ".join(self.mails)] if command == "fetch": return "OK", [(b"1 (BODY[] {n}", self.mails[args[0]]), b")"] return "OK", [b""] def expunge(self): self.calls.append(("expunge", ())) def logout(self): pass def test_collect_links_keys_by_uid(): fake = FakeIMAP({b"41": _mail('unsubscribe'), b"42": _mail("
no links
")}) result = unsub.collect_links(fake) assert list(result) == ["41"] and result["41"]["links"] == ["https://ex.org/u"] def test_move_to_trash_uses_uids_and_ignores_garbage(monkeypatch): fake = FakeIMAP({}) monkeypatch.setattr(unsub, "connect", lambda: fake) assert unsub.move_emails_to_trash(["7", "1:*", "8 OR ALL"]) == 1 assert ("copy", ("7", "Trash")) in fake.calls assert ("store", ("7", "+FLAGS", "(\\Deleted)")) in fake.calls assert not any(c[0] in ("copy", "store") and c[1][0] != "7" for c in fake.calls) def test_move_all_form_posts_every_uid(monkeypatch): moved = [] monkeypatch.setattr(unsub, "move_emails_to_trash", lambda uids: moved.extend(uids) or len(uids)) client = unsub.app.test_client() assert client.post("/move-all-to-trash", data={"email_id": ["41", "42"]}).status_code == 302 assert moved == ["41", "42"] def test_result_page_renders_hidden_ids_for_move_all(monkeypatch): monkeypatch.setattr(unsub, "connect", lambda: FakeIMAP({b"41": _mail('Abmelden')})) html = unsub.app.test_client().get("/result").get_data(as_text=True) assert html.count('name="email_id" value="41"') == 2 # single form + move-all form assert 'rel="noopener noreferrer"' in html # --- link named "hier": the word that says "unsubscribe" stands next to the link, in the same sentence --- LIEFERANDO = ( '| Wir schicken Dir diese E-Mail, weil Du mit der E-Mail-Adresse '
'philipp@example.org unseren Newsletter abonniert hast. ' 'Wenn Du keinen Newsletter von uns mehr bekommen möchtest, kannst Du ihn ' 'hier abbestellen. ' 'Lieferando ist ein Angebot von Takeaway.com ' 'E-Mail online ansehen. |
Du kannst dich hier abmelden.
', 'Zum Abbestellen bitte antworten. Mehr dazu gibt es hier.
', 'Mehr dazu gibt es hier. Abbestellen geht per Antwort.
', # a line break ends the sentence 'Newsletter abbestellen ist einfach
hier zum Shop
', 'hier zum Shop
Abbestellen kannst du jederzeit, lies dazu unsere ausführlichen ' 'Hinweise zum Datenschutz.
', # the word belongs to another link (navigation row) or to a URL written out in the text '', 'https://ex.org/unsubscribe/1 oder Home
', # not a web link 'Zum Abbestellen hier schreiben.
', ]) def test_short_link_outside_the_unsubscribe_sentence_is_not_taken(html): assert "https://click.example/x" not in unsub.find_unsubscribe_links(html) assert all("mailto" not in link for link in unsub.find_unsubscribe_links(html)) def test_link_found_by_its_own_text_is_not_listed_twice_and_comes_first(): html = ('Du kannst dich hier abmelden oder ' 'Newsletter abbestellen.
') assert unsub.find_unsubscribe_links(html) == ["https://click.example/b", "https://click.example/a"]