From 0c2cfe7ca9d29a962d4a0481afe584a3caaabed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Wed, 6 Sep 2023 13:15:33 +0200 Subject: [PATCH] fix(test): error initialize tests --- python/apps/taiga/tests/conftest.py | 3 ++- python/apps/taiga/tests/utils/testclient.py | 28 ++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/python/apps/taiga/tests/conftest.py b/python/apps/taiga/tests/conftest.py index dd784da98..cd0801e65 100644 --- a/python/apps/taiga/tests/conftest.py +++ b/python/apps/taiga/tests/conftest.py @@ -10,7 +10,6 @@ import pytest import pytest_asyncio from taiga.base.django.commands import call_django_command -from taiga.events import connect_events_manager from .fixtures import * # noqa @@ -56,6 +55,8 @@ def django_db_setup(django_db_setup, django_db_blocker): @pytest_asyncio.fixture(scope="session", autouse=True) async def connect_events_manage_on_startup(): + from taiga.events import connect_events_manager + await connect_events_manager() diff --git a/python/apps/taiga/tests/utils/testclient.py b/python/apps/taiga/tests/utils/testclient.py index 0ce7fe43b..cd34e9867 100644 --- a/python/apps/taiga/tests/utils/testclient.py +++ b/python/apps/taiga/tests/utils/testclient.py @@ -5,22 +5,21 @@ # # Copyright (c) 2023-present Kaleidos INC +from typing import TYPE_CHECKING + import pytest from fastapi import FastAPI from fastapi.testclient import TestClient as TestClientBase -from taiga.auth.tokens import AccessToken from taiga.base.utils.concurrency import run_async_as_sync -from taiga.events import app as events_app -from taiga.main import api as api_app -from taiga.users.models import User -test_app = FastAPI() -test_app.mount("/events/", app=events_app) -test_app.mount("/", app=api_app) +if TYPE_CHECKING: + from taiga.users.models import User class TestClient(TestClientBase): - def login(self, user: User) -> None: + def login(self, user: "User") -> None: + from taiga.auth.tokens import AccessToken + token = run_async_as_sync(AccessToken.create_for_object(user)) self.headers["Authorization"] = f"Bearer {str(token)}" @@ -28,9 +27,20 @@ def logout(self) -> None: self.headers.pop("Authorization", None) +def _get_test_app() -> FastAPI: + from taiga.events import app as events_app + from taiga.main import api as api_app + + test_app = FastAPI() + test_app.mount("/events/", app=events_app) + test_app.mount("/", app=api_app) + + return test_app + + @pytest.fixture def client() -> TestClient: - return TestClient(test_app) + return TestClient(_get_test_app()) @pytest.fixture