This commit is contained in:
Rachid Mrad 2023-09-06 15:25:17 -04:00
parent 5fd84b534d
commit 0568928f1a
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
3 changed files with 58 additions and 57 deletions

View file

@ -614,8 +614,8 @@ class DomainApplication(TimeStampedModel):
def reject(self): def reject(self):
"""Reject an application that has been submitted. """Reject an application that has been submitted.
As a side effect 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""" (will cascade), and send an email notification."""
if self.status == self.APPROVED: if self.status == self.APPROVED:
self.approved_domain.delete_request() self.approved_domain.delete_request()
@ -641,7 +641,7 @@ class DomainApplication(TimeStampedModel):
any existing domains/applications and from submitting new aplications. any existing domains/applications and from submitting new aplications.
We do this by setting an ineligible status on the user, which the We do this by setting an ineligible status on the user, which the
permissions classes test against. This will also delete the domain 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: if self.status == self.APPROVED:
self.approved_domain.delete_request() self.approved_domain.delete_request()

View file

@ -1,6 +1,5 @@
from django.test import TestCase, RequestFactory, Client from django.test import TestCase, RequestFactory, Client
from django.contrib.admin.sites import AdminSite from django.contrib.admin.sites import AdminSite
from unittest.mock import patch
from contextlib import ExitStack from contextlib import ExitStack
from django.contrib import messages from django.contrib import messages
@ -436,7 +435,7 @@ class TestDomainApplicationAdmin(TestCase):
request, request,
"Cannot edit an application with a restricted creator.", "Cannot edit an application with a restricted creator.",
) )
def test_error_when_saving_approved_to_rejected_and_domain_is_active(self): def test_error_when_saving_approved_to_rejected_and_domain_is_active(self):
# Create an instance of the model # Create an instance of the model
application = completed_application(status=DomainApplication.APPROVED) application = completed_application(status=DomainApplication.APPROVED)
@ -449,33 +448,34 @@ class TestDomainApplicationAdmin(TestCase):
"/admin/registrar/domainapplication/{}/change/".format(application.pk) "/admin/registrar/domainapplication/{}/change/".format(application.pk)
) )
request.user = self.superuser request.user = self.superuser
# Define a custom implementation for is_active # Define a custom implementation for is_active
def custom_is_active(self): def custom_is_active(self):
return True # Override to return True return True # Override to return True
# Use ExitStack to combine patch contexts # Use ExitStack to combine patch contexts
with ExitStack() as stack: with ExitStack() as stack:
# Patch Domain.is_active and django.contrib.messages.error simultaneously # 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(Domain, "is_active", custom_is_active))
stack.enter_context(patch.object(messages, 'error')) stack.enter_context(patch.object(messages, "error"))
# Simulate saving the model # Simulate saving the model
application.status = DomainApplication.REJECTED application.status = DomainApplication.REJECTED
self.admin.save_model(request, application, None, True) self.admin.save_model(request, application, None, True)
# Assert that the error message was called with the correct argument # Assert that the error message was called with the correct argument
messages.error.assert_called_once_with( messages.error.assert_called_once_with(
request, request,
"This action is not permitted, the domain " "This action is not permitted, the domain " + "is already active.",
+ "is already active.",
) )
def test_side_effects_when_saving_approved_to_rejected(self): def test_side_effects_when_saving_approved_to_rejected(self):
# Create an instance of the model # Create an instance of the model
application = completed_application(status=DomainApplication.APPROVED) application = completed_application(status=DomainApplication.APPROVED)
domain = Domain.objects.create(name=application.requested_domain.name) 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.approved_domain = domain
application.save() application.save()
@ -484,34 +484,34 @@ class TestDomainApplicationAdmin(TestCase):
"/admin/registrar/domainapplication/{}/change/".format(application.pk) "/admin/registrar/domainapplication/{}/change/".format(application.pk)
) )
request.user = self.superuser request.user = self.superuser
# Define a custom implementation for is_active # Define a custom implementation for is_active
def custom_is_active(self): def custom_is_active(self):
return False # Override to return False return False # Override to return False
# Use ExitStack to combine patch contexts # Use ExitStack to combine patch contexts
with ExitStack() as stack: with ExitStack() as stack:
# Patch Domain.is_active and django.contrib.messages.error simultaneously # 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(Domain, "is_active", custom_is_active))
stack.enter_context(patch.object(messages, 'error')) stack.enter_context(patch.object(messages, "error"))
# Simulate saving the model # Simulate saving the model
application.status = DomainApplication.REJECTED application.status = DomainApplication.REJECTED
self.admin.save_model(request, application, None, True) self.admin.save_model(request, application, None, True)
# Assert that the error message was never called # Assert that the error message was never called
messages.error.assert_not_called() messages.error.assert_not_called()
self.assertEqual(application.approved_domain, None) self.assertEqual(application.approved_domain, None)
# Assert that Domain got Deleted # Assert that Domain got Deleted
with self.assertRaises(Domain.DoesNotExist): with self.assertRaises(Domain.DoesNotExist):
domain.refresh_from_db() domain.refresh_from_db()
# Assert that DomainInformation got Deleted # Assert that DomainInformation got Deleted
with self.assertRaises(DomainInformation.DoesNotExist): with self.assertRaises(DomainInformation.DoesNotExist):
domain_information.refresh_from_db() domain_information.refresh_from_db()
def test_error_when_saving_approved_to_ineligible_and_domain_is_active(self): def test_error_when_saving_approved_to_ineligible_and_domain_is_active(self):
# Create an instance of the model # Create an instance of the model
application = completed_application(status=DomainApplication.APPROVED) application = completed_application(status=DomainApplication.APPROVED)
@ -524,33 +524,34 @@ class TestDomainApplicationAdmin(TestCase):
"/admin/registrar/domainapplication/{}/change/".format(application.pk) "/admin/registrar/domainapplication/{}/change/".format(application.pk)
) )
request.user = self.superuser request.user = self.superuser
# Define a custom implementation for is_active # Define a custom implementation for is_active
def custom_is_active(self): def custom_is_active(self):
return True # Override to return True return True # Override to return True
# Use ExitStack to combine patch contexts # Use ExitStack to combine patch contexts
with ExitStack() as stack: with ExitStack() as stack:
# Patch Domain.is_active and django.contrib.messages.error simultaneously # 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(Domain, "is_active", custom_is_active))
stack.enter_context(patch.object(messages, 'error')) stack.enter_context(patch.object(messages, "error"))
# Simulate saving the model # Simulate saving the model
application.status = DomainApplication.INELIGIBLE application.status = DomainApplication.INELIGIBLE
self.admin.save_model(request, application, None, True) self.admin.save_model(request, application, None, True)
# Assert that the error message was called with the correct argument # Assert that the error message was called with the correct argument
messages.error.assert_called_once_with( messages.error.assert_called_once_with(
request, request,
"This action is not permitted, the domain " "This action is not permitted, the domain " + "is already active.",
+ "is already active.",
) )
def test_side_effects_when_saving_approved_to_ineligible(self): def test_side_effects_when_saving_approved_to_ineligible(self):
# Create an instance of the model # Create an instance of the model
application = completed_application(status=DomainApplication.APPROVED) application = completed_application(status=DomainApplication.APPROVED)
domain = Domain.objects.create(name=application.requested_domain.name) 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.approved_domain = domain
application.save() application.save()
@ -559,34 +560,34 @@ class TestDomainApplicationAdmin(TestCase):
"/admin/registrar/domainapplication/{}/change/".format(application.pk) "/admin/registrar/domainapplication/{}/change/".format(application.pk)
) )
request.user = self.superuser request.user = self.superuser
# Define a custom implementation for is_active # Define a custom implementation for is_active
def custom_is_active(self): def custom_is_active(self):
return False # Override to return False return False # Override to return False
# Use ExitStack to combine patch contexts # Use ExitStack to combine patch contexts
with ExitStack() as stack: with ExitStack() as stack:
# Patch Domain.is_active and django.contrib.messages.error simultaneously # 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(Domain, "is_active", custom_is_active))
stack.enter_context(patch.object(messages, 'error')) stack.enter_context(patch.object(messages, "error"))
# Simulate saving the model # Simulate saving the model
application.status = DomainApplication.INELIGIBLE application.status = DomainApplication.INELIGIBLE
self.admin.save_model(request, application, None, True) self.admin.save_model(request, application, None, True)
# Assert that the error message was never called # Assert that the error message was never called
messages.error.assert_not_called() messages.error.assert_not_called()
self.assertEqual(application.approved_domain, None) self.assertEqual(application.approved_domain, None)
# Assert that Domain got Deleted # Assert that Domain got Deleted
with self.assertRaises(Domain.DoesNotExist): with self.assertRaises(Domain.DoesNotExist):
domain.refresh_from_db() domain.refresh_from_db()
# Assert that DomainInformation got Deleted # Assert that DomainInformation got Deleted
with self.assertRaises(DomainInformation.DoesNotExist): with self.assertRaises(DomainInformation.DoesNotExist):
domain_information.refresh_from_db() domain_information.refresh_from_db()
def tearDown(self): def tearDown(self):
Domain.objects.all().delete() Domain.objects.all().delete()
DomainInformation.objects.all().delete() DomainInformation.objects.all().delete()

