Skip to content

Commit

Permalink
refactor(enrollment): retry responds to GET and POST
Browse files Browse the repository at this point in the history
this change simplifies thee view logic and updates the corresponding tests

throwing an error on GET is unnecessary -- the behavior was a kind of an
assertion on how the user got to the /retry route, but in fact it doesn't
really matter if they made a GET or POST. Supporting GET makes testing
the route easier too.
  • Loading branch information
thekaveman committed Apr 18, 2024
1 parent 215ca88 commit 159a831
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
13 changes: 2 additions & 11 deletions benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,8 @@ def reenrollment_error(request):
@decorator_from_middleware(EligibleSessionRequired)
def retry(request):
"""View handler for a recoverable failure condition."""
if request.method == "POST":
analytics.returned_retry(request)
form = forms.CardTokenizeFailForm(request.POST)
if form.is_valid():
return TemplateResponse(request, TEMPLATE_RETRY)
else:
analytics.returned_error(request, "Invalid retry submission.")
raise Exception("Invalid retry submission.")
else:
analytics.returned_error(request, "This view method only supports POST.")
raise Exception("This view method only supports POST.")
analytics.returned_retry(request)
return TemplateResponse(request, TEMPLATE_RETRY)


@pageview_decorator
Expand Down
23 changes: 7 additions & 16 deletions tests/pytest/enrollment/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,28 +623,19 @@ def test_retry_ineligible(client):


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_eligibility")
def test_retry_get(client):
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_eligibility")
def test_retry_get(client, mocked_analytics_module):
path = reverse(ROUTE_RETRY)
with pytest.raises(Exception, match=r"POST"):
client.get(path)


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_eligibility")
def test_retry_invalid_form(mocker, client):
mocker.patch("benefits.enrollment.views.forms.CardTokenizeFailForm.is_valid", return_value=False)
response = client.get(path)

path = reverse(ROUTE_RETRY)
with pytest.raises(Exception, match=r"Invalid"):
client.post(path)
assert response.status_code == 200
assert response.template_name == TEMPLATE_RETRY
mocked_analytics_module.returned_retry.assert_called_once()


@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_eligibility")
def test_retry_valid_form(mocker, client, mocked_analytics_module):
mocker.patch("benefits.enrollment.views.forms.CardTokenizeFailForm.is_valid", return_value=True)

def test_retry_valid_form(client, mocked_analytics_module):
path = reverse(ROUTE_RETRY)
response = client.post(path)

Expand Down

0 comments on commit 159a831

Please sign in to comment.