Buckets:
| import asyncio | |
| import contextlib | |
| import json | |
| import secrets | |
| import uuid | |
| from collections.abc import AsyncGenerator, Callable, Coroutine, Generator | |
| from typing import cast | |
| from unittest.mock import MagicMock, patch | |
| import asgi_lifespan | |
| import httpx | |
| import pytest | |
| import pytest_asyncio | |
| from dramatiq import Actor, Message | |
| from fastapi import FastAPI | |
| from fief_client import FiefAsync | |
| from sqlalchemy_utils import create_database, drop_database | |
| from fief.apps import api_app, auth_app, dashboard_app | |
| from fief.crypto.token import generate_token | |
| from fief.db import AsyncEngine, AsyncSession | |
| from fief.db.engine import create_engine | |
| from fief.db.migration import migrate_schema | |
| from fief.db.types import DatabaseConnectionParameters, DatabaseType, get_driver | |
| from fief.dependencies.db import get_main_async_session | |
| from fief.dependencies.fief import get_fief | |
| from fief.dependencies.tasks import get_send_task | |
| from fief.dependencies.tenant_email_domain import get_tenant_email_domain | |
| from fief.dependencies.theme import get_theme_preview | |
| from fief.models import AdminAPIKey, AdminSessionToken, User | |
| from fief.services.tenant_email_domain import TenantEmailDomain | |
| from fief.services.theme_preview import ThemePreview | |
| from fief.settings import settings | |
| from tests.data import ModelMapping, TestData, data_mapping, session_token_tokens | |
| from tests.types import GetTestDatabase, HTTPClientGeneratorType, TenantParams | |
| pytest.register_assert_rewrite("tests.helpers") | |
| def event_loop(): | |
| """Force the pytest-asyncio loop to be the main one.""" | |
| loop = asyncio.new_event_loop() | |
| yield loop | |
| loop.close() | |
| def get_test_database(worker_id: str) -> GetTestDatabase: | |
| async def _get_test_database( | |
| *, name: str = "fief-test", drop: bool = True | |
| ) -> AsyncGenerator[tuple[DatabaseConnectionParameters, DatabaseType], None]: | |
| url, connect_args = settings.get_database_connection_parameters(False) | |
| name = f"{name}-{worker_id}" | |
| url = url.set(database=name) | |
| assert url.database == name | |
| create_database(url) | |
| yield ((url, connect_args), settings.database_type) | |
| try: | |
| drop_database(url) | |
| # Silently ignore error when we try to drop an already removed SQLite DB | |
| except FileNotFoundError: | |
| pass | |
| return _get_test_database | |
| async def main_test_database( | |
| get_test_database: GetTestDatabase, | |
| ) -> AsyncGenerator[tuple[DatabaseConnectionParameters, DatabaseType], None]: | |
| async with get_test_database() as (database_connection_parameters, database_type): | |
| url, connect_args = database_connection_parameters | |
| url = url.set(drivername=get_driver(database_type, asyncio=True)) | |
| yield (url, connect_args), database_type | |
| async def main_engine( | |
| main_test_database: tuple[DatabaseConnectionParameters, DatabaseType], | |
| ) -> AsyncGenerator[AsyncEngine, None]: | |
| database_connection_parameters, _ = main_test_database | |
| engine = create_engine(database_connection_parameters) | |
| yield engine | |
| await engine.dispose() | |
| async def test_data(main_engine: AsyncEngine) -> AsyncGenerator[TestData, None]: | |
| await migrate_schema(main_engine) | |
| async with main_engine.begin() as connection: | |
| async with AsyncSession(bind=connection, expire_on_commit=False) as session: | |
| for model in data_mapping.values(): | |
| for object in cast(ModelMapping, model).values(): | |
| session.add(object) | |
| await session.commit() | |
| yield data_mapping | |
| async def main_session(main_engine: AsyncEngine) -> AsyncGenerator[AsyncSession, None]: | |
| connection = await main_engine.connect() | |
| transaction = await connection.begin() | |
| session = AsyncSession( | |
| bind=connection, | |
| expire_on_commit=False, | |
| autocommit=False, | |
| autoflush=False, | |
| ) | |
| yield session | |
| session.expunge_all() | |
| await transaction.rollback() | |
| await connection.close() | |
| def main_session_manager(main_session: AsyncSession): | |
| async def _main_session_manager(*args, **kwargs): | |
| yield main_session | |
| return _main_session_manager | |
| def not_existing_uuid() -> uuid.UUID: | |
| return uuid.uuid4() | |
| async def fief_client_mock() -> MagicMock: | |
| return MagicMock(spec=FiefAsync) | |
| async def tenant_email_domain_mock() -> MagicMock: | |
| return MagicMock(spec=TenantEmailDomain) | |
| async def send_task_mock() -> MagicMock: | |
| def _send_task(task: Actor, *args, **kwargs): | |
| message: Message = Message( | |
| queue_name=task.queue_name, | |
| actor_name=task.actor_name, | |
| args=args, | |
| kwargs=kwargs or {}, | |
| options={}, | |
| ) | |
| message.encode() | |
| return MagicMock(side_effect=_send_task) | |
| async def theme_preview_mock() -> MagicMock: | |
| return MagicMock(spec=ThemePreview) | |
| def smtplib_mock() -> Generator[MagicMock, None, None]: | |
| with patch("smtplib.SMTP", autospec=True) as mock: | |
| yield mock | |
| async def create_admin_session_token( | |
| user: User, main_session: AsyncSession | |
| ) -> tuple[AdminSessionToken, str]: | |
| token, token_hash = generate_token() | |
| session_token = AdminSessionToken( | |
| token=token_hash, | |
| raw_tokens="{}", | |
| raw_userinfo=json.dumps(user.get_claims()), | |
| ) | |
| main_session.add(session_token) | |
| await main_session.commit() | |
| return session_token, token | |
| async def create_api_key( | |
| main_session: AsyncSession, | |
| ) -> AsyncGenerator[tuple[AdminAPIKey, str], None]: | |
| token, token_hash = generate_token() | |
| admin_api_key = AdminAPIKey(name="API Key", token=token_hash) | |
| main_session.add(admin_api_key) | |
| await main_session.commit() | |
| yield (admin_api_key, token) | |
| await main_session.delete(admin_api_key) | |
| async def admin_api_key( | |
| main_session: AsyncSession, | |
| ) -> AsyncGenerator[tuple[AdminAPIKey, str], None]: | |
| async with create_api_key(main_session) as result: | |
| yield result | |
| def authenticated_admin( | |
| request: pytest.FixtureRequest, | |
| admin_api_key: tuple[AdminAPIKey, str], | |
| test_data: TestData, | |
| main_session: AsyncSession, | |
| ) -> Callable[[httpx.AsyncClient], Coroutine[None, None, httpx.AsyncClient]]: | |
| async def _authenticated_admin(client: httpx.AsyncClient) -> httpx.AsyncClient: | |
| marker = request.node.get_closest_marker("authenticated_admin") | |
| if marker: | |
| mode = marker.kwargs.get("mode", "api_key") | |
| assert mode in {"session", "api_key"} | |
| if mode == "session": | |
| user_alias = marker.kwargs.get("user", "admin") | |
| user = test_data["users"][user_alias] | |
| _, token = await create_admin_session_token(user, main_session) | |
| client.cookies.set(settings.fief_admin_session_cookie_name, token) | |
| elif mode == "api_key": | |
| _, token = admin_api_key | |
| client.headers["Authorization"] = f"Bearer {token}" | |
| return client | |
| return _authenticated_admin | |
| def tenant_params(request, test_data: TestData) -> TenantParams: | |
| params = request.param | |
| return TenantParams( | |
| path_prefix=params["path_prefix"], | |
| tenant=test_data["tenants"][params["tenant_alias"]], | |
| client=test_data["clients"][params["client_alias"]], | |
| user=test_data["users"][params["user_alias"]], | |
| login_session=test_data["login_sessions"][params["login_session_alias"]], | |
| registration_session_password=test_data["registration_sessions"][ | |
| params["registration_session_password_alias"] | |
| ], | |
| registration_session_oauth=test_data["registration_sessions"][ | |
| params["registration_session_oauth_alias"] | |
| ], | |
| session_token=test_data["session_tokens"][params["session_token_alias"]], | |
| session_token_token=session_token_tokens[params["session_token_alias"]], | |
| ) | |
| def csrf_token() -> str: | |
| return secrets.token_urlsafe() | |
| async def test_client_generator( | |
| main_session: AsyncSession, | |
| send_task_mock: MagicMock, | |
| fief_client_mock: MagicMock, | |
| theme_preview_mock: MagicMock, | |
| tenant_email_domain_mock: MagicMock, | |
| authenticated_admin: Callable[ | |
| [httpx.AsyncClient], Coroutine[None, None, httpx.AsyncClient] | |
| ], | |
| ) -> HTTPClientGeneratorType: | |
| async def _test_client_generator(app: FastAPI): | |
| app.dependency_overrides = {} | |
| app.dependency_overrides[get_main_async_session] = lambda: main_session | |
| app.dependency_overrides[get_send_task] = lambda: send_task_mock | |
| app.dependency_overrides[get_fief] = lambda: fief_client_mock | |
| app.dependency_overrides[get_theme_preview] = lambda: theme_preview_mock | |
| app.dependency_overrides[get_tenant_email_domain] = ( | |
| lambda: tenant_email_domain_mock | |
| ) | |
| settings.fief_admin_session_cookie_domain = "" | |
| async with asgi_lifespan.LifespanManager(app): | |
| async with httpx.AsyncClient( | |
| transport=httpx.ASGITransport(app=app), base_url="http://api.fief.dev" | |
| ) as test_client: | |
| test_client = await authenticated_admin(test_client) | |
| yield test_client | |
| return _test_client_generator | |
| async def test_client_api( | |
| test_client_generator: HTTPClientGeneratorType, | |
| ) -> AsyncGenerator[httpx.AsyncClient, None]: | |
| async with test_client_generator(api_app) as test_client: | |
| yield test_client | |
| def htmx(request: pytest.FixtureRequest): | |
| def _htmx(client: httpx.AsyncClient) -> httpx.AsyncClient: | |
| if request.param: | |
| client.headers["HX-Request"] = "true" | |
| marker = request.node.get_closest_marker("htmx") | |
| if marker: | |
| target: str | None = marker.kwargs.get("target") | |
| if target: | |
| client.headers["HX-Target"] = target | |
| return client | |
| return _htmx | |
| async def test_client_dashboard( | |
| test_client_generator: HTTPClientGeneratorType, | |
| htmx: Callable[[httpx.AsyncClient], httpx.AsyncClient], | |
| csrf_token: str, | |
| ) -> AsyncGenerator[httpx.AsyncClient, None]: | |
| async with test_client_generator(dashboard_app) as test_client: | |
| test_client.cookies.set(settings.csrf_cookie_name, csrf_token) | |
| test_client = htmx(test_client) | |
| yield test_client | |
| async def test_client_auth( | |
| test_client_generator: HTTPClientGeneratorType, | |
| ) -> AsyncGenerator[httpx.AsyncClient, None]: | |
| async with test_client_generator(auth_app) as test_client: | |
| yield test_client | |
| async def test_client_auth_csrf( | |
| test_client_auth: httpx.AsyncClient, csrf_token: str | |
| ) -> AsyncGenerator[httpx.AsyncClient, None]: | |
| test_client_auth.cookies.set(settings.csrf_cookie_name, csrf_token) | |
| yield test_client_auth | |
| def pytest_generate_tests(metafunc: pytest.Metafunc): | |
| """ | |
| Automatically parametrize tests which include | |
| `unauthorized_dashboard_assertions` or `unauthorized_api_assertions` fixture. | |
| The parametrization adds two cases: | |
| * A case where the client is not authenticated. | |
| * A case where the client is authenticated but not an admin. | |
| The parametrization injects the corresponding assertion function for each case. | |
| It helps to quickly add tests for those cases that are critical for security. | |
| """ | |
| if "unauthorized_dashboard_assertions" in metafunc.fixturenames: | |
| from tests.helpers import ( | |
| dashboard_forbidden_assertions, | |
| dashboard_unauthorized_assertions, | |
| ) | |
| metafunc.parametrize( | |
| "unauthorized_dashboard_assertions", | |
| [ | |
| pytest.param(dashboard_unauthorized_assertions, id="Unauthorized"), | |
| pytest.param( | |
| dashboard_forbidden_assertions, | |
| marks=pytest.mark.authenticated_admin( | |
| mode="session", user="regular" | |
| ), | |
| id="Forbidden", | |
| ), | |
| ], | |
| ) | |
| elif "unauthorized_api_assertions" in metafunc.fixturenames: | |
| from tests.helpers import api_unauthorized_assertions | |
| metafunc.parametrize( | |
| "unauthorized_api_assertions", | |
| [pytest.param(api_unauthorized_assertions, id="Unauthorized")], | |
| ) | |
Xet Storage Details
- Size:
- 13.9 kB
- Xet hash:
- eb37315fd67dd9f7908081b97e0c200ac50af4d207c524780ba9fc381ff23101
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.