Skip to content

Commit

Permalink
tests: expressions and disabled authz plugin import
Browse files Browse the repository at this point in the history
  • Loading branch information
v-rocheleau committed Nov 22, 2024
1 parent 1ababf6 commit 96a1fc8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions tests/test_authz_plugin.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions tests/test_expressions.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 4 additions & 5 deletions transcriptomics_data_service/authz/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
)

0 comments on commit 96a1fc8

Please sign in to comment.