Add logic for if just the email is changed

This commit is contained in:
zandercymatics 2024-07-08 08:47:44 -06:00
parent 204b907a54
commit f18b10d669
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 28 additions and 6 deletions

View file

@ -598,6 +598,7 @@ class DomainRequest(TimeStampedModel):
def _cache_status_and_action_needed_reason(self):
"""Maintains a cache of properties so we can avoid a DB call"""
self._cached_action_needed_reason = self.action_needed_reason
self._cached_action_needed_reason_email = self.action_needed_reason_email
self._cached_status = self.status
def __init__(self, *args, **kwargs):
@ -614,7 +615,8 @@ class DomainRequest(TimeStampedModel):
# Handle the action needed email. We send one when moving to action_needed,
# but we don't send one when we are _already_ in the state and change the reason.
self.sync_action_needed_reason()
if self.status == self.DomainRequestStatus.ACTION_NEEDED and self.action_needed_reason:
self.sync_action_needed_reason()
# Update the cached values after saving
self._cache_status_and_action_needed_reason()
@ -624,7 +626,8 @@ class DomainRequest(TimeStampedModel):
was_already_action_needed = self._cached_status == self.DomainRequestStatus.ACTION_NEEDED
reason_exists = self._cached_action_needed_reason is not None and self.action_needed_reason is not None
reason_changed = self._cached_action_needed_reason != self.action_needed_reason
if was_already_action_needed and (reason_exists and reason_changed):
reason_email_changed = self._cached_action_needed_reason_email != self.action_needed_reason_email
if was_already_action_needed and (reason_exists and reason_changed or reason_email_changed):
# We don't send emails out in state "other"
if self.action_needed_reason != self.ActionNeededReasons.OTHER:
self._send_action_needed_reason_email(email_content=self.action_needed_reason_email)

View file

@ -1376,7 +1376,9 @@ class TestDomainRequestAdmin(MockEppLib):
self.assertContains(response, "status in [submitted,in review,action needed]", count=1)
@less_console_noise_decorator
def transition_state_and_send_email(self, domain_request, status, rejection_reason=None, action_needed_reason=None):
def transition_state_and_send_email(
self, domain_request, status, rejection_reason=None, action_needed_reason=None, action_needed_reason_email=None
):
"""Helper method for the email test cases."""
with boto3_mocking.clients.handler_for("sesv2", self.mock_client):
@ -1392,6 +1394,9 @@ class TestDomainRequestAdmin(MockEppLib):
if action_needed_reason:
domain_request.action_needed_reason = action_needed_reason
if action_needed_reason_email:
domain_request.action_needed_reason_email = action_needed_reason_email
# Use the model admin's save_model method
self.admin.save_model(request, domain_request, form=None, change=True)
@ -1481,13 +1486,27 @@ class TestDomainRequestAdmin(MockEppLib):
# Tests if an analyst can override existing email content
questionable_so = DomainRequest.ActionNeededReasons.QUESTIONABLE_SENIOR_OFFICIAL
domain_request.action_needed_reason_email = "custom email content"
domain_request.save()
self.transition_state_and_send_email(domain_request, action_needed, action_needed_reason=questionable_so)
self.transition_state_and_send_email(
domain_request,
action_needed,
action_needed_reason=questionable_so,
action_needed_reason_email="custom email content",
)
self.assert_email_is_accurate("custom email content", 4, EMAIL, bcc_email_address=BCC_EMAIL)
self.assertEqual(len(self.mock_client.EMAILS_SENT), 5)
# Tests if a new email gets sent when just the email is changed
self.transition_state_and_send_email(
domain_request,
action_needed,
action_needed_reason=questionable_so,
action_needed_reason_email="dummy email content",
)
self.assert_email_is_accurate("dummy email content", 5, EMAIL, bcc_email_address=BCC_EMAIL)
self.assertEqual(len(self.mock_client.EMAILS_SENT), 6)
def test_save_model_sends_submitted_email(self):
"""When transitioning to submitted from started or withdrawn on a domain request,
an email is sent out.