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

23 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-13 19:35 +0000

1import logging 

2 

3import sentry_sdk 

4from django.shortcuts import redirect 

5 

6from benefits.core import session 

7from benefits.core.middleware import FlowSessionRequired, user_error 

8from benefits.routes import routes 

9 

10from . import analytics 

11 

12logger = logging.getLogger(__name__) 

13 

14 

15class FlowUsesClaimsVerificationSessionRequired(FlowSessionRequired): 

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

17 

18 def process_request(self, request): 

19 result = super().process_request(request) 

20 if result: 

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

22 return result 

23 

24 flow = session.flow(request) 

25 

26 if flow.uses_claims_verification: 

27 # all good, the chosen flow is configured correctly 

28 return None 

29 elif not (flow.uses_api_verification): 

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

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

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

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

34 sentry_sdk.capture_exception(Exception(message)) 

35 return redirect(routes.OAUTH_SYSTEM_ERROR) 

36 else: 

37 # the chosen flow was for Eligibility API 

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

39 return user_error(request)