Skip to content
Merged
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
20 changes: 15 additions & 5 deletions scanpipe/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def load_policies_yaml(policies_yaml):

def load_policies_file(policies_file, validate=True):
"""
Load provided ``policies_file`` into a Python dictionary.
The policies format is validated by default.
Load provided ``policies_file`` into a Python dictionary. The policies format
is validated by default to ensure at least one policy type exists.
"""
policies_dict = load_policies_yaml(policies_yaml=policies_file.read_text())
if validate:
Expand All @@ -45,13 +45,23 @@ def load_policies_file(policies_file, validate=True):


def validate_policies(policies_dict):
"""Return True if the provided ``policies_dict`` is valid."""
"""
Return True if the provided ``policies_dict`` contains at least
one supported policy type.
"""
if not isinstance(policies_dict, dict):
raise ValidationError("The `policies_dict` argument must be a dictionary.")

if "license_policies" not in policies_dict:
supported_keys = {
"license_policies",
"license_clarity_thresholds",
"scorecard_score_thresholds",
}

if not any(key in policies_dict for key in supported_keys):
raise ValidationError(
"The `license_policies` key is missing from provided policies data."
"At least one of the following policy types must be present: "
f"{', '.join(sorted(supported_keys))}"
)

return True
Expand Down
4 changes: 3 additions & 1 deletion scanpipe/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ def test_scanpipe_forms_project_settings_form_policies(self):
self.assertFalse(form.is_valid())
expected = {
"policies": [
"The `license_policies` key is missing from provided policies data."
"At least one of the following policy types must be present: "
"license_clarity_thresholds, license_policies, "
"scorecard_score_thresholds"
]
}
self.assertEqual(expected, form.errors)
Expand Down
6 changes: 5 additions & 1 deletion scanpipe/tests/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def test_scanpipe_policies_validate_policies(self):
with self.assertRaisesMessage(ValidationError, error_msg):
validate_policies(policies_dict)

error_msg = "The `license_policies` key is missing from provided policies data."
error_msg = (
"At least one of the following policy types must be present: "
"license_clarity_thresholds, license_policies, "
"scorecard_score_thresholds"
)
policies_dict = {}
with self.assertRaisesMessage(ValidationError, error_msg):
validate_policies(policies_dict)
Expand Down