Files
iishko-public/tests/test_entry_autoban_default.py
T
Vlad Gothbreach af292ff15d
checks / pytest (push) Has been cancelled
checks / moderation (push) Has been cancelled
checks / node (push) Has been cancelled
Harden entry safety, unify runtime config, bound document unpacking
Shared-IP safety: entry autoban and entry notifications default off, and a
shared IP is treated as review evidence only. ban_user/ban_cluster refuse
reason=entry_ip_collision (also with the legacy flag set to 1), so token status
and quota are never touched; explicit admin moderation still works.

Reproducible startup: new bot/runtime_config.py and scripts/run.py load the
repository .env (or --env-file) without shell interpolation, normalize
config/DB/upstream aliases, and make the bot, Mini App, gateway, econ cap,
account admin, mk_daemon, entry admin and key notifications resolve the same
paths. "run.py <target> --check" is an offline preflight that starts nothing
and reports placeholders, missing upstream DB and invalid data directories.

Resource limits: aggregate Office decompression, a 400 read-entry cap and a
per-row XLSX cell budget stop early; truncated or corrupt archives are
reported instead of raising.

Deploy and docs: only source-backed systemd units remain, and
deploy/runtime.env.example documents the environment contract including the
0600 session-secret file rules. Removed the destructive
"cp bot/iishko_bot.py bot/grok_bot.py" instruction, the unused generated site/
snapshot and the unsupported iishko-ts staging unit; corrected .env.example,
README, ARCHITECTURE, SECURITY, docs/native-gateway.md and iishko-ts/README.md.

Tests: entry safety, runtime bootstrap, economy paths, file intake, deploy
contract, abilities_guard CLI, and a startup smoke that launches all three
components through scripts/run.py with non-loopback network blocked. Every
config reader, selfcheck included, accepts a UTF-8 BOM.

Verified on this tree: pytest 716 passed + 322 subtests, node --test 73/73,
go test and go vet clean, selfcheck --source-only ok, gitleaks worktree and
history 0 findings. No production or live-split changes are included.
2026-10-31 00:39:35 +03:00

79 lines
3.6 KiB
Python

"""Shared IP remains an operator signal even when legacy enforcement is enabled."""
from contextlib import closing
import os
from pathlib import Path
import sqlite3
import sys
import tempfile
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "bot"))
import entry_autoban # noqa: E402
import entry_telemetry # noqa: E402
NAT_IP = "203.0.113.7" # public_ip is stubbed: TEST-NET would be filtered
class NatUsersTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
root = Path(self.tmp.name)
self.bot_db, self.gw_db = root / "bot.db", root / "one-api.db"
with closing(sqlite3.connect(self.bot_db)) as con:
con.execute("CREATE TABLE users(tg_id INTEGER PRIMARY KEY, username TEXT, token_name TEXT,"
" admin INTEGER DEFAULT 0, banned INTEGER DEFAULT 0, privacy_ok INTEGER DEFAULT 1)")
con.executemany("INSERT INTO users(tg_id,username,token_name) VALUES(?,?,?)",
[(101, "alice", "tg101"), (102, "bob", "tg102")])
con.commit()
with closing(sqlite3.connect(self.gw_db)) as con:
con.execute("CREATE TABLE tokens(id INTEGER PRIMARY KEY, name TEXT, status INTEGER,"
" remain_quota INTEGER, unlimited_quota INTEGER DEFAULT 0, deleted_at TEXT)")
con.execute("CREATE TABLE users(id INTEGER PRIMARY KEY, username TEXT, status INTEGER)")
con.executemany("INSERT INTO tokens(id,name,status,remain_quota) VALUES(?,?,1,5000)",
[(1, "tg101"), (2, "tg102")])
con.executemany("INSERT INTO users(username,status) VALUES(?,1)", [("tg101",), ("tg102",)])
con.commit()
self.patches = [mock.patch.object(entry_autoban, "BOT_DB", str(self.bot_db)),
mock.patch.object(entry_autoban, "GW_DB", str(self.gw_db)),
mock.patch.object(entry_autoban, "SKIN_DB", str(root / "skin.db")),
mock.patch.object(entry_telemetry, "public_ip", lambda ip: ip)]
for p in self.patches:
p.start()
self.store = entry_telemetry.EntryTelemetry(self.bot_db, b"k", owners=[1])
self.store.init_db()
def tearDown(self):
for p in self.patches:
p.stop()
self.tmp.cleanup()
def _visit_both(self):
self.store.record(101, NAT_IP)
self.store.record(102, NAT_IP)
with closing(sqlite3.connect(self.gw_db)) as gw, closing(sqlite3.connect(self.bot_db)) as bot:
return (gw.execute("SELECT status,remain_quota FROM tokens ORDER BY id").fetchall(),
bot.execute("SELECT banned FROM users ORDER BY tg_id").fetchall(),
bot.execute("SELECT COUNT(*) FROM wa_entry_alerts").fetchone()[0])
def test_env_absent_keeps_access_and_quota(self):
with mock.patch.dict(os.environ, {}, clear=False):
os.environ.pop("IISHKO_ENTRY_AUTOBAN", None)
tokens, banned, alerts = self._visit_both()
self.assertEqual(tokens, [(1, 5000), (1, 5000)])
self.assertEqual(banned, [(0,), (0,)])
self.assertEqual(alerts, 1) # operator still gets the signal
def test_opt_in_still_keeps_shared_ip_access_and_quota(self):
with mock.patch.dict(os.environ, {"IISHKO_ENTRY_AUTOBAN": "1"}):
tokens, banned, alerts = self._visit_both()
self.assertEqual(tokens, [(1, 5000), (1, 5000)])
self.assertEqual(banned, [(0,), (0,)])
self.assertEqual(alerts, 1)
if __name__ == "__main__":
unittest.main()