Coverage for benefits/eligibility/views.py: 97%
89 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 18:00 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 18:00 +0000
1"""
2The eligibility application: view definitions for the eligibility verification flow.
3"""
5from django.contrib import messages
6from django.shortcuts import redirect
7from django.template.response import TemplateResponse
8from django.urls import reverse
9from django.utils.decorators import decorator_from_middleware
11from benefits.routes import routes
12from benefits.core import recaptcha, session
13from benefits.core.middleware import AgencySessionRequired, LoginRequired, RecaptchaEnabled, FlowSessionRequired
14from benefits.core.models import EnrollmentFlow
15from . import analytics, forms, verify
17TEMPLATE_CONFIRM = "eligibility/confirm.html"
20@decorator_from_middleware(AgencySessionRequired)
21@decorator_from_middleware(RecaptchaEnabled)
22def index(request):
23 """View handler for the enrollment flow selection form."""
24 agency = session.agency(request)
25 session.update(request, eligible=False, origin=agency.index_url)
27 # clear any prior OAuth token as the user is choosing their desired flow
28 # this may or may not require OAuth, with a different set of scope/claims than what is already stored
29 session.logout(request)
31 context = {"form": forms.EnrollmentFlowSelectionForm(agency=agency)}
33 if request.method == "POST":
34 form = forms.EnrollmentFlowSelectionForm(data=request.POST, agency=agency)
36 if form.is_valid():
37 flow_id = form.cleaned_data.get("flow")
38 flow = EnrollmentFlow.objects.get(id=flow_id)
39 session.update(request, flow=flow)
41 analytics.selected_flow(request, flow)
43 eligibility_start = reverse(routes.ELIGIBILITY_START)
44 response = redirect(eligibility_start)
45 else:
46 # form was not valid, allow for correction/resubmission
47 if recaptcha.has_error(form): 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true
48 messages.error(request, "Recaptcha failed. Please try again.")
49 context["form"] = form
50 response = TemplateResponse(request, agency.eligibility_index_template, context)
51 else:
52 response = TemplateResponse(request, agency.eligibility_index_template, context)
54 return response
57@decorator_from_middleware(AgencySessionRequired)
58@decorator_from_middleware(FlowSessionRequired)
59def start(request):
60 """View handler for the eligibility verification getting started screen."""
61 session.update(request, eligible=False, origin=reverse(routes.ELIGIBILITY_START))
63 flow = session.flow(request)
65 return TemplateResponse(request, flow.eligibility_start_template)
68@decorator_from_middleware(AgencySessionRequired)
69@decorator_from_middleware(LoginRequired)
70@decorator_from_middleware(RecaptchaEnabled)
71@decorator_from_middleware(FlowSessionRequired)
72def confirm(request):
73 """View handler for the eligibility verification form."""
75 # GET from an already verified user, no need to verify again
76 if request.method == "GET" and session.eligible(request):
77 return verified(request)
79 unverified_view = reverse(routes.ELIGIBILITY_UNVERIFIED)
81 agency = session.agency(request)
82 flow = session.flow(request)
84 # GET for OAuth verification
85 if request.method == "GET" and flow.uses_claims_verification:
86 analytics.started_eligibility(request, flow)
88 is_verified = verify.eligibility_from_oauth(flow, session.oauth_claims(request), agency)
90 if is_verified:
91 return verified(request)
92 else:
93 return redirect(unverified_view)
95 form = flow.eligibility_form_instance()
97 # GET/POST for Eligibility API verification
98 context = {"form": form}
100 # GET from an unverified user, present the form
101 if request.method == "GET":
102 session.update(request, origin=reverse(routes.ELIGIBILITY_CONFIRM))
103 return TemplateResponse(request, TEMPLATE_CONFIRM, context)
104 # POST form submission, process form data, make Eligibility Verification API call
105 elif request.method == "POST": 105 ↛ exitline 105 didn't return from function 'confirm' because the condition on line 105 was always true
106 analytics.started_eligibility(request, flow)
108 form = flow.eligibility_form_instance(data=request.POST)
109 # form was not valid, allow for correction/resubmission
110 if not form.is_valid():
111 if recaptcha.has_error(form):
112 messages.error(request, "Recaptcha failed. Please try again.")
113 context["form"] = form
114 return TemplateResponse(request, TEMPLATE_CONFIRM, context)
116 # form is valid, make Eligibility Verification request to get the verified confirmation
117 is_verified = verify.eligibility_from_api(flow, form, agency)
119 # form was not valid, allow for correction/resubmission
120 if is_verified is None:
121 analytics.returned_error(request, flow, form.errors)
122 context["form"] = form
123 return TemplateResponse(request, TEMPLATE_CONFIRM, context)
124 # no type was verified
125 elif not is_verified:
126 return redirect(unverified_view)
127 # type was verified
128 else:
129 return verified(request)
132@decorator_from_middleware(AgencySessionRequired)
133@decorator_from_middleware(LoginRequired)
134def verified(request):
135 """View handler for the verified eligibility page."""
137 flow = session.flow(request)
138 analytics.returned_success(request, flow)
140 session.update(request, eligible=True)
142 return redirect(routes.ENROLLMENT_INDEX)
145@decorator_from_middleware(AgencySessionRequired)
146@decorator_from_middleware(FlowSessionRequired)
147def unverified(request):
148 """View handler for the unverified eligibility page."""
150 flow = session.flow(request)
152 analytics.returned_fail(request, flow)
154 return TemplateResponse(request, flow.eligibility_unverified_template)