-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement facades to simplify the usage
- Loading branch information
Showing
2 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |