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
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-08 19:50 +0000
1import logging
3from django.core.exceptions import ValidationError
4from django.db import models
6from benefits.core.models import EnrollmentGroup, SecretNameField, TransitProcessorConfig
8logger = logging.getLogger(__name__)
11class InitConfig(TransitProcessorConfig):
12 """Configuration for connecting to INIT, an entity that applies transit agency fare rules to rider transactions."""
14 tokenization_api_key = models.CharField(help_text="The Collect.js API key used for tokenization.", default="", blank=True)
16 registration_api_base_url = models.URLField(
17 help_text="The absolute base url of the MOBILEvario API instance.", default="", blank=True
18 )
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 )
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 )
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)
38 def clean(self):
39 field_errors = {}
41 if self.pk and self.transitagency_set and any([agency.active for agency in self.transitagency_set.all()]):
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})
52 if field_errors:
53 raise ValidationError(field_errors)
55 class Meta:
56 verbose_name = "INIT config"
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 )
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)
70 class Meta:
71 verbose_name = "INIT group"