From 321538f8c5ca5c2fbd3d1a79cd9736f4f102cc2a Mon Sep 17 00:00:00 2001 From: Kegan Maher Date: Tue, 12 Nov 2024 22:54:47 +0000 Subject: [PATCH] test(models): consolidate EnrollmentFlow.clean() tests --- tests/pytest/conftest.py | 20 ------- tests/pytest/core/test_models.py | 97 ++++++++++++++++---------------- 2 files changed, 49 insertions(+), 68 deletions(-) diff --git a/tests/pytest/conftest.py b/tests/pytest/conftest.py index f60533b27..b9e620c6a 100644 --- a/tests/pytest/conftest.py +++ b/tests/pytest/conftest.py @@ -132,26 +132,6 @@ def model_EnrollmentFlow_does_not_support_expiration(model_EnrollmentFlow): return model_EnrollmentFlow -@pytest.fixture -def model_EnrollmentFlow_zero_expiration_days(model_EnrollmentFlow): - model_EnrollmentFlow.supports_expiration = True - model_EnrollmentFlow.expiration_days = 0 - model_EnrollmentFlow.expiration_reenrollment_days = 14 - model_EnrollmentFlow.save() - - return model_EnrollmentFlow - - -@pytest.fixture -def model_EnrollmentFlow_zero_expiration_reenrollment_days(model_EnrollmentFlow): - model_EnrollmentFlow.supports_expiration = True - model_EnrollmentFlow.expiration_days = 14 - model_EnrollmentFlow.expiration_reenrollment_days = 0 - model_EnrollmentFlow.save() - - return model_EnrollmentFlow - - @pytest.fixture def model_EnrollmentFlow_supports_expiration(model_EnrollmentFlow): model_EnrollmentFlow.supports_expiration = True diff --git a/tests/pytest/core/test_models.py b/tests/pytest/core/test_models.py index 6290350b8..e2539a8fc 100644 --- a/tests/pytest/core/test_models.py +++ b/tests/pytest/core/test_models.py @@ -160,54 +160,6 @@ def test_EnrollmentFlow_agency_card_name__claims(model_EnrollmentFlow_with_scope assert model_EnrollmentFlow_with_scope_and_claim.agency_card_name == "" -@pytest.mark.django_db -def test_EnrollmentFlow_supports_expiration_False(model_EnrollmentFlow, model_EnrollmentFlow_does_not_support_expiration): - # test will fail if any error is raised - model_EnrollmentFlow_does_not_support_expiration.full_clean() - - -@pytest.mark.django_db -def test_EnrollmentFlow_zero_expiration_days(model_EnrollmentFlow_zero_expiration_days): - with pytest.raises(ValidationError) as exception_info: - model_EnrollmentFlow_zero_expiration_days.full_clean() - - error_dict = exception_info.value.error_dict - assert len(error_dict["expiration_days"]) == 1 - assert error_dict["expiration_days"][0].message == "When support_expiration is True, this value must be greater than 0." - - -@pytest.mark.django_db -def test_EnrollmentFlow_zero_expiration_reenrollment_days(model_EnrollmentFlow_zero_expiration_reenrollment_days): - with pytest.raises(ValidationError) as exception_info: - model_EnrollmentFlow_zero_expiration_reenrollment_days.full_clean() - - error_dict = exception_info.value.error_dict - assert len(error_dict["expiration_reenrollment_days"]) == 1 - assert ( - error_dict["expiration_reenrollment_days"][0].message - == "When support_expiration is True, this value must be greater than 0." - ) - - -@pytest.mark.django_db -def test_EnrollmentFlow_missing_reenrollment_template(model_EnrollmentFlow_supports_expiration): - model_EnrollmentFlow_supports_expiration.reenrollment_error_template = None - model_EnrollmentFlow_supports_expiration.save() - - with pytest.raises(ValidationError) as exception_info: - model_EnrollmentFlow_supports_expiration.full_clean() - - error_dict = exception_info.value.error_dict - assert len(error_dict["reenrollment_error_template"]) == 1 - assert error_dict["reenrollment_error_template"][0].message == "Required when supports expiration is True." - - -@pytest.mark.django_db -def test_EnrollmentFlow_supports_expiration(model_EnrollmentFlow_supports_expiration): - # test will fail if any error is raised - model_EnrollmentFlow_supports_expiration.full_clean() - - @pytest.mark.django_db def test_EnrollmentFlow_enrollment_index_template(model_EnrollmentFlow_with_scope_and_claim): assert model_EnrollmentFlow_with_scope_and_claim.enrollment_index_template == "enrollment/index.html" @@ -420,6 +372,55 @@ def test_EnrollmentFlow_template_overrides_eligibility_api(model_EnrollmentFlow_ ) +@pytest.mark.django_db +def test_EnrollmentFlow_clean_does_not_supports_expiration(model_EnrollmentFlow_does_not_support_expiration): + # test will fail if any error is raised + model_EnrollmentFlow_does_not_support_expiration.clean() + + +@pytest.mark.django_db +def test_EnrollmentFlow_clean_supports_expiration(model_EnrollmentFlow_supports_expiration): + # test will fail if any error is raised + model_EnrollmentFlow_supports_expiration.clean() + + model_EnrollmentFlow_supports_expiration.expiration_days = 0 + model_EnrollmentFlow_supports_expiration.expiration_reenrollment_days = 0 + model_EnrollmentFlow_supports_expiration.reenrollment_error_template = None + + with pytest.raises(ValidationError) as e: + model_EnrollmentFlow_supports_expiration.clean() + + assert "expiration_days" in e.value.error_dict + assert "expiration_reenrollment_days" in e.value.error_dict + assert "reenrollment_error_template" in e.value.error_dict + + +@pytest.mark.django_db +def test_EnrollmentFlow_clean_templates(model_EnrollmentFlow_supports_expiration, model_TransitAgency_inactive): + model_EnrollmentFlow_supports_expiration.transit_agency = model_TransitAgency_inactive + test_template = "does/not/exist.html" + model_EnrollmentFlow_supports_expiration.selection_label_template_override = test_template + model_EnrollmentFlow_supports_expiration.eligibility_start_template_override = test_template + model_EnrollmentFlow_supports_expiration.eligibility_unverified_template_override = test_template + model_EnrollmentFlow_supports_expiration.enrollment_index_template_override = test_template + model_EnrollmentFlow_supports_expiration.enrollment_success_template_override = test_template + model_EnrollmentFlow_supports_expiration.reenrollment_error_template = test_template + # agency is inactive, OK to have bad template fields + model_EnrollmentFlow_supports_expiration.clean() + + # now mark it active and expect failure on clean() + model_TransitAgency_inactive.active = True + with pytest.raises(ValidationError) as e: + model_EnrollmentFlow_supports_expiration.clean() + + assert "selection_label_template" in e.value.error_dict + assert "eligibility_start_template" in e.value.error_dict + assert "eligibility_unverified_template" in e.value.error_dict + assert "enrollment_index_template" in e.value.error_dict + assert "enrollment_success_template" in e.value.error_dict + assert "reenrollment_error_template" in e.value.error_dict + + @pytest.mark.django_db def test_TransitProcessor_str(model_TransitProcessor): assert str(model_TransitProcessor) == model_TransitProcessor.name