Speichere die neue Position einer verschobenen IMAP-Nachricht, damit der nächste Aufruf sie direkt öffnet.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
cd239a3ab8
commit
7a1206b18c
4 files changed
+299
-19
No files matched your search
@@ -285,3 +285,195 @@ def test_an_adopted_message_loads_in_full_with_its_attachment(client, db, monkey
|
||||
ada.request("GET", f"/api/v1/external-refs/{wrong['element']['id']}/mail", status=422)
|
||||
ben.login()
|
||||
ben.request("GET", f"/api/v1/external-refs/{created['id']}/mail", status=404)
|
||||
|
||||
|
||||
def _letter(message_id: str, body: str) -> bytes:
|
||||
return (
|
||||
f"From: Ada Beispiel <ada@example.test>\r\n"
|
||||
f"To: Gegenseite <gegen@example.test>\r\n"
|
||||
f"Subject: Verschoben\r\n"
|
||||
f"Date: Thu, 02 May 2024 08:00:00 +0000\r\n"
|
||||
f"Message-ID: {message_id}\r\n"
|
||||
f"MIME-Version: 1.0\r\n"
|
||||
f"Content-Type: text/plain; charset=utf-8\r\n"
|
||||
f"\r\n"
|
||||
f"{body}\r\n"
|
||||
).encode()
|
||||
|
||||
|
||||
class RelocatingIMAP:
|
||||
"""The saved UID holds another message. The Message-ID lives in Archiv."""
|
||||
|
||||
log: list[tuple] = []
|
||||
copies: dict[tuple[str, int], bytes] = {}
|
||||
header_uids: dict[str, bytes] | None = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.selected = ""
|
||||
|
||||
def login(self, username, secret):
|
||||
assert secret == "Postfach-Geheim"
|
||||
return "OK", [b"logged"]
|
||||
|
||||
def list(self):
|
||||
return "OK", [
|
||||
b'(\\HasNoChildren) "/" "INBOX"',
|
||||
b'(\\HasNoChildren) "/" "Archiv"',
|
||||
]
|
||||
|
||||
def select(self, mailbox, readonly=False):
|
||||
assert readonly is True
|
||||
self.selected = mailbox
|
||||
RelocatingIMAP.log.append(("select", mailbox))
|
||||
return "OK", [b"2"]
|
||||
|
||||
def uid(self, command, *args):
|
||||
if command == "SEARCH":
|
||||
if args and args[0] == "HEADER":
|
||||
RelocatingIMAP.log.append(("header", self.selected))
|
||||
override = (RelocatingIMAP.header_uids or {}).get(self.selected)
|
||||
if override is not None:
|
||||
return "OK", [override]
|
||||
found = [
|
||||
str(uid)
|
||||
for (folder, uid), raw in RelocatingIMAP.copies.items()
|
||||
if folder == self.selected and b"Message-ID: <moved@example.test>" in raw
|
||||
]
|
||||
return "OK", [" ".join(found).encode()]
|
||||
return "OK", [b""]
|
||||
uid = int(args[0])
|
||||
spec = " ".join(str(part) for part in args[1:])
|
||||
if "BODY.PEEK[]" in spec:
|
||||
RelocatingIMAP.log.append(("fetch", self.selected, uid))
|
||||
raw = RelocatingIMAP.copies.get((self.selected, uid))
|
||||
if raw is None:
|
||||
return "OK", [None]
|
||||
return "OK", [(b"rfc822", raw)]
|
||||
return "OK", [None]
|
||||
|
||||
def logout(self):
|
||||
return "BYE", [b""]
|
||||
|
||||
|
||||
def _stored_mail(db, element_id: str) -> dict:
|
||||
row = db.execute(
|
||||
"SELECT detail FROM external_references WHERE element_id = %s",
|
||||
(element_id,),
|
||||
).fetchone()
|
||||
return dict(row["detail"])
|
||||
|
||||
|
||||
def _linked_mail(actor: Actor, account: str, matter_id: str) -> dict:
|
||||
return actor.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Verschoben",
|
||||
"source": "imap",
|
||||
"external_id": "<moved@example.test>",
|
||||
"context": {"kind": "matter", "id": matter_id},
|
||||
"detail": {
|
||||
"connector_id": account,
|
||||
"mailbox": "INBOX",
|
||||
"uid": 2,
|
||||
"direction": "in",
|
||||
},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
|
||||
|
||||
def test_a_moved_message_remembers_its_new_folder(client, db, monkeypatch):
|
||||
from uuid import UUID
|
||||
|
||||
from app.retrieval import MailPayload, load_external
|
||||
|
||||
ada = Actor(client, "ada-move@example.test", "Ada Umzug")
|
||||
account_row = register(db, ada.email, ada.name)
|
||||
ada.login()
|
||||
RelocatingIMAP.log = []
|
||||
RelocatingIMAP.header_uids = None
|
||||
RelocatingIMAP.copies = {
|
||||
("INBOX", 2): _letter("<other@example.test>", "Eine andere Nachricht."),
|
||||
("Archiv", 9): _letter("<moved@example.test>", "Der Brief liegt jetzt im Archiv."),
|
||||
}
|
||||
account = _account(monkeypatch, ada)
|
||||
monkeypatch.setattr(sources.imaplib, "IMAP4_SSL", RelocatingIMAP)
|
||||
matter = matter_of(ada, "Ablage")
|
||||
created = _linked_mail(ada, account, matter["id"])
|
||||
|
||||
opened = ada.request("GET", f"/api/v1/external-refs/{created['id']}/mail")
|
||||
assert "Archiv" in opened["text"]
|
||||
stored = _stored_mail(db, created["id"])
|
||||
assert stored["mailbox"] == "Archiv"
|
||||
assert stored["uid"] == 9
|
||||
assert stored["direction"] == "in"
|
||||
assert stored["connector_id"] == account
|
||||
assert any(item[0] == "header" for item in RelocatingIMAP.log)
|
||||
|
||||
RelocatingIMAP.log = []
|
||||
again = ada.request("GET", f"/api/v1/external-refs/{created['id']}/mail")
|
||||
assert again["text"] == opened["text"]
|
||||
assert ("fetch", "Archiv", 9) in RelocatingIMAP.log
|
||||
assert not any(item[0] == "header" for item in RelocatingIMAP.log)
|
||||
assert _stored_mail(db, created["id"])["uid"] == 9
|
||||
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE external_references
|
||||
SET detail = jsonb_set(
|
||||
jsonb_set(detail, '{mailbox}', to_jsonb('INBOX'::text)),
|
||||
'{uid}',
|
||||
to_jsonb(2)
|
||||
)
|
||||
WHERE element_id = %s
|
||||
""",
|
||||
(created["id"],),
|
||||
)
|
||||
db.commit()
|
||||
RelocatingIMAP.log = []
|
||||
loaded = load_external(db, account_row["id"], UUID(created["id"]), None)
|
||||
assert isinstance(loaded, MailPayload)
|
||||
assert "Archiv" in loaded.parsed.text
|
||||
exported = _stored_mail(db, created["id"])
|
||||
assert exported["mailbox"] == "Archiv"
|
||||
assert exported["uid"] == 9
|
||||
assert exported["direction"] == "in"
|
||||
assert any(item[0] == "header" for item in RelocatingIMAP.log)
|
||||
|
||||
|
||||
def test_an_ambiguous_or_truncated_move_does_not_replace_the_stored_place(client, db, monkeypatch):
|
||||
ada = Actor(client, "ada-ambiguous@example.test", "Ada Zweifach")
|
||||
register(db, ada.email, ada.name)
|
||||
ada.login()
|
||||
RelocatingIMAP.header_uids = None
|
||||
RelocatingIMAP.copies = {
|
||||
("INBOX", 2): _letter("<other@example.test>", "Altbestand."),
|
||||
("INBOX", 4): _letter("<moved@example.test>", "Erste Kopie."),
|
||||
("Archiv", 9): _letter("<moved@example.test>", "Zweite Kopie."),
|
||||
}
|
||||
account = _account(monkeypatch, ada)
|
||||
monkeypatch.setattr(sources.imaplib, "IMAP4_SSL", RelocatingIMAP)
|
||||
matter = matter_of(ada, "Zweifach")
|
||||
created = _linked_mail(ada, account, matter["id"])
|
||||
ada.request("GET", f"/api/v1/external-refs/{created['id']}/mail", status=422)
|
||||
assert _stored_mail(db, created["id"])["mailbox"] == "INBOX"
|
||||
assert _stored_mail(db, created["id"])["uid"] == 2
|
||||
|
||||
RelocatingIMAP.copies = {
|
||||
("INBOX", 2): _letter("<other@example.test>", "Altbestand."),
|
||||
("INBOX", 11): _letter("<moved@example.test>", "Treffer in der abgebrochenen Suche."),
|
||||
("INBOX", 12): _letter("<a@example.test>", "a"),
|
||||
("INBOX", 13): _letter("<b@example.test>", "b"),
|
||||
("INBOX", 14): _letter("<c@example.test>", "c"),
|
||||
("INBOX", 15): _letter("<d@example.test>", "d"),
|
||||
("INBOX", 16): _letter("<e@example.test>", "e"),
|
||||
("Archiv", 9): _letter("<moved@example.test>", "Eigentlich auch hier."),
|
||||
}
|
||||
RelocatingIMAP.header_uids = {"INBOX": b"11 12 13 14 15 16"}
|
||||
RelocatingIMAP.log = []
|
||||
opened = ada.request("GET", f"/api/v1/external-refs/{created['id']}/mail")
|
||||
assert "abgebrochenen Suche" in opened["text"]
|
||||
assert not any(item == ("header", "Archiv") for item in RelocatingIMAP.log)
|
||||
assert _stored_mail(db, created["id"])["mailbox"] == "INBOX"
|
||||
assert _stored_mail(db, created["id"])["uid"] == 2
|
||||
Reference in new issue
Block a user