def test_sender_is_not_a_user(self, mock_mail): "If sender is not a User, send DEFAULT_FROM_EMAIL" notification = NotificationFactory() notification.send_mail() # we called send ... mock_mail.send.assert_called() call_kwargs = mock_mail.send.call_args[1] # ... with the proper email self.assertEqual(settings.DEFAULT_FROM_EMAIL, call_kwargs['sender'])
def test_ignore_mail_sending_error(self, mock_mail): "If sending throws an error, we log and continue." mock_mail.send.side_effect = Exception() notification = NotificationFactory() with patch('notification.models.logger') as mock_logger: notification.send_mail() mock_logger.exception.assert_called_with('Failed to send mail.') # recipients weren't marked as successful self.assertEqual(notification.sent_recipients, [])
def test_sender_is_user(self, mock_mail): "If sender is a User, send from their email address" sender = UserFactory() notification = NotificationFactory(sender=sender) notification.send_mail() # we called send ... mock_mail.send.assert_called() call_kwargs = mock_mail.send.call_args[1] # ... with the proper email self.assertEqual(sender.email, call_kwargs['sender'])
def test_template_data_is_str(self, mock_mail): "We accept string data for the template context." template_data = '{"foo": "bar"}' notification = NotificationFactory(template_data=template_data) notification.send_mail() # we called send ... mock_mail.send.assert_called() call_kwargs = mock_mail.send.call_args[1] # ... with the proper context self.assertEqual({'foo': 'bar'}, call_kwargs['context'])
def test_success(self, mock_mail): "On successful notification, sent_recipients should be populated." notification = NotificationFactory(template_data={'foo': 'bar'}) notification.send_mail() # we called send with all the proper args mock_mail.send.assert_called_with( recipients=notification.recipients, sender=settings.DEFAULT_FROM_EMAIL, template=notification.template_name, context=notification.template_data, ) # we marked the recipients as sent self.assertEqual(notification.recipients, notification.sent_recipients)