From 4ec4e6b2b8637a9c2b0114186f3a3824fc8a0ad6 Mon Sep 17 00:00:00 2001 From: tdruez Date: Fri, 4 Jul 2025 17:55:17 +0400 Subject: [PATCH 1/2] Refactor policies implementation to support more than licenses Signed-off-by: tdruez --- scancodeio/settings.py | 1 + scanpipe/apps.py | 7 ++-- scanpipe/models.py | 31 ++++++++------- scanpipe/tests/pipes/test_scancode.py | 4 +- scanpipe/tests/test_apps.py | 26 ++++++------- scanpipe/tests/test_forms.py | 2 +- scanpipe/tests/test_models.py | 54 ++++++++++++--------------- scanpipe/tests/test_policies.py | 10 +++-- 8 files changed, 66 insertions(+), 69 deletions(-) diff --git a/scancodeio/settings.py b/scancodeio/settings.py index 2ffacb19f9..2ff10579fa 100644 --- a/scancodeio/settings.py +++ b/scancodeio/settings.py @@ -292,6 +292,7 @@ SCANCODEIO_WORKSPACE_LOCATION = tempfile.mkdtemp() SCANCODEIO_REQUIRE_AUTHENTICATION = True SCANCODEIO_SCAN_FILE_TIMEOUT = 120 + SCANCODEIO_POLICIES_FILE = None # The default password hasher is rather slow by design. # Using a faster hashing algorithm in the testing context to speed up the run. PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"] diff --git a/scanpipe/apps.py b/scanpipe/apps.py index 127245b291..252e3e55b3 100644 --- a/scanpipe/apps.py +++ b/scanpipe/apps.py @@ -39,7 +39,6 @@ from licensedcode.models import load_licenses from scanpipe.policies import load_policies_file -from scanpipe.policies import make_license_policy_index try: from importlib import metadata as importlib_metadata @@ -61,7 +60,7 @@ def __init__(self, app_name, app_module): # Mapping of registered pipeline names to pipeline classes. self._pipelines = {} - self.license_policies_index = {} + self.policies = {} workspace_location = settings.SCANCODEIO_WORKSPACE_LOCATION self.workspace_path = Path(workspace_location).expanduser().resolve() @@ -226,7 +225,7 @@ def get_scancode_licenses(self): def set_policies(self): """ - Compute and sets the `license_policies` on the app instance. + Set the global app policies on the app instance. If the policies file is available but not formatted properly or doesn't include the proper content, we want to raise an exception while the app @@ -240,7 +239,7 @@ def set_policies(self): if policies_file.exists(): policies = load_policies_file(policies_file) logger.debug(style.SUCCESS(f"Loaded policies from {policies_file}")) - self.license_policies_index = make_license_policy_index(policies) + self.policies = policies else: logger.debug(style.WARNING("Policies file not found.")) diff --git a/scanpipe/models.py b/scanpipe/models.py index 4158eee97e..617f3d49ec 100644 --- a/scanpipe/models.py +++ b/scanpipe/models.py @@ -1495,32 +1495,35 @@ def has_single_resource(self): """ return self.resource_count == 1 - def get_policy_index(self): + def get_policies_dict(self): """ - Return the policy index for this project instance. + Load and return the policies from the following locations in that order: - The policies are loaded from the following locations in that order: - 1. the project local settings - 2. the "policies.yml" file in the project input/ directory - 3. the global app settings license policies + 1. project local settings (stored in the database) + 2. "policies.yml" file in the project ``input/`` directory + 3. global app settings policies, from SCANCODEIO_POLICIES_FILE setting """ if policies_from_settings := self.get_env("policies"): policies_dict = policies_from_settings if isinstance(policies_from_settings, str): policies_dict = policies.load_policies_yaml(policies_from_settings) - return policies.make_license_policy_index(policies_dict) + return policies_dict - elif policies_file := self.get_input_policies_file(): - policies_dict = policies.load_policies_file(policies_file) - return policies.make_license_policy_index(policies_dict) + elif project_input_policies_file := self.get_input_policies_file(): + return policies.load_policies_file(project_input_policies_file) - else: - return scanpipe_app.license_policies_index + return scanpipe_app.policies + + def get_license_policy_index(self): + """Return the policy license index for this project instance.""" + if policies_dict := self.get_policies_dict(): + return policies.make_license_policy_index(policies_dict) + return {} @cached_property def policy_index(self): - """Return the cached policy index for this project instance.""" - return self.get_policy_index() + """Return the cached license policy index for this project instance.""" + return self.get_license_policy_index() @property def policies_enabled(self): diff --git a/scanpipe/tests/pipes/test_scancode.py b/scanpipe/tests/pipes/test_scancode.py index 2e729a2748..7ff55fcaf5 100644 --- a/scanpipe/tests/pipes/test_scancode.py +++ b/scanpipe/tests/pipes/test_scancode.py @@ -467,8 +467,8 @@ def test_scanpipe_max_file_size_works(self): self.assertEqual(resource1.status, flag.IGNORED_BY_MAX_FILE_SIZE) def test_scanpipe_pipes_scancode_make_results_summary(self, regen=FIXTURES_REGEN): - # Ensure the policies index is empty to avoid any side effect on results - scanpipe_app.license_policies_index = None + # Ensure the policies are empty to avoid any side effect on results + scanpipe_app.policies = None # Run the scan_single_package pipeline to have a proper DB and local files setup pipeline_name = "scan_single_package" project1 = Project.objects.create(name="Analysis") diff --git a/scanpipe/tests/test_apps.py b/scanpipe/tests/test_apps.py index 53a0363937..b0859d085d 100644 --- a/scanpipe/tests/test_apps.py +++ b/scanpipe/tests/test_apps.py @@ -23,6 +23,7 @@ import uuid from pathlib import Path from unittest import mock +from unittest.mock import patch from django.apps import apps from django.core.exceptions import ImproperlyConfigured @@ -33,7 +34,7 @@ from scanpipe.models import Project from scanpipe.models import Run from scanpipe.tests import filter_warnings -from scanpipe.tests import license_policies_index +from scanpipe.tests import global_policies from scanpipe.tests.pipelines.register_from_file import RegisterFromFile scanpipe_app = apps.get_app_config("scanpipe") @@ -43,26 +44,23 @@ class ScanPipeAppsTest(TestCase): data = Path(__file__).parent / "data" pipelines_location = Path(__file__).parent / "pipelines" - def test_scanpipe_apps_set_policies(self): - scanpipe_app.license_policies_index = {} - policies_files = None - with override_settings(SCANCODEIO_POLICIES_FILE=policies_files): + @patch.object(scanpipe_app, "policies", new_callable=dict) + def test_scanpipe_apps_set_policies(self, mock_policies): + # Case 1: No file set + with override_settings(SCANCODEIO_POLICIES_FILE=None): scanpipe_app.set_policies() - self.assertEqual({}, scanpipe_app.license_policies_index) + self.assertEqual({}, scanpipe_app.policies) - scanpipe_app.license_policies_index = {} - policies_files = "not_existing" - with override_settings(SCANCODEIO_POLICIES_FILE=policies_files): + # Case 2: Non-existing file + with override_settings(SCANCODEIO_POLICIES_FILE="not_existing"): scanpipe_app.set_policies() - self.assertEqual({}, scanpipe_app.license_policies_index) + self.assertEqual({}, scanpipe_app.policies) - scanpipe_app.license_policies_index = {} + # Case 3: Valid file policies_files = self.data / "policies" / "policies.yml" with override_settings(SCANCODEIO_POLICIES_FILE=str(policies_files)): scanpipe_app.set_policies() - self.assertEqual( - license_policies_index, scanpipe_app.license_policies_index - ) + self.assertEqual(global_policies, scanpipe_app.policies) def test_scanpipe_apps_register_pipeline_from_file(self): path = self.pipelines_location / "do_nothing.py" diff --git a/scanpipe/tests/test_forms.py b/scanpipe/tests/test_forms.py index 5d352ef0b4..98e0f83ff4 100644 --- a/scanpipe/tests/test_forms.py +++ b/scanpipe/tests/test_forms.py @@ -230,7 +230,7 @@ def test_scanpipe_forms_project_settings_form_policies(self): self.assertTrue(form.is_valid()) project = form.save() self.assertEqual(policies_as_yaml.strip(), project.settings["policies"]) - self.assertEqual(license_policies_index, project.get_policy_index()) + self.assertEqual(license_policies_index, project.get_license_policy_index()) def test_scanpipe_forms_project_settings_form_purl(self): data_invalid_purl = { diff --git a/scanpipe/tests/test_models.py b/scanpipe/tests/test_models.py index d0968f5235..a303a5f1dc 100644 --- a/scanpipe/tests/test_models.py +++ b/scanpipe/tests/test_models.py @@ -75,6 +75,7 @@ from scanpipe.pipes.input import copy_input from scanpipe.tests import dependency_data1 from scanpipe.tests import dependency_data2 +from scanpipe.tests import global_policies from scanpipe.tests import license_policies_index from scanpipe.tests import make_dependency from scanpipe.tests import make_message @@ -697,13 +698,11 @@ def test_scanpipe_project_get_input_policies_file(self): policies_file_location = str(self.project1.get_input_policies_file()) self.assertTrue(policies_file_location.endswith("input/policies.yml")) - def test_scanpipe_project_model_get_policy_index(self): - scanpipe_app.license_policies_index = None - self.assertFalse(self.project1.policies_enabled) - - policies_from_app_settings = {"from": "scanpipe_app"} - scanpipe_app.license_policies_index = policies_from_app_settings - self.assertEqual(policies_from_app_settings, self.project1.get_policy_index()) + @patch.object(scanpipe_app, "policies", new=global_policies) + def test_scanpipe_project_model_get_license_policy_index(self): + self.assertEqual( + license_policies_index, self.project1.get_license_policy_index() + ) policies_from_input_dir = {"license_policies": [{"license_key": "input_dir"}]} policies_file = self.project1.input_path / "policies.yml" @@ -711,7 +710,9 @@ def test_scanpipe_project_model_get_policy_index(self): policies_as_yaml = saneyaml.dump(policies_from_input_dir) policies_file.write_text(policies_as_yaml) expected_index_from_input = {"input_dir": {"license_key": "input_dir"}} - self.assertEqual(expected_index_from_input, self.project1.get_policy_index()) + self.assertEqual( + expected_index_from_input, self.project1.get_license_policy_index() + ) # Refresh the instance to bypass the cached_property cache. self.project1 = Project.objects.get(uuid=self.project1.uuid) self.assertTrue(self.project1.policies_enabled) @@ -723,10 +724,9 @@ def test_scanpipe_project_model_get_policy_index(self): self.project1.settings = config self.project1.save() expected_index_from_env = {"project_env": {"license_key": "project_env"}} - self.assertEqual(expected_index_from_env, self.project1.get_policy_index()) - - # Reset the index value - scanpipe_app.license_policies_index = None + self.assertEqual( + expected_index_from_env, self.project1.get_license_policy_index() + ) def test_scanpipe_project_get_settings_as_yml(self): self.assertEqual("{}\n", self.project1.get_settings_as_yml()) @@ -1547,28 +1547,31 @@ def test_scanpipe_codebase_resource_model_commoncode_methods_extracted_to_from( self.assertEqual(extracted_dir_resource, archive_resource.extracted_to()) self.assertEqual(archive_resource, extracted_dir_resource.extracted_from()) + @patch.object(scanpipe_app, "policies", new=global_policies) def test_scanpipe_codebase_resource_model_compliance_alert(self): - scanpipe_app.license_policies_index = license_policies_index + project_license_policies_index = self.project1.policy_index + self.assertEqual(license_policies_index, project_license_policies_index) + resource = CodebaseResource.objects.create(project=self.project1, path="file") self.assertEqual("", resource.compliance_alert) license_expression = "bsd-new" - self.assertNotIn(license_expression, scanpipe_app.license_policies_index) + self.assertNotIn(license_expression, project_license_policies_index) resource.update(detected_license_expression=license_expression) self.assertEqual("missing", resource.compliance_alert) license_expression = "apache-2.0" - self.assertIn(license_expression, scanpipe_app.license_policies_index) + self.assertIn(license_expression, project_license_policies_index) resource.update(detected_license_expression=license_expression) self.assertEqual("ok", resource.compliance_alert) license_expression = "mpl-2.0" - self.assertIn(license_expression, scanpipe_app.license_policies_index) + self.assertIn(license_expression, project_license_policies_index) resource.update(detected_license_expression=license_expression) self.assertEqual("warning", resource.compliance_alert) license_expression = "gpl-3.0" - self.assertIn(license_expression, scanpipe_app.license_policies_index) + self.assertIn(license_expression, project_license_policies_index) resource.update(detected_license_expression=license_expression) self.assertEqual("error", resource.compliance_alert) @@ -1584,11 +1587,8 @@ def test_scanpipe_codebase_resource_model_compliance_alert(self): resource.update(detected_license_expression=license_expression) self.assertEqual("warning", resource.compliance_alert) - # Reset the index value - scanpipe_app.license_policies_index = None - + @patch.object(scanpipe_app, "policies", new=global_policies) def test_scanpipe_codebase_resource_model_compliance_alert_update_fields(self): - scanpipe_app.license_policies_index = license_policies_index resource = CodebaseResource.objects.create(project=self.project1, path="file") self.assertEqual("", resource.compliance_alert) @@ -1598,9 +1598,6 @@ def test_scanpipe_codebase_resource_model_compliance_alert_update_fields(self): resource.refresh_from_db() self.assertEqual("ok", resource.compliance_alert) - # Reset the index value - scanpipe_app.license_policies_index = None - def test_scanpipe_scan_fields_model_mixin_methods(self): expected = [ "detected_license_expression", @@ -2422,20 +2419,20 @@ def test_scanpipe_discovered_package_model_as_cyclonedx(self): cyclonedx_component.evidence.licenses[0].value, ) + @patch.object(scanpipe_app, "policies", new=global_policies) def test_scanpipe_discovered_package_model_compliance_alert(self): - scanpipe_app.license_policies_index = license_policies_index package_data = package_data1.copy() package_data["declared_license_expression"] = "" package = DiscoveredPackage.create_from_data(self.project1, package_data) self.assertEqual("", package.compliance_alert) license_expression = "bsd-new" - self.assertNotIn(license_expression, scanpipe_app.license_policies_index) + self.assertNotIn(license_expression, self.project1.policy_index) package.update(declared_license_expression=license_expression) self.assertEqual("missing", package.compliance_alert) license_expression = "apache-2.0" - self.assertIn(license_expression, scanpipe_app.license_policies_index) + self.assertIn(license_expression, self.project1.policy_index) package.update(declared_license_expression=license_expression) self.assertEqual("ok", package.compliance_alert) @@ -2443,9 +2440,6 @@ def test_scanpipe_discovered_package_model_compliance_alert(self): package.update(declared_license_expression=license_expression) self.assertEqual("error", package.compliance_alert) - # Reset the index value - scanpipe_app.license_policies_index = None - def test_scanpipe_discovered_package_model_spdx_id(self): package1 = make_package(self.project1, "pkg:type/a") expected = f"SPDXRef-scancodeio-discoveredpackage-{package1.uuid}" diff --git a/scanpipe/tests/test_policies.py b/scanpipe/tests/test_policies.py index 9dfed0d688..e18513bfd5 100644 --- a/scanpipe/tests/test_policies.py +++ b/scanpipe/tests/test_policies.py @@ -98,10 +98,12 @@ def test_scanpipe_policies_scan_codebase_pipeline_integration(self): pipeline = run.make_pipeline_instance() # Capture the real method's return value - real_get_policy_index = project1.get_policy_index + real_get_license_policy_index = project1.get_license_policy_index - with mock.patch("scanpipe.models.Project.get_policy_index") as mock_get_index: - mock_get_index.side_effect = real_get_policy_index + with mock.patch( + "scanpipe.models.Project.get_license_policy_index" + ) as mock_get_index: + mock_get_index.side_effect = real_get_license_policy_index exitcode, out = pipeline.execute() mock_get_index.assert_called_once() @@ -132,7 +134,7 @@ def test_scanpipe_policies_scan_codebase_pipeline_integration(self): "apache-2.0": {"license_key": "apache-2.0", "compliance_alert": ""}, "gpl-2.0": {"license_key": "gpl-2.0", "compliance_alert": "error"}, } - self.assertEqual(expected_index, project1.get_policy_index()) + self.assertEqual(expected_index, project1.get_license_policy_index()) def test_scanpipe_policies_through_scancode_config_file(self): project1 = make_project() From f15a4db9733414fc8c0cc5ad272619b42f2acf70 Mon Sep 17 00:00:00 2001 From: tdruez Date: Fri, 4 Jul 2025 18:21:52 +0400 Subject: [PATCH 2/2] Rename policy related method for clarity Signed-off-by: tdruez --- CHANGELOG.rst | 10 ++++ scanpipe/models.py | 25 +++++----- scanpipe/pipes/output.py | 2 +- .../scanpipe/forms/project_settings_form.html | 2 +- .../templates/scanpipe/project_detail.html | 2 +- scanpipe/tests/test_models.py | 47 +++++++++++++++++-- scanpipe/tests/test_policies.py | 2 +- scanpipe/tests/test_views.py | 22 +++++---- scanpipe/views.py | 8 ++-- 9 files changed, 88 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 36a4817549..50ea051c03 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,16 @@ Changelog v35.2.0 (unreleased) -------------------- +- Refactor policies implementation to support more than licenses. + The entire ``policies`` data is now stored on the ``ScanPipeConfig`` in place of the + ``license_policy_index``. + Also, a new method ``get_policies_dict`` methods is now available on the ``Project`` + model to easily retrieve all the policies data as a dictionary. + Renamed for clarity: + * ``policy_index`` to ``license_policy_index`` + * ``policies_enabled`` to ``license_policies_enabled`` + https://github.com/aboutcode-org/scancode.io/pull/1718 + - Add support for SPDX license identifiers as ``license_key`` in license policies ``policies.yml`` file. https://github.com/aboutcode-org/scancode.io/issues/1348 diff --git a/scanpipe/models.py b/scanpipe/models.py index 617f3d49ec..fd72fe711f 100644 --- a/scanpipe/models.py +++ b/scanpipe/models.py @@ -1521,14 +1521,14 @@ def get_license_policy_index(self): return {} @cached_property - def policy_index(self): + def license_policy_index(self): """Return the cached license policy index for this project instance.""" return self.get_license_policy_index() @property - def policies_enabled(self): - """Return True if the policies are enabled for this project.""" - return bool(self.policy_index) + def license_policies_enabled(self): + """Return True if the license policies are available for this project.""" + return bool(self.license_policy_index) class GroupingQuerySetMixin: @@ -2543,7 +2543,7 @@ def save(self, codebase=None, *args, **kwargs): ``codebase`` is not used in this context but required for compatibility with the commoncode.resource.Codebase class API. """ - if self.policies_enabled: + if self.license_policies_enabled: loaded_license_expression = getattr(self, "_loaded_license_expression", "") license_expression = getattr(self, self.license_expression_field, "") if license_expression != loaded_license_expression: @@ -2569,12 +2569,12 @@ def from_db(cls, db, field_names, values): return new @property - def policy_index(self): - return self.project.policy_index + def license_policy_index(self): + return self.project.license_policy_index @cached_property - def policies_enabled(self): - return self.project.policies_enabled + def license_policies_enabled(self): + return self.project.license_policies_enabled def compute_compliance_alert(self): """ @@ -2582,15 +2582,16 @@ def compute_compliance_alert(self): Chooses the most severe compliance_alert found among licenses. """ license_expression = getattr(self, self.license_expression_field, "") - policy_index = self.policy_index - if not license_expression or not policy_index: + license_policy_index = self.license_policy_index + if not license_expression or not license_policy_index: return "" licensing = get_licensing() parsed_symbols = licensing.parse(license_expression, simple=True).symbols alerts = [ - self.get_alert_for_symbol(policy_index, symbol) for symbol in parsed_symbols + self.get_alert_for_symbol(license_policy_index, symbol) + for symbol in parsed_symbols ] most_severe_alert = max(alerts, key=self.COMPLIANCE_SEVERITY_MAP.get) return most_severe_alert or self.Compliance.OK diff --git a/scanpipe/pipes/output.py b/scanpipe/pipes/output.py index 30737d606c..ba159d7844 100644 --- a/scanpipe/pipes/output.py +++ b/scanpipe/pipes/output.py @@ -541,7 +541,7 @@ def to_xlsx(project): exclude_fields = XLSX_EXCLUDE_FIELDS.copy() output_file = project.get_output_file_path("results", "xlsx") - if not project.policies_enabled: + if not project.license_policies_enabled: exclude_fields.append("compliance_alert") model_names = [ diff --git a/scanpipe/templates/scanpipe/forms/project_settings_form.html b/scanpipe/templates/scanpipe/forms/project_settings_form.html index cd707cb5ca..0118de619f 100644 --- a/scanpipe/templates/scanpipe/forms/project_settings_form.html +++ b/scanpipe/templates/scanpipe/forms/project_settings_form.html @@ -153,7 +153,7 @@

