diff --git a/src/facades.py b/src/facades.py new file mode 100644 index 0000000..4b208f0 --- /dev/null +++ b/src/facades.py @@ -0,0 +1,73 @@ +from src.adapters import EmailMIMEAdapter +from src.services import EmailService +from src.clients import BaseEmailClient, GmailClient, OutlookClient + + +def send_email_with_gmail( + account: str, + password: str, + subject: str, + body: str, + to_addrs: list[str], + cc_addrs: list[str], + bcc_addrs: list[str], + attatchments: list[tuple[bytes, str]], + sender: str | tuple[str, str] = None, +): + return send_email( + GmailClient(account, password), + sender or account, + subject, + body, + to_addrs, + cc_addrs, + bcc_addrs, + attatchments, + ) + + +def send_email_with_outlook( + account: str, + password: str, + subject: str, + body: str, + to_addrs: list[str], + cc_addrs: list[str], + bcc_addrs: list[str], + attatchments: list[tuple[bytes, str]], + sender: str | tuple[str, str] = None, +): + return send_email( + OutlookClient(account, password), + sender or account, + subject, + body, + to_addrs, + cc_addrs, + bcc_addrs, + attatchments, + ) + + +def send_email( + client: BaseEmailClient, + sender: str | tuple[str, str], + subject: str, + body: str, + to_addrs: list[str], + cc_addrs: list[str], + bcc_addrs: list[str], + attatchments: list[tuple[bytes, str]], +): + service = EmailService(client) + return service.send_email( + EmailMIMEAdapter( + sender, + subject, + body, + to_addrs, + cc_addrs, + bcc_addrs, + attatchments, + ) + ) diff --git a/tests/test_facades.py b/tests/test_facades.py new file mode 100644 index 0000000..ae44a9b --- /dev/null +++ b/tests/test_facades.py @@ -0,0 +1,90 @@ +from unittest import mock +from src.facades import send_email, send_email_with_gmail, send_email_with_outlook + +ACCOUNT_MOCK = "account@email.com" +PASSWORD_MOCK = "password" +SUBJECT_MOCK = "Email Title" +BODY_MOCK = "Email Body" +SENDER_MOCK = "sender@email.com" +TO_ADDRS_MOCK = ["to_example@email.com"] +CC_ADDRS_MOCK = ["cc_example@email.com"] +BCC_ADDRS_MOCK = ["bcc_example@email.com"] +ATTATCHMENTS_MOCK = [(b"", "filename.txt")] + + +@mock.patch("src.facades.GmailClient") +@mock.patch("src.facades.send_email") +def test_send_email_with_gmail(send_email_mock, gmail_client_mock): + send_email_with_gmail( + account=ACCOUNT_MOCK, + password=PASSWORD_MOCK, + subject=SUBJECT_MOCK, + body=BODY_MOCK, + to_addrs=SENDER_MOCK, + cc_addrs=TO_ADDRS_MOCK, + bcc_addrs=CC_ADDRS_MOCK, + attatchments=BCC_ADDRS_MOCK, + sender=ATTATCHMENTS_MOCK, + ) + assert send_email_mock.called_once_with( + SUBJECT_MOCK, + BODY_MOCK, + SENDER_MOCK, + TO_ADDRS_MOCK, + CC_ADDRS_MOCK, + BCC_ADDRS_MOCK, + ATTATCHMENTS_MOCK + ) + assert gmail_client_mock.called_once_with(ACCOUNT_MOCK, PASSWORD_MOCK) + + +@mock.patch("src.facades.OutlookClient") +@mock.patch("src.facades.send_email") +def test_send_email_with_outlook(send_email_mock, outlook_client_mock): + send_email_with_outlook( + account=ACCOUNT_MOCK, + password=PASSWORD_MOCK, + subject=SUBJECT_MOCK, + body=BODY_MOCK, + to_addrs=SENDER_MOCK, + cc_addrs=TO_ADDRS_MOCK, + bcc_addrs=CC_ADDRS_MOCK, + attatchments=BCC_ADDRS_MOCK, + sender=ATTATCHMENTS_MOCK, + ) + assert send_email_mock.called_once( + SUBJECT_MOCK, + BODY_MOCK, + SENDER_MOCK, + TO_ADDRS_MOCK, + CC_ADDRS_MOCK, + BCC_ADDRS_MOCK, + ATTATCHMENTS_MOCK + ) + assert outlook_client_mock.called_once_with(ACCOUNT_MOCK, PASSWORD_MOCK) + + +@mock.patch("src.facades.EmailMIMEAdapter") +@mock.patch("src.facades.EmailService") +def test_send_email(email_mime_mock, email_service_mock): + client_obj = mock.Mock() + send_email( + client=client_obj, + subject=SUBJECT_MOCK, + body=BODY_MOCK, + to_addrs=SENDER_MOCK, + cc_addrs=TO_ADDRS_MOCK, + bcc_addrs=CC_ADDRS_MOCK, + attatchments=BCC_ADDRS_MOCK, + sender=ATTATCHMENTS_MOCK, + ) + assert email_mime_mock.called_once_with( + SUBJECT_MOCK, + BODY_MOCK, + SENDER_MOCK, + TO_ADDRS_MOCK, + CC_ADDRS_MOCK, + BCC_ADDRS_MOCK, + ATTATCHMENTS_MOCK + ) + assert email_service_mock.called_once_with(client_obj)