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

Commit

Permalink
Add send_mail test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lfir committed Jan 15, 2021
1 parent 3c2032a commit 6203732
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test_mail_error_notifs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import io
import unittest
from unittest.mock import Mock, patch

from dotenv import load_dotenv

from mail_error_notifs import SendGridAPIClient, send_email


class TestScripts(unittest.TestCase):

@classmethod
def setUpClass(cls):
load_dotenv('./.env')

@patch('sys.stdout', new_callable=io.StringIO)
def test_when_email_sent_then_statuscode_printed_is_202(self, mock_stdout):
send_email('TestCase subject', 'TestCase body')
self.assertIn('status code: 202', mock_stdout.getvalue())

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

def mockinit(self, apikey):
raise RuntimeError(err_msg)

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))

0 comments on commit 6203732

Please sign in to comment.