diff --git a/benefits/in_person/views.py b/benefits/in_person/views.py index 71a2f2bfb..7a222fc3b 100644 --- a/benefits/in_person/views.py +++ b/benefits/in_person/views.py @@ -9,8 +9,7 @@ from benefits.routes import routes -from benefits.core import session -from benefits.core.models import EnrollmentFlow +from benefits.core import models, session from benefits.enrollment.enrollment import Status, request_card_tokenization_access, enroll from benefits.in_person import forms @@ -29,7 +28,7 @@ def eligibility(request): if form.is_valid(): flow_id = form.cleaned_data.get("flow") - flow = EnrollmentFlow.objects.get(id=flow_id) + flow = models.EnrollmentFlow.objects.get(id=flow_id) session.update(request, flow=flow) in_person_enrollment = reverse(routes.IN_PERSON_ENROLLMENT) @@ -79,6 +78,18 @@ def enrollment(request): match (status): case Status.SUCCESS: + agency = session.agency(request) + flow = session.flow(request) + expiry = session.enrollment_expiry(request) + verified_by = f"{request.user.first_name} {request.user.last_name}" + event = models.EnrollmentEvent.objects.create( + transit_agency=agency, + enrollment_flow=flow, + enrollment_method=models.EnrollmentMethods.IN_PERSON, + verified_by=verified_by, + expiration_datetime=expiry, + ) + event.save() return redirect(routes.IN_PERSON_ENROLLMENT_SUCCESS) case Status.SYSTEM_ERROR: diff --git a/tests/pytest/conftest.py b/tests/pytest/conftest.py index a775d2a9f..6be63571e 100644 --- a/tests/pytest/conftest.py +++ b/tests/pytest/conftest.py @@ -1,4 +1,5 @@ from unittest.mock import create_autospec +from django.contrib.auth.models import User from django.contrib.sessions.middleware import SessionMiddleware from django.middleware.locale import LocaleMiddleware from django.utils import timezone @@ -33,6 +34,11 @@ def app_request(rf): return app_request +@pytest.fixture +def model_User(): + return User.objects.create(is_active=True, is_staff=True, first_name="Test", last_name="User") + + # autouse this fixture so we never call out to the real secret store @pytest.fixture(autouse=True) def mock_models_get_secret_by_name(mocker): diff --git a/tests/pytest/in_person/test_views.py b/tests/pytest/in_person/test_views.py index 3871a31e1..0bbcd0f32 100644 --- a/tests/pytest/in_person/test_views.py +++ b/tests/pytest/in_person/test_views.py @@ -6,7 +6,9 @@ from requests import HTTPError +from benefits.core import models from benefits.enrollment.enrollment import Status, CardTokenizationAccessResponse +import benefits.in_person.views from benefits.routes import routes @@ -242,12 +244,27 @@ def test_enrollment_post_invalid_form(admin_client, invalid_form_data): @pytest.mark.django_db @pytest.mark.usefixtures("mocked_session_agency", "mocked_session_flow", "model_EnrollmentFlow") -def test_enrollment_post_valid_form_success(mocker, admin_client, card_tokenize_form_data): +def test_enrollment_post_valid_form_success( + mocker, admin_client, card_tokenize_form_data, model_TransitAgency, model_EnrollmentFlow, model_User +): mocker.patch("benefits.in_person.views.enroll", return_value=(Status.SUCCESS, None)) + spy = mocker.spy(benefits.in_person.views.models.EnrollmentEvent.objects, "create") + + # force the model_User to be the logged in user + # e.g. the TransitAgency staff person assisting this in-person enrollment + admin_client.force_login(model_User) path = reverse(routes.IN_PERSON_ENROLLMENT) response = admin_client.post(path, card_tokenize_form_data) + spy.assert_called_once_with( + transit_agency=model_TransitAgency, + enrollment_flow=model_EnrollmentFlow, + enrollment_method=models.EnrollmentMethods.IN_PERSON, + verified_by=f"{model_User.first_name} {model_User.last_name}", + expiration_datetime=None, + ) + assert response.status_code == 302 assert response.url == reverse(routes.IN_PERSON_ENROLLMENT_SUCCESS) diff --git a/tests/pytest/test_admin.py b/tests/pytest/test_admin.py index 83e0feb10..91e78d27e 100644 --- a/tests/pytest/test_admin.py +++ b/tests/pytest/test_admin.py @@ -1,15 +1,10 @@ import pytest from django.conf import settings -from django.contrib.auth.models import Group, User +from django.contrib.auth.models import Group from django.urls import reverse from benefits.routes import routes -@pytest.fixture -def model_User(): - return User.objects.create(is_active=True, is_staff=True) - - @pytest.fixture def model_AgencyUser(model_User): cst_group = Group.objects.create(name="CST")