Policies

- {% if project.policies_enabled %} + {% if project.license_policies_enabled %} Policies are enabled for this project. {% else %} diff --git a/scanpipe/templates/scanpipe/project_detail.html b/scanpipe/templates/scanpipe/project_detail.html index e494166dea..91698c5e56 100644 --- a/scanpipe/templates/scanpipe/project_detail.html +++ b/scanpipe/templates/scanpipe/project_detail.html @@ -117,7 +117,7 @@
- {% if policies_enabled %} + {% if license_policies_enabled %}
diff --git a/scanpipe/tests/test_models.py b/scanpipe/tests/test_models.py index a303a5f1dc..a81af58bf2 100644 --- a/scanpipe/tests/test_models.py +++ b/scanpipe/tests/test_models.py @@ -698,6 +698,28 @@ def test_scanpipe_project_get_input_policies_file(self): policies_file_location = str(self.project1.get_input_policies_file()) self.assertTrue(policies_file_location.endswith("input/policies.yml")) + @patch.object(scanpipe_app, "policies", new=global_policies) + def test_scanpipe_project_model_get_policies_dict(self): + self.assertEqual(scanpipe_app.policies, self.project1.get_policies_dict()) + + policies_from_input_dir = {"license_policies": [{"license_key": "input_dir"}]} + policies_file = self.project1.input_path / "policies.yml" + policies_file.touch() + policies_as_yaml = saneyaml.dump(policies_from_input_dir) + policies_file.write_text(policies_as_yaml) + self.assertEqual(policies_from_input_dir, self.project1.get_policies_dict()) + # Refresh the instance to bypass the cached_property cache. + self.project1 = Project.objects.get(uuid=self.project1.uuid) + self.assertTrue(self.project1.license_policies_enabled) + + policies_from_project_env = { + "license_policies": [{"license_key": "project_env"}] + } + config = {"policies": policies_from_project_env} + self.project1.settings = config + self.project1.save() + self.assertEqual(policies_from_project_env, self.project1.get_policies_dict()) + @patch.object(scanpipe_app, "policies", new=global_policies) def test_scanpipe_project_model_get_license_policy_index(self): self.assertEqual( @@ -715,7 +737,7 @@ def test_scanpipe_project_model_get_license_policy_index(self): ) # Refresh the instance to bypass the cached_property cache. self.project1 = Project.objects.get(uuid=self.project1.uuid) - self.assertTrue(self.project1.policies_enabled) + self.assertTrue(self.project1.license_policies_enabled) policies_from_project_env = { "license_policies": [{"license_key": "project_env"}] @@ -728,6 +750,23 @@ def test_scanpipe_project_model_get_license_policy_index(self): expected_index_from_env, self.project1.get_license_policy_index() ) + def test_scanpipe_models_license_policies_enabled(self): + resource1 = make_resource_file(self.project1, path="example") + package1 = make_package(self.project1, "pkg:type/a") + + self.assertFalse(self.project1.license_policies_enabled) + self.assertFalse(resource1.license_policies_enabled) + self.assertFalse(package1.license_policies_enabled) + + with patch.object(scanpipe_app, "policies", new=global_policies): + # Refresh the instance to bypass the cached_property cache. + self.project1 = Project.objects.get(uuid=self.project1.uuid) + resource1 = self.project1.codebaseresources.get() + package1 = self.project1.discoveredpackages.get() + self.assertTrue(self.project1.license_policies_enabled) + self.assertTrue(resource1.license_policies_enabled) + self.assertTrue(package1.license_policies_enabled) + def test_scanpipe_project_get_settings_as_yml(self): self.assertEqual("{}\n", self.project1.get_settings_as_yml()) @@ -1549,7 +1588,7 @@ def test_scanpipe_codebase_resource_model_commoncode_methods_extracted_to_from( @patch.object(scanpipe_app, "policies", new=global_policies) def test_scanpipe_codebase_resource_model_compliance_alert(self): - project_license_policies_index = self.project1.policy_index + project_license_policies_index = self.project1.license_policy_index self.assertEqual(license_policies_index, project_license_policies_index) resource = CodebaseResource.objects.create(project=self.project1, path="file") @@ -2427,12 +2466,12 @@ def test_scanpipe_discovered_package_model_compliance_alert(self): self.assertEqual("", package.compliance_alert) license_expression = "bsd-new" - self.assertNotIn(license_expression, self.project1.policy_index) + self.assertNotIn(license_expression, self.project1.license_policy_index) package.update(declared_license_expression=license_expression) self.assertEqual("missing", package.compliance_alert) license_expression = "apache-2.0" - self.assertIn(license_expression, self.project1.policy_index) + self.assertIn(license_expression, self.project1.license_policy_index) package.update(declared_license_expression=license_expression) self.assertEqual("ok", package.compliance_alert) diff --git a/scanpipe/tests/test_policies.py b/scanpipe/tests/test_policies.py index e18513bfd5..fc7be2f8d3 100644 --- a/scanpipe/tests/test_policies.py +++ b/scanpipe/tests/test_policies.py @@ -129,7 +129,7 @@ def test_scanpipe_policies_scan_codebase_pipeline_integration(self): expected = "codebase/include_policies_file.zip-extract/policies.yml" project_policies_file = project1.get_input_policies_file() self.assertTrue(str(project_policies_file).endswith(expected)) - self.assertTrue(project1.policies_enabled) + self.assertTrue(project1.license_policies_enabled) expected_index = { "apache-2.0": {"license_key": "apache-2.0", "compliance_alert": ""}, "gpl-2.0": {"license_key": "gpl-2.0", "compliance_alert": "error"}, diff --git a/scanpipe/tests/test_views.py b/scanpipe/tests/test_views.py index b9868cfb22..4c06995b16 100644 --- a/scanpipe/tests/test_views.py +++ b/scanpipe/tests/test_views.py @@ -618,9 +618,11 @@ def test_scanpipe_views_project_details_codebase_root(self): expected = ["Dir", "Zdir", "a", "z", "a.txt", "z.txt"] self.assertEqual(expected, [path.name for path in codebase_root]) - @mock.patch.object(Project, "policies_enabled", new_callable=mock.PropertyMock) + @mock.patch.object( + Project, "license_policies_enabled", new_callable=mock.PropertyMock + ) def test_scanpipe_views_project_details_compliance_panel_availability( - self, mock_policies_enabled + self, mock_license_policies_enabled ): url = self.project1.get_absolute_url() make_package( @@ -630,11 +632,11 @@ def test_scanpipe_views_project_details_compliance_panel_availability( ) expected_url = reverse("project_compliance_panel", args=[self.project1.slug]) - mock_policies_enabled.return_value = False + mock_license_policies_enabled.return_value = False response = self.client.get(url) self.assertNotContains(response, expected_url) - mock_policies_enabled.return_value = True + mock_license_policies_enabled.return_value = True response = self.client.get(url) self.assertContains(response, expected_url) @@ -988,8 +990,12 @@ def test_scanpipe_views_project_run_step_selection_view(self): ) self.assertContains(response, expected_input2) - @mock.patch.object(Project, "policies_enabled", new_callable=mock.PropertyMock) - def test_scanpipe_views_project_compliance_panel_view(self, mock_policies_enabled): + @mock.patch.object( + Project, "license_policies_enabled", new_callable=mock.PropertyMock + ) + def test_scanpipe_views_project_compliance_panel_view( + self, mock_license_policies_enabled + ): url = reverse("project_compliance_panel", args=[self.project1.slug]) make_package( self.project1, @@ -997,11 +1003,11 @@ def test_scanpipe_views_project_compliance_panel_view(self, mock_policies_enable compliance_alert=DiscoveredPackage.Compliance.ERROR, ) - mock_policies_enabled.return_value = False + mock_license_policies_enabled.return_value = False response = self.client.get(url) self.assertEqual(404, response.status_code) - mock_policies_enabled.return_value = True + mock_license_policies_enabled.return_value = True response = self.client.get(url) self.assertContains(response, "Compliance alerts") self.assertContains(response, "1 Error") diff --git a/scanpipe/views.py b/scanpipe/views.py index d9f5a71177..d630628fbe 100644 --- a/scanpipe/views.py +++ b/scanpipe/views.py @@ -808,9 +808,9 @@ def get_context_data(self, **kwargs): pipeline_runs = project.runs.all() self.check_run_scancode_version(pipeline_runs) - policies_enabled = False + license_policies_enabled = False try: - policies_enabled = project.policies_enabled + license_policies_enabled = project.license_policies_enabled except ValidationError as e: messages.error(self.request, str(e)) @@ -830,7 +830,7 @@ def get_context_data(self, **kwargs): "pipeline_runs": pipeline_runs, "codebase_root": codebase_root, "file_filter": self.request.GET.get("file-filter", "all"), - "policies_enabled": policies_enabled, + "license_policies_enabled": license_policies_enabled, } ) @@ -1201,7 +1201,7 @@ def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) project = self.object - if not project.policies_enabled: + if not project.license_policies_enabled: raise Http404 compliance_alerts = compliance.get_project_compliance_alerts(