132 lines
4.6 KiB
Python
132 lines
4.6 KiB
Python
"""IMAP folders are browsed; search stays inside the open folder."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import app.sources as sources
|
|
from tests.support import Actor, register
|
|
|
|
|
|
class FakeIMAP:
|
|
selects: list[str] = []
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
pass
|
|
|
|
def login(self, username, secret):
|
|
assert secret == "Postfach-Geheim"
|
|
return "OK", [b"logged"]
|
|
|
|
def list(self):
|
|
return "OK", [
|
|
b'(\\HasNoChildren) "/" "INBOX"',
|
|
b'(\\HasNoChildren) "/" "&AMQ-nderungen"',
|
|
b'(\\Noselect \\HasChildren) "/" "[Gmail]"',
|
|
b'(\\HasNoChildren) "/" "[Gmail]/Gesendet"',
|
|
]
|
|
|
|
def select(self, mailbox, readonly=False):
|
|
assert readonly is True
|
|
FakeIMAP.selects.append(mailbox)
|
|
return "OK", [b"5"]
|
|
|
|
def uid(self, command, *args):
|
|
if command == "SEARCH":
|
|
if any(isinstance(arg, str) and "Einwendung" in arg for arg in args):
|
|
return "OK", [b"2"]
|
|
return "OK", [b"1 2 3 4 5"]
|
|
uid = int(args[0])
|
|
header = (
|
|
f"From: Ada Beispiel <ada@example.test>\r\n"
|
|
f"Subject: Nachricht {uid}\r\n"
|
|
f"Date: Thu, 02 May 2024 08:00:00 +0000\r\n"
|
|
f"Message-ID: <{uid}@example.test>\r\n"
|
|
).encode()
|
|
return "OK", [(b"body", header), b")"]
|
|
|
|
def logout(self):
|
|
return "BYE", [b""]
|
|
|
|
|
|
def _account(monkeypatch, actor: Actor) -> str:
|
|
monkeypatch.setattr(sources.imaplib, "IMAP4_SSL", FakeIMAP)
|
|
created = actor.request(
|
|
"POST",
|
|
"/api/v1/connectors",
|
|
json={
|
|
"kind": "imap",
|
|
"name": "Kanzlei",
|
|
"host": "imap.example.test",
|
|
"port": 993,
|
|
"username": "ada@example.test",
|
|
"password": "Postfach-Geheim",
|
|
"security": "ssl",
|
|
},
|
|
status=201,
|
|
)
|
|
return created["id"]
|
|
|
|
|
|
def test_mailboxes_are_named_and_inbox_comes_first(client, db, monkeypatch):
|
|
ada = Actor(client, "ada-mail@example.test", "Ada Post")
|
|
register(db, ada.email, ada.name)
|
|
ada.login()
|
|
account = _account(monkeypatch, ada)
|
|
listed = ada.request("GET", "/api/v1/sources/imap/mailboxes", params={"connector_id": account})
|
|
assert [item["label"] for item in listed["items"]] == ["Posteingang", "[Gmail]", "[Gmail]/Gesendet", "Änderungen"]
|
|
gmail = next(item for item in listed["items"] if item["name"] == "[Gmail]")
|
|
sent = next(item for item in listed["items"] if item["name"] == "[Gmail]/Gesendet")
|
|
assert gmail["selectable"] is False
|
|
assert sent["selectable"] is True
|
|
assert sent["depth"] == 1
|
|
|
|
|
|
def test_folder_pages_newest_first_and_search_stays_inside(client, db, monkeypatch):
|
|
ada = Actor(client, "ada-page@example.test", "Ada Seite")
|
|
register(db, ada.email, ada.name)
|
|
ada.login()
|
|
account = _account(monkeypatch, ada)
|
|
monkeypatch.setattr(sources, "PAGE_SIZE", 2)
|
|
FakeIMAP.selects = []
|
|
first = ada.request(
|
|
"GET",
|
|
"/api/v1/sources/imap/messages",
|
|
params={"connector_id": account, "mailbox": "[Gmail]/Gesendet"},
|
|
)
|
|
assert first["mailbox"] == "[Gmail]/Gesendet"
|
|
assert [item["title"] for item in first["items"]] == ["Nachricht 5", "Nachricht 4"]
|
|
assert first["items"][0]["document_on"] == "2024-05-02"
|
|
assert first["items"][0]["message_from"] == "Ada Beispiel <ada@example.test>"
|
|
assert first["next_before"] == 4
|
|
older = ada.request(
|
|
"GET",
|
|
"/api/v1/sources/imap/messages",
|
|
params={"connector_id": account, "mailbox": "[Gmail]/Gesendet", "before": first["next_before"]},
|
|
)
|
|
assert [item["title"] for item in older["items"]] == ["Nachricht 3", "Nachricht 2"]
|
|
found = ada.request(
|
|
"GET",
|
|
"/api/v1/sources/imap/messages",
|
|
params={"connector_id": account, "mailbox": "INBOX", "q": "Einwendung"},
|
|
)
|
|
assert [item["title"] for item in found["items"]] == ["Nachricht 2"]
|
|
assert found["next_before"] is None
|
|
ada.request(
|
|
"GET",
|
|
"/api/v1/sources/imap/messages",
|
|
params={"connector_id": account, "mailbox": "Geheim"},
|
|
status=422,
|
|
)
|
|
assert "Geheim" not in FakeIMAP.selects
|
|
|
|
|
|
def test_foreign_mailbox_stays_hidden(client, db, monkeypatch):
|
|
ada = Actor(client, "ada-own@example.test", "Ada Eigen")
|
|
ben = Actor(client, "ben-own@example.test", "Ben Fremd")
|
|
register(db, ada.email, ada.name)
|
|
register(db, ben.email, ben.name)
|
|
ada.login()
|
|
account = _account(monkeypatch, ada)
|
|
ben.login()
|
|
ben.request("GET", "/api/v1/sources/imap/mailboxes", params={"connector_id": account}, status=404)
|
|
ben.request("GET", "/api/v1/sources/imap/messages", params={"connector_id": account}, status=404)
|