cmvr-visual/backend/tests/mysql_v2_linux.py

80 lines
3.1 KiB
Python
Raw Permalink Normal View History

"""Opt-in MySQL API regression, restricted to the isolated verification host."""
import asyncio
import os
from pathlib import Path
from types import SimpleNamespace
import uuid
import httpx
import pytest_asyncio
from sqlalchemy import text
from sqlalchemy.engine import make_url
from sqlalchemy.ext.asyncio import create_async_engine
from alembic import command
from alembic.config import Config
from app.core.config import Settings
from app.main import create_app
from conftest import FakeProvider, PASSWORD
from test_auth import test_login_cookie_csrf_and_revocation
from test_jobs import test_lost_submit_response_recovers_same_id_and_known_id_never_resubmits
from test_core_v2 import test_submission_changed_body_conflicts_and_recovers
from test_fifo_json import test_prepared_fifo_uses_native_json_and_preserves_payload_and_holds
from test_workflows_v2 import (
test_snapshot_version_idempotency_owner,
test_engine_durable_sequential_cancel_and_no_new_job,
test_batch_mixed_persistent_one_active_member,
test_parallel_run_and_patch_versions_accept_one,
test_batch_does_not_advance_while_previous_cleanup_unknown,
)
@pytest_asyncio.fixture
async def system(tmp_path):
url = make_url(os.environ["CMVR_VERIFY_MYSQL_URL"])
if url.host not in {"cmvr-visual-v2-test-mysql", "cmvr-visual-v4-test-mysql"}:
raise RuntimeError("Refusing non-isolated database")
name = "cmvr_visual_verify_" + uuid.uuid4().hex[:12]
engine = create_async_engine(url.set(database="mysql"))
async with engine.begin() as c:
await c.execute(text(f"CREATE DATABASE `{name}` CHARACTER SET utf8mb4"))
config = Config(str(Path(__file__).parents[1] / "alembic.ini"))
config.attributes["url"] = url.set(database=name)
try:
await asyncio.to_thread(command.upgrade, config, "head")
settings = Settings(
mode="production",
database_url=url.set(database=name),
workspace_root=tmp_path,
admin_username="admin",
admin_password=PASSWORD,
run_coordinator=False,
cookie_secure=False,
)
provider = FakeProvider()
async def caps():
return {"protocol_version": 2, "admission_policy": True}
provider.upstream_capabilities = caps
app = create_app(settings, provider=provider)
async with app.router.lifespan_context(app):
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=app), base_url="http://test"
) as client:
login = await client.post(
"/api/auth/login", json={"username": "admin", "password": PASSWORD}
)
assert login.status_code == 200
client.headers["X-CSRF-Token"] = login.json()["csrf_token"]
yield SimpleNamespace(app=app, client=client, settings=settings, provider=provider)
finally:
async with engine.begin() as c:
await c.execute(text(f"DROP DATABASE `{name}`"))
await engine.dispose()
@pytest_asyncio.fixture
async def wf(system):
return system