def test_multiple_recipients_same_message_id(self): # status.message_id collapses when it's the same for all recipients recipients = { "*****@*****.**": AnymailRecipientStatus("12345", "sent"), "*****@*****.**": AnymailRecipientStatus("12345", "queued"), } status = AnymailStatus() status.set_recipient_status(recipients) self.assertEqual(status.message_id, "12345") self.assertEqual( repr(status), "AnymailStatus<status={'queued', 'sent'}, message_id='12345', 2 recipients>" )
def test_multiple_recipients(self): recipients = { "*****@*****.**": AnymailRecipientStatus("12345", "sent"), "*****@*****.**": AnymailRecipientStatus("45678", "queued"), } status = AnymailStatus() status.set_recipient_status(recipients) self.assertEqual(status.status, {"queued", "sent"}) self.assertEqual(status.message_id, {"12345", "45678"}) self.assertEqual(status.recipients, recipients) self.assertEqual( repr(status), "AnymailStatus<status={'queued', 'sent'}, message_id={'12345', '45678'}, 2 recipients>" )
def test_single_recipient(self): recipients = { "*****@*****.**": AnymailRecipientStatus("12345", "sent"), } status = AnymailStatus() status.set_recipient_status(recipients) self.assertEqual(status.status, {"sent"}) self.assertEqual(status.message_id, "12345") self.assertEqual(status.recipients, recipients) self.assertEqual( repr(status), "AnymailStatus<status={'sent'}, message_id='12345', 1 recipients>") self.assertEqual(repr(status.recipients["*****@*****.**"]), "AnymailRecipientStatus('12345', 'sent')")
def post_to_esp(self, payload, message): # Keep track of the send params (for test-case access) self.recorded_send_params.append(payload.params) try: # Tests can supply their own message.test_response: response = message.test_response if isinstance(response, AnymailAPIError): raise response except AttributeError: # Default is to return 'sent' for each recipient status = AnymailRecipientStatus(message_id=1, status='sent') response = { 'recipient_status': {email: status for email in payload.recipient_emails} } return response
def test_rejected_recipients(self): """Post-send receiver even if AnymailRecipientsRefused is raised""" @receiver(post_send, weak=False) def handle_post_send(sender, message, status, esp_name, **kwargs): self.receiver_called = True self.addCleanup(post_send.disconnect, receiver=handle_post_send) self.message.anymail_test_response = { 'recipient_status': { '*****@*****.**': AnymailRecipientStatus(message_id=None, status='rejected') } } self.receiver_called = False with self.assertRaises(AnymailRecipientsRefused): self.message.send() self.assertTrue(self.receiver_called)
def parse_recipient_status(self, response, payload, message): return {'*****@*****.**': AnymailRecipientStatus('message-id', 'sent')}
def test_invalid_status(self): with self.assertRaisesMessage(ValueError, "Invalid status"): AnymailRecipientStatus("12345", "not-a-known-status")
def test_invalid_message_id(self): with self.assertRaisesMessage(TypeError, "Invalid message_id"): AnymailRecipientStatus(["id-list", "is-not-valid"], "queued")