Skip to content

Commit

Permalink
feat(enrollment): EnrollmentEvent for in-person success
Browse files Browse the repository at this point in the history
moved the model_User test fixture so it could be re-used to confirm
the EnrollmentEvent.verified_by field for in-person
  • Loading branch information
thekaveman committed Sep 18, 2024
1 parent 4f53558 commit 074971d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
17 changes: 14 additions & 3 deletions benefits/in_person/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions tests/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
19 changes: 18 additions & 1 deletion tests/pytest/in_person/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)

Expand Down
7 changes: 1 addition & 6 deletions tests/pytest/test_admin.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down

0 comments on commit 074971d

Please sign in to comment.