View file

@ -441,7 +441,7 @@ class TestDomainApplication(TestCase):
with self.assertRaises(TransitionNotAllowed): with self.assertRaises(TransitionNotAllowed):
application.reject() application.reject()
def test_transition_not_allowed_approved_rejected_when_domain_is_active(self): def test_transition_not_allowed_approved_rejected_when_domain_is_active(self):
"""Create an application with status approved, create a matching domain that """Create an application with status approved, create a matching domain that
is active, and call reject against transition rules""" is active, and call reject against transition rules"""
@ -450,13 +450,13 @@ class TestDomainApplication(TestCase):
domain = Domain.objects.create(name=application.requested_domain.name) domain = Domain.objects.create(name=application.requested_domain.name)
application.approved_domain = domain application.approved_domain = domain
application.save() application.save()
# Define a custom implementation for is_active # Define a custom implementation for is_active
def custom_is_active(self): def custom_is_active(self):
return True # Override to return True return True # Override to return True
# Use patch to temporarily replace is_active with the custom implementation # 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 # Now, when you call is_active on Domain, it will return True
with self.assertRaises(TransitionNotAllowed): with self.assertRaises(TransitionNotAllowed):
application.reject() application.reject()
@ -514,7 +514,7 @@ class TestDomainApplication(TestCase):
with self.assertRaises(TransitionNotAllowed): with self.assertRaises(TransitionNotAllowed):
application.reject_with_prejudice() application.reject_with_prejudice()
def test_transition_not_allowed_approved_ineligible_when_domain_is_active(self): def test_transition_not_allowed_approved_ineligible_when_domain_is_active(self):
"""Create an application with status approved, create a matching domain that """Create an application with status approved, create a matching domain that
is active, and call reject_with_prejudice against transition rules""" 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) domain = Domain.objects.create(name=application.requested_domain.name)
application.approved_domain = domain application.approved_domain = domain
application.save() application.save()
# Define a custom implementation for is_active # Define a custom implementation for is_active
def custom_is_active(self): def custom_is_active(self):
return True # Override to return True return True # Override to return True
# Use patch to temporarily replace is_active with the custom implementation # 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 # Now, when you call is_active on Domain, it will return True
with self.assertRaises(TransitionNotAllowed): with self.assertRaises(TransitionNotAllowed):
application.reject_with_prejudice() application.reject_with_prejudice()