Skip to content

Commit

Permalink
Merge pull request #763 from Crown-Commercial-Service/handle-removed-…
Browse files Browse the repository at this point in the history
…email

Handle removed email race condition
  • Loading branch information
bjgill authored Apr 19, 2022
2 parents 023e006 + 4caa811 commit 4915b64
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dmutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .flask_init import init_app


__version__ = '60.7.1'
__version__ = '60.8.0'
6 changes: 4 additions & 2 deletions dmutils/email/dm_mailchimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,10 @@ def permanently_remove_email_from_list(self, email_address: str, list_id: str) -
)
return True
except (RequestException, MailChimpError) as e:
self.logger.error(
if get_response_from_exception(e).get('status') == 404:
self.logger.info(f"User ({hashed_email}) not found in list ({list_id})")
return True
self.logger.exception(
f"Mailchimp failed to permanently remove user ({hashed_email}) from list ({list_id})",
extra={"error": str(e), "mailchimp_response": get_response_from_exception(e)},
)
return False
24 changes: 24 additions & 0 deletions tests/email/test_dm_mailchimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,27 @@ def test_permanently_remove_email_from_list_failure(self, exception):
subscriber_hash="ee5ae5f54bdf3394d48ea4e79e6d0e39",
),
]

def test_permanently_remove_email_from_list_already_removed(self):
dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp'))
with mock.patch.object(
dm_mailchimp_client._client.lists.members,
'delete_permanent',
autospec=True,
) as del_perm:
del_perm.side_effect = MailChimpError({"status": 404})

with assert_external_service_log_entry(successful_call=False, extra_modules=['mailchimp'], count=1):
result = dm_mailchimp_client.permanently_remove_email_from_list(
"[email protected]",
"modern_society",
)

assert result is True

assert del_perm.call_args_list == [
mock.call(
list_id="modern_society",
subscriber_hash="ee5ae5f54bdf3394d48ea4e79e6d0e39",
),
]

0 comments on commit 4915b64

Please sign in to comment.