From 45859a4809a8651e484b8d540b5e781dea2b9798 Mon Sep 17 00:00:00 2001 From: tiptenbrink <75669206+tiptenbrink@users.noreply.github.com> Date: Thu, 21 Dec 2023 14:37:16 +0100 Subject: [PATCH] chore: add warning mypy takes a while, fix ruff, types --- backend/src/apiserver/app/dependencies.py | 2 +- backend/src/apiserver/app/ops/header.py | 10 +++++++--- backend/src/apiserver/app_def.py | 7 +++++-- backend/src/apiserver/lib/resource/header.py | 2 +- backend/tests/query_test/data_test.py | 2 +- backend/tests/query_test/store_test.py | 4 +++- backend/tests/utilities_test.py | 8 ++++---- test.nu | 1 + 8 files changed, 23 insertions(+), 13 deletions(-) diff --git a/backend/src/apiserver/app/dependencies.py b/backend/src/apiserver/app/dependencies.py index 39948991..5d5f9c8f 100644 --- a/backend/src/apiserver/app/dependencies.py +++ b/backend/src/apiserver/app/dependencies.py @@ -37,7 +37,7 @@ async def dep_auth_context(request: Request) -> AuthContexts: async def dep_header_token( authorization: Authorization, dsrc: SourceDep, app_ctx: AppContext -) -> AccessToken: +) -> AccessToken: try: return await ctxlize(verify_token_header)( app_ctx.authrz_ctx, authorization, dsrc diff --git a/backend/src/apiserver/app/ops/header.py b/backend/src/apiserver/app/ops/header.py index 6548acf7..ad57c103 100644 --- a/backend/src/apiserver/app/ops/header.py +++ b/backend/src/apiserver/app/ops/header.py @@ -20,12 +20,16 @@ class AuthBearerHeader(SecurityBase): - """This allows using the OpenAPI docs (/docs) and enter the access token. Note that it already prepends 'Bearer' + """This allows using the OpenAPI docs (/docs) and enter the access token. Note that it already prepends 'Bearer' to the value, so that's not necessary to add when using the docs.""" + scheme: Literal["bearer"] = "bearer" - def __init__(self): - self.model = HTTPBaseModel(scheme="bearer", description="Provide the access token like 'Bearer '.") + def __init__(self) -> None: + self.model = HTTPBaseModel( + scheme="bearer", + description="Provide the access token like 'Bearer '.", + ) self.scheme_name = "Bearer authorization scheme." async def __call__(self, request: Request) -> str: diff --git a/backend/src/apiserver/app_def.py b/backend/src/apiserver/app_def.py index 6ccf790e..174bd5ce 100644 --- a/backend/src/apiserver/app_def.py +++ b/backend/src/apiserver/app_def.py @@ -58,8 +58,11 @@ async def validation_exception_handler( def define_static_routes() -> list[Mount]: static_credential_path = res_path.joinpath("static/credentials") if not static_credential_path.exists(): - raise AppEnvironmentError(f"Could not find the static HTML files at {static_credential_path}. Did you build " + - "the files for the authpage?") + raise AppEnvironmentError( + f"Could not find the static HTML files at {static_credential_path}. Did you" + " build " + + "the files for the authpage?" + ) credential_mount = Mount( "/credentials", diff --git a/backend/src/apiserver/lib/resource/header.py b/backend/src/apiserver/lib/resource/header.py index 9242501a..6f20279a 100644 --- a/backend/src/apiserver/lib/resource/header.py +++ b/backend/src/apiserver/lib/resource/header.py @@ -22,7 +22,7 @@ def extract_token_and_kid(authorization: str) -> tuple[str, str]: err_type="invalid_request", err_desc="Authorization must follow 'Bearer' scheme", ) - + token = authorization.removeprefix("Bearer ") try: diff --git a/backend/tests/query_test/data_test.py b/backend/tests/query_test/data_test.py index 0b7f03cd..9ed3bc9c 100644 --- a/backend/tests/query_test/data_test.py +++ b/backend/tests/query_test/data_test.py @@ -35,7 +35,6 @@ def event_loop_policy(): return uvloop.EventLoopPolicy() - @pytest.fixture(scope="module") def api_config() -> Fixture[Config]: test_config_path = res_path.joinpath("querytestenv.toml") @@ -84,6 +83,7 @@ async def new_db_store(api_config: Config, admin_engine: Engine): drop_db = text(f"DROP DATABASE {db_name};") conn.execute(drop_db) + @pytest.mark.asyncio async def test_create_class(new_db_store: Store): async with get_conn(new_db_store) as conn: diff --git a/backend/tests/query_test/store_test.py b/backend/tests/query_test/store_test.py index 5dca105d..56ee1ce6 100644 --- a/backend/tests/query_test/store_test.py +++ b/backend/tests/query_test/store_test.py @@ -1,4 +1,3 @@ -import asyncio import os from random import randint from typing import LiteralString @@ -23,15 +22,18 @@ "Skipping store_test as QUERY_TEST is not set.", allow_module_level=True ) + @pytest.fixture(scope="module") def event_loop_policy(): return uvloop.EventLoopPolicy() + @pytest.fixture(scope="module") def api_config() -> Fixture[Config]: test_config_path = res_path.joinpath("querytestenv.toml") yield load_config(test_config_path) + @pytest_asyncio.fixture(scope="module") async def local_store(api_config: Config): # each module makes its own database diff --git a/backend/tests/utilities_test.py b/backend/tests/utilities_test.py index d115fccc..94cbe5aa 100644 --- a/backend/tests/utilities_test.py +++ b/backend/tests/utilities_test.py @@ -1,6 +1,3 @@ -import pytest -from pytest_mock import MockerFixture - import apiserver.lib.utilities as util import auth.core.util @@ -13,8 +10,11 @@ # secrets_patch.side_effect = lambda b: bytes.fromhex("8ca96077b3191e3c") +HASH_LENGTH = 32 + + def test_hash_hex(): - assert len(auth.core.util.random_time_hash_hex()) == 32 + assert len(auth.core.util.random_time_hash_hex()) == HASH_LENGTH def test_usp_hex(): diff --git a/test.nu b/test.nu index 3adbcd11..282cb996 100755 --- a/test.nu +++ b/test.nu @@ -13,6 +13,7 @@ def check_backend [] { cd $backend_dir poetry run black src tests poetry run ruff src tests + print "Running mypy, this could take a while..." poetry run mypy }