Fix test + some minor cleanup stuff

This commit is contained in:
zandercymatics 2024-10-10 09:06:10 -06:00
parent ec1df251b8
commit dbfc54837a
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 39 additions and 7 deletions

View file

@ -704,6 +704,7 @@ class DomainRequest(TimeStampedModel):
# We should never send an email if no reason was specified. # We should never send an email if no reason was specified.
# Additionally, Don't send out emails for reasons that shouldn't send them. # Additionally, Don't send out emails for reasons that shouldn't send them.
if status_info.get("reason") is None or status_info.get("reason") in status_info.get("excluded_reasons"): if status_info.get("reason") is None or status_info.get("reason") in status_info.get("excluded_reasons"):
logger.warning("send_custom_status_update_email() => Tried sending a status email without a reason.")
return return
# Only send out an email if the underlying reason itself changed or if no email was sent previously. # Only send out an email if the underlying reason itself changed or if no email was sent previously.
@ -1064,8 +1065,15 @@ class DomainRequest(TimeStampedModel):
def reject(self): def reject(self):
"""Reject an domain request that has been submitted. """Reject an domain request that has been submitted.
This action is logged.
This action cleans up the action needed status if moving away from action needed.
As side effects this will delete the domain and domain_information As side effects this will delete the domain and domain_information
(will cascade), and send an email notification using send_custom_status_update_email. (will cascade) when they exist.
Afterwards, we send out an email for reject in def save().
See the function send_custom_status_update_email.
""" """
if self.status == self.DomainRequestStatus.APPROVED: if self.status == self.DomainRequestStatus.APPROVED:

View file

@ -221,9 +221,9 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
</div> </div>
{% if original_object.action_needed_reason_email %} {% if original_object.action_needed_reason_email %}
<input id="last-sent-action-needed-email-content" class="display-none" value="{{original_object.action_needed_reason_email}}"> <input id="last-sent-action-needed-email-content" class="display-none" value="{{original_object.action_needed_reason_email}}">
{% else %} {% else %}
<input id="last-sent-action-needed-email-content" class="display-none" value="None"> <input id="last-sent-action-needed-email-content" class="display-none" value="None">
{% endif %} {% endif %}
{% elif field.field.name == "rejection_reason_email" %} {% elif field.field.name == "rejection_reason_email" %}
@ -310,9 +310,9 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
</div> </div>
{% if original_object.rejection_reason_email %} {% if original_object.rejection_reason_email %}
<input id="last-sent-rejection-email-content" class="display-none" value="{{original_object.rejection_reason_email}}"> <input id="last-sent-rejection-email-content" class="display-none" value="{{original_object.rejection_reason_email}}">
{% else %} {% else %}
<input id="last-sent-rejection-email-content" class="display-none" value="None"> <input id="last-sent-rejection-email-content" class="display-none" value="None">
{% endif %} {% endif %}
{% else %} {% else %}
{{ field.field }} {{ field.field }}

View file

@ -637,7 +637,6 @@ class TestDomainRequestAdmin(MockEppLib):
with less_console_noise(): with less_console_noise():
# Access the arguments passed to send_email # Access the arguments passed to send_email
call_args = self.mock_client.EMAILS_SENT call_args = self.mock_client.EMAILS_SENT
logger.info(f"what are the call args? {call_args}")
kwargs = call_args[email_index]["kwargs"] kwargs = call_args[email_index]["kwargs"]
# Retrieve the email details from the arguments # Retrieve the email details from the arguments

View file

@ -267,7 +267,6 @@ class TestDomainRequest(TestCase):
domain_request.submit() domain_request.submit()
self.assertEqual(domain_request.status, domain_request.DomainRequestStatus.SUBMITTED) self.assertEqual(domain_request.status, domain_request.DomainRequestStatus.SUBMITTED)
@less_console_noise_decorator
def check_email_sent( def check_email_sent(
self, domain_request, msg, action, expected_count, expected_content=None, expected_email="mayor@igorville.com" self, domain_request, msg, action, expected_count, expected_content=None, expected_email="mayor@igorville.com"
): ):
@ -278,6 +277,7 @@ class TestDomainRequest(TestCase):
# Perform the specified action # Perform the specified action
action_method = getattr(domain_request, action) action_method = getattr(domain_request, action)
action_method() action_method()
domain_request.save()
# Check if an email was sent # Check if an email was sent
sent_emails = [ sent_emails = [
@ -337,6 +337,31 @@ class TestDomainRequest(TestCase):
domain_request, msg, "withdraw", 1, expected_content="withdrawn", expected_email=user.email domain_request, msg, "withdraw", 1, expected_content="withdrawn", expected_email=user.email
) )
def test_reject_sends_email(self):
"Create a domain request and reject it and see if email was sent."
user, _ = User.objects.get_or_create(username="testy")
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.APPROVED, user=user)
expected_email=user.email
email_allowed, _ = AllowedEmail.objects.get_or_create(email=expected_email)
with boto3_mocking.clients.handler_for("sesv2", self.mock_client):
domain_request.reject()
domain_request.rejection_reason = domain_request.RejectionReasons.CONTACTS_NOT_VERIFIED
domain_request.rejection_reason_email = "test"
domain_request.save()
# Check if an email was sent
sent_emails = [
email
for email in MockSESClient.EMAILS_SENT
if expected_email in email["kwargs"]["Destination"]["ToAddresses"]
]
self.assertEqual(len(sent_emails), 1)
email_content = sent_emails[0]["kwargs"]["Content"]["Simple"]["Body"]["Text"]["Data"]
self.assertIn("test", email_content)
email_allowed.delete()
@less_console_noise_decorator @less_console_noise_decorator
def test_reject_with_prejudice_does_not_send_email(self): def test_reject_with_prejudice_does_not_send_email(self):
msg = "Create a domain request and reject it with prejudice and see if email was sent." msg = "Create a domain request and reject it with prejudice and see if email was sent."