def test_raises_erorrs(self, fake_SMTP, fake_log, fake_const): """``_send_email`` Raises NotifyError if sending email fails""" fake_const.QUOTA_EMAIL_SSL = False fake_mailer = MagicMock() fake_SMTP.return_value = fake_mailer fake_mailer.sendmail.return_value = {'*****@*****.**', (500, "no such user")} with self.assertRaises(notify.NotifyError): notify._send_email('*****@*****.**', 'asdfwed')
def test_cleartext(self, fake_SMTP, fake_log, fake_const): """``_send_email`` can send emails via clear text""" fake_const.QUOTA_EMAIL_SSL = False fake_mailer = MagicMock() fake_mailer.sendmail.return_value = None fake_SMTP.return_value = fake_mailer notify._send_email('*****@*****.**', 'some email') self.assertTrue(fake_mailer.sendmail.called)
def test_login_no_password(self, fake_SMTP, fake_log, fake_const): """``_send_email`` does not attempt to login when no password is defined""" fake_const.QUOTA_EMAIL_SSL = False fake_const.QUOTA_EMAIL_PASSWORD = '' fake_mailer = MagicMock() fake_mailer.sendmail.return_value = None fake_SMTP.return_value = fake_mailer notify._send_email('*****@*****.**', 'some email') self.assertFalse(fake_mailer.login.called)
def test_login(self, fake_SMTP, fake_log, fake_const): """``_send_email`` Attempts to login when username and password are defined""" fake_const.QUOTA_EMAIL_SSL = False fake_mailer = MagicMock() fake_mailer.sendmail.return_value = None fake_SMTP.return_value = fake_mailer notify._send_email('*****@*****.**', 'some email') # const.QUOTA_EMAIL_USERNAME and const.QUOTA_EMAIL_PASSWORD are implictly # true because of the mock/patch self.assertTrue(fake_mailer.login.called)
def test_always_closes(self, fake_SMTP, fake_log, fake_const): """``_send_email`` closes the server connection, even upon error""" fake_const.QUOTA_EMAIL_SSL = False fake_mailer = MagicMock() fake_SMTP.return_value = fake_mailer fake_mailer.sendmail.side_effect = [RuntimeError('testing')] try: notify._send_email('*****@*****.**', 'some email') except RuntimeError: pass self.assertTrue(fake_mailer.close.called)
def test_no_bcc(self, fake_SMTP, fake_log, fake_const): """``_send_email`` will not BCC an email if it is not defined""" fake_const.QUOTA_EMAIL_SSL = False fake_const.QUOTA_EMAIL_BCC = '' fake_mailer = MagicMock() fake_mailer.sendmail.return_value = None fake_SMTP.return_value = fake_mailer notify._send_email('*****@*****.**', 'some email') the_args, _ = fake_mailer.sendmail.call_args sent_to = the_args[1] expected = ['*****@*****.**'] self.assertEqual(sent_to, expected)
def test_bcc(self, fake_SMTP, fake_log, fake_const): """``_send_email`` will BCC an email if it's defined""" fake_const.QUOTA_EMAIL_SSL = False fake_const.QUOTA_EMAIL_BCC = '*****@*****.**' fake_mailer = MagicMock() fake_mailer.sendmail.return_value = None fake_SMTP.return_value = fake_mailer notify._send_email('*****@*****.**', 'some email') the_args, _ = fake_mailer.sendmail.call_args sent_to = the_args[1] expected = ['*****@*****.**', '*****@*****.**'] # set() avoids false positive due to ordering self.assertEqual(set(sent_to), set(expected))