Files
webmail-client/backend/tests/conftest.py
T
lukasandClaude Opus 5.5 b88855931e M6 hosted mailboxes (off by default); dark message mode; fixes
Hosted mailboxes (SPEC App. D), behind HOSTED_MAILBOXES (off: unchanged):
- Stalwart mail server, provisioned by `python -m app.hosted.provision`;
  directory read from a Postgres view, app passwords only for mail apps
- native account per user, the worker signs in as master for the user
- raw messages of hosted mailboxes only cached, fetched back on demand
- migrations 0025 (accounts.kind, directory view), 0026 (IMAP ids bigint)

Mail:
- dark message mode switch in the reading pane (Thunderbird's algorithm)
- moves and folder creation quote the folder name ("Deleted Items"):
  swipe-to-delete / Trash no longer bounce back
- thread unread counters: lock before recount, sorted lock order,
  UIDVALIDITY re-import recounts, nightly repair_threads
- sidebar accounts sorted alphabetically, more space above headings

Settings: Security headings renamed; antispam accounts are explicit
(none ticked = none filtered), input widths, Activity hidden.
Plugin bundles served with Cache-Control: no-cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 19:25:10 +02:00

83 lines
3.1 KiB
Python

"""Shared test fixtures and environment defaults.
Ensures the SPEC §14 env vars exist even when tests run outside the conda
activate hook (e.g. CI), so ``app.config`` imports cleanly.
"""
from __future__ import annotations
import base64
import os
os.environ.setdefault("APP_DOMAIN", "test.webmail.local")
os.environ.setdefault(
"DATABASE_URL", "postgresql+asyncpg://webmail:webmail_dev@127.0.0.1:5433/webmail_dev"
)
# Force a dedicated Redis DB for tests (index 15) so the suite is isolated from the
# dev app's Redis (DB 0) and can be flushed between tests without touching real data.
os.environ["REDIS_URL"] = "redis://127.0.0.1:6380/15"
os.environ.setdefault("MASTER_KEY", base64.b64encode(b"\x00" * 32).decode())
os.environ.setdefault("BLOB_ROOT", "./var/blobs-test")
# Hosted mailboxes off: tests that need the mail server turn them on themselves
# (tests/hosted/), against the dev stack's Stalwart container.
os.environ["HOSTED_MAILBOXES"] = "false"
for _key, _value in {
"STALWART_URL": "http://127.0.0.1:8180",
"STALWART_ADMIN_SECRET": "stalwart_admin_dev",
"STALWART_MASTER_SECRET": "stalwart_master_dev",
"STALWART_IMAP_HOST": "127.0.0.1",
"STALWART_IMAP_PORT": "1993",
"STALWART_SMTP_HOST": "127.0.0.1",
"STALWART_SMTP_PORT": "1465",
}.items():
os.environ.setdefault(_key, _value)
from collections.abc import AsyncIterator # noqa: E402
import pytest_asyncio # noqa: E402
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine # noqa: E402
from sqlalchemy.pool import NullPool # noqa: E402
from app.config import get_settings # noqa: E402
@pytest_asyncio.fixture(autouse=True)
async def _reset_global_engine() -> AsyncIterator[None]:
"""Dispose process-wide async resources after each test.
Each test runs on its own event loop; disposing here ensures the next test
(and any code using the global engine/redis, e.g. the search backend) rebinds
to its own loop rather than reusing a closed one.
"""
from app.redis_client import get_redis
# Flush the dedicated test Redis DB so rate-limit / session / token state does
# not leak between tests (each test starts from a clean slate).
await get_redis().flushdb()
yield
from app.db import dispose_engine
from app.queue import close_arq_pool
from app.redis_client import close_redis
await dispose_engine()
await close_redis()
# The arq enqueue pool is a loop-bound global like the engine/redis; reset it so
# a test that enqueues doesn't reuse a pool from a previous (closed) event loop.
await close_arq_pool()
@pytest_asyncio.fixture
async def db_session() -> AsyncIterator[AsyncSession]:
"""Rollback-per-test session (nothing persists to the dev database)."""
engine = create_async_engine(get_settings().database_url, poolclass=NullPool)
conn = await engine.connect()
trans = await conn.begin()
session = AsyncSession(bind=conn, expire_on_commit=False)
try:
yield session
finally:
await session.close()
await trans.rollback()
await conn.close()
await engine.dispose()