fix(mail): quote IMAP mailbox commands

This commit is contained in:
2026-07-20 20:06:36 +02:00
parent 2e906d9ddd
commit 764577dc23
2 changed files with 48 additions and 2 deletions

View File

@@ -1,14 +1,18 @@
from __future__ import annotations
import unittest
from unittest.mock import patch
from govoplan_mail.backend.config import ImapConfig
from govoplan_mail.backend.sending.imap import (
_detect_sent_folder,
_extract_mailbox_name,
_normalize_mailbox_page,
_paged_descending_sequences,
_parse_fetch_sequence,
_select_readonly,
_sequence_set,
append_message_to_sent,
)
@@ -69,5 +73,47 @@ class ImapMessagePaginationTests(unittest.TestCase):
self.assertEqual(_normalize_mailbox_page(folder=" Sent ", limit=500, offset=3), ("Sent", 100, 3))
class ImapMailboxCommandTests(unittest.TestCase):
def test_select_quotes_mailbox_name_with_spaces(self):
class Client:
untagged_responses = {"EXISTS": [b"0"], "UIDVALIDITY": [b"1"]}
def select(self, mailbox, readonly=False):
self.mailbox = mailbox
self.readonly = readonly
return "OK", [b"0"]
def response(self, code):
return "OK", [b"1"] if code == "UIDVALIDITY" else []
client = Client()
self.assertEqual(_select_readonly(client, "Gesendete Elemente"), (0, "1"))
self.assertEqual(client.mailbox, '"Gesendete Elemente"')
self.assertTrue(client.readonly)
def test_append_quotes_mailbox_name_with_spaces(self):
class Client:
def append(self, mailbox, flags, date_time, message):
self.mailbox = mailbox
self.flags = flags
self.date_time = date_time
self.message = message
return "OK", [b"APPEND completed"]
def logout(self):
return "BYE", [b"logged out"]
client = Client()
config = ImapConfig(host="imap.example.org", username="user", password="secret", sent_folder="auto")
with patch("govoplan_mail.backend.sending.imap._open_imap", return_value=client):
result = append_message_to_sent(b"Subject: test\r\n\r\nBody", imap_config=config, folder="Gesendete Elemente")
self.assertEqual(client.mailbox, '"Gesendete Elemente"')
self.assertEqual(result.folder, "Gesendete Elemente")
self.assertEqual(result.bytes_appended, len(b"Subject: test\r\n\r\nBody"))
if __name__ == "__main__":
unittest.main()