Skip to content

Commit

Permalink
refactor: sort fixtures to renamed plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
andiserg committed Feb 29, 2024
1 parent 0640953 commit d63798b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 53 deletions.
55 changes: 2 additions & 53 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,6 @@
import os
from unittest.mock import Mock

import pytest
from pytest_asyncio import fixture

from costy.application.common.id_provider import IdProvider
from costy.domain.models.user import UserId

pytest_plugins = [
"tests.fixtures.adapters",
"tests.fixtures.infrastructure",
"tests.fixtures.db",
"tests.fixtures.templates",
"tests.fixtures.data",
"tests.fixtures.database",
]


@fixture
async def credentials() -> dict[str, str]: # type: ignore
try:
return {
"username": os.environ["TEST_AUTH_USER"],
"password": os.environ["TEST_AUTH_PASSWORD"]
}
except KeyError:
pytest.skip("No test user credentials.")


# global is used because tests cannot use a "session" fixed fixture in this case
user_token_state = None


@fixture
async def user_token(auth_adapter, credentials): # type: ignore
global user_token_state
if not user_token_state:
response = await auth_adapter.authenticate(credentials["username"], credentials["password"])
if response:
return response
pytest.skip("Failed to test user authenticate.")
else:
return user_token_state


@fixture
async def auth_sub() -> str: # type: ignore
try:
return os.environ["TEST_AUTH_USER_SUB"].replace("auth|0", "")
except KeyError:
pytest.skip("No test user sub environment variable.")


@fixture
async def id_provider(user_id: UserId) -> IdProvider:
provider = Mock(spec=IdProvider)
provider.get_current_user_id.return_value = user_id
return provider
11 changes: 11 additions & 0 deletions tests/fixtures/adapters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from unittest.mock import Mock

from pytest_asyncio import fixture

from costy.adapters.auth.auth_gateway import AuthGateway
from costy.adapters.db.category_gateway import CategoryGateway
from costy.adapters.db.operation_gateway import OperationGateway
from costy.adapters.db.user_gateway import UserGateway
from costy.application.common.auth_gateway import AuthLoger
from costy.application.common.id_provider import IdProvider
from costy.domain.models.user import UserId
from costy.infrastructure.config import AuthSettings, get_auth_settings


Expand All @@ -31,3 +35,10 @@ async def category_gateway(db_session, db_tables, retort) -> CategoryGateway:
@fixture
async def operation_gateway(db_session, db_tables, retort) -> OperationGateway:
return OperationGateway(db_session, db_tables["operations"], retort)


@fixture
async def id_provider(user_id: UserId) -> IdProvider:
provider = Mock(spec=IdProvider)
provider.get_current_user_id.return_value = user_id
return provider
38 changes: 38 additions & 0 deletions tests/fixtures/templates.py → tests/fixtures/data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os

import pytest
from pytest_asyncio import fixture

from costy.application.authenticate import LoginInputDTO
Expand Down Expand Up @@ -62,3 +65,38 @@ async def token() -> str:
@fixture
async def auth_id() -> str:
return "auth_id"


# global is used because tests cannot use a "session" fixed fixture in this case
user_token_state = None


@fixture
async def user_token(auth_adapter, credentials): # type: ignore
global user_token_state
if not user_token_state:
response = await auth_adapter.authenticate(credentials["username"], credentials["password"])
if response:
return response
pytest.skip("Failed to test user authenticate.")
else:
return user_token_state


@fixture
async def auth_sub() -> str: # type: ignore
try:
return os.environ["TEST_AUTH_USER_SUB"].replace("auth|0", "")
except KeyError:
pytest.skip("No test user sub environment variable.")


@fixture
async def credentials() -> dict[str, str]: # type: ignore
try:
return {
"username": os.environ["TEST_AUTH_USER"],
"password": os.environ["TEST_AUTH_PASSWORD"]
}
except KeyError:
pytest.skip("No test user credentials.")
File renamed without changes.

0 comments on commit d63798b

Please sign in to comment.