From cef38bd35b8f7e64de8eb48cc1520f001d57e876 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Tue, 30 Apr 2024 22:11:54 +0000 Subject: [PATCH] refactor: show system-enrollment-error page if 500 during linking --- .../templates/enrollment/system_error.html | 0 benefits/enrollment/views.py | 10 +++++++ tests/pytest/enrollment/test_views.py | 29 +++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 benefits/enrollment/templates/enrollment/system_error.html diff --git a/benefits/enrollment/templates/enrollment/system_error.html b/benefits/enrollment/templates/enrollment/system_error.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/benefits/enrollment/views.py b/benefits/enrollment/views.py index 6ea2edf290..4c23fa783a 100644 --- a/benefits/enrollment/views.py +++ b/benefits/enrollment/views.py @@ -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__) @@ -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()}") @@ -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): diff --git a/tests/pytest/enrollment/test_views.py b/tests/pytest/enrollment/test_views.py index fba0b7c53f..f853ecbfc6 100644 --- a/tests/pytest/enrollment/test_views.py +++ b/tests/pytest/enrollment/test_views.py @@ -14,6 +14,7 @@ ROUTE_SUCCESS, ROUTE_RETRY, TEMPLATE_SUCCESS, + TEMPLATE_SYSTEM_ERROR, TEMPLATE_RETRY, ) @@ -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