Skip to content

Commit

Permalink
Chore: use only Pytest in the boefjes (#3536)
Browse files Browse the repository at this point in the history
Signed-off-by: Donny Peeters <[email protected]>
  • Loading branch information
Donnype authored Sep 17, 2024
1 parent 8821f3f commit 32aa48a
Show file tree
Hide file tree
Showing 96 changed files with 5,230 additions and 5,788 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def upgrade() -> None:

for plugin in local_repo.get_all():
schema = local_repo.schema(plugin.id)

if schema:
try:
# This way we avoid the safeguard that updating static boefjes is not allowed
Expand Down
1,632 changes: 846 additions & 786 deletions boefjes/poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions boefjes/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ maxminddb = "^2.6.2"
[tool.poetry.group.dev.dependencies]
pytest = "^8.2.0"
pytest-env = "^1.1.3"
pytest-mock = "^3.14.0"

[build-system]
requires = ["setuptools>=45", "wheel"]
Expand Down
1,556 changes: 791 additions & 765 deletions boefjes/requirements-dev.txt

Large diffs are not rendered by default.

1,541 changes: 782 additions & 759 deletions boefjes/requirements.txt

Large diffs are not rendered by default.

95 changes: 83 additions & 12 deletions boefjes/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
from uuid import UUID

import alembic.config
import pytest
from fastapi.testclient import TestClient
from pydantic import TypeAdapter
Expand All @@ -16,16 +17,21 @@
from boefjes.clients.bytes_client import BytesAPIClient
from boefjes.clients.scheduler_client import Queue, SchedulerClientInterface, Task, TaskStatus
from boefjes.config import Settings, settings
from boefjes.dependencies.plugins import PluginService
from boefjes.dependencies.plugins import PluginService, get_plugin_service
from boefjes.job_handler import bytes_api_client
from boefjes.job_models import BoefjeMeta, NormalizerMeta
from boefjes.local_repository import get_local_repository
from boefjes.katalogus.organisations import check_organisation_exists
from boefjes.katalogus.root import app
from boefjes.local import LocalBoefjeJobRunner, LocalNormalizerJobRunner
from boefjes.local_repository import LocalPluginRepository, get_local_repository
from boefjes.models import Organisation
from boefjes.runtime_interfaces import Handler, WorkerManager
from boefjes.sql.config_storage import SQLConfigStorage, create_encrypter
from boefjes.sql.db import SQL_BASE, get_engine
from boefjes.sql.organisation_storage import SQLOrganisationStorage
from boefjes.sql.organisation_storage import SQLOrganisationStorage, get_organisations_store
from boefjes.sql.plugin_storage import SQLPluginStorage
from boefjes.storage.interfaces import OrganisationNotFound
from boefjes.storage.memory import ConfigStorageMemory, OrganisationStorageMemory, PluginStorageMemory
from octopoes.api.models import Declaration, Observation
from octopoes.connector.octopoes import OctopoesAPIConnector
from octopoes.models import OOI
Expand Down Expand Up @@ -152,14 +158,14 @@ def api(tmp_path):

@pytest.fixture
def session():
alembic.config.main(argv=["--config", "/app/boefjes/boefjes/alembic.ini", "upgrade", "head"])
engine = get_engine()
session = sessionmaker(bind=engine)()

yield session

session.execute(";".join([f"TRUNCATE TABLE {t} CASCADE" for t in SQL_BASE.metadata.tables]))
session.commit()
session.close()
engine.execute(";".join([f"TRUNCATE TABLE {t} CASCADE" for t in SQL_BASE.metadata.tables]))


@pytest.fixture
Expand All @@ -178,23 +184,88 @@ def plugin_storage(session):


@pytest.fixture
def local_repo():
def local_repository():
return get_local_repository()


@pytest.fixture
def plugin_service(plugin_storage, config_storage, local_repo):
return PluginService(plugin_storage, config_storage, local_repo)
def mock_local_repository():
return LocalPluginRepository(Path(__file__).parent / "modules")


@pytest.fixture
def organisation(organisation_storage) -> Organisation:
organisation = Organisation(id="test", name="Test org")
def normalizer_runner(local_repository: LocalPluginRepository):
return LocalNormalizerJobRunner(local_repository)


@pytest.fixture
def boefje_runner(local_repository: LocalPluginRepository):
return LocalBoefjeJobRunner(local_repository)


@pytest.fixture
def mock_normalizer_runner(mock_local_repository: LocalPluginRepository):
return LocalNormalizerJobRunner(mock_local_repository)


@pytest.fixture
def mock_boefje_runner(mock_local_repository: LocalPluginRepository):
return LocalBoefjeJobRunner(mock_local_repository)


@pytest.fixture
def plugin_service(plugin_storage, config_storage, local_repository):
return PluginService(plugin_storage, config_storage, local_repository)


@pytest.fixture
def test_organisation():
return Organisation(id="test", name="Test org")


@pytest.fixture
def mock_plugin_service(mock_local_repository, test_organisation) -> PluginService:
storage = ConfigStorageMemory()
storage.upsert(test_organisation.id, "test_plugin", {"DUMMY_VAR": "123"})

return PluginService(PluginStorageMemory(), storage, mock_local_repository)


@pytest.fixture
def organisation(organisation_storage, test_organisation) -> Organisation:
with organisation_storage as repo:
repo.create(organisation)
repo.create(test_organisation)

return test_organisation


@pytest.fixture
def unit_test_client(mock_plugin_service) -> TestClient:
client = TestClient(app)
_store = OrganisationStorageMemory({"test": Organisation(id="test", name="Test")})

services = {
"test": mock_plugin_service,
}

return organisation
def get_service(organisation_id: str):
if organisation_id in services:
return services.get(organisation_id)

raise OrganisationNotFound(organisation_id)

app.dependency_overrides[get_organisations_store] = lambda: _store
app.dependency_overrides[get_plugin_service] = get_service
app.dependency_overrides[check_organisation_exists] = lambda: None

yield client

app.dependency_overrides = {}


@pytest.fixture
def test_client() -> TestClient:
return TestClient(app)


@pytest.fixture
Expand Down
17 changes: 0 additions & 17 deletions boefjes/tests/examples/adr-validator-output.json

This file was deleted.

13 changes: 0 additions & 13 deletions boefjes/tests/examples/crt-normalize.json

This file was deleted.

13 changes: 0 additions & 13 deletions boefjes/tests/examples/crt.json

This file was deleted.

20 changes: 0 additions & 20 deletions boefjes/tests/examples/dns-job.json

This file was deleted.

17 changes: 0 additions & 17 deletions boefjes/tests/examples/dns-normalize2.json

This file was deleted.

18 changes: 0 additions & 18 deletions boefjes/tests/examples/dummy.json

This file was deleted.

20 changes: 0 additions & 20 deletions boefjes/tests/examples/ipv6_nameservers.json

This file was deleted.

27 changes: 0 additions & 27 deletions boefjes/tests/examples/ipv6_nameservers_normalize.json

This file was deleted.

19 changes: 0 additions & 19 deletions boefjes/tests/examples/ipv6_webservers.json

This file was deleted.

26 changes: 0 additions & 26 deletions boefjes/tests/examples/ipv6_webservers_normalize.json

This file was deleted.

9 changes: 0 additions & 9 deletions boefjes/tests/examples/nmap-250-normalize.json

This file was deleted.

13 changes: 0 additions & 13 deletions boefjes/tests/examples/nmap-250.json

This file was deleted.

9 changes: 0 additions & 9 deletions boefjes/tests/examples/nmap-normalize.json

This file was deleted.

Loading

0 comments on commit 32aa48a

Please sign in to comment.