From c54138feee661b6646c38320ffcc25cfb83998bc Mon Sep 17 00:00:00 2001 From: Donny Peeters <46660228+Donnype@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:43:27 +0100 Subject: [PATCH] Do not let enabling plugins affect the global plugin cache (#3944) Co-authored-by: Jan Klopper --- boefjes/boefjes/dependencies/plugins.py | 4 ++-- boefjes/boefjes/sql/config_storage.py | 2 +- boefjes/boefjes/storage/interfaces.py | 2 +- boefjes/boefjes/storage/memory.py | 2 +- boefjes/tests/conftest.py | 13 +++++++++++++ boefjes/tests/integration/test_api.py | 12 +++++++++++- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/boefjes/boefjes/dependencies/plugins.py b/boefjes/boefjes/dependencies/plugins.py index a4d4fb5dc6a..4828302b969 100644 --- a/boefjes/boefjes/dependencies/plugins.py +++ b/boefjes/boefjes/dependencies/plugins.py @@ -43,13 +43,13 @@ def __exit__(self, exc_type, exc_val, exc_tb): def get_all(self, organisation_id: str) -> list[PluginType]: all_plugins = self._get_all_without_enabled() - plugin_states = self.config_storage.get_state_by_id(organisation_id) + plugin_states = self.config_storage.get_states_for_organisation(organisation_id) for plugin in all_plugins.values(): if plugin.id not in plugin_states: continue - plugin.enabled = plugin_states[plugin.id] + all_plugins[plugin.id] = plugin.model_copy(update={"enabled": plugin_states[plugin.id]}) return list(all_plugins.values()) diff --git a/boefjes/boefjes/sql/config_storage.py b/boefjes/boefjes/sql/config_storage.py index b944088bc32..a7057dfb0ad 100644 --- a/boefjes/boefjes/sql/config_storage.py +++ b/boefjes/boefjes/sql/config_storage.py @@ -111,7 +111,7 @@ def get_enabled_normalizers(self, organisation_id: str) -> list[str]: return [plugin[0] for plugin in enabled_normalizers.all()] - def get_state_by_id(self, organisation_id: str) -> dict[str, bool]: + def get_states_for_organisation(self, organisation_id: str) -> dict[str, bool]: enabled_boefjes = ( self.session.query(BoefjeInDB.plugin_id, BoefjeConfigInDB.enabled) .join(BoefjeConfigInDB) diff --git a/boefjes/boefjes/storage/interfaces.py b/boefjes/boefjes/storage/interfaces.py index be6132b2cdd..044257d7b6d 100644 --- a/boefjes/boefjes/storage/interfaces.py +++ b/boefjes/boefjes/storage/interfaces.py @@ -157,5 +157,5 @@ def get_enabled_boefjes(self, organisation_id: str) -> list[str]: def get_enabled_normalizers(self, organisation_id: str) -> list[str]: raise NotImplementedError - def get_state_by_id(self, organisation_id: str) -> dict[str, bool]: + def get_states_for_organisation(self, organisation_id: str) -> dict[str, bool]: raise NotImplementedError diff --git a/boefjes/boefjes/storage/memory.py b/boefjes/boefjes/storage/memory.py index 1db992ba7f1..c4048be5e3b 100644 --- a/boefjes/boefjes/storage/memory.py +++ b/boefjes/boefjes/storage/memory.py @@ -131,5 +131,5 @@ def get_enabled_normalizers(self, organisation_id: str) -> list[str]: if enabled and "norm" in plugin_id ] - def get_state_by_id(self, organisation_id: str) -> dict[str, bool]: + def get_states_for_organisation(self, organisation_id: str) -> dict[str, bool]: return self._enabled.get(organisation_id, {}) diff --git a/boefjes/tests/conftest.py b/boefjes/tests/conftest.py index 917af0d4a71..202d3d0a778 100644 --- a/boefjes/tests/conftest.py +++ b/boefjes/tests/conftest.py @@ -238,6 +238,11 @@ def test_organisation(): return Organisation(id="test", name="Test org") +@pytest.fixture +def second_test_organisation(): + return Organisation(id="test2", name="Test org2") + + @pytest.fixture def mock_plugin_service(mock_local_repository, test_organisation) -> PluginService: storage = ConfigStorageMemory() @@ -254,6 +259,14 @@ def organisation(organisation_storage, test_organisation) -> Organisation: return test_organisation +@pytest.fixture +def second_organisation(organisation_storage, second_test_organisation) -> Organisation: + with organisation_storage as repo: + repo.create(second_test_organisation) + + return second_test_organisation + + @pytest.fixture def unit_test_client(mock_plugin_service) -> TestClient: client = TestClient(app) diff --git a/boefjes/tests/integration/test_api.py b/boefjes/tests/integration/test_api.py index 787d5b8cbf8..6c2ae495f47 100644 --- a/boefjes/tests/integration/test_api.py +++ b/boefjes/tests/integration/test_api.py @@ -73,6 +73,16 @@ def test_add_boefje(test_client, organisation): assert response.json() == boefje_dict +def test_enable_boefje(test_client, organisation, second_organisation): + test_client.patch(f"/v1/organisations/{organisation.id}/plugins/dns-records", json={"enabled": True}) + + response = test_client.get(f"/v1/organisations/{organisation.id}/plugins/dns-records") + assert response.json()["enabled"] is True + + response = test_client.get(f"/v1/organisations/{second_organisation.id}/plugins/dns-records") + assert response.json()["enabled"] is False + + def test_cannot_add_static_plugin_with_duplicate_name(test_client, organisation): boefje = Boefje(id="test_plugin", name="DNS records", static=False) response = test_client.post(f"/v1/organisations/{organisation.id}/plugins", content=boefje.model_dump_json()) @@ -137,7 +147,7 @@ def test_delete_normalizer(test_client, organisation): assert response.status_code == 404 -def test_update_plugins(test_client, organisation): +def test_update_plugins(test_client, organisation, second_organisation): normalizer = Normalizer(id="norm_id", name="My test normalizer") boefje = Boefje(id="test_plugin", name="My test boefje", description="123", interval=20)