Coverage for benefits / enrollment_switchio / models.py: 100%
53 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-13 19:35 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-13 19:35 +0000
1from django.core.exceptions import ValidationError
2from django.db import models
4from benefits.core.models import EnrollmentGroup, Environment, PemData, SecretNameField, TransitProcessorConfig
5from benefits.secrets import get_secret_by_name
8class SwitchioConfig(TransitProcessorConfig):
9 """Configuration for connecting to Switchio, an entity that applies transit agency fare rules to rider transactions."""
11 tokenization_api_key = models.TextField(
12 help_text="The API key used to access the Switchio API for tokenization.", default="", blank=True
13 )
14 tokenization_api_secret_name = SecretNameField(
15 help_text="The name of the secret containing the api_secret value used to access the Switchio API for tokenization.", # noqa: E501
16 default="",
17 blank=True,
18 )
19 enrollment_api_authorization_header = models.TextField(
20 help_text="The value to use for the 'Authorization' header when accessing the Switchio API for enrollment.",
21 default="",
22 blank=True,
23 )
24 pto_id = models.PositiveIntegerField(
25 help_text="The Public Transport Operator ID to use with the Switchio API for enrollment.",
26 default=0,
27 blank=True,
28 )
29 client_certificate = models.ForeignKey(
30 PemData,
31 related_name="+",
32 on_delete=models.PROTECT,
33 help_text="The client certificate for accessing the Switchio API.",
34 null=True,
35 blank=True,
36 default=None,
37 )
38 ca_certificate = models.ForeignKey(
39 PemData,
40 related_name="+",
41 on_delete=models.PROTECT,
42 help_text="The CA certificate chain for accessing the Switchio API.",
43 null=True,
44 blank=True,
45 default=None,
46 )
47 private_key = models.ForeignKey(
48 PemData,
49 related_name="+",
50 on_delete=models.PROTECT,
51 help_text="The private key for accessing the Switchio API.",
52 null=True,
53 blank=True,
54 default=None,
55 )
57 @property
58 def tokenization_api_base_url(self):
59 if self.environment == Environment.DEV.value:
60 return get_secret_by_name("switchio-int-tokenization-api-base-url")
61 if self.environment == Environment.TEST.value:
62 return get_secret_by_name("switchio-acc-tokenization-api-base-url")
63 elif self.environment == Environment.PROD.value:
64 return get_secret_by_name("switchio-prod-tokenization-api-base-url")
65 else:
66 raise ValueError(f"Unexpected value for environment: {self.environment}")
68 @property
69 def enrollment_api_base_url(self):
70 if self.environment == Environment.DEV.value:
71 return get_secret_by_name("switchio-int-enrollment-api-base-url")
72 if self.environment == Environment.TEST.value:
73 return get_secret_by_name("switchio-acc-enrollment-api-base-url")
74 elif self.environment == Environment.PROD.value:
75 return get_secret_by_name("switchio-prod-enrollment-api-base-url")
76 else:
77 raise ValueError(f"Unexpected value for environment: {self.environment}")
79 @property
80 def tokenization_api_secret(self):
81 secret_field = self._meta.get_field("tokenization_api_secret_name")
82 return secret_field.secret_value(self)
84 @property
85 def client_certificate_data(self):
86 """This SwitchioConfig's client certificate as a string."""
87 return self.client_certificate.data
89 @property
90 def ca_certificate_data(self):
91 """This SwitchioConfig's CA certificate as a string."""
92 return self.ca_certificate.data
94 @property
95 def private_key_data(self):
96 """This SwitchioConfig's private key as a string."""
97 return self.private_key.data
99 def clean(self):
100 field_errors = {}
102 if self.transit_agency and self.transit_agency.active:
103 message = "This field is required when this configuration is referenced by an active transit agency."
104 needed = dict(
105 tokenization_api_key=self.tokenization_api_key,
106 tokenization_api_secret_name=self.tokenization_api_secret_name,
107 enrollment_api_authorization_header=self.enrollment_api_authorization_header,
108 pto_id=self.pto_id,
109 client_certificate=self.client_certificate,
110 ca_certificate=self.ca_certificate,
111 private_key=self.private_key,
112 )
113 field_errors.update({k: ValidationError(message) for k, v in needed.items() if not v})
115 if field_errors:
116 raise ValidationError(field_errors)
119class SwitchioGroup(EnrollmentGroup):
120 group_id = models.TextField(default=None, blank=True, help_text="The ID of the Switchio group for user enrollment.")