Fix unit test for is pending delete and remove all the print statements)

This commit is contained in:
Rebecca Hsieh 2025-03-24 17:47:49 -07:00
parent 273400c908
commit 210d5293d6
No known key found for this signature in database
3 changed files with 20 additions and 65 deletions

View file

@ -2888,7 +2888,6 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
Returns a tuple: (obj: DomainRequest, should_proceed: bool)
"""
# print("$$$$ IN THE _HANDLE_STATUS_CHANGE FUNCTION")
should_proceed = True
error_message = None
domain_name = original_obj.requested_domain.name
@ -2917,15 +2916,12 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
original_obj.status != models.DomainRequest.DomainRequestStatus.APPROVED
and obj.status == models.DomainRequest.DomainRequestStatus.APPROVED
and original_obj.requested_domain is not None
and Domain.objects.filter(name=original_obj.requested_domain.name).exists()
and Domain.is_pending_delete(domain_name)
):
# 1. If the domain request is not approved in previous state (original status)
# 2. If the new status that's supposed to be triggered IS approved
# 3. That it's a valid domain
# 4. That the domain name already exists
# 5. AND that the domain is currently in pendingDelete state
print("$$$$ in _handle_status_change")
# 4. AND that the domain is currently in pendingDelete state
error_message = FSMDomainRequestError.get_error_message(FSMErrorCodes.DOMAIN_IS_PENDING_DELETE)
elif (
original_obj.status != models.DomainRequest.DomainRequestStatus.APPROVED
@ -2961,8 +2957,6 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
logger.warning(f"An error encountered when trying to change status: {err}")
error_message = err.message
print(f"##### error_message is - {error_message}")
if error_message is not None:
# Clear the success message
messages.set_level(request, messages.ERROR)

View file

@ -256,9 +256,6 @@ class Domain(TimeStampedModel, DomainHelper):
req = commands.CheckDomain([domain_name])
response = registry.send(req, cleaned=True)
print("***** MODELS/DOMAIN.PY Response:", response)
print("***** MODELS/DOMAIN.PY Response res_data[0]:", response.res_data[0])
if response.res_data[0].avail:
return True
@ -798,11 +795,9 @@ class Domain(TimeStampedModel, DomainHelper):
"""
try:
statuses = self._get_property("statuses")
print(f"!!!!! Fetched statuses: {statuses}")
return statuses
# return self._get_property("statuses")
except KeyError:
print("!!!! Returns KeyError")
logger.error("Can't retrieve status from domain info")
return []
@ -2043,13 +2038,6 @@ class Domain(TimeStampedModel, DomainHelper):
"up_date": getattr(data, "up_date", ...),
}
# Have it return/raise this for checking for Deleted
# epplibwrapper/errors.py -> OBJECT_DOES_NOT_EXIST = 2303
# raise RegistryError(
# message=f"Domain '{domain_name}' no longer exists in the registry and is DELETED.",
# error_code=2303,
# )
### Original code
# data = data_response.res_data[0]

View file

@ -47,6 +47,7 @@ from unittest.mock import ANY, patch
from django.conf import settings
import boto3_mocking # type: ignore
import logging
from django.contrib.messages.storage.fallback import FallbackStorage
logger = logging.getLogger(__name__)
@ -2267,75 +2268,47 @@ 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."""
"""If in pendingDelete state from in review -> approve not allowed."""
# 1. Create two domain requests with the same name
to_be_pending_delete_state_domain_request = completed_domain_request(
# 1. Create domain
to_be_in_pending_deleted = 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_pending_deleted.creator = self.superuser
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. Put domain into in-review state
to_be_in_pending_deleted.status = DomainRequest.DomainRequestStatus.IN_REVIEW
to_be_in_pending_deleted.save()
# 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
# 3. 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/"
f"/admin/registrar/domainrequest/{to_be_in_pending_deleted.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
)
setattr(request, "_messages", FallbackStorage(request))
# 5. Use ExitStack for combine patching like above
with ExitStack() as stack:
print("$$$$$ Do we get into the with ExitStack statement")
# 4. Use ExitStack for combine patching like above
with boto3_mocking.clients.handler_for("sesv2", self.mock_client), ExitStack() as stack:
# 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)
with patch("registrar.models.domain.Domain.is_pending_delete", return_value=True):
# Attempt to approve to_be_in_pending_deleted
# (which should fail due is_pending_delete == True so nothing can get approved)
to_be_in_pending_deleted.status = DomainRequest.DomainRequestStatus.APPROVED
# Save the model with the patched request
self.admin.save_model(request, to_be_in_review_to_try_to_approve_domain_request, None, True)
self.admin.save_model(request, to_be_in_pending_deleted, 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."""