diff --git a/tests/conftest.py b/tests/conftest.py index 866ba89..fa9b405 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,8 +3,8 @@ from fastapi.testclient import TestClient import pytest import os - import pytest_asyncio +from httpx._types import HeaderTypes from transcriptomics_data_service.db import Database, get_db from transcriptomics_data_service.logger import get_logger @@ -56,3 +56,11 @@ def test_client(db: Database): with TestClient(app) as client: app.dependency_overrides[get_db] = get_test_db yield client + + +@pytest.fixture +def authz_headers(config) -> HeaderTypes: + api_key = config.model_extra.get("api_key") + return { + "x-api-key": api_key + } diff --git a/tests/test_authz_plugin.py b/tests/test_authz_plugin.py new file mode 100644 index 0000000..d12d2ab --- /dev/null +++ b/tests/test_authz_plugin.py @@ -0,0 +1,8 @@ +from transcriptomics_data_service.authz.plugin import import_module_from_path + + +def test_import_authz_plugin_disabled(): + config = lambda: None + config.bento_authz_enabled = False + authz_plugin = import_module_from_path("", config) + assert authz_plugin == None diff --git a/tests/test_expressions.py b/tests/test_expressions.py new file mode 100644 index 0000000..bc71e20 --- /dev/null +++ b/tests/test_expressions.py @@ -0,0 +1,18 @@ +from fastapi import status +from fastapi.testclient import TestClient +from transcriptomics_data_service.config import get_config +from transcriptomics_data_service.logger import get_logger + + +config = get_config() +logger = get_logger(config) + +api_key = config.model_extra.get("api_key") + +def test_expressions_unauthorized(test_client: TestClient): + response = test_client.get("/expressions") + assert response.status_code == status.HTTP_400_BAD_REQUEST + +def test_expressions_authorized(test_client: TestClient, authz_headers): + response = test_client.get("/expressions", headers=authz_headers) + assert response.status_code == status.HTTP_200_OK diff --git a/transcriptomics_data_service/authz/plugin.py b/transcriptomics_data_service/authz/plugin.py index e1a097f..6a2a7e2 100644 --- a/transcriptomics_data_service/authz/plugin.py +++ b/transcriptomics_data_service/authz/plugin.py @@ -6,10 +6,7 @@ __all__ = ["authz_plugin"] -config = get_config() - - -def import_module_from_path(path) -> None | ModuleType: +def import_module_from_path(path, config) -> None | ModuleType: if config.bento_authz_enabled: spec = importlib.util.spec_from_file_location("authz_plugin", path) module = importlib.util.module_from_spec(spec) @@ -20,4 +17,6 @@ def import_module_from_path(path) -> None | ModuleType: # Get the concrete authz middleware from the provided plugin module -authz_plugin: BaseAuthzMiddleware = import_module_from_path("./lib/authz.module.py") +authz_plugin: BaseAuthzMiddleware = import_module_from_path( + "./lib/authz.module.py", get_config() +)