Skip to content

Commit

Permalink
feat(enrollment): decorate relevant views with error handler middleware
Browse files Browse the repository at this point in the history
update tests now that exceptions are handled
  • Loading branch information
thekaveman committed Mar 7, 2024
1 parent 9fb192a commit eb1015e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
6 changes: 4 additions & 2 deletions benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
pageview_decorator,
)
from benefits.core.views import ROUTE_LOGGED_OUT
from benefits.enrollment.middleware import TEMPLATE_RETRY
from benefits.enrollment.middleware import TEMPLATE_RETRY, HandleEnrollmentError
from . import analytics, forms


Expand All @@ -35,6 +35,7 @@


@decorator_from_middleware(EligibleSessionRequired)
@decorator_from_middleware(HandleEnrollmentError)
def token(request):
"""View handler for the enrollment auth token."""
if not session.enrollment_token_valid(request):
Expand All @@ -56,6 +57,7 @@ def token(request):


@decorator_from_middleware(EligibleSessionRequired)
@decorator_from_middleware(HandleEnrollmentError)
def index(request):
"""View handler for the enrollment landing page."""
session.update(request, origin=reverse(ROUTE_INDEX))
Expand Down Expand Up @@ -126,6 +128,7 @@ def index(request):


@decorator_from_middleware(EligibleSessionRequired)
@decorator_from_middleware(HandleEnrollmentError)
def retry(request):
"""View handler for a recoverable failure condition."""
if request.method == "POST":
Expand All @@ -137,7 +140,6 @@ def retry(request):
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.")


Expand Down
34 changes: 22 additions & 12 deletions tests/pytest/enrollment/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ def test_index_eligible_get(client):
def test_index_eligible_post_invalid_form(client, invalid_form_data):
path = reverse(ROUTE_INDEX)

with pytest.raises(Exception, match=r"form"):
client.post(path, invalid_form_data)
response = client.post(path, invalid_form_data)

assert response.status_code == 200
assert response.template_name == TEMPLATE_RETRY


@pytest.mark.django_db
Expand All @@ -141,8 +143,10 @@ def test_index_eligible_post_valid_form_http_error(mocker, client, card_tokenize
)

path = reverse(ROUTE_INDEX)
with pytest.raises(Exception, match=mock_error["message"]):
client.post(path, card_tokenize_form_data)
response = client.post(path, card_tokenize_form_data)

assert response.status_code == 200
assert response.template_name == TEMPLATE_RETRY


@pytest.mark.django_db
Expand All @@ -154,8 +158,10 @@ def test_index_eligible_post_valid_form_failure(mocker, client, card_tokenize_fo
mock_client.link_concession_group_funding_source.side_effect = Exception("some other exception")

path = reverse(ROUTE_INDEX)
with pytest.raises(Exception, match=r"some other exception"):
client.post(path, card_tokenize_form_data)
response = client.post(path, card_tokenize_form_data)

assert response.status_code == 200
assert response.template_name == TEMPLATE_RETRY


@pytest.mark.django_db
Expand Down Expand Up @@ -223,21 +229,25 @@ def test_retry_ineligible(client):


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

assert response.status_code == 200
assert response.template_name == TEMPLATE_RETRY


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

path = reverse(ROUTE_RETRY)
with pytest.raises(Exception, match=r"Invalid"):
client.post(path)
response = client.get(path)

assert response.status_code == 200
assert response.template_name == TEMPLATE_RETRY


@pytest.mark.django_db
Expand Down

0 comments on commit eb1015e

Please sign in to comment.