Missing unit tests and linting

This commit is contained in:
rachidatecs 2023-07-12 16:10:30 -04:00
parent a6f8e64c0a
commit cbc2975c1d
No known key found for this signature in database
GPG key ID: 3CEBBFA7325E5525
4 changed files with 128 additions and 34 deletions

View file

@ -272,7 +272,7 @@ class DomainApplicationAdmin(ListHeaderAdmin):
elif obj.status == models.DomainApplication.WITHDRAWN: elif obj.status == models.DomainApplication.WITHDRAWN:
original_obj.withdraw() original_obj.withdraw()
elif obj.status == models.DomainApplication.REJECTED: elif obj.status == models.DomainApplication.REJECTED:
original_obj.reject() original_obj.reject(updated_domain_application=obj)
else: else:
logger.warning("Unknown status selected in django admin") logger.warning("Unknown status selected in django admin")

View file

@ -501,7 +501,9 @@ class DomainApplication(TimeStampedModel):
except EmailSendingError: except EmailSendingError:
logger.warning("Failed to send confirmation email", exc_info=True) logger.warning("Failed to send confirmation email", exc_info=True)
@transition(field="status", source=[STARTED, ACTION_NEEDED, WITHDRAWN], target=SUBMITTED) @transition(
field="status", source=[STARTED, ACTION_NEEDED, WITHDRAWN], target=SUBMITTED
)
def submit(self, updated_domain_application=None): def submit(self, updated_domain_application=None):
"""Submit an application that is started. """Submit an application that is started.
@ -571,7 +573,9 @@ class DomainApplication(TimeStampedModel):
"emails/status_change_action_needed_subject.txt", "emails/status_change_action_needed_subject.txt",
) )
@transition(field="status", source=[SUBMITTED, INVESTIGATING, REJECTED], target=APPROVED) @transition(
field="status", source=[SUBMITTED, INVESTIGATING, REJECTED], target=APPROVED
)
def approve(self, updated_domain_application=None): def approve(self, updated_domain_application=None):
"""Approve an application that has been submitted. """Approve an application that has been submitted.
@ -622,8 +626,16 @@ class DomainApplication(TimeStampedModel):
"""Withdraw an application that has been submitted.""" """Withdraw an application that has been submitted."""
@transition(field="status", source=[INVESTIGATING, APPROVED], target=REJECTED) @transition(field="status", source=[INVESTIGATING, APPROVED], target=REJECTED)
def reject(self): def reject(self, updated_domain_application):
"""Reject an application that has been submitted.""" """Reject an application that has been submitted.
As a side effect, an email notification is sent, similar to in_review"""
updated_domain_application._send_status_update_email(
"action needed",
"emails/status_change_rejected.txt",
"emails/status_change_rejected_subject.txt",
)
# ## Form policies ### # ## Form policies ###
# #

View file

