Simplify test cases further, linting

This commit is contained in:
zandercymatics 2024-02-26 15:02:54 -07:00
parent 3b4e470f0c
commit 3d6f638462
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 33 additions and 91 deletions

View file

@ -849,7 +849,7 @@ class DomainApplicationAdminForm(forms.ModelForm):
DomainApplication.ApplicationStatus.IN_REVIEW, DomainApplication.ApplicationStatus.IN_REVIEW,
DomainApplication.ApplicationStatus.ACTION_NEEDED, DomainApplication.ApplicationStatus.ACTION_NEEDED,
DomainApplication.ApplicationStatus.REJECTED, DomainApplication.ApplicationStatus.REJECTED,
DomainApplication.ApplicationStatus.INELIGIBLE DomainApplication.ApplicationStatus.INELIGIBLE,
] ]
# If a status change occured, check for validity # If a status change occured, check for validity
@ -864,7 +864,7 @@ class DomainApplicationAdminForm(forms.ModelForm):
def _check_for_valid_investigator(self, investigator) -> bool: def _check_for_valid_investigator(self, investigator) -> bool:
""" """
Checks if the investigator field is not none, and is staff. Checks if the investigator field is not none, and is staff.
Adds form errors on failure. Adds form errors on failure.
""" """
is_valid = False is_valid = False
@ -881,7 +881,7 @@ class DomainApplicationAdminForm(forms.ModelForm):
if error_message is not None: if error_message is not None:
self.add_error("investigator", error_message) self.add_error("investigator", error_message)
return is_valid return is_valid

View file

@ -731,7 +731,7 @@ class DomainApplication(TimeStampedModel):
ApplicationStatus.REJECTED, ApplicationStatus.REJECTED,
], ],
target=ApplicationStatus.APPROVED, target=ApplicationStatus.APPROVED,
conditions=[investigator_exists_and_is_staff] conditions=[investigator_exists_and_is_staff],
) )
def approve(self, send_email=True): def approve(self, send_email=True):
"""Approve an application that has been submitted. """Approve an application that has been submitted.

View file

