Skip to content

Commit

Permalink
refactor: show server error page if 400 or other exception during /token
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed May 22, 2024
1 parent 3769a00 commit c184b6c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions benefits/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ def to_url(self, agency):
path("<agency:agency>", views.agency_index, name="agency_index"),
path("<agency:agency>/publickey", views.agency_public_key, name="agency_public_key"),
path("logged_out", views.logged_out, name="logged_out"),
path("error", views.server_error, name="server-error"),
]
26 changes: 21 additions & 5 deletions benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ROUTE_INDEX = "enrollment:index"
ROUTE_REENROLLMENT_ERROR = "enrollment:reenrollment-error"
ROUTE_RETRY = "enrollment:retry"
ROUTE_SERVER_ERROR = "core:server-error"
ROUTE_SUCCESS = "enrollment:success"
ROUTE_SYSTEM_ERROR = "enrollment:system-error"
ROUTE_TOKEN = "enrollment:token"
Expand All @@ -47,16 +48,31 @@ def token(request):
client.oauth.ensure_active_token(client.token)

try:

class response:
def __init__(self):
self.status_code = 400

raise HTTPError(response=response())
response = client.request_card_tokenization_access()
except Exception as e:
sentry_sdk.capture_exception(e)

if isinstance(e, HTTPError) and e.response.status_code >= 500:
analytics.failed_access_token_request(request, e.response.status_code)
data = {"redirect": reverse(ROUTE_SYSTEM_ERROR)}
return JsonResponse(data)
if isinstance(e, HTTPError):
status_code = e.response.status_code

if e.response.status_code >= 500:
redirect = reverse(ROUTE_SYSTEM_ERROR)
else:
redirect = reverse(ROUTE_SERVER_ERROR)
else:
raise e
status_code = None
redirect = reverse(ROUTE_SERVER_ERROR)

analytics.failed_access_token_request(request, status_code)

data = {"redirect": redirect}
return JsonResponse(data)
else:
session.update(
request, enrollment_token=response.get("access_token"), enrollment_token_exp=response.get("expires_at")
Expand Down
29 changes: 29 additions & 0 deletions tests/pytest/enrollment/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ROUTE_INDEX,
ROUTE_REENROLLMENT_ERROR,
ROUTE_RETRY,
ROUTE_SERVER_ERROR,
ROUTE_SUCCESS,
ROUTE_SYSTEM_ERROR,
ROUTE_TOKEN,
Expand Down Expand Up @@ -131,6 +132,34 @@ def test_token_http_error_500(mocker, client, mocked_analytics_module, mocked_se
mocked_sentry_sdk_module.capture_exception.assert_called_once()


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_eligibility")
def test_token_http_error_400(mocker, client, mocked_analytics_module, mocked_sentry_sdk_module):
mocker.patch("benefits.core.session.enrollment_token_valid", return_value=False)

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=400, **mock_error)
mock_error_response.json.return_value = mock_error
mock_client.request_card_tokenization_access.side_effect = HTTPError(
response=mock_error_response,
)

path = reverse(ROUTE_TOKEN)
response = client.get(path)

assert response.status_code == 200
data = response.json()
assert "token" not in data
assert "redirect" in data
assert data["redirect"] == reverse(ROUTE_SERVER_ERROR)
mocked_analytics_module.failed_access_token_request.assert_called_once()
assert 400 in mocked_analytics_module.failed_access_token_request.call_args.args
mocked_sentry_sdk_module.capture_exception.assert_called_once()


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_verifier", "mocked_session_eligibility")
def test_index_eligible_get(client, model_EligibilityType):
Expand Down

0 comments on commit c184b6c

Please sign in to comment.