Skip to content

Commit

Permalink
Do not let enabling plugins affect the global plugin cache (#3944)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Klopper <[email protected]>
  • Loading branch information
Donnype and underdarknl authored Dec 10, 2024
1 parent ec9d80e commit c54138f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions boefjes/boefjes/dependencies/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
2 changes: 1 addition & 1 deletion boefjes/boefjes/sql/config_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion boefjes/boefjes/storage/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion boefjes/boefjes/storage/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
13 changes: 13 additions & 0 deletions boefjes/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion boefjes/tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit c54138f

Please sign in to comment.