821cf12ade
Append CR when executing quick commands (fixes trim stripping saved \r) and allow optional dedicated Telegram bot for server offline alerts only. Co-authored-by: Cursor <cursoragent@cursor.com>
63 lines
2.3 KiB
Python
63 lines
2.3 KiB
Python
"""Offline alert Telegram channel routing."""
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
from server.infrastructure.telegram import resolve_offline_telegram_credentials, send_telegram_offline_alert
|
|
|
|
|
|
def test_resolve_offline_credentials_prefers_dedicated(monkeypatch):
|
|
from server.config import settings
|
|
|
|
monkeypatch.setattr(settings, "TELEGRAM_OFFLINE_BOT_TOKEN", "off-token")
|
|
monkeypatch.setattr(settings, "TELEGRAM_OFFLINE_CHAT_ID", "off-chat")
|
|
monkeypatch.setattr(settings, "TELEGRAM_BOT_TOKEN", "main-token")
|
|
monkeypatch.setattr(settings, "TELEGRAM_CHAT_ID", "main-chat")
|
|
|
|
assert resolve_offline_telegram_credentials() == ("off-token", "off-chat")
|
|
|
|
|
|
def test_resolve_offline_credentials_falls_back_to_main(monkeypatch):
|
|
from server.config import settings
|
|
|
|
monkeypatch.setattr(settings, "TELEGRAM_OFFLINE_BOT_TOKEN", "")
|
|
monkeypatch.setattr(settings, "TELEGRAM_OFFLINE_CHAT_ID", "")
|
|
monkeypatch.setattr(settings, "TELEGRAM_BOT_TOKEN", "main-token")
|
|
monkeypatch.setattr(settings, "TELEGRAM_CHAT_ID", "main-chat")
|
|
|
|
assert resolve_offline_telegram_credentials() == ("main-token", "main-chat")
|
|
|
|
|
|
def test_resolve_offline_credentials_partial_offline_uses_main(monkeypatch):
|
|
from server.config import settings
|
|
|
|
monkeypatch.setattr(settings, "TELEGRAM_OFFLINE_BOT_TOKEN", "off-only")
|
|
monkeypatch.setattr(settings, "TELEGRAM_OFFLINE_CHAT_ID", "")
|
|
monkeypatch.setattr(settings, "TELEGRAM_BOT_TOKEN", "main-token")
|
|
monkeypatch.setattr(settings, "TELEGRAM_CHAT_ID", "main-chat")
|
|
|
|
assert resolve_offline_telegram_credentials() == ("main-token", "main-chat")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_offline_alert_uses_dedicated_bot(monkeypatch):
|
|
from server.config import settings
|
|
|
|
monkeypatch.setattr(settings, "TELEGRAM_OFFLINE_BOT_TOKEN", "off-token")
|
|
monkeypatch.setattr(settings, "TELEGRAM_OFFLINE_CHAT_ID", "off-chat")
|
|
monkeypatch.setattr(settings, "NOTIFY_ALERT_OFFLINE", "true")
|
|
|
|
with patch(
|
|
"server.infrastructure.telegram.send_telegram",
|
|
new_callable=AsyncMock,
|
|
return_value=True,
|
|
) as mock_send:
|
|
ok = await send_telegram_offline_alert("srv-1", server_id=9)
|
|
|
|
assert ok is True
|
|
mock_send.assert_awaited_once()
|
|
_, kwargs = mock_send.call_args
|
|
assert kwargs["bot_token"] == "off-token"
|
|
assert kwargs["chat_id"] == "off-chat"
|