feat(projekt-matching): Cfg dataclass, IMAP mailbox with keyword/trash handling, SMTP send
This commit is contained in:
53
projekt-matching/tests/test_mailer.py
Normal file
53
projekt-matching/tests/test_mailer.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import email
|
||||
from unittest import mock
|
||||
|
||||
from projektmatch import mailer
|
||||
|
||||
|
||||
def fake_conn():
|
||||
conn = mock.Mock()
|
||||
conn.uid.return_value = ("OK", [b"3 7"])
|
||||
conn.list.return_value = ("OK", [
|
||||
b'(\\HasNoChildren) "." "INBOX"',
|
||||
b'(\\HasNoChildren \\Trash) "." "Trash"'])
|
||||
return conn
|
||||
|
||||
|
||||
def test_unchecked_uids_search():
|
||||
box = mailer.MailBox("u", "p", conn=fake_conn())
|
||||
assert box.unchecked_uids() == ["3", "7"]
|
||||
box.conn.uid.assert_called_with("SEARCH", None,
|
||||
"UNKEYWORD $ProjektChecked")
|
||||
|
||||
|
||||
def test_flag_checked():
|
||||
box = mailer.MailBox("u", "p", conn=fake_conn())
|
||||
box.flag_checked("3")
|
||||
box.conn.uid.assert_called_with("STORE", "3", "+FLAGS",
|
||||
"($ProjektChecked)")
|
||||
|
||||
|
||||
def test_trash_folder_by_special_use():
|
||||
box = mailer.MailBox("u", "p", conn=fake_conn())
|
||||
assert box.trash_folder() == "Trash"
|
||||
|
||||
|
||||
def test_move_uses_move_then_fallback():
|
||||
conn = fake_conn()
|
||||
box = mailer.MailBox("u", "p", conn=conn)
|
||||
conn.uid.return_value = ("OK", [b""])
|
||||
box.move_to_trash("3")
|
||||
assert conn.uid.call_args_list[-1].args[:2] == ("MOVE", "3")
|
||||
conn.uid.side_effect = [("NO", [b""]), ("OK", [b""]), ("OK", [b""])]
|
||||
box.move_to_trash("4")
|
||||
assert conn.expunge.called
|
||||
|
||||
|
||||
def test_send_mail_starttls():
|
||||
with mock.patch("projektmatch.mailer.smtplib.SMTP") as smtp:
|
||||
server = smtp.return_value.__enter__.return_value
|
||||
mailer.send_mail("u@x", "pw", "to@x", "Subj", "Body")
|
||||
server.starttls.assert_called_once()
|
||||
server.login.assert_called_once_with("u@x", "pw")
|
||||
msg = server.send_message.call_args.args[0]
|
||||
assert msg["Subject"] == "Subj" and msg["To"] == "to@x"
|
||||
Reference in New Issue
Block a user