Skip to content

Commit

Permalink
feat: implement facades to simplify the usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaspary committed May 18, 2023
1 parent 25c7ac4 commit ab588b1
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/facades.py
Original file line number Diff line number Diff line change
@@ -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,
)
)
90 changes: 90 additions & 0 deletions tests/test_facades.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from unittest import mock
from src.facades import send_email, send_email_with_gmail, send_email_with_outlook

ACCOUNT_MOCK = "[email protected]"
PASSWORD_MOCK = "password"
SUBJECT_MOCK = "Email Title"
BODY_MOCK = "Email Body"
SENDER_MOCK = "[email protected]"
TO_ADDRS_MOCK = ["[email protected]"]
CC_ADDRS_MOCK = ["[email protected]"]
BCC_ADDRS_MOCK = ["[email protected]"]
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)

0 comments on commit ab588b1

Please sign in to comment.