Coverage for benefits/enrollment_littlepay/models.py: 84%
42 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-14 01:41 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-14 01:41 +0000
1from django.core.exceptions import ValidationError
2from django.db import models
4from benefits.core import context as core_context
5from benefits.core.models import SecretNameField, Environment
8class LittlepayConfig(models.Model):
9 """Configuration for connecting to Littlepay, an entity that applies transit agency fare rules to rider transactions."""
11 id = models.AutoField(primary_key=True)
12 environment = models.TextField(
13 choices=Environment,
14 help_text="A label to indicate which environment this configuration is for.",
15 )
16 agency_slug = models.SlugField(
17 choices=core_context.AgencySlug,
18 help_text="A label to indicate which agency this configuration is for. Note: the field that controls which configuration an agency actually uses is on the TransitAgency model.", # noqa
19 )
20 audience = models.TextField(
21 help_text="This agency's audience value used to access the TransitProcessor's API.", default="", blank=True
22 )
23 client_id = models.TextField(
24 help_text="This agency's client_id value used to access the TransitProcessor's API.", default="", blank=True
25 )
26 client_secret_name = SecretNameField(
27 help_text="The name of the secret containing this agency's client_secret value used to access the TransitProcessor's API.", # noqa: E501
28 default="",
29 blank=True,
30 )
32 @property
33 def client_secret(self):
34 secret_field = self._meta.get_field("client_secret_name")
35 return secret_field.secret_value(self)
37 @property
38 def transit_processor_context(self):
39 match self.environment:
40 case Environment.QA.value: 40 ↛ 43line 40 didn't jump to line 43 because the pattern on line 40 always matched
41 url = "https://verify.qa.littlepay.com/assets/js/littlepay.min.js"
42 card_tokenize_env = "https://verify.qa.littlepay.com"
43 case Environment.PROD.value:
44 url = "https://verify.littlepay.com/assets/js/littlepay.min.js"
45 card_tokenize_env = "https://verify.littlepay.com"
46 case _:
47 raise ValueError("Unrecognized environment value")
49 return dict(
50 name="Littlepay", website="https://littlepay.com", card_tokenize_url=url, card_tokenize_env=card_tokenize_env
51 )
53 @property
54 def enrollment_index_template(self):
55 return "enrollment/index--littlepay.html"
57 def clean(self):
58 field_errors = {}
60 if hasattr(self, "transitagency") and self.transitagency.active:
61 message = "This field is required when this configuration is referenced by an active transit agency."
62 needed = dict(audience=self.audience, client_id=self.client_id, client_secret_name=self.client_secret_name)
63 field_errors.update({k: ValidationError(message) for k, v in needed.items() if not v})
65 if field_errors:
66 raise ValidationError(field_errors)
68 def __str__(self):
69 environment_label = Environment(self.environment).label if self.environment else "unknown"
70 agency_slug = self.agency_slug if self.agency_slug else "(no agency)"
71 return f"({environment_label}) {agency_slug}"