Skip to content

Commit

Permalink
feat(enrollment): EnrollmentEvent for digital success
Browse files Browse the repository at this point in the history
refactor the view tests to move away from expiration vs. non-expiration;
this distinction isn't relevant in the tests since they are mocking
the enrollment.enroll() function, so expiration calculation never occurs

instead test an enrollment for eligibility API vs. claims verification,
the respective EnrollmentEvents should contain different data
  • Loading branch information
thekaveman committed Sep 13, 2024
1 parent 5be2f37 commit 3ecc9fb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
13 changes: 13 additions & 0 deletions benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from benefits.core import session
from benefits.core.middleware import EligibleSessionRequired, FlowSessionRequired, pageview_decorator

from benefits.core import models
from . import analytics, forms
from .enrollment import Status, request_card_tokenization_access, enroll

Expand Down Expand Up @@ -67,6 +68,18 @@ def index(request):

match (status):
case Status.SUCCESS:
agency = session.agency(request)
flow = session.flow(request)
expiry = session.enrollment_expiry(request)
verified_by = flow.claims_provider.client_name if flow.uses_claims_verification else flow.eligibility_api_url
event = models.EnrollmentEvent.objects.create(
transit_agency=agency,
enrollment_flow=flow,
method=models.EnrollmentMethods.DIGITAL,
verified_by=verified_by,
expiration_datetime=expiry,
)
event.save()
return success(request)

case Status.SYSTEM_ERROR:
Expand Down
37 changes: 29 additions & 8 deletions tests/pytest/enrollment/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.urls import reverse
from requests import HTTPError

from benefits.core import models
from benefits.routes import routes
import benefits.enrollment.views
from benefits.enrollment.enrollment import Status, CardTokenizationAccessResponse
Expand Down Expand Up @@ -297,42 +298,62 @@ def test_index_eligible_post_valid_form_exception(mocker, client, card_tokenize_

@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_flow", "mocked_session_eligible")
def test_index_eligible_post_valid_form_success_does_not_support_expiration(
def test_index_eligible_post_valid_form_success_claims(
mocker,
client,
card_tokenize_form_data,
mocked_analytics_module,
model_EnrollmentFlow_does_not_support_expiration,
model_TransitAgency,
model_EnrollmentFlow_with_scope_and_claim,
):
mocker.patch("benefits.enrollment.views.enroll", return_value=(Status.SUCCESS, None))
spy = mocker.spy(benefits.enrollment.views.models.EnrollmentEvent.objects, "create")

path = reverse(routes.ENROLLMENT_INDEX)
response = client.post(path, card_tokenize_form_data)

spy.assert_called_once_with(
transit_agency=model_TransitAgency,
enrollment_flow=model_EnrollmentFlow_with_scope_and_claim,
method=models.EnrollmentMethods.DIGITAL,
verified_by=model_EnrollmentFlow_with_scope_and_claim.claims_provider.client_name,
expiration_datetime=None,
)

assert response.status_code == 200
assert response.template_name == model_EnrollmentFlow_does_not_support_expiration.enrollment_success_template
assert response.template_name == model_EnrollmentFlow_with_scope_and_claim.enrollment_success_template
mocked_analytics_module.returned_success.assert_called_once()
assert model_EnrollmentFlow_does_not_support_expiration.group_id in mocked_analytics_module.returned_success.call_args.args
assert model_EnrollmentFlow_with_scope_and_claim.group_id in mocked_analytics_module.returned_success.call_args.args


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_flow", "mocked_session_eligible")
def test_index_eligible_post_valid_form_success_supports_expiration(
def test_index_eligible_post_valid_form_success_eligibility_api(
mocker,
client,
card_tokenize_form_data,
mocked_analytics_module,
model_EnrollmentFlow_supports_expiration,
model_TransitAgency,
model_EnrollmentFlow_with_eligibility_api,
):
mocker.patch("benefits.enrollment.views.enroll", return_value=(Status.SUCCESS, None))
spy = mocker.spy(benefits.enrollment.views.models.EnrollmentEvent.objects, "create")

path = reverse(routes.ENROLLMENT_INDEX)
response = client.post(path, card_tokenize_form_data)

spy.assert_called_once_with(
transit_agency=model_TransitAgency,
enrollment_flow=model_EnrollmentFlow_with_eligibility_api,
method=models.EnrollmentMethods.DIGITAL,
verified_by=model_EnrollmentFlow_with_eligibility_api.eligibility_api_url,
expiration_datetime=None,
)

assert response.status_code == 200
assert response.template_name == model_EnrollmentFlow_supports_expiration.enrollment_success_template
assert response.template_name == model_EnrollmentFlow_with_eligibility_api.enrollment_success_template
mocked_analytics_module.returned_success.assert_called_once()
assert model_EnrollmentFlow_supports_expiration.group_id in mocked_analytics_module.returned_success.call_args.args
assert model_EnrollmentFlow_with_eligibility_api.group_id in mocked_analytics_module.returned_success.call_args.args


@pytest.mark.django_db
Expand Down

0 comments on commit 3ecc9fb

Please sign in to comment.