Skip to content

Commit

Permalink
feat(models): improve EnrollmentFlow validation
Browse files Browse the repository at this point in the history
when the flow's agency.active=True, validate that templates exist
  • Loading branch information
thekaveman committed Nov 12, 2024
1 parent cc23d42 commit e26b479
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions benefits/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,13 @@ def enrollment_success_template(self):
return self.enrollment_success_template_override or f"{prefix}--{self.agency_card_name}.html"

def clean(self):
supports_expiration = self.supports_expiration
expiration_days = self.expiration_days
expiration_reenrollment_days = self.expiration_reenrollment_days
reenrollment_error_template = self.reenrollment_error_template
errors = {}

if self.supports_expiration:
expiration_days = self.expiration_days
expiration_reenrollment_days = self.expiration_reenrollment_days
reenrollment_error_template = self.reenrollment_error_template

if supports_expiration:
errors = {}
message = "When support_expiration is True, this value must be greater than 0."
if expiration_days is None or expiration_days <= 0:
errors.update(expiration_days=ValidationError(message))
Expand All @@ -607,8 +607,25 @@ def clean(self):
if reenrollment_error_template is None:
errors.update(reenrollment_error_template=ValidationError("Required when supports expiration is True."))

if errors:
raise ValidationError(errors)
if self.transit_agency and self.transit_agency.active:
self.transit_agency.clean()

templates = dict(
selection_label_template=self.selection_label_template,
eligibility_start_template=self.eligibility_start_template,
eligibility_unverified_template=self.eligibility_unverified_template,
enrollment_index_template=self.enrollment_index_template,
enrollment_success_template=self.enrollment_success_template,
)
if self.supports_expiration:
templates.update(reenrollment_error_template=self.reenrollment_error_template)

for name, tmpl in templates.items():
if not template_path(tmpl):
errors.update({name: ValidationError(f"Template not found: {tmpl}")})

if errors:
raise ValidationError(errors)

def eligibility_form_instance(self, *args, **kwargs):
"""Return an instance of this flow's EligibilityForm, or None."""
Expand Down

0 comments on commit e26b479

Please sign in to comment.