Coverage for benefits/eligibility/verify.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-19 16:31 +0000

1from django.conf import settings 

2 

3from eligibility_api.client import Client 

4 

5from benefits.core import models 

6 

7 

8def eligibility_from_api(flow: models.EnrollmentFlow, form, agency: models.TransitAgency): 

9 sub, name = form.cleaned_data.get("sub"), form.cleaned_data.get("name") 

10 

11 client = Client( 

12 verify_url=flow.eligibility_api_url, 

13 headers={flow.eligibility_api_auth_header: flow.eligibility_api_auth_key}, 

14 issuer=settings.ALLOWED_HOSTS[0], 

15 agency=agency.eligibility_api_id, 

16 jws_signing_alg=agency.eligibility_api_jws_signing_alg, 

17 client_private_key=agency.eligibility_api_private_key_data, 

18 jwe_encryption_alg=flow.eligibility_api_jwe_encryption_alg, 

19 jwe_cek_enc=flow.eligibility_api_jwe_cek_enc, 

20 server_public_key=flow.eligibility_api_public_key_data, 

21 timeout=settings.REQUESTS_TIMEOUT, 

22 ) 

23 

24 response = client.verify(sub, name, [flow.system_name]) 

25 

26 if response.error and any(response.error): 

27 return None 

28 # response.eligibility is a single item list containing the type of eligibility we are trying to verify, e.g. ["senior"] 

29 elif flow.system_name in response.eligibility: 

30 return True 

31 else: 

32 return False 

33 

34 

35def eligibility_from_oauth(flow: models.EnrollmentFlow, oauth_claims, agency: models.TransitAgency): 

36 if flow.uses_claims_verification and flow.claims_eligibility_claim in oauth_claims: 

37 return True 

38 else: 

39 return False