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

1""" 

2The eligibility application: view definitions for the eligibility verification flow. 

3""" 

4 

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 

10 

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 

16 

17TEMPLATE_CONFIRM = "eligibility/confirm.html" 

18 

19 

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) 

26 

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) 

30 

31 context = {"form": forms.EnrollmentFlowSelectionForm(agency=agency)} 

32 

33 if request.method == "POST": 

34 form = forms.EnrollmentFlowSelectionForm(data=request.POST, agency=agency) 

35 

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) 

40 

41 analytics.selected_flow(request, flow) 

42 

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) 

53 

54 return response 

55 

56 

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)) 

62 

63 flow = session.flow(request) 

64 

65 return TemplateResponse(request, flow.eligibility_start_template) 

66 

67 

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.""" 

74 

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) 

78 

79 unverified_view = reverse(routes.ELIGIBILITY_UNVERIFIED) 

80 

81 agency = session.agency(request) 

82 flow = session.flow(request) 

83 

84 # GET for OAuth verification 

85 if request.method == "GET" and flow.uses_claims_verification: 

86 analytics.started_eligibility(request, flow) 

87 

88 is_verified = verify.eligibility_from_oauth(flow, session.oauth_claims(request), agency) 

89 

90 if is_verified: 

91 return verified(request) 

92 else: 

93 return redirect(unverified_view) 

94 

95 form = flow.eligibility_form_instance() 

96 

97 # GET/POST for Eligibility API verification 

98 context = {"form": form} 

99 

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) 

107 

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) 

115 

116 # form is valid, make Eligibility Verification request to get the verified confirmation 

117 is_verified = verify.eligibility_from_api(flow, form, agency) 

118 

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) 

130 

131 

132@decorator_from_middleware(AgencySessionRequired) 

133@decorator_from_middleware(LoginRequired) 

134def verified(request): 

135 """View handler for the verified eligibility page.""" 

136 

137 flow = session.flow(request) 

138 analytics.returned_success(request, flow) 

139 

140 session.update(request, eligible=True) 

141 

142 return redirect(routes.ENROLLMENT_INDEX) 

143 

144 

145@decorator_from_middleware(AgencySessionRequired) 

146@decorator_from_middleware(FlowSessionRequired) 

147def unverified(request): 

148 """View handler for the unverified eligibility page.""" 

149 

150 flow = session.flow(request) 

151 

152 analytics.returned_fail(request, flow) 

153 

154 return TemplateResponse(request, flow.eligibility_unverified_template)