Skip to content

Commit

Permalink
Eligibility: Filter enrollment flows by supported method (#2420)
Browse files Browse the repository at this point in the history
  • Loading branch information
machikoyasuda authored Oct 2, 2024
2 parents 00d0009 + 673c989 commit 3c39dc3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion benefits/eligibility/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class EnrollmentFlowSelectionForm(forms.Form):

def __init__(self, agency: models.TransitAgency, *args, **kwargs):
super().__init__(*args, **kwargs)
flows = agency.enrollment_flows.all()
flows = agency.enrollment_flows.filter(supported_enrollment_methods__contains=models.EnrollmentMethods.DIGITAL)

self.classes = "col-lg-8"
# second element is not used since we render the whole label using selection_label_template,
Expand Down
2 changes: 1 addition & 1 deletion benefits/in_person/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class InPersonEligibilityForm(forms.Form):

def __init__(self, agency: models.TransitAgency, *args, **kwargs):
super().__init__(*args, **kwargs)
flows = agency.enrollment_flows.all()
flows = agency.enrollment_flows.filter(supported_enrollment_methods__contains=models.EnrollmentMethods.IN_PERSON)

self.classes = "checkbox-parent"
flow_field = self.fields["flow"]
Expand Down
33 changes: 33 additions & 0 deletions tests/pytest/eligibility/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from benefits.core import models
from benefits.routes import routes
from benefits.core.middleware import TEMPLATE_USER_ERROR
import benefits.core.session
Expand Down Expand Up @@ -71,6 +72,36 @@ def model_EnrollmentFlow_with_form_class(mocker, model_EnrollmentFlow):
return model_EnrollmentFlow


@pytest.mark.django_db
def test_index_filtering_flows(mocker, model_TransitAgency, client):
digital = models.EnrollmentFlow.objects.create(
transit_agency=model_TransitAgency,
supported_enrollment_methods=[models.EnrollmentMethods.DIGITAL],
label="Digital",
selection_label_template="eligibility/includes/selection-label.html",
)
in_person = models.EnrollmentFlow.objects.create(
transit_agency=model_TransitAgency,
supported_enrollment_methods=[models.EnrollmentMethods.IN_PERSON],
label="In-Person",
selection_label_template="eligibility/includes/selection-label.html",
)
both = models.EnrollmentFlow.objects.create(
transit_agency=model_TransitAgency,
supported_enrollment_methods=[models.EnrollmentMethods.DIGITAL, models.EnrollmentMethods.IN_PERSON],
label="Both",
selection_label_template="eligibility/includes/selection-label.html",
)
mocker.patch("benefits.core.session.agency", autospec=True, return_value=model_TransitAgency)

path = reverse(routes.ELIGIBILITY_INDEX)
response = client.get(path)
filtered_flow_ids = [choice[0] for choice in response.context_data["form"].fields["flow"].choices]

assert digital.id, both.id in filtered_flow_ids
assert in_person.id not in filtered_flow_ids


@pytest.mark.django_db
def test_index_get_agency_multiple_flows(mocker, model_TransitAgency, model_EnrollmentFlow, mocked_session_agency, client):
# override the mocked session agency with a mock agency that has multiple flows
Expand All @@ -80,6 +111,7 @@ def test_index_get_agency_multiple_flows(mocker, model_TransitAgency, model_Enro
mock_manager = mocker.Mock()
mock_manager.all.return_value = [model_EnrollmentFlow, model_EnrollmentFlow]
type(mock_agency).enrollment_flows = mocker.PropertyMock(return_value=mock_manager)
type(mock_agency).enrollment_flows.filter.return_value = [model_EnrollmentFlow, model_EnrollmentFlow]

mock_agency.index_url = "/agency"
mock_agency.eligibility_index_template = "eligibility/index.html"
Expand All @@ -103,6 +135,7 @@ def test_index_get_agency_single_flow(mocker, model_TransitAgency, model_Enrollm
mock_manager = mocker.Mock()
mock_manager.all.return_value = [model_EnrollmentFlow]
type(mock_agency).enrollment_flows = mocker.PropertyMock(return_value=mock_manager)
type(mock_agency).enrollment_flows.filter.return_value = [model_EnrollmentFlow, model_EnrollmentFlow]

mock_agency.index_url = "/agency"
mock_agency.eligibility_index_template = "eligibility/index.html"
Expand Down
25 changes: 25 additions & 0 deletions tests/pytest/in_person/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ def test_eligibility_logged_in(admin_client):
assert response.template_name == "in_person/eligibility.html"


@pytest.mark.django_db
def test_eligibility_logged_in_filtering_flows(mocker, model_TransitAgency, admin_client):
digital = models.EnrollmentFlow.objects.create(
transit_agency=model_TransitAgency, supported_enrollment_methods=[models.EnrollmentMethods.DIGITAL], label="Digital"
)
in_person = models.EnrollmentFlow.objects.create(
transit_agency=model_TransitAgency,
supported_enrollment_methods=[models.EnrollmentMethods.IN_PERSON],
label="In-Person",
)
both = models.EnrollmentFlow.objects.create(
transit_agency=model_TransitAgency,
supported_enrollment_methods=[models.EnrollmentMethods.DIGITAL, models.EnrollmentMethods.IN_PERSON],
label="Both",
)
mocker.patch("benefits.core.session.agency", autospec=True, return_value=model_TransitAgency)

path = reverse(routes.IN_PERSON_ELIGIBILITY)
response = admin_client.get(path)
filtered_flow_ids = [choice[0] for choice in response.context_data["form"].fields["flow"].choices]

assert in_person.id, both.id in filtered_flow_ids
assert digital.id not in filtered_flow_ids


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_flow")
def test_confirm_post_valid_form_eligibility_verified(admin_client):
Expand Down

0 comments on commit 3c39dc3

Please sign in to comment.