kin: BATON-008-backend_dev
This commit is contained in:
parent
36087c3d9e
commit
fde7f57a7a
5 changed files with 48 additions and 20 deletions
|
|
@ -22,6 +22,7 @@ os.environ.setdefault("WEBHOOK_SECRET", "test-webhook-secret")
|
|||
os.environ.setdefault("WEBHOOK_URL", "https://example.com/api/webhook/telegram")
|
||||
os.environ.setdefault("FRONTEND_ORIGIN", "http://localhost:3000")
|
||||
os.environ.setdefault("ADMIN_TOKEN", "test-admin-token")
|
||||
os.environ.setdefault("ADMIN_CHAT_ID", "5694335584")
|
||||
|
||||
# ── 2. aiosqlite monkey-patch ────────────────────────────────────────────────
|
||||
import aiosqlite
|
||||
|
|
@ -69,14 +70,20 @@ def temp_db():
|
|||
|
||||
# ── 5. App client factory ────────────────────────────────────────────────────
|
||||
|
||||
def make_app_client():
|
||||
def make_app_client(capture_send_requests: list | None = None):
|
||||
"""
|
||||
Async context manager that:
|
||||
1. Assigns a fresh temp-file DB path
|
||||
2. Mocks Telegram setWebhook, sendMessage, answerCallbackQuery, editMessageText
|
||||
3. Runs the FastAPI lifespan (startup → test → shutdown)
|
||||
4. Yields an httpx.AsyncClient wired to the app
|
||||
|
||||
Args:
|
||||
capture_send_requests: if provided, each sendMessage request body (dict) is
|
||||
appended to this list, enabling HTTP-level assertions on chat_id, text, etc.
|
||||
"""
|
||||
import json as _json
|
||||
|
||||
tg_set_url = f"https://api.telegram.org/bot{config.BOT_TOKEN}/setWebhook"
|
||||
send_url = f"https://api.telegram.org/bot{config.BOT_TOKEN}/sendMessage"
|
||||
get_me_url = f"https://api.telegram.org/bot{config.BOT_TOKEN}/getMe"
|
||||
|
|
@ -95,9 +102,18 @@ def make_app_client():
|
|||
mock_router.post(tg_set_url).mock(
|
||||
return_value=httpx.Response(200, json={"ok": True, "result": True})
|
||||
)
|
||||
mock_router.post(send_url).mock(
|
||||
return_value=httpx.Response(200, json={"ok": True})
|
||||
)
|
||||
if capture_send_requests is not None:
|
||||
def _capture_send(request: httpx.Request) -> httpx.Response:
|
||||
try:
|
||||
capture_send_requests.append(_json.loads(request.content))
|
||||
except Exception:
|
||||
pass
|
||||
return httpx.Response(200, json={"ok": True})
|
||||
mock_router.post(send_url).mock(side_effect=_capture_send)
|
||||
else:
|
||||
mock_router.post(send_url).mock(
|
||||
return_value=httpx.Response(200, json={"ok": True})
|
||||
)
|
||||
mock_router.post(answer_cb_url).mock(
|
||||
return_value=httpx.Response(200, json={"ok": True})
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue