def test_messages_for_recipients(): """Tests that messages_for_recipients works as expected""" users = UserFactory.create_batch(5) messages = list( messages_for_recipients( [ ( recipient, context_for_user( user=user, extra_context={"url": "https://example.com"} ), ) for recipient, user in safe_format_recipients(users) ], "sample", ) ) assert len(messages) == len(users) for user, msg in zip(users, messages): assert user.email in str(msg.to[0]) assert msg.subject == "Welcome {}".format(user.name)
def test_send_message_failure(mocker): """Tests that send_messages logs all exceptions""" sendmail = mocker.patch("mail.api.AnymailMessage.send", side_effect=ConnectionError) patched_logger = mocker.patch("mail.api.log") users = UserFactory.create_batch(2) messages = list( messages_for_recipients( [ ( recipient, context_for_user( user=user, extra_context={"url": "https://example.com"} ), ) for recipient, user in safe_format_recipients(users) ], "sample", ) ) send_messages(messages) assert sendmail.call_count == len(users) assert patched_logger.exception.call_count == len(users)
def test_safe_format_recipients(): """Test that we get a list of emailable recipients""" user = UserFactory.create() user_no_email = UserFactory.create(email="") user_no_name = UserFactory.create(name="") assert safe_format_recipients([user, user_no_email, user_no_name]) == [ (formataddr((user.name, user.email)), user), (formataddr((None, user_no_name.email)), user_no_name), ]
def send_notification(self, email_notification): """ Sends the notification to the user Args: email_notification (EmailNotification): the notification to be sent """ messages = None user = email_notification.user with utils.mark_as_sent_or_canceled(email_notification) as will_send: # check against programmer error if user != self.user: raise Exception( "Notification user doesn't match settings user") # if we're trying to send an email, but the preference is never, we should just cancel it if self.notification_settings.is_triggered_never: raise CancelNotificationError() if not will_send: return last_notification = self._get_most_recent_notification( before=email_notification) data = self._get_notification_data(email_notification, last_notification) # generate the message (there's only 1) messages = list( api.messages_for_recipients( [(recipient, api.context_for_user(user=user, extra_context=data)) for recipient, user in api.safe_format_recipients([user]) ], self._template_name, )) if not messages: raise CancelNotificationError() # we don't want an error sending to cause a resend, because it could cause us to actually send it twice if messages: # if we got this far and have messages, send them api.send_messages(messages)
def test_send_message(mailoutbox): """Tests that send_messages works as expected""" users = UserFactory.create_batch(5) messages = list( messages_for_recipients( [( recipient, context_for_user(user=user, extra_context={"url": "https://example.com"}), ) for recipient, user in safe_format_recipients(users)], "sample", )) send_messages(messages) for message in mailoutbox: assert message in messages
def test_safe_format_recipients_override(user, settings): """Test that the recipient override works""" settings.MAILGUN_RECIPIENT_OVERRIDE = "admin@localhost" assert safe_format_recipients([user]) == [("admin@localhost", user)]