@ -51,7 +51,7 @@ class TestDomainApplication(TestCase):
self.ineligible_application = completed_application( self.ineligible_application = completed_application(
status=DomainApplication.ApplicationStatus.INELIGIBLE, name="ineligible.gov" status=DomainApplication.ApplicationStatus.INELIGIBLE, name="ineligible.gov"
) )
# Store all aplpication statuses in a variable for ease of use # Store all aplpication statuses in a variable for ease of use
self.all_applications = [ self.all_applications = [
self.started_application, self.started_application,
@ -63,7 +63,7 @@ class TestDomainApplication(TestCase):
self.rejected_application, self.rejected_application,
self.ineligible_application, self.ineligible_application,
] ]
self.mock_client = MockSESClient() self.mock_client = MockSESClient()
def tearDown(self): def tearDown(self):
@ -249,6 +249,19 @@ class TestDomainApplication(TestCase):
# Call the method # Call the method
method() method()
def assert_fsm_transition_does_not_raise_error(self, test_cases, method_to_run):
"""Given a list of test cases, ensure that none of them throw transition errors"""
with boto3_mocking.clients.handler_for("sesv2", self.mock_client), less_console_noise():
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
# Retrieve the method by name from the application object and call it
method = getattr(application, method_to_run)
# Call the method
method()
except exception_type:
self.fail(f"{exception_type} was raised, but it was not expected.")
def test_submit_transition_allowed_with_no_investigator(self): def test_submit_transition_allowed_with_no_investigator(self):
""" """
Tests for attempting to transition without an investigator. Tests for attempting to transition without an investigator.
@ -263,13 +276,7 @@ class TestDomainApplication(TestCase):
# Set all investigators to none # Set all investigators to none
set_applications_investigators(self.all_applications, None) set_applications_investigators(self.all_applications, None)
with boto3_mocking.clients.handler_for("sesv2", self.mock_client), less_console_noise(): self.assert_fsm_transition_does_not_raise_error(test_cases, "submit")
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.submit()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_submit_transition_allowed_with_investigator_not_staff(self): def test_submit_transition_allowed_with_investigator_not_staff(self):
""" """
@ -286,13 +293,7 @@ class TestDomainApplication(TestCase):
user, _ = User.objects.get_or_create(username="pancakesyrup", is_staff=False) user, _ = User.objects.get_or_create(username="pancakesyrup", is_staff=False)
set_applications_investigators(self.all_applications, user) set_applications_investigators(self.all_applications, user)
with boto3_mocking.clients.handler_for("sesv2", self.mock_client), less_console_noise(): self.assert_fsm_transition_does_not_raise_error(test_cases, "submit")
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.submit()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_submit_transition_allowed(self): def test_submit_transition_allowed(self):
""" """
@ -305,14 +306,7 @@ class TestDomainApplication(TestCase):
(self.withdrawn_application, TransitionNotAllowed), (self.withdrawn_application, TransitionNotAllowed),
] ]
with boto3_mocking.clients.handler_for("sesv2", self.mock_client): self.assert_fsm_transition_does_not_raise_error(test_cases, "submit")
with less_console_noise():
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.submit()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_submit_transition_allowed_twice(self): def test_submit_transition_allowed_twice(self):
""" """
@ -331,7 +325,7 @@ class TestDomainApplication(TestCase):
self.in_review_application.submit() self.in_review_application.submit()
except TransitionNotAllowed: except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.") self.fail("TransitionNotAllowed was raised, but it was not expected.")
self.assertEqual(self.in_review_application.status, DomainApplication.ApplicationStatus.SUBMITTED) self.assertEqual(self.in_review_application.status, DomainApplication.ApplicationStatus.SUBMITTED)
def test_submit_transition_not_allowed(self): def test_submit_transition_not_allowed(self):
@ -359,14 +353,7 @@ class TestDomainApplication(TestCase):
(self.ineligible_application, TransitionNotAllowed), (self.ineligible_application, TransitionNotAllowed),
] ]
with boto3_mocking.clients.handler_for("sesv2", self.mock_client): self.assert_fsm_transition_does_not_raise_error(test_cases, "in_review")
with less_console_noise():
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.in_review()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_in_review_transition_not_allowed_with_no_investigator(self): def test_in_review_transition_not_allowed_with_no_investigator(self):
""" """
@ -384,7 +371,7 @@ class TestDomainApplication(TestCase):
set_applications_investigators(self.all_applications, None) set_applications_investigators(self.all_applications, None)
self.assert_fsm_transition_raises_error(test_cases, "in_review") self.assert_fsm_transition_raises_error(test_cases, "in_review")
def test_in_review_transition_not_allowed_with_investigator_not_staff(self): def test_in_review_transition_not_allowed_with_investigator_not_staff(self):
""" """
Tests for attempting to transition with an investigator that is not staff. Tests for attempting to transition with an investigator that is not staff.
@ -426,13 +413,8 @@ class TestDomainApplication(TestCase):
(self.rejected_application, TransitionNotAllowed), (self.rejected_application, TransitionNotAllowed),
(self.ineligible_application, TransitionNotAllowed), (self.ineligible_application, TransitionNotAllowed),
] ]
with less_console_noise():
for application, exception_type in test_cases: self.assert_fsm_transition_does_not_raise_error(test_cases, "action_needed")
with self.subTest(application=application, exception_type=exception_type):
try:
application.action_needed()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_action_needed_transition_not_allowed_with_no_investigator(self): def test_action_needed_transition_not_allowed_with_no_investigator(self):
""" """
@ -493,14 +475,7 @@ class TestDomainApplication(TestCase):
(self.rejected_application, TransitionNotAllowed), (self.rejected_application, TransitionNotAllowed),
] ]
with boto3_mocking.clients.handler_for("sesv2", self.mock_client): self.assert_fsm_transition_does_not_raise_error(test_cases, "approve")
with less_console_noise():
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.approve()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_approved_transition_not_allowed_with_no_investigator(self): def test_approved_transition_not_allowed_with_no_investigator(self):
""" """
@ -570,14 +545,7 @@ class TestDomainApplication(TestCase):
(self.action_needed_application, TransitionNotAllowed), (self.action_needed_application, TransitionNotAllowed),
] ]
with boto3_mocking.clients.handler_for("sesv2", self.mock_client): self.assert_fsm_transition_does_not_raise_error(test_cases, "withdraw")
with less_console_noise():
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.withdraw()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_withdraw_transition_allowed_with_no_investigator(self): def test_withdraw_transition_allowed_with_no_investigator(self):
""" """
@ -594,13 +562,7 @@ class TestDomainApplication(TestCase):
# Set all investigators to none # Set all investigators to none
set_applications_investigators(self.all_applications, None) set_applications_investigators(self.all_applications, None)
with boto3_mocking.clients.handler_for("sesv2", self.mock_client), less_console_noise(): self.assert_fsm_transition_does_not_raise_error(test_cases, "withdraw")
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.withdraw()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_withdraw_transition_allowed_with_investigator_not_staff(self): def test_withdraw_transition_allowed_with_investigator_not_staff(self):
""" """
@ -618,13 +580,7 @@ class TestDomainApplication(TestCase):
user, _ = User.objects.get_or_create(username="pancakesyrup", is_staff=False) user, _ = User.objects.get_or_create(username="pancakesyrup", is_staff=False)
set_applications_investigators(self.all_applications, user) set_applications_investigators(self.all_applications, user)
with boto3_mocking.clients.handler_for("sesv2", self.mock_client), less_console_noise(): self.assert_fsm_transition_does_not_raise_error(test_cases, "withdraw")
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.withdraw()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_withdraw_transition_not_allowed(self): def test_withdraw_transition_not_allowed(self):
""" """
@ -650,14 +606,7 @@ class TestDomainApplication(TestCase):
(self.approved_application, TransitionNotAllowed), (self.approved_application, TransitionNotAllowed),
] ]
with boto3_mocking.clients.handler_for("sesv2", self.mock_client): self.assert_fsm_transition_does_not_raise_error(test_cases, "reject")
with less_console_noise():
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.reject()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_reject_transition_not_allowed_with_no_investigator(self): def test_reject_transition_not_allowed_with_no_investigator(self):
""" """
@ -717,14 +666,7 @@ class TestDomainApplication(TestCase):
(self.rejected_application, TransitionNotAllowed), (self.rejected_application, TransitionNotAllowed),
] ]
with boto3_mocking.clients.handler_for("sesv2", self.mock_client): self.assert_fsm_transition_does_not_raise_error(test_cases, "reject_with_prejudice")
with less_console_noise():
for application, exception_type in test_cases:
with self.subTest(application=application, exception_type=exception_type):
try:
application.reject_with_prejudice()
except TransitionNotAllowed:
self.fail("TransitionNotAllowed was raised, but it was not expected.")
def test_reject_with_prejudice_transition_not_allowed_with_no_investigator(self): def test_reject_with_prejudice_transition_not_allowed_with_no_investigator(self):
""" """