@ -76,9 +76,6 @@ class TestDomainApplicationAdmin(TestCase):
# Perform assertions on the mock call itself # Perform assertions on the mock call itself
mock_client_instance.send_email.assert_called_once() mock_client_instance.send_email.assert_called_once()
# Cleanup
application.delete()
@boto3_mocking.patching @boto3_mocking.patching
def test_save_model_sends_in_review_email(self): def test_save_model_sends_in_review_email(self):
# make sure there is no user with this email # make sure there is no user with this email
@ -125,9 +122,6 @@ class TestDomainApplicationAdmin(TestCase):
# Perform assertions on the mock call itself # Perform assertions on the mock call itself
mock_client_instance.send_email.assert_called_once() mock_client_instance.send_email.assert_called_once()
# Cleanup
application.delete()
@boto3_mocking.patching @boto3_mocking.patching
def test_save_model_sends_approved_email(self): def test_save_model_sends_approved_email(self):
# make sure there is no user with this email # make sure there is no user with this email
@ -174,10 +168,97 @@ class TestDomainApplicationAdmin(TestCase):
# Perform assertions on the mock call itself # Perform assertions on the mock call itself
mock_client_instance.send_email.assert_called_once() mock_client_instance.send_email.assert_called_once()
# Cleanup @boto3_mocking.patching
if DomainInformation.objects.get(id=application.pk) is not None: def test_save_model_sends_action_needed_email(self):
DomainInformation.objects.get(id=application.pk).delete() # make sure there is no user with this email
application.delete() EMAIL = "mayor@igorville.gov"
User.objects.filter(email=EMAIL).delete()
mock_client = MagicMock()
mock_client_instance = mock_client.return_value
with boto3_mocking.clients.handler_for("sesv2", mock_client):
# Create a sample application
application = completed_application(status=DomainApplication.INVESTIGATING)
# Create a mock request
request = self.factory.post(
"/admin/registrar/domainapplication/{}/change/".format(application.pk)
)
# Create an instance of the model admin
model_admin = DomainApplicationAdmin(DomainApplication, self.site)
# Modify the application's property
application.status = DomainApplication.ACTION_NEEDED
# Use the model admin's save_model method
model_admin.save_model(request, application, form=None, change=True)
# Access the arguments passed to send_email
call_args = mock_client_instance.send_email.call_args
args, kwargs = call_args
# Retrieve the email details from the arguments
from_email = kwargs.get("FromEmailAddress")
to_email = kwargs["Destination"]["ToAddresses"][0]
email_content = kwargs["Content"]
email_body = email_content["Simple"]["Body"]["Text"]["Data"]
# Assert or perform other checks on the email details
expected_string = "Your .gov domain request requires your attention."
self.assertEqual(from_email, settings.DEFAULT_FROM_EMAIL)
self.assertEqual(to_email, EMAIL)
self.assertIn(expected_string, email_body)
# Perform assertions on the mock call itself
mock_client_instance.send_email.assert_called_once()
@boto3_mocking.patching
def test_save_model_sends_rejected_email(self):
# make sure there is no user with this email
EMAIL = "mayor@igorville.gov"
User.objects.filter(email=EMAIL).delete()
mock_client = MagicMock()
mock_client_instance = mock_client.return_value
with boto3_mocking.clients.handler_for("sesv2", mock_client):
# Create a sample application
application = completed_application(status=DomainApplication.INVESTIGATING)
# Create a mock request
request = self.factory.post(
"/admin/registrar/domainapplication/{}/change/".format(application.pk)
)
# Create an instance of the model admin
model_admin = DomainApplicationAdmin(DomainApplication, self.site)
# Modify the application's property
application.status = DomainApplication.REJECTED
# Use the model admin's save_model method
model_admin.save_model(request, application, form=None, change=True)
# Access the arguments passed to send_email
call_args = mock_client_instance.send_email.call_args
args, kwargs = call_args
# Retrieve the email details from the arguments
from_email = kwargs.get("FromEmailAddress")
to_email = kwargs["Destination"]["ToAddresses"][0]
email_content = kwargs["Content"]
email_body = email_content["Simple"]["Body"]["Text"]["Data"]
# Assert or perform other checks on the email details
expected_string = "Your .gov domain request has been rejected."
self.assertEqual(from_email, settings.DEFAULT_FROM_EMAIL)
self.assertEqual(to_email, EMAIL)
self.assertIn(expected_string, email_body)
# Perform assertions on the mock call itself
mock_client_instance.send_email.assert_called_once()
def test_changelist_view(self): def test_changelist_view(self):
# Have to get creative to get past linter # Have to get creative to get past linter
@ -241,6 +322,7 @@ class TestDomainApplicationAdmin(TestCase):
def tearDown(self): def tearDown(self):
# delete any applications too # delete any applications too
DomainInformation.objects.all().delete()
DomainApplication.objects.all().delete() DomainApplication.objects.all().delete()
User.objects.all().delete() User.objects.all().delete()
self.superuser.delete() self.superuser.delete()