Last active
January 9, 2018 18:25
-
-
Save kognate/ec3efec9b923b1714a05c3a2d151636c to your computer and use it in GitHub Desktop.
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
from unittest import TestCase | |
from unittest.mock import patch | |
from email.mime.text import MIMEText | |
import os | |
import smtplib | |
def send_email(email_from, email_to, email_subject, email_msg): | |
with smtplib.SMTP(os.getenv('SMTP_SERVER','localhost')) as smtpObj: | |
to_msg = MIMEText(email_msg) | |
to_msg['Subject'] = email_subject | |
to_msg['From'] = email_from | |
to_msg['To'] = email_to | |
smtpObj.sendmail(email_from, email_to.split(','), to_msg.as_string()) | |
class TestFunction(TestCase): | |
@patch.object(smtplib.SMTP, 'sendmail') | |
@patch.object(smtplib.SMTP, 'close') | |
def test_send_email(self, mock_close, mock_sendmail): | |
test_message = MIMEText('hello') | |
test_message['Subject'] = 'TEST' | |
test_message['From'] = '[email protected]' | |
test_message['To'] = '[email protected],[email protected]' | |
send_email(test_message['From'], | |
test_message['TO'], | |
test_message['Subject'], | |
'hello') | |
mock_close.assert_called() | |
mock_sendmail.assert_called_with(test_message['From'], | |
test_message['To'].split(','), | |
test_message.as_string()) | |
if __name__ == '__main__': | |
TestFunction() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment