78 lines
3.5 KiB
Python
78 lines
3.5 KiB
Python
"""Shared IP is a signal, not a sanction: autoban is opt-in (IISHKO_ENTRY_AUTOBAN=1)."""
|
|
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_enforces(self):
|
|
with mock.patch.dict(os.environ, {"IISHKO_ENTRY_AUTOBAN": "1"}):
|
|
tokens, banned, _ = self._visit_both()
|
|
self.assertEqual(tokens, [(2, 0), (2, 0)])
|
|
self.assertEqual(banned, [(1,), (1,)])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|