from __future__ import annotations import asyncio import uuid from pathlib import Path from fastapi.testclient import TestClient from app.config import Settings from app.main import create_app def _build_settings() -> Settings: data_dir = Path("data/testdb") data_dir.mkdir(parents=True, exist_ok=True) db_path = data_dir / f"web_{uuid.uuid4().hex}.db" return Settings( db_path=str(db_path), session_secret="test-secret", web_admin_login="admin", web_admin_password="admin", max_bot_token="", max_use_webhook=True, max_webhook_secret="s3cr3t", channel_local_enabled=True, channel_external_enabled=False, context_backend="memory", operator_chat_ids=[], request_categories=["A", "B"], ) def test_login_and_applications_page(): app = create_app(_build_settings()) with TestClient(app) as client: # Protected endpoint redirects to login. response = client.get("/applications", follow_redirects=False) assert response.status_code == 303 assert response.headers["location"] == "/login" response = client.post("/login", data={"username": "admin", "password": "admin"}, follow_redirects=False) assert response.status_code == 303 assert response.headers["location"] == "/applications" # Create one request via service and check list page. outcome = asyncio.run( client.app.state.container.app_service.create_application( customer_id=1, customer_name="Client", chat_id=100, category="A", description="Need support", attachments=[], source="test", ) ) assert outcome.application is not None page = client.get("/applications") assert page.status_code == 200 assert "Заявки" in page.text assert f"#{outcome.application.id}" in page.text def test_webhook_secret_validation(): app = create_app(_build_settings()) with TestClient(app) as client: bad = client.post("/max/webhook", json={"foo": "bar"}) assert bad.status_code == 401 ok = client.post("/max/webhook", json={"foo": "bar"}, headers={"x-max-webhook-secret": "s3cr3t"}) assert ok.status_code == 200