Skip to content

Commit

Permalink
refactor(enrollment): look up the funding source's groups
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed Jun 18, 2024
1 parent 5057968 commit 5b90082
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
30 changes: 15 additions & 15 deletions benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ def index(request):
group_id = eligibility.group_id

try:
group_funding_source = _get_group_funding_source(
funding_source_group = _get_funding_source_group(
client=client, group_id=group_id, funding_source_id=funding_source.id
)

already_enrolled = group_funding_source is not None
already_enrolled = funding_source_group is not None

if eligibility.supports_expiration:
# set expiry on session
if already_enrolled and group_funding_source.expiry_date is not None:
session.update(request, enrollment_expiry=group_funding_source.expiry_date)
if already_enrolled and funding_source_group.expiry_date is not None:
session.update(request, enrollment_expiry=funding_source_group.expiry_date)
else:
session.update(request, enrollment_expiry=_calculate_expiry(eligibility.expiration_days))

Expand All @@ -128,7 +128,7 @@ def index(request):
)
return success(request)
else: # already_enrolled
if group_funding_source.expiry_date is None:
if funding_source_group.expiry_date is None:
# update expiration of existing enrollment, return success
client.update_concession_group_funding_source_expiry(
group_id=group_id,
Expand All @@ -137,9 +137,9 @@ def index(request):
)
return success(request)
else:
is_expired = _is_expired(group_funding_source.expiry_date)
is_expired = _is_expired(funding_source_group.expiry_date)
is_within_reenrollment_window = _is_within_reenrollment_window(
group_funding_source.expiry_date, session.enrollment_reenrollment(request)
funding_source_group.expiry_date, session.enrollment_reenrollment(request)
)

if is_expired or is_within_reenrollment_window:
Expand All @@ -159,7 +159,7 @@ def index(request):
client.link_concession_group_funding_source(group_id=group_id, funding_source_id=funding_source.id)
return success(request)
else: # already_enrolled
if group_funding_source.expiry_date is None:
if funding_source_group.expiry_date is None:
# no action, return success
return success(request)
else:
Expand Down Expand Up @@ -204,15 +204,15 @@ def index(request):
return TemplateResponse(request, eligibility.enrollment_index_template, context)


def _get_group_funding_source(client: Client, group_id, funding_source_id):
group_funding_sources = client.get_concession_group_linked_funding_sources(group_id)
matching_group_funding_source = None
for group_funding_source in group_funding_sources:
if group_funding_source.id == funding_source_id:
matching_group_funding_source = group_funding_source
def _get_funding_source_group(client: Client, group_id, funding_source_id):
funding_source_groups = client.get_funding_source_linked_concession_groups(funding_source_id)
matching_funding_source_group = None
for funding_source_group in funding_source_groups:
if funding_source_group.group_id == group_id:
matching_funding_source_group = funding_source_group
break

return matching_group_funding_source
return matching_funding_source_group


def _is_expired(expiry_date):
Expand Down
57 changes: 30 additions & 27 deletions tests/pytest/enrollment/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from django.urls import reverse
from django.utils import timezone

from littlepay.api.funding_sources import FundingSourceResponse
from littlepay.api.groups import GroupFundingSourceResponse
from littlepay.api.funding_sources import FundingSourceResponse, FundingSourceGroupResponse
from requests import HTTPError

import benefits.enrollment.views
Expand All @@ -23,7 +22,7 @@
ROUTE_TOKEN,
TEMPLATE_SYSTEM_ERROR,
TEMPLATE_RETRY,
_get_group_funding_source,
_get_funding_source_group,
_calculate_expiry,
_is_expired,
_is_within_reenrollment_window,
Expand Down Expand Up @@ -67,19 +66,23 @@ def mocked_funding_source():


