Adding in unit tests

This commit is contained in:
Rebecca Hsieh 2025-03-24 15:39:23 -07:00
parent 119d3cc8fd
commit 273400c908
No known key found for this signature in database
6 changed files with 131 additions and 64 deletions

View file

@ -2155,7 +2155,6 @@ class TestDomainRequestAdmin(MockEppLib):
Used to test errors when saving a change with an active domain, also used to test side effects
when saving a change goes through."""
with less_console_noise():
# Create an instance of the model
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.APPROVED)
@ -2268,6 +2267,75 @@ class TestDomainRequestAdmin(MockEppLib):
"Cannot approve. Requested domain is already in use.",
)
# @less_console_noise
def test_error_when_approving_domain_in_pending_delete_state(self):
"""Prevent approving a domain when another request with the same name is in pendingDelete."""
# 1. Create two domain requests with the same name
to_be_pending_delete_state_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.SUBMITTED, name="meoward1.gov"
)
print("#1 to_be_pending_delete_state_domain_request is", to_be_pending_delete_state_domain_request)
to_be_in_review_to_try_to_approve_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.SUBMITTED, name="meoward1.gov"
)
print(
"#1 to_be_in_review_to_try_to_approve_domain_request is", to_be_in_review_to_try_to_approve_domain_request
)
# 2. Approve to_be_pending_delete_state_domain_request
to_be_pending_delete_state_domain_request.status = DomainRequest.DomainRequestStatus.APPROVED
to_be_pending_delete_state_domain_request.save()
# # 2.5 And put it into pendingDelete state
# with patch("registrar.models.domain.Domain.is_pending_delete", return_value=True):
# to_be_pending_delete_state_domain_request.save()
# 3. Put to_be_in_review_to_try_to_approve_domain_request into in-review state
to_be_in_review_to_try_to_approve_domain_request.status = DomainRequest.DomainRequestStatus.IN_REVIEW
to_be_in_review_to_try_to_approve_domain_request.save()
# 4. Update request as a superuser
request = self.factory.post(
f"/admin/registrar/domainrequest/{to_be_in_review_to_try_to_approve_domain_request.pk}/change/"
)
request.user = self.superuser
request.session = {}
print("#4 to_be_pending_delete_state_domain_request is", to_be_pending_delete_state_domain_request)
print(
"#4 to_be_in_review_to_try_to_approve_domain_request is", to_be_in_review_to_try_to_approve_domain_request
)
# 5. Use ExitStack for combine patching like above
with ExitStack() as stack:
print("$$$$$ Do we get into the with ExitStack statement")
# Patch django.contrib.messages.error
stack.enter_context(patch.object(messages, "error"))
with patch("registrar.models.domain.Domain.is_pending_delete", return_value=True) as pending_patch:
# to_be_in_review_to_try_to_approve_domain_request.save()
print("$$$$$ inside the second with statement")
# Attempt to approve to_be_in_review_to_try_to_approve_domain_request
# (which should fail due to to_be_pending_delete_state_domain_request being in pendingDelete)
to_be_in_review_to_try_to_approve_domain_request.status = DomainRequest.DomainRequestStatus.APPROVED
print("to_be_in_review_to_try_to_approve_domain_request is ", to_be_in_review_to_try_to_approve_domain_request)
# Save the model with the patched request
self.admin.save_model(request, to_be_in_review_to_try_to_approve_domain_request, None, True)
print("$$$$ pending_patch.call_args_list is", pending_patch.call_args_list)
print("$$$$ pending_patch.called is", pending_patch.called)
# Assert that the correct error message is displayed
messages.error.assert_called_once_with(
request,
"Domain of same name is currently in pending delete state.",
)
@less_console_noise
def test_no_error_when_saving_to_approved_and_domain_exists(self):
"""The negative of the redundant admin check on model transition not allowed."""