Coverage for benefits/oauth/middleware.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-21 19:31 +0000

1import logging 

2 

3from django.shortcuts import redirect 

4import sentry_sdk 

5 

6from benefits.routes import routes 

7from benefits.core import session 

8from benefits.core.middleware import FlowSessionRequired, user_error 

9 

10from . import analytics 

11 

12 

13logger = logging.getLogger(__name__) 

14 

15 

16class FlowUsesClaimsVerificationSessionRequired(FlowSessionRequired): 

17 """Middleware raises an exception for sessions lacking an enrollment flow that uses claims verification.""" 

18 

19 def process_request(self, request): 

20 result = super().process_request(request) 

21 if result: 

22 # from the base middleware class, the session didn't have an enrollment flow 

23 return result 

24 

25 flow = session.flow(request) 

26 

27 if flow.uses_claims_verification: 

28 # all good, the chosen flow is configured correctly 

29 return None 

30 elif not (flow.eligibility_api_url or flow.eligibility_form_class): 

31 # the chosen flow doesn't have Eligibility API config OR claims provider config 

32 # this is likely a misconfiguration on the backend, not a user error 

33 message = f"Flow with no API or claims config: {flow.system_name} (id={flow.id})" 

34 analytics.error(request, message=message, operation=request.path) 

35 sentry_sdk.capture_exception(Exception(message)) 

36 return redirect(routes.OAUTH_SYSTEM_ERROR) 

37 else: 

38 # the chosen flow was for Eligibility API 

39 logger.debug("Session not configured with enrollment flow that uses claims verification") 

40 return user_error(request)