Coverage for benefits/enrollment_init/enrollment.py: 85%

53 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-23 20:53 +0000

1from dataclasses import asdict 

2 

3from requests import HTTPError 

4 

5from benefits.core import session 

6from benefits.core.models.enrollment import EnrollmentFlow 

7from benefits.enrollment.enrollment import Status, resolve_enrollment_decision 

8from benefits.enrollment_init.models import InitConfig, InitGroup 

9 

10from .api import CardholderResponse, CardholderUpdateModel, Client, TransitAccountResponse 

11 

12 

13def enroll(request, init_config: InitConfig, flow: EnrollmentFlow, group: InitGroup, token: str): 

14 client = Client( 

15 init_config.registration_api_base_url, 

16 init_config.registration_api_username, 

17 init_config.registration_api_password, 

18 ) 

19 

20 exception = None 

21 try: 

22 transit_account = _get_transit_account(client, token) 

23 cardholder = _get_cardholder(client, transit_account) 

24 already_enrolled = _is_already_enrolled(cardholder, group) 

25 existing_expiry = cardholder.FareCategoryExpiry if already_enrolled else None 

26 

27 decision = resolve_enrollment_decision(flow, already_enrolled, existing_expiry) 

28 status = decision.status 

29 

30 if decision.expiry_to_store is not None: 30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true

31 session.update(request, enrollment_expiry=decision.expiry_to_store) 

32 

33 if status is Status.SUCCESS: 33 ↛ 63line 33 didn't jump to line 63 because the condition on line 33 was always true

34 if decision.should_remove_expiry: 34 ↛ 36line 34 didn't jump to line 36 because the condition on line 34 was never true

35 # TODO: put_cardholder with empty expiry 

36 raise NotImplementedError() 

37 elif decision.should_enroll: 

38 if not already_enrolled: 38 ↛ 51line 38 didn't jump to line 51 because the condition on line 38 was always true

39 if decision.expiry_to_send is None: 39 ↛ 48line 39 didn't jump to line 48 because the condition on line 39 was always true

40 if cardholder is None: 

41 client.post_cardholder(fare_category=group.group_id, transit_account_id=transit_account.Id) 

42 else: 

43 cardholder_update_model = CardholderUpdateModel.from_kwargs(**asdict(cardholder)) 

44 cardholder_update_model.FareCategory = group.group_id # overwrite existing FareCategory 

45 client.put_cardholder(cardholder_id=cardholder.Id, cardholder_update_model=cardholder_update_model) 

46 else: 

47 # TODO: post_cardholder with expiry_to_send 

48 raise NotImplementedError() 

49 else: 

50 # TODO: put_cardholder with expiry_to_send 

51 raise NotImplementedError() 

52 except HTTPError as e: 

53 if e.response.status_code >= 500: 

54 status = Status.SYSTEM_ERROR 

55 exception = e 

56 else: 

57 status = Status.EXCEPTION 

58 exception = Exception(f"{e}: {e.response.text}") 

59 except Exception as e: 

60 status = Status.EXCEPTION 

61 exception = e 

62 

63 return status, exception 

64 

65 

66def _get_transit_account(client: Client, token: str): 

67 return client.get_transit_account(token) 

68 

69 

70def _get_cardholder(client: Client, transit_account: TransitAccountResponse): 

71 if transit_account and transit_account.CardholderId: 71 ↛ 74line 71 didn't jump to line 74 because the condition on line 71 was always true

72 cardholder = client.get_cardholder(transit_account.CardholderId) 

73 else: 

74 cardholder = None 

75 

76 return cardholder 

77 

78 

79def _is_already_enrolled(cardholder: CardholderResponse, group: InitGroup): 

80 """ 

81 A token is considered "already enrolled" with a `group` if its TransitAccount has a Cardholder whose FareCategory ID 

82 matches the InitGroup ID. 

83 

84 Otherwise, the token will be considered as not enrolled yet and should be enrolled into the FareCategory 

85 for the given `group`. 

86 """ 

87 if cardholder: 

88 return cardholder.FareCategory == group.group_id 

89 else: 

90 return False