diff --git a/src/registrar/models/domain_application.py b/src/registrar/models/domain_application.py index db962fda0..14244311d 100644 --- a/src/registrar/models/domain_application.py +++ b/src/registrar/models/domain_application.py @@ -614,8 +614,8 @@ class DomainApplication(TimeStampedModel): def reject(self): """Reject an application that has been submitted. - As a side effect this will delete the domain and domain_information - (will cascade), and send an email notification""" + As side effects this will delete the domain and domain_information + (will cascade), and send an email notification.""" if self.status == self.APPROVED: self.approved_domain.delete_request() @@ -641,7 +641,7 @@ class DomainApplication(TimeStampedModel): any existing domains/applications and from submitting new aplications. We do this by setting an ineligible status on the user, which the permissions classes test against. This will also delete the domain - and domain_information (will cascade)""" + and domain_information (will cascade) when they exist.""" if self.status == self.APPROVED: self.approved_domain.delete_request() diff --git a/src/registrar/tests/test_admin.py b/src/registrar/tests/test_admin.py index 9cde6d5cf..2b347287d 100644 --- a/src/registrar/tests/test_admin.py +++ b/src/registrar/tests/test_admin.py @@ -1,6 +1,5 @@ from django.test import TestCase, RequestFactory, Client from django.contrib.admin.sites import AdminSite -from unittest.mock import patch from contextlib import ExitStack from django.contrib import messages @@ -436,7 +435,7 @@ class TestDomainApplicationAdmin(TestCase): request, "Cannot edit an application with a restricted creator.", ) - + def test_error_when_saving_approved_to_rejected_and_domain_is_active(self): # Create an instance of the model application = completed_application(status=DomainApplication.APPROVED) @@ -449,33 +448,34 @@ class TestDomainApplicationAdmin(TestCase): "/admin/registrar/domainapplication/{}/change/".format(application.pk) ) request.user = self.superuser - + # Define a custom implementation for is_active def custom_is_active(self): return True # Override to return True - - # Use ExitStack to combine patch contexts + + # Use ExitStack to combine patch contexts with ExitStack() as stack: # Patch Domain.is_active and django.contrib.messages.error simultaneously - stack.enter_context(patch.object(Domain, 'is_active', custom_is_active)) - stack.enter_context(patch.object(messages, 'error')) - + stack.enter_context(patch.object(Domain, "is_active", custom_is_active)) + stack.enter_context(patch.object(messages, "error")) + # Simulate saving the model - application.status = DomainApplication.REJECTED + application.status = DomainApplication.REJECTED self.admin.save_model(request, application, None, True) # Assert that the error message was called with the correct argument messages.error.assert_called_once_with( request, - "This action is not permitted, the domain " - + "is already active.", + "This action is not permitted, the domain " + "is already active.", ) - + def test_side_effects_when_saving_approved_to_rejected(self): # Create an instance of the model application = completed_application(status=DomainApplication.APPROVED) domain = Domain.objects.create(name=application.requested_domain.name) - domain_information = DomainInformation.objects.create(creator=self.superuser, domain=domain) + domain_information = DomainInformation.objects.create( + creator=self.superuser, domain=domain + ) application.approved_domain = domain application.save() @@ -484,34 +484,34 @@ class TestDomainApplicationAdmin(TestCase): "/admin/registrar/domainapplication/{}/change/".format(application.pk) ) request.user = self.superuser - + # Define a custom implementation for is_active def custom_is_active(self): return False # Override to return False - - # Use ExitStack to combine patch contexts + + # Use ExitStack to combine patch contexts with ExitStack() as stack: # Patch Domain.is_active and django.contrib.messages.error simultaneously - stack.enter_context(patch.object(Domain, 'is_active', custom_is_active)) - stack.enter_context(patch.object(messages, 'error')) - + stack.enter_context(patch.object(Domain, "is_active", custom_is_active)) + stack.enter_context(patch.object(messages, "error")) + # Simulate saving the model - application.status = DomainApplication.REJECTED + application.status = DomainApplication.REJECTED self.admin.save_model(request, application, None, True) # Assert that the error message was never called messages.error.assert_not_called() - + self.assertEqual(application.approved_domain, None) - + # Assert that Domain got Deleted with self.assertRaises(Domain.DoesNotExist): domain.refresh_from_db() - + # Assert that DomainInformation got Deleted with self.assertRaises(DomainInformation.DoesNotExist): domain_information.refresh_from_db() - + def test_error_when_saving_approved_to_ineligible_and_domain_is_active(self): # Create an instance of the model application = completed_application(status=DomainApplication.APPROVED) @@ -524,33 +524,34 @@ class TestDomainApplicationAdmin(TestCase): "/admin/registrar/domainapplication/{}/change/".format(application.pk) ) request.user = self.superuser - + # Define a custom implementation for is_active def custom_is_active(self): return True # Override to return True - - # Use ExitStack to combine patch contexts + + # Use ExitStack to combine patch contexts with ExitStack() as stack: # Patch Domain.is_active and django.contrib.messages.error simultaneously - stack.enter_context(patch.object(Domain, 'is_active', custom_is_active)) - stack.enter_context(patch.object(messages, 'error')) - + stack.enter_context(patch.object(Domain, "is_active", custom_is_active)) + stack.enter_context(patch.object(messages, "error")) + # Simulate saving the model - application.status = DomainApplication.INELIGIBLE + application.status = DomainApplication.INELIGIBLE self.admin.save_model(request, application, None, True) # Assert that the error message was called with the correct argument messages.error.assert_called_once_with( request, - "This action is not permitted, the domain " - + "is already active.", + "This action is not permitted, the domain " + "is already active.", ) - + def test_side_effects_when_saving_approved_to_ineligible(self): # Create an instance of the model application = completed_application(status=DomainApplication.APPROVED) domain = Domain.objects.create(name=application.requested_domain.name) - domain_information = DomainInformation.objects.create(creator=self.superuser, domain=domain) + domain_information = DomainInformation.objects.create( + creator=self.superuser, domain=domain + ) application.approved_domain = domain application.save() @@ -559,34 +560,34 @@ class TestDomainApplicationAdmin(TestCase): "/admin/registrar/domainapplication/{}/change/".format(application.pk) ) request.user = self.superuser - + # Define a custom implementation for is_active def custom_is_active(self): return False # Override to return False - - # Use ExitStack to combine patch contexts + + # Use ExitStack to combine patch contexts with ExitStack() as stack: # Patch Domain.is_active and django.contrib.messages.error simultaneously - stack.enter_context(patch.object(Domain, 'is_active', custom_is_active)) - stack.enter_context(patch.object(messages, 'error')) - + stack.enter_context(patch.object(Domain, "is_active", custom_is_active)) + stack.enter_context(patch.object(messages, "error")) + # Simulate saving the model - application.status = DomainApplication.INELIGIBLE + application.status = DomainApplication.INELIGIBLE self.admin.save_model(request, application, None, True) # Assert that the error message was never called messages.error.assert_not_called() - + self.assertEqual(application.approved_domain, None) - + # Assert that Domain got Deleted with self.assertRaises(Domain.DoesNotExist): domain.refresh_from_db() - + # Assert that DomainInformation got Deleted with self.assertRaises(DomainInformation.DoesNotExist): domain_information.refresh_from_db() - + def tearDown(self): Domain.objects.all().delete() DomainInformation.objects.all().delete() diff --git a/src/registrar/tests/test_models.py b/src/registrar/tests/test_models.py index a6a6fb3c9..2c6f78ef5 100644 --- a/src/registrar/tests/test_models.py +++ b/src/registrar/tests/test_models.py @@ -441,7 +441,7 @@ class TestDomainApplication(TestCase): with self.assertRaises(TransitionNotAllowed): application.reject() - + def test_transition_not_allowed_approved_rejected_when_domain_is_active(self): """Create an application with status approved, create a matching domain that is active, and call reject against transition rules""" @@ -450,13 +450,13 @@ class TestDomainApplication(TestCase): domain = Domain.objects.create(name=application.requested_domain.name) application.approved_domain = domain application.save() - + # Define a custom implementation for is_active def custom_is_active(self): return True # Override to return True - + # Use patch to temporarily replace is_active with the custom implementation - with patch.object(Domain, 'is_active', custom_is_active): + with patch.object(Domain, "is_active", custom_is_active): # Now, when you call is_active on Domain, it will return True with self.assertRaises(TransitionNotAllowed): application.reject() @@ -514,7 +514,7 @@ class TestDomainApplication(TestCase): with self.assertRaises(TransitionNotAllowed): application.reject_with_prejudice() - + def test_transition_not_allowed_approved_ineligible_when_domain_is_active(self): """Create an application with status approved, create a matching domain that is active, and call reject_with_prejudice against transition rules""" @@ -523,13 +523,13 @@ class TestDomainApplication(TestCase): domain = Domain.objects.create(name=application.requested_domain.name) application.approved_domain = domain application.save() - + # Define a custom implementation for is_active def custom_is_active(self): return True # Override to return True - + # Use patch to temporarily replace is_active with the custom implementation - with patch.object(Domain, 'is_active', custom_is_active): + with patch.object(Domain, "is_active", custom_is_active): # Now, when you call is_active on Domain, it will return True with self.assertRaises(TransitionNotAllowed): application.reject_with_prejudice()