Skip to content

Commit 8fa7931

Browse files
authored
fix policies validation (#1814)
Signed-off-by: NucleonGodX <[email protected]>
1 parent 9d41ad3 commit 8fa7931

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

scanpipe/policies.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def load_policies_yaml(policies_yaml):
3535

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

4646

4747
def validate_policies(policies_dict):
48-
"""Return True if the provided ``policies_dict`` is valid."""
48+
"""
49+
Return True if the provided ``policies_dict`` contains at least
50+
one supported policy type.
51+
"""
4952
if not isinstance(policies_dict, dict):
5053
raise ValidationError("The `policies_dict` argument must be a dictionary.")
5154

52-
if "license_policies" not in policies_dict:
55+
supported_keys = {
56+
"license_policies",
57+
"license_clarity_thresholds",
58+
"scorecard_score_thresholds",
59+
}
60+
61+
if not any(key in policies_dict for key in supported_keys):
5362
raise ValidationError(
54-
"The `license_policies` key is missing from provided policies data."
63+
"At least one of the following policy types must be present: "
64+
f"{', '.join(sorted(supported_keys))}"
5565
)
5666

5767
return True

scanpipe/tests/test_forms.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ def test_scanpipe_forms_project_settings_form_policies(self):
219219
self.assertFalse(form.is_valid())
220220
expected = {
221221
"policies": [
222-
"The `license_policies` key is missing from provided policies data."
222+
"At least one of the following policy types must be present: "
223+
"license_clarity_thresholds, license_policies, "
224+
"scorecard_score_thresholds"
223225
]
224226
}
225227
self.assertEqual(expected, form.errors)

scanpipe/tests/test_policies.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ def test_scanpipe_policies_validate_policies(self):
6666
with self.assertRaisesMessage(ValidationError, error_msg):
6767
validate_policies(policies_dict)
6868

69-
error_msg = "The `license_policies` key is missing from provided policies data."
69+
error_msg = (
70+
"At least one of the following policy types must be present: "
71+
"license_clarity_thresholds, license_policies, "
72+
"scorecard_score_thresholds"
73+
)
7074
policies_dict = {}
7175
with self.assertRaisesMessage(ValidationError, error_msg):
7276
validate_policies(policies_dict)

0 commit comments

Comments
 (0)