Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: prevent agency from being saved if it doesn't have copy for index #2713

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions benefits/core/models/transit.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def enrollment_flows(self):

def clean(self):
field_errors = {}
template_errors = []
non_field_errors = []

if self.active:
message = "This field is required for active transit agencies."
Expand All @@ -219,17 +219,22 @@ def clean(self):
)
field_errors.update({k: ValidationError(message) for k, v in needed.items() if not v})

try:
self.index_context
except KeyError:
non_field_errors.append(ValidationError("Agency Index copy is missing"))

# since templates are calculated from the pattern or the override field
# we can't add a field-level validation error
# so just create directly for a missing template
for t in [self.eligibility_index_template]:
if not template_path(t):
template_errors.append(ValidationError(f"Template not found: {t}"))
non_field_errors.append(ValidationError(f"Template not found: {t}"))

if field_errors:
raise ValidationError(field_errors)
if template_errors:
raise ValidationError(template_errors)
if non_field_errors:
raise ValidationError(non_field_errors)

@staticmethod
def by_id(id):
Expand Down
11 changes: 11 additions & 0 deletions tests/pytest/core/models/test_transit.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ def test_TransitAgency_clean(model_TransitAgency_inactive, model_TransitProcesso
assert "transit_processor_client_secret_name" in errors


@pytest.mark.django_db
def test_TransitAgency_clean_index_context_missing(model_TransitAgency_inactive):
model_TransitAgency_inactive.slug = "new_slug_we_forgot_to_add_copy_for"
# agency is inactive, OK to have missing index context
model_TransitAgency_inactive.clean()

model_TransitAgency_inactive.active = True
with pytest.raises(ValidationError, match="Agency Index copy is missing"):
model_TransitAgency_inactive.clean()


@pytest.mark.django_db
@pytest.mark.parametrize("template_attribute", ["eligibility_index_template_override"])
def test_TransitAgency_clean_templates(model_TransitAgency_inactive, template_attribute):
Expand Down
Loading