Internal: validate AnymailRecipientStatus at init

Catch invalid message_id and status when initializing
AnymailRecipientStatus, so problems with ESP response
are caught earlier (in individual backend
parse_recipient_status, rather than base backend _send).
This commit is contained in:
medmunds
2021-01-25 10:45:39 -08:00
committed by Mike Edmunds
parent 3e0056fa2f
commit fb5912f0e7
3 changed files with 18 additions and 0 deletions

View File

@@ -42,6 +42,8 @@ Other
* Move CI testing to GitHub Actions (and stop using Travis-CI). * Move CI testing to GitHub Actions (and stop using Travis-CI).
* Internal: catch invalid recipient status earlier in ESP response parsing
v8.1 v8.1

View File

@@ -85,6 +85,14 @@ class AnymailRecipientStatus:
"""Information about an EmailMessage's send status for a single recipient""" """Information about an EmailMessage's send status for a single recipient"""
def __init__(self, message_id, status): def __init__(self, message_id, status):
try:
# message_id must be something that can be put in a set
# (see AnymailStatus.set_recipient_status)
set([message_id])
except TypeError:
raise TypeError("Invalid message_id %r is not scalar type" % message_id)
if status is not None and status not in ANYMAIL_STATUSES:
raise ValueError("Invalid status %r" % status)
self.message_id = message_id # ESP message id self.message_id = message_id # ESP message id
self.status = status # one of ANYMAIL_STATUSES, or None for not yet sent to ESP self.status = status # one of ANYMAIL_STATUSES, or None for not yet sent to ESP

View File

@@ -76,3 +76,11 @@ class AnymailStatusTests(AnymailTestMixin, SimpleTestCase):
self.assertIsNone(status.status) self.assertIsNone(status.status)
self.assertIsNone(status.message_id) self.assertIsNone(status.message_id)
self.assertEqual(repr(status), "AnymailStatus<status=None>") self.assertEqual(repr(status), "AnymailStatus<status=None>")
def test_invalid_message_id(self):
with self.assertRaisesMessage(TypeError, "Invalid message_id"):
AnymailRecipientStatus(["id-list", "is-not-valid"], "queued")
def test_invalid_status(self):
with self.assertRaisesMessage(ValueError, "Invalid status"):
AnymailRecipientStatus("12345", "not-a-known-status")