Coverage for benefits/enrollment_init/models.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-08 19:50 +0000

1import logging 

2 

3from django.core.exceptions import ValidationError 

4from django.db import models 

5 

6from benefits.core.models import EnrollmentGroup, SecretNameField, TransitProcessorConfig 

7 

8logger = logging.getLogger(__name__) 

9 

10 

11class InitConfig(TransitProcessorConfig): 

12 """Configuration for connecting to INIT, an entity that applies transit agency fare rules to rider transactions.""" 

13 

14 tokenization_api_key = models.CharField(help_text="The Collect.js API key used for tokenization.", default="", blank=True) 

15 

16 registration_api_base_url = models.URLField( 

17 help_text="The absolute base url of the MOBILEvario API instance.", default="", blank=True 

18 ) 

19 

20 registration_api_username = models.CharField( 

21 help_text="The username used to authenticate with MOBILEvario.", 

22 default="", 

23 blank=True, 

24 max_length=50, 

25 ) 

26 

27 registration_api_password_secret_name = SecretNameField( 

28 help_text="The name of the secret containing the password used to authenticate with MOBILEvario, typically: [agency]-init-registration-api-password", # noqa: E501 

29 default="", 

30 blank=True, 

31 ) 

32 

33 @property 

34 def registration_api_password(self): 

35 secret_field = self._meta.get_field("registration_api_password_secret_name") 

36 return secret_field.secret_value(self) 

37 

38 def clean(self): 

39 field_errors = {} 

40 

41 if self.pk and self.transitagency_set and any([agency.active for agency in self.transitagency_set.all()]): 

42 

43 message = "This field is required when this configuration is referenced by an active transit agency." 

44 needed = dict( 

45 tokenization_api_key=self.tokenization_api_key, 

46 registration_api_base_url=self.registration_api_base_url, 

47 registration_api_username=self.registration_api_username, 

48 registration_api_password_secret_name=self.registration_api_password_secret_name, 

49 ) 

50 field_errors.update({k: ValidationError(message) for k, v in needed.items() if not v}) 

51 

52 if field_errors: 

53 raise ValidationError(field_errors) 

54 

55 class Meta: 

56 verbose_name = "INIT config" 

57 

58 

59class InitGroup(EnrollmentGroup): 

60 group_id = models.PositiveIntegerField( 

61 default=None, blank=True, help_text="The ID of the INIT FareCategory for user enrollment." 

62 ) 

63 

64 @staticmethod 

65 def by_id(id): 

66 """Get a InitGroup instance by its ID.""" 

67 logger.debug(f"Get {InitGroup.__name__} by id: {id}") 

68 return InitGroup.objects.get(id=id) 

69 

70 class Meta: 

71 verbose_name = "INIT group"