generated from ghga-de/microservice-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
142 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2021 - 2023 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln | ||
# for the German Human Genome-Phenome Archive (GHGA) | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
"""Setup for testing the access request service.""" | ||
|
||
import pytest | ||
from hexkit.providers.akafka.testutils import get_kafka_fixture | ||
from hexkit.providers.mongodb.testutils import get_mongodb_fixture | ||
|
||
from .fixtures import JointFixture, get_joint_fixture | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def reset_state(joint_fixture: JointFixture): | ||
"""Clear joint_fixture state before tests. | ||
This is a function-level fixture because it needs to run in each test. | ||
""" | ||
joint_fixture.mongodb.empty_collections() | ||
|
||
|
||
kafka_fixture = get_kafka_fixture("session") | ||
mongodb_fixture = get_mongodb_fixture("session") | ||
joint_fixture = get_joint_fixture("session") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,19 +16,21 @@ | |
"""Fixtures that are used in both integration and unit tests""" | ||
|
||
from collections.abc import AsyncGenerator | ||
from typing import NamedTuple | ||
|
||
import pytest_asyncio | ||
from ghga_service_commons.api.testing import AsyncTestClient | ||
from ghga_service_commons.utils.jwt_helpers import ( | ||
generate_jwk, | ||
sign_and_serialize_token, | ||
) | ||
from hexkit.custom_types import PytestScope | ||
from hexkit.providers.akafka.testutils import KafkaFixture | ||
from hexkit.providers.mongodb.testutils import MongoDbFixture | ||
from pytest import fixture | ||
from pytest_asyncio import fixture as async_fixture | ||
|
||
from ars.config import Config | ||
from ars.inject import prepare_rest_app | ||
from ars.inject import prepare_core, prepare_rest_app | ||
|
||
__all__ = [ | ||
"AUTH_KEY_PAIR", | ||
|
@@ -38,9 +40,9 @@ | |
"fixture_auth_headers_steward", | ||
"fixture_auth_headers_doe_inactive", | ||
"fixture_auth_headers_steward_inactive", | ||
"fixture_client", | ||
"get_joint_fixture", | ||
"JointFixture", | ||
"headers_for_token", | ||
"non_mocked_hosts", | ||
] | ||
|
||
|
||
|
@@ -98,27 +100,42 @@ def fixture_auth_headers_steward() -> dict[str, str]: | |
return headers_for_token(token) | ||
|
||
|
||
@async_fixture(name="client") | ||
async def fixture_client( | ||
kafka_fixture: KafkaFixture, | ||
mongodb_fixture: MongoDbFixture, | ||
) -> AsyncGenerator[AsyncTestClient, None]: | ||
"""Get test client for the access request service""" | ||
class JointFixture(NamedTuple): | ||
"""Joint fixture object.""" | ||
|
||
config: Config | ||
kafka: KafkaFixture | ||
mongodb: MongoDbFixture | ||
rest_client: AsyncTestClient | ||
|
||
|
||
async def joint_fixture_function( | ||
mongodb_fixture: MongoDbFixture, kafka_fixture: KafkaFixture | ||
) -> AsyncGenerator[JointFixture, None]: | ||
"""A fixture that embeds all other fixtures for API-level integration testing | ||
**Do not call directly** Instead, use get_joint_fixture(). | ||
""" | ||
config = Config( | ||
auth_key=AUTH_KEY_PAIR.export_public(), # pyright: ignore | ||
download_access_url="http://access", | ||
data_steward_email="[email protected]", | ||
**kafka_fixture.config.model_dump(), | ||
**mongodb_fixture.config.model_dump(), | ||
) | ||
|
||
async with prepare_rest_app(config=config) as app: | ||
async with AsyncTestClient(app=app) as client: | ||
yield client | ||
|
||
|
||
@fixture | ||
def non_mocked_hosts() -> list[str]: | ||
"""Get hosts that are not mocked by pytest-httpx.""" | ||
return ["test", "localhost"] | ||
async with prepare_core(config=config) as core: | ||
async with ( | ||
prepare_rest_app(config=config, core_override=core) as app, | ||
): | ||
async with AsyncTestClient(app=app) as rest_client: | ||
yield JointFixture( | ||
config=config, | ||
kafka=kafka_fixture, | ||
mongodb=mongodb_fixture, | ||
rest_client=rest_client, | ||
) | ||
|
||
|
||
def get_joint_fixture(scope: PytestScope = "function"): | ||
"""Produce a joint fixture with desired scope""" | ||
return pytest_asyncio.fixture(joint_fixture_function, scope=scope) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.