Skip to content

Commit

Permalink
refactor: show system-enrollment-error page if 500 during linking
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed Apr 30, 2024
1 parent fecc1cc commit cef38bd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Empty file.
10 changes: 10 additions & 0 deletions benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

TEMPLATE_RETRY = "enrollment/retry.html"
TEMPLATE_SUCCESS = "enrollment/success.html"
TEMPLATE_SYSTEM_ERROR = "enrollment/system_error.html"


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -91,6 +92,9 @@ def index(request):
if e.response.status_code == 409:
analytics.returned_success(request, eligibility.group_id)
return success(request)
elif e.response.status_code >= 500:
analytics.returned_error(request, str(e))
return system_error(request)
else:
analytics.returned_error(request, str(e))
raise Exception(f"{e}: {e.response.json()}")
Expand Down Expand Up @@ -129,6 +133,12 @@ def retry(request):
return TemplateResponse(request, TEMPLATE_RETRY)


@decorator_from_middleware(EligibleSessionRequired)
def system_error(request):
"""View handler for an enrollment system error."""
return TemplateResponse(request, TEMPLATE_SYSTEM_ERROR)


@pageview_decorator
@decorator_from_middleware(VerifierSessionRequired)
def success(request):
Expand Down
29 changes: 27 additions & 2 deletions tests/pytest/enrollment/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ROUTE_SUCCESS,
ROUTE_RETRY,
TEMPLATE_SUCCESS,
TEMPLATE_SYSTEM_ERROR,
TEMPLATE_RETRY,
)

Expand Down Expand Up @@ -126,12 +127,36 @@ def test_index_eligible_post_invalid_form(client, invalid_form_data):


@pytest.mark.django_db
@pytest.mark.parametrize("status_code", [500, 501, 502, 503, 504])
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_eligibility")
def test_index_eligible_post_valid_form_http_error(mocker, client, card_tokenize_form_data):
def test_index_eligible_post_valid_form_http_error_500(
mocker, client, mocked_analytics_module, card_tokenize_form_data, status_code
):
mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
mock_client = mock_client_cls.return_value

mock_error = {"message": "Mock error message"}
mock_error_response = mocker.Mock(status_code=status_code, **mock_error)
mock_error_response.json.return_value = mock_error
mock_client.link_concession_group_funding_source.side_effect = HTTPError(
response=mock_error_response,
)

path = reverse(ROUTE_INDEX)
response = client.post(path, card_tokenize_form_data)

assert response.status_code == 200
assert response.template_name == TEMPLATE_SYSTEM_ERROR
mocked_analytics_module.returned_error.assert_called_once()


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_eligibility")
def test_index_eligible_post_valid_form_http_error_400(mocker, client, card_tokenize_form_data):
mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
mock_client = mock_client_cls.return_value

# any status_code that isn't 409 is considered an error
# any 400 level status_code that isn't 409 is considered an error
mock_error = {"message": "Mock error message"}
mock_error_response = mocker.Mock(status_code=400, **mock_error)
mock_error_response.json.return_value = mock_error
Expand Down

0 comments on commit cef38bd

Please sign in to comment.