Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Black formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfir committed Apr 9, 2021
1 parent d25efba commit dc1121d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
30 changes: 15 additions & 15 deletions mail_error_notifs/mail_error_notifs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,39 @@ def send_email(subject, mailbody):
mailbody -- A string with the body of the email. Content type will be text/plain.
"""
try:
sg = SendGridAPIClient(os.getenv('SENDGRID_API_KEY'))
from_email = From(os.getenv('FROM'), 'Chronos Maybelambda')
to_emails = To(os.getenv('TO'))
sg = SendGridAPIClient(os.getenv("SENDGRID_API_KEY"))
from_email = From(os.getenv("FROM"), "Chronos Maybelambda")
to_emails = To(os.getenv("TO"))
subject = Subject(subject)
content = PlainTextContent(mailbody)
mail = Mail(from_email, to_emails, subject, content)

response = sg.send(mail)
print('SendGrid response status code:', response.status_code)
print('SendGrid response body:', response.body)
print("SendGrid response status code:", response.status_code)
print("SendGrid response body:", response.body)
except Exception as e:
logging.error(e)


if __name__ == '__main__':
load_dotenv('./.env')
if __name__ == "__main__":
load_dotenv("./.env")
logging.basicConfig(
filename=PurePath(os.getenv('LOGDIR')) / 'mail_error_notifs.log',
filename=PurePath(os.getenv("LOGDIR")) / "mail_error_notifs.log",
level=logging.ERROR,
format='%(asctime)s|%(levelname)s|%(message)s'
format="%(asctime)s|%(levelname)s|%(message)s",
)
errors = set()
msg = 'URL: {}.\nStatus code: {}.\nMessage: {}.'
req0 = request.Request(os.getenv('HV1'))
headers = {os.getenv('H1'): req0.full_url}
req1 = request.Request(url=os.getenv('URL1'), headers=headers)
msg = "URL: {}.\nStatus code: {}.\nMessage: {}."
req0 = request.Request(os.getenv("HV1"))
headers = {os.getenv("H1"): req0.full_url}
req1 = request.Request(url=os.getenv("URL1"), headers=headers)

for req in [req0, req1]:
try:
request.urlopen(req)
except HTTPError as e:
logging.error(e.geturl() + ': ' + str(e))
logging.error(e.geturl() + ": " + str(e))
errors.add(msg.format(req.full_url, e.code, e.read()))

if len(errors) > 0:
send_email('Notification of site errors', '\n\n'.join(errors))
send_email("Notification of site errors", "\n\n".join(errors))
27 changes: 15 additions & 12 deletions tests/test_mail_error_notifs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@


class TestScripts(unittest.TestCase):

def test_when_env_file_present_environment_variables_can_be_read(self):
load_dotenv('./.env')
self.assertIsNotNone(os.getenv('LOGDIR'))
load_dotenv("./.env")
self.assertIsNotNone(os.getenv("LOGDIR"))

@patch('sys.stdout', new_callable=io.StringIO)
def test_when_email_sent_then_statuscode_printed_is_the_one_returned_by_sendgridclient(self, mock_stdout):
@patch("sys.stdout", new_callable=io.StringIO)
def test_when_email_sent_then_statuscode_printed_is_the_one_returned_by_sendgridclient(
self, mock_stdout
):
SendGridAPIClient.send = Mock()
send_email('TestCase subject', 'TestCase body')
self.assertIn('SendGrid response status', mock_stdout.getvalue())
send_email("TestCase subject", "TestCase body")

self.assertIn("SendGrid response status", mock_stdout.getvalue())

def test_when_email_not_sent_then_error_is_logged(self):
err_msg = 'Send mail error'
err_msg = "Send mail error"

def mockinit(self, apikey):
raise RuntimeError(err_msg)
sendgridmock = patch.object(SendGridAPIClient, '__init__', mockinit)

sendgridmock = patch.object(SendGridAPIClient, "__init__", mockinit)

with sendgridmock, self.assertLogs() as cm:
send_email('TestCase subject', 'TestCase body')
self.assertIn(err_msg, ' '.join(cm.output))
send_email("TestCase subject", "TestCase body")

self.assertIn(err_msg, " ".join(cm.output))

0 comments on commit dc1121d

Please sign in to comment.