Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /spraakbanken_karp-backend /tests /e2e /conftest.py
| """Pytest entry point.""" | |
| import os | |
| import typing | |
| from typing import Any, Generator, Optional, Tuple | |
| import elasticsearch_test | |
| import pytest | |
| from fastapi import FastAPI | |
| from starlette.testclient import TestClient | |
| # TODO do not reorder these, fix deps | |
| from karp.main.config import env # isort: skip | |
| from karp.globals import new_session # isort: skip | |
| from karp import auth, resource_commands # isort: skip | |
| from karp.lex.domain.value_objects import ResourceConfig | |
| from karp.search.infrastructure.opensearch import mapping_repo | |
| from tests import common_data, utils | |
| from tests.integration.auth.adapters import create_access_token | |
| class AccessToken: | |
| def __init__(self, access_token): | |
| self.access_token = access_token | |
| def as_header(self) -> typing.Dict[str, str]: | |
| return { | |
| "Authorization": f"Bearer {self.access_token}", | |
| } | |
| def apply_migrations(): | |
| from karp.main.migrations import use_cases | |
| use_cases.run_migrations_up() | |
| yield | |
| def fixture_app(apply_migrations: None, init_search_service: None) -> Generator[FastAPI, None, None]: | |
| from karp.api.main import create_app | |
| yield create_app() | |
| print("dropping app") | |
| app = None # noqa: F841 | |
| def fixture_client(app: FastAPI) -> Generator[TestClient, None, None]: | |
| with TestClient(app) as client: | |
| yield client | |
| def reset_caches(): | |
| """ | |
| The resources are recreated multiple times during the test run and indices will change name. | |
| Because the names are cached in mapping_repo on module level, we have to manually reset the | |
| caches to make all tests pass in the same run. | |
| """ | |
| yield | |
| for func in dir(mapping_repo): | |
| func = getattr(mapping_repo, func) | |
| clear_fun = getattr(func, "cache_clear", None) | |
| if clear_fun: | |
| clear_fun() | |
| def create_and_publish_resource(*, path_to_config: str) -> Tuple[bool, Optional[dict[str, Any]]]: | |
| resource_config = ResourceConfig.from_path(path_to_config) | |
| resource_id = resource_config.resource_id | |
| with new_session(): | |
| resource_commands.create_resource(resource_config, "") | |
| resource_commands.publish_resource(user="", resource_id=resource_id, version=1, message="") | |
| def fixture_fa_data_client( # noqa: ANN201 | |
| fa_client, | |
| admin_token: AccessToken, | |
| ): | |
| create_and_publish_resource(path_to_config="assets/testing/config/municipalities.yaml") | |
| create_and_publish_resource(path_to_config="assets/testing/config/places.yaml") | |
| utils.add_entries( | |
| fa_client, | |
| {"municipalities": common_data.MUNICIPALITIES, "places": common_data.PLACES}, | |
| access_token=admin_token, | |
| ) | |
| return fa_client | |
| auth_levels: typing.Dict[str, int] = {level.value: (idx + 1) * 10 for idx, level in enumerate(auth.PermissionLevel)} | |
| def create_bearer_token( | |
| scope: typing.Dict, | |
| user: Optional[str] = "abc123", | |
| ) -> AccessToken: | |
| levels = auth_levels | |
| return AccessToken( | |
| access_token=create_access_token(user, levels, scope), | |
| ) | |
| def user1_token() -> AccessToken: | |
| return create_bearer_token( | |
| user="user1", | |
| scope={ | |
| "lexica": { | |
| "places": auth_levels[auth.PermissionLevel.write], | |
| } | |
| }, | |
| ) | |
| def user2_token() -> AccessToken: | |
| return create_bearer_token( | |
| user="user2", | |
| scope={ | |
| "lexica": { | |
| "places": auth_levels[auth.PermissionLevel.write], | |
| } | |
| }, | |
| ) | |
| def user4_token() -> AccessToken: | |
| return create_bearer_token( | |
| scope={ | |
| "lexica": { | |
| "places": auth_levels[auth.PermissionLevel.admin], | |
| } | |
| }, | |
| ) | |
| def admin_token() -> AccessToken: | |
| return create_bearer_token( | |
| scope={ | |
| "lexica": { | |
| "places": auth_levels[auth.PermissionLevel.admin], | |
| "test_resource": auth_levels[auth.PermissionLevel.admin], | |
| "municipalities": auth_levels[auth.PermissionLevel.admin], | |
| } | |
| }, | |
| ) | |
| def read_token() -> AccessToken: | |
| return create_bearer_token( | |
| scope={ | |
| "lexica": { | |
| "places": auth_levels[auth.PermissionLevel.read], | |
| "test_resource": auth_levels[auth.PermissionLevel.read], | |
| "municipalities": auth_levels[auth.PermissionLevel.read], | |
| } | |
| }, | |
| ) | |
| def write_token() -> AccessToken: | |
| return create_bearer_token( | |
| scope={ | |
| "lexica": { | |
| "places": auth_levels[auth.PermissionLevel.write], | |
| "test_resource": auth_levels[auth.PermissionLevel.write], | |
| "municipalities": auth_levels[auth.PermissionLevel.write], | |
| } | |
| }, | |
| ) | |
| def no_municipalities_token() -> AccessToken: | |
| return create_bearer_token( | |
| scope={"lexica": {}}, | |
| ) | |
| def fixture_init_search_service(): | |
| if not env("TEST_ES_HOME"): | |
| raise RuntimeError("must set ES_HOME to run tests that use elasticsearch") | |
| es_port = int(os.environ.get("TEST_ELASTICSEARCH_PORT", "9202")) | |
| es_test = elasticsearch_test.ElasticsearchTest(port=es_port, es_path=env("TEST_ES_HOME")) | |
| # if there is an instance running with this configuration, use it | |
| if es_test.is_started(): | |
| yield | |
| # else, start it | |
| else: | |
| with es_test: | |
| yield | |
Xet Storage Details
- Size:
- 5.88 kB
- Xet hash:
- 2b2e874d482c1c581e18fd1517fa633d1d5dd65d303999dd09a11bb4082f6645
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.