@pytest.fixture
def mocked_group_funding_source_no_expiry(mocked_funding_source):
return GroupFundingSourceResponse(
def mocked_funding_source_group_no_expiry(mocked_funding_source):
return FundingSourceGroupResponse(
id=mocked_funding_source.id,
group_id="group123",
label="Group 123",
created_date=None,
updated_date=None,
expiry_date=None,
)


@pytest.fixture
def mocked_group_funding_source_with_expiry(mocked_funding_source):
return GroupFundingSourceResponse(
def mocked_funding_source_group_with_expiry(mocked_funding_source):
return FundingSourceGroupResponse(
id=mocked_funding_source.id,
group_id="group123",
label="Group 123",
created_date="2023-01-01T00:00:00Z",
updated_date="2021-01-01T00:00:00Z",
expiry_date="2021-01-01T00:00:00Z",
Expand Down Expand Up @@ -330,26 +333,26 @@ def test_index_eligible_post_valid_form_failure(mocker, client, card_tokenize_fo

@pytest.mark.django_db
@pytest.mark.usefixtures("model_EligibilityType")
def test_get_group_funding_sources_funding_source_not_enrolled_yet(mocker, mocked_funding_source):
def test_get_funding_source_groups_funding_source_not_enrolled_yet(mocker, mocked_funding_source):
mock_client = mocker.Mock()
mock_client.get_concession_group_linked_funding_sources.return_value = []
mock_client.get_funding_source_linked_concession_groups.return_value = []

matching_group_funding_source = _get_group_funding_source(mock_client, "group123", mocked_funding_source.id)
matching_group_funding_source = _get_funding_source_group(mock_client, "group123", mocked_funding_source.id)

assert matching_group_funding_source is None


@pytest.mark.django_db
@pytest.mark.usefixtures("model_EligibilityType")
def test_get_group_funding_sources_funding_source_already_enrolled(
mocker, mocked_funding_source, mocked_group_funding_source_no_expiry
def test_get_funding_source_groups_funding_source_already_enrolled(
mocker, mocked_funding_source, mocked_funding_source_group_no_expiry
):
mock_client = mocker.Mock()
mock_client.get_concession_group_linked_funding_sources.return_value = [mocked_group_funding_source_no_expiry]
mock_client.get_funding_source_linked_concession_groups.return_value = [mocked_funding_source_group_no_expiry]

matching_group_funding_source = _get_group_funding_source(mock_client, "group123", mocked_funding_source.id)
matching_group_funding_source = _get_funding_source_group(mock_client, "group123", mocked_funding_source.id)

assert matching_group_funding_source == mocked_group_funding_source_no_expiry
assert matching_group_funding_source == mocked_funding_source_group_no_expiry


@pytest.mark.django_db
Expand All @@ -361,13 +364,13 @@ def test_index_eligible_post_valid_form_success_does_not_support_expiration_cust
mocked_analytics_module,
model_EligibilityType_does_not_support_expiration,
mocked_funding_source,
mocked_group_funding_source_no_expiry,
mocked_funding_source_group_no_expiry,
):
mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
mock_client = mock_client_cls.return_value
mock_client.get_funding_source_by_token.return_value = mocked_funding_source

mocker.patch("benefits.enrollment.views._get_group_funding_source", return_value=mocked_group_funding_source_no_expiry)
mocker.patch("benefits.enrollment.views._get_funding_source_group", return_value=mocked_funding_source_group_no_expiry)

path = reverse(ROUTE_INDEX)
response = client.post(path, card_tokenize_form_data)
Expand Down Expand Up @@ -472,14 +475,14 @@ def test_index_eligible_post_valid_form_success_supports_expiration_no_expiry(
mocked_analytics_module,
model_EligibilityType_supports_expiration,
mocked_funding_source,
mocked_group_funding_source_no_expiry,
mocked_funding_source_group_no_expiry,
mocked_session_enrollment_expiry,
):
mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
mock_client = mock_client_cls.return_value
mock_client.get_funding_source_by_token.return_value = mocked_funding_source

mocker.patch("benefits.enrollment.views._get_group_funding_source", return_value=mocked_group_funding_source_no_expiry)
mocker.patch("benefits.enrollment.views._get_funding_source_group", return_value=mocked_funding_source_group_no_expiry)

path = reverse(ROUTE_INDEX)
response = client.post(path, card_tokenize_form_data)
Expand Down Expand Up @@ -540,15 +543,15 @@ def test_index_eligible_post_valid_form_success_supports_expiration_is_expired(
mocked_analytics_module,
model_EligibilityType_supports_expiration,
mocked_funding_source,
mocked_group_funding_source_with_expiry,
mocked_funding_source_group_with_expiry,
mocked_session_enrollment_expiry,
):
mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
mock_client = mock_client_cls.return_value
mock_client.get_funding_source_by_token.return_value = mocked_funding_source

# mock that a funding source already exists, doesn't matter what expiry_date is
mocker.patch("benefits.enrollment.views._get_group_funding_source", return_value=mocked_group_funding_source_with_expiry)
mocker.patch("benefits.enrollment.views._get_funding_source_group", return_value=mocked_funding_source_group_with_expiry)

mocker.patch("benefits.enrollment.views._is_expired", return_value=True)

Expand Down Expand Up @@ -650,15 +653,15 @@ def test_index_eligible_post_valid_form_success_supports_expiration_is_within_re
mocked_analytics_module,
model_EligibilityType_supports_expiration,
mocked_funding_source,
mocked_group_funding_source_with_expiry,
mocked_funding_source_group_with_expiry,
mocked_session_enrollment_expiry,
):
mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
mock_client = mock_client_cls.return_value
mock_client.get_funding_source_by_token.return_value = mocked_funding_source

# mock that a funding source already exists, doesn't matter what expiry_date is
mocker.patch("benefits.enrollment.views._get_group_funding_source", return_value=mocked_group_funding_source_with_expiry)
mocker.patch("benefits.enrollment.views._get_funding_source_group", return_value=mocked_funding_source_group_with_expiry)

mocker.patch("benefits.enrollment.views._is_within_reenrollment_window", return_value=True)

Expand All @@ -684,15 +687,15 @@ def test_index_eligible_post_valid_form_success_supports_expiration_is_not_expir
card_tokenize_form_data,
mocked_analytics_module,
mocked_funding_source,
mocked_group_funding_source_with_expiry,
mocked_funding_source_group_with_expiry,
model_EligibilityType_supports_expiration,
):
mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
mock_client = mock_client_cls.return_value
mock_client.get_funding_source_by_token.return_value = mocked_funding_source

# mock that a funding source already exists, doesn't matter what expiry_date is
mocker.patch("benefits.enrollment.views._get_group_funding_source", return_value=mocked_group_funding_source_with_expiry)
mocker.patch("benefits.enrollment.views._get_funding_source_group", return_value=mocked_funding_source_group_with_expiry)

mocker.patch("benefits.enrollment.views._is_expired", return_value=False)
mocker.patch("benefits.enrollment.views._is_within_reenrollment_window", return_value=False)
Expand All @@ -714,14 +717,14 @@ def test_index_eligible_post_valid_form_success_does_not_support_expiration_has_
mocked_analytics_module,
model_EligibilityType_does_not_support_expiration,
mocked_funding_source,
mocked_group_funding_source_with_expiry,
mocked_funding_source_group_with_expiry,
):
mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
mock_client = mock_client_cls.return_value
mock_client.get_funding_source_by_token.return_value = mocked_funding_source

# mock that a funding source already exists, doesn't matter what expiry_date is
mocker.patch("benefits.enrollment.views._get_group_funding_source", return_value=mocked_group_funding_source_with_expiry)
mocker.patch("benefits.enrollment.views._get_funding_source_group", return_value=mocked_funding_source_group_with_expiry)

path = reverse(ROUTE_INDEX)
with pytest.raises(NotImplementedError):
Expand Down

0 comments on commit 5b90082

Please sign in to comment.