mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
Second, deeper pass over tools/gateway/hermes_cli plus first pass over the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker, dashboard, conformance, monitoring, secret_sources, hermes_state, providers). Same rubric as wave 1 (AGENTS.md test policy); security, alternation/caching invariants, issue-number regressions, and E2E kept. Real test-quality fixes found and rooted out along the way: - tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls (DEFAULT_CONFIG smart-approval leaked in) — pinned approval mode=manual via autouse fixture: 17.4s → 0.4s. - test_model_switch_custom_providers.py / test_user_providers_model_switch.py silently probed live provider catalogs (~2s/test) — stubbed cached_provider_model_ids/provider_model_ids/fetch_api_models. - test_telegram_noise_filter.py: 15-platform copy-paste matrix over shared gateway.run logic → 3 representative platforms (55s → 3.9s). - test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on MagicMock agents — interrupt.side_effect now clears _running_agents (22s → 1.0s). - test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait 5s → 0.5s. - test_telegram_init_deadline.py: loop-block margin restored to 1.0s with rationale comment — the watchdog-dump assertion needs the loop blocked well past deadline+grace under parallel load (flaked once in the 40-worker verification run at a 0.2s margin). Verification: full hermetic suite via scripts/run_tests.sh — 2,438 files, 21,718 tests passed, 0 failed, 293.9s wall. Suite totals vs original baseline: 46,820 → 19,757 test functions (−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
222 lines
8.1 KiB
Python
222 lines
8.1 KiB
Python
"""Tests for the allowed_{channels,chats,rooms} whitelist extension
|
|
added alongside PR #7401 (Slack).
|
|
|
|
Covers: Telegram, Matrix, Mattermost, DingTalk.
|
|
|
|
For each platform:
|
|
- Empty = no restriction (fully backward compatible).
|
|
- When set, messages from non-listed chats/rooms are silently ignored.
|
|
- DMs are never filtered.
|
|
- @mention does NOT bypass the whitelist.
|
|
- config.yaml → env var bridging (via load_gateway_config) where applicable.
|
|
"""
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from gateway.config import Platform, PlatformConfig
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Telegram
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _make_telegram_adapter(*, allowed_chats=None, require_mention=None, guest_mode=False):
|
|
from plugins.platforms.telegram.adapter import TelegramAdapter
|
|
|
|
extra = {"guest_mode": guest_mode}
|
|
if allowed_chats is not None:
|
|
extra["allowed_chats"] = allowed_chats
|
|
if require_mention is not None:
|
|
extra["require_mention"] = require_mention
|
|
|
|
adapter = object.__new__(TelegramAdapter)
|
|
adapter.platform = Platform.TELEGRAM
|
|
adapter.config = PlatformConfig(enabled=True, token="***", extra=extra)
|
|
adapter._bot = SimpleNamespace(id=999, username="hermes_bot")
|
|
adapter._message_handler = AsyncMock()
|
|
adapter._mention_patterns = adapter._compile_mention_patterns()
|
|
# PR db50af910 added a TELEGRAM_ALLOWED_USERS allowlist gate to
|
|
# _should_process_message; stub it for tests that exercise the
|
|
# allowed-channels widening logic that runs after.
|
|
adapter._is_callback_user_authorized = lambda *_a, **_kw: True
|
|
return adapter
|
|
|
|
|
|
def _tg_group_message(chat_id=-100, text="hello"):
|
|
return SimpleNamespace(
|
|
text=text,
|
|
caption=None,
|
|
entities=[],
|
|
caption_entities=[],
|
|
message_thread_id=None,
|
|
chat=SimpleNamespace(id=chat_id, type="group"),
|
|
from_user=SimpleNamespace(id=111),
|
|
reply_to_message=None,
|
|
)
|
|
|
|
|
|
def _tg_dm_message(text="hello"):
|
|
return SimpleNamespace(
|
|
text=text,
|
|
caption=None,
|
|
entities=[],
|
|
caption_entities=[],
|
|
message_thread_id=None,
|
|
chat=SimpleNamespace(id=111, type="private"),
|
|
from_user=SimpleNamespace(id=111),
|
|
reply_to_message=None,
|
|
)
|
|
|
|
|
|
class TestTelegramAllowedChats:
|
|
def test_empty_is_no_restriction(self, monkeypatch):
|
|
monkeypatch.delenv("TELEGRAM_ALLOWED_CHATS", raising=False)
|
|
adapter = _make_telegram_adapter()
|
|
assert adapter._telegram_allowed_chats() == set()
|
|
assert adapter._should_process_message(_tg_group_message(-100)) is True
|
|
|
|
def test_list_form(self):
|
|
adapter = _make_telegram_adapter(allowed_chats=[-100, -200])
|
|
assert adapter._telegram_allowed_chats() == {"-100", "-200"}
|
|
|
|
|
|
def test_mention_cannot_bypass_whitelist(self):
|
|
"""@mention in a non-allowed chat is still ignored."""
|
|
adapter = _make_telegram_adapter(allowed_chats=["-100"])
|
|
msg = _tg_group_message(-999, text="@hermes_bot hello")
|
|
msg.entities = [SimpleNamespace(
|
|
type="mention", offset=0, length=len("@hermes_bot"),
|
|
)]
|
|
assert adapter._should_process_message(msg) is False
|
|
|
|
|
|
def test_config_bridge(self, monkeypatch, tmp_path):
|
|
"""slack-style config.yaml → env var bridge works."""
|
|
from gateway.config import load_gateway_config
|
|
|
|
hermes_home = tmp_path / ".hermes"
|
|
hermes_home.mkdir()
|
|
(hermes_home / "config.yaml").write_text(
|
|
"telegram:\n"
|
|
" allowed_chats:\n"
|
|
" - -100\n"
|
|
" - -200\n",
|
|
encoding="utf-8",
|
|
)
|
|
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
|
|
monkeypatch.setenv("TELEGRAM_ALLOWED_CHATS", "__sentinel__")
|
|
monkeypatch.delenv("TELEGRAM_ALLOWED_CHATS")
|
|
|
|
load_gateway_config()
|
|
|
|
import os as _os
|
|
assert _os.environ["TELEGRAM_ALLOWED_CHATS"] == "-100,-200"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# DingTalk
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _make_dingtalk_adapter(*, allowed_chats=None, require_mention=None):
|
|
# Import lazily — DingTalk SDK may not be installed.
|
|
pytest.importorskip("plugins.platforms.dingtalk.adapter", reason="DingTalk adapter not importable")
|
|
from plugins.platforms.dingtalk.adapter import DingTalkAdapter
|
|
|
|
extra = {}
|
|
if allowed_chats is not None:
|
|
extra["allowed_chats"] = allowed_chats
|
|
if require_mention is not None:
|
|
extra["require_mention"] = require_mention
|
|
|
|
adapter = object.__new__(DingTalkAdapter)
|
|
adapter.platform = Platform.DINGTALK
|
|
adapter.config = PlatformConfig(enabled=True, extra=extra)
|
|
return adapter
|
|
|
|
|
|
class TestDingTalkAllowedChats:
|
|
def test_empty_is_no_restriction(self, monkeypatch):
|
|
monkeypatch.delenv("DINGTALK_ALLOWED_CHATS", raising=False)
|
|
adapter = _make_dingtalk_adapter()
|
|
assert adapter._dingtalk_allowed_chats() == set()
|
|
|
|
def test_list_form(self):
|
|
adapter = _make_dingtalk_adapter(allowed_chats=["cidABC", "cidDEF"])
|
|
assert adapter._dingtalk_allowed_chats() == {"cidABC", "cidDEF"}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Mattermost (env-var only — no config.yaml bridge)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestMattermostAllowedChannels:
|
|
"""Mattermost whitelist logic — replicated since the adapter reads config
|
|
with env-var fallback inline inside _handle_post rather than through a
|
|
helper method."""
|
|
|
|
@staticmethod
|
|
def _would_process(channel_id, channel_type="O", allowed_cfg=None, allowed_env=""):
|
|
"""Replicate the whitelist gate from gateway/platforms/mattermost.py."""
|
|
if channel_type == "D":
|
|
return True
|
|
# config-first, env-var fallback (matching the adapter)
|
|
allowed_raw = allowed_cfg
|
|
if allowed_raw is None:
|
|
allowed_raw = allowed_env
|
|
if isinstance(allowed_raw, list):
|
|
allowed = {str(c).strip() for c in allowed_raw if str(c).strip()}
|
|
else:
|
|
allowed = {c.strip() for c in str(allowed_raw).split(",") if c.strip()}
|
|
if allowed and channel_id not in allowed:
|
|
return False
|
|
return True
|
|
|
|
def test_empty_config_is_no_restriction(self):
|
|
assert self._would_process("chan123", allowed_cfg=None, allowed_env="") is True
|
|
|
|
|
|
def test_config_bridge(self, monkeypatch, tmp_path):
|
|
from gateway.config import load_gateway_config
|
|
|
|
hermes_home = tmp_path / ".hermes"
|
|
hermes_home.mkdir()
|
|
(hermes_home / "config.yaml").write_text(
|
|
"mattermost:\n"
|
|
" allowed_channels:\n"
|
|
" - chanABC\n"
|
|
" - chanDEF\n",
|
|
encoding="utf-8",
|
|
)
|
|
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
|
|
# Pre-register the key with monkeypatch so teardown cleans it up
|
|
# even though load_gateway_config mutates os.environ directly
|
|
# (monkeypatch only restores keys it's touched via setenv/delenv;
|
|
# delenv on an absent key is a no-op for teardown purposes).
|
|
monkeypatch.setenv("MATTERMOST_ALLOWED_CHANNELS", "__sentinel__")
|
|
monkeypatch.delenv("MATTERMOST_ALLOWED_CHANNELS")
|
|
|
|
load_gateway_config()
|
|
|
|
import os as _os
|
|
assert _os.environ["MATTERMOST_ALLOWED_CHANNELS"] == "chanABC,chanDEF"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Matrix
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestMatrixAllowedRooms:
|
|
"""Matrix whitelist behavior — tested via the env-var-initialized
|
|
instance attribute _allowed_rooms."""
|
|
|
|
def test_empty_env_empty_set(self, monkeypatch):
|
|
monkeypatch.delenv("MATRIX_ALLOWED_ROOMS", raising=False)
|
|
# Replicate __init__ parsing without needing the real adapter.
|
|
raw = "" or ""
|
|
allowed = {r.strip() for r in raw.split(",") if r.strip()}
|
|
assert allowed == set()
|
|
|
|
|