Cleanup + (mostly) fix tests

This commit is contained in:
zandercymatics 2024-06-20 12:44:22 -06:00
parent 4c84ad8456
commit 3441a3974f
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
7 changed files with 41 additions and 12 deletions

View file

@ -213,7 +213,11 @@ urlpatterns = [
), ),
path("get-domains-json/", get_domains_json, name="get_domains_json"), path("get-domains-json/", get_domains_json, name="get_domains_json"),
path("get-domain-requests-json/", get_domain_requests_json, name="get_domain_requests_json"), path("get-domain-requests-json/", get_domain_requests_json, name="get_domain_requests_json"),
path("get-domain-requests-json/<int:pk>/action-needed-email/<str:reason>", get_action_needed_email, name="get_action_needed_email"), path(
"get-domain-requests-json/<int:pk>/action-needed-email/<str:reason>",
get_action_needed_email,
name="get_action_needed_email",
),
] ]
# Djangooidc strips out context data from that context, so we define a custom error # Djangooidc strips out context data from that context, so we define a custom error

View file

@ -557,7 +557,6 @@ class DomainRequest(TimeStampedModel):
blank=True, blank=True,
) )
def get_action_needed_reason_default_email_text(self, action_needed_reason: str): def get_action_needed_reason_default_email_text(self, action_needed_reason: str):
"""Returns the default email associated with the given action needed reason""" """Returns the default email associated with the given action needed reason"""
if action_needed_reason is None or action_needed_reason == self.ActionNeededReasons.OTHER: if action_needed_reason is None or action_needed_reason == self.ActionNeededReasons.OTHER:
@ -579,10 +578,9 @@ class DomainRequest(TimeStampedModel):
return { return {
"subject_text": subject_template.render(context=context), "subject_text": subject_template.render(context=context),
"email_body_text": template.render(context=context) "email_body_text": template.render(context=context),
} }
def sync_organization_type(self): def sync_organization_type(self):
""" """
Updates the organization_type (without saving) to match Updates the organization_type (without saving) to match

View file

@ -146,11 +146,12 @@ def format_phone(value):
return phone_number.as_national return phone_number.as_national
return value return value
@register.filter @register.filter
def strip_beginning_newline_and_spaces(value): def strip_beginning_newline_and_spaces(value):
"""Removes any newline characters (and spaces) """Removes any newline characters (and spaces)
on the first line of a given string""" on the first line of a given string"""
if value and isinstance(value, str): if value and isinstance(value, str):
return value.lstrip("\n").lstrip(" ") return value.lstrip("\n").lstrip(" ")
else: else:
return value return value

View file

@ -858,6 +858,7 @@ def completed_domain_request( # noqa
is_election_board=False, is_election_board=False,
organization_type=None, organization_type=None,
federal_agency=None, federal_agency=None,
action_needed_reason=None,
): ):
"""A completed domain request.""" """A completed domain request."""
if not user: if not user:
@ -921,6 +922,9 @@ def completed_domain_request( # noqa
if organization_type: if organization_type:
domain_request_kwargs["organization_type"] = organization_type domain_request_kwargs["organization_type"] = organization_type
if action_needed_reason:
domain_request_kwargs["action_needed_reason"] = action_needed_reason
domain_request, _ = DomainRequest.objects.get_or_create(**domain_request_kwargs) domain_request, _ = DomainRequest.objects.get_or_create(**domain_request_kwargs)

View file

@ -1595,6 +1595,29 @@ class TestDomainRequestAdmin(MockEppLib):
# Test Submitted Status Again from in ACTION_NEEDED, no new email should be sent # Test Submitted Status Again from in ACTION_NEEDED, no new email should be sent
self.transition_state_and_send_email(domain_request, DomainRequest.DomainRequestStatus.SUBMITTED) self.transition_state_and_send_email(domain_request, DomainRequest.DomainRequestStatus.SUBMITTED)
self.assertEqual(len(self.mock_client.EMAILS_SENT), 3) self.assertEqual(len(self.mock_client.EMAILS_SENT), 3)
@less_console_noise_decorator
def test_model_displays_action_needed_email(self):
"""Tests if the action needed email is visible for Domain Requests"""
_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.ACTION_NEEDED,
action_needed_reason=DomainRequest.ActionNeededReasons.BAD_NAME
)
p = "userpass"
self.client.login(username="staffuser", password=p)
response = self.client.get(
"/admin/registrar/domainrequest/{}/change/".format(_domain_request.pk),
follow=True,
)
self.assertContains(response, "DOMAIN NAME DOES NOT MEET .GOV REQUIREMENTS")
_domain_request.action_needed_reason = DomainRequest.ActionNeededReasons.OTHER
_domain_request.save()
self.assertContains(response, "No email will be sent")
@override_settings(IS_PRODUCTION=True) @override_settings(IS_PRODUCTION=True)
def test_save_model_sends_submitted_email_with_bcc_on_prod(self): def test_save_model_sends_submitted_email_with_bcc_on_prod(self):
@ -2290,6 +2313,7 @@ class TestDomainRequestAdmin(MockEppLib):
"status", "status",
"rejection_reason", "rejection_reason",
"action_needed_reason", "action_needed_reason",
"action_needed_reason_email",
"federal_agency", "federal_agency",
"portfolio", "portfolio",
"creator", "creator",

View file

@ -24,6 +24,7 @@ SAMPLE_KWARGS = {
"object_id": "3", "object_id": "3",
"domain": "whitehouse.gov", "domain": "whitehouse.gov",
"user_pk": "1", "user_pk": "1",
"reason": "bad_name",
} }
# Our test suite will ignore some namespaces. # Our test suite will ignore some namespaces.

View file

@ -104,17 +104,14 @@ def get_domain_requests_json(request):
) )
@login_required @login_required
def get_action_needed_email(request, pk, reason): def get_action_needed_email(request, pk, reason):
has_access = request.user.is_staff or request.user.is_superuser has_access = request.user.is_staff or request.user.is_superuser
# TODO also check the perm group # TODO also check the perm group
if not has_access: if not has_access:
raise PermissionDenied("You do not have permission to access this resource.") raise PermissionDenied("You do not have permission to access this resource.")
domain_request = DomainRequest.objects.filter(id=pk).first() domain_request = DomainRequest.objects.filter(id=pk).first()
reason_dict = domain_request.get_action_needed_reason_default_email_text(reason) reason_dict = domain_request.get_action_needed_reason_default_email_text(reason)
return JsonResponse( return JsonResponse(reason_dict)
reason_dict
)