Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
"""Телеграм через местный вход впн сервера (TG_PROXY_URL).
|
|
|
|
Напрямую с сервера до api.telegram.org доходит треть запросов (замер 24.09.2026: 2 из 6),
|
|
через xray на 127.0.0.1:10809 — все. Прокси из окружения по-прежнему не берём
|
|
(trust_env=False): там адрес для песочницы агента, а не для Телеграма.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
if ROOT not in sys.path:
|
|
sys.path.insert(0, ROOT)
|
|
|
|
PROXY = "http://127.0.0.1:10809"
|
|
|
|
|
|
@pytest.fixture()
|
|
def seen(monkeypatch):
|
|
got = {}
|
|
|
|
class R:
|
|
status_code = 200
|
|
|
|
def json(self):
|
|
return {"ok": True, "result": []}
|
|
|
|
def fake_post(url, **kw):
|
|
got.update(kw, url=url)
|
|
return R()
|
|
|
|
monkeypatch.setattr(httpx, "post", fake_post)
|
|
monkeypatch.setenv("TG_VERIFY_BOT_TOKEN", "123:abc")
|
|
return got
|
|
|
|
|
|
def test_alert_goes_through_the_proxy_when_configured(seen, monkeypatch):
|
|
"""Порча: не читать настройку → тревоги снова идут напрямую и теряются."""
|
|
import alerts
|
|
monkeypatch.setenv("TG_PROXY_URL", PROXY)
|
|
alerts._post("https://api.telegram.org/botX/sendMessage", {"chat_id": 1, "text": "т"})
|
|
assert seen["proxy"] == PROXY
|
|
assert seen["trust_env"] is False
|
|
|
|
|
|
@pytest.mark.parametrize("value", [None, "", " "])
|
|
def test_alert_goes_direct_without_the_setting(seen, monkeypatch, value):
|
|
"""Порча: подставлять прокси всегда → на машинах без впн тревоги умрут."""
|
|
import alerts
|
|
if value is None:
|
|
monkeypatch.delenv("TG_PROXY_URL", raising=False)
|
|
else:
|
|
monkeypatch.setenv("TG_PROXY_URL", value)
|
|
alerts._post("https://api.telegram.org/botX/sendMessage", {"chat_id": 1, "text": "т"})
|
|
assert "proxy" not in seen
|
|
assert seen["trust_env"] is False
|
|
|
|
|
|
def test_verify_bot_uses_the_same_proxy(seen, monkeypatch):
|
|
"""Бот подтверждения при регистрации — тот же канал. Порча: забыть его → коды теряются."""
|
|
import telegram_verify
|
|
monkeypatch.setenv("TG_PROXY_URL", PROXY)
|
|
telegram_verify._api("getUpdates", {"offset": 0, "timeout": 25})
|
|
assert seen["proxy"] == PROXY
|
|
assert seen["trust_env"] is False
|
|
|
|
|
|
def test_verify_bot_goes_direct_without_the_setting(seen, monkeypatch):
|
|
import telegram_verify
|
|
monkeypatch.delenv("TG_PROXY_URL", raising=False)
|
|
telegram_verify._api("getUpdates", {"offset": 0, "timeout": 25})
|
|
assert "proxy" not in seen
|