61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
"""
|
|
Tests for BATON-ARCH-012: WEBHOOK_ENABLED flag for local development.
|
|
|
|
Acceptance criteria:
|
|
1. When WEBHOOK_ENABLED=False — set_webhook is NOT called during lifespan startup
|
|
(mock set_webhook.call_count == 0).
|
|
2. When WEBHOOK_ENABLED=True (default) — set_webhook IS called exactly once.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
os.environ.setdefault("BOT_TOKEN", "test-bot-token")
|
|
os.environ.setdefault("CHAT_ID", "-1001234567890")
|
|
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")
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
from tests.conftest import temp_db
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Criterion 1 — WEBHOOK_ENABLED=False: set_webhook must NOT be called
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_lifespan_webhook_disabled_set_webhook_not_called():
|
|
"""При WEBHOOK_ENABLED=False вызов set_webhook не должен происходить (call_count == 0)."""
|
|
from backend.main import app
|
|
|
|
with temp_db():
|
|
with patch("backend.telegram.set_webhook", new_callable=AsyncMock) as mock_set_webhook:
|
|
with patch("backend.config.WEBHOOK_ENABLED", False):
|
|
async with app.router.lifespan_context(app):
|
|
pass
|
|
|
|
assert mock_set_webhook.call_count == 0
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Criterion 2 — WEBHOOK_ENABLED=True (default): set_webhook must be called
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_lifespan_webhook_enabled_set_webhook_called_once():
|
|
"""При WEBHOOK_ENABLED=True вызов set_webhook должен произойти ровно один раз."""
|
|
from backend.main import app
|
|
|
|
with temp_db():
|
|
with patch("backend.telegram.set_webhook", new_callable=AsyncMock) as mock_set_webhook:
|
|
with patch("backend.config.WEBHOOK_ENABLED", True):
|
|
async with app.router.lifespan_context(app):
|
|
pass
|
|
|
|
assert mock_set_webhook.call_count == 1
|