Initial public release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
4.7 KiB
Python
128 lines
4.7 KiB
Python
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
import account_key_status as aks
|
|
|
|
|
|
UID = 1001
|
|
|
|
|
|
def _bot(path, rows):
|
|
con = sqlite3.connect(str(path))
|
|
try:
|
|
con.execute("CREATE TABLE users(tg_id INTEGER PRIMARY KEY, token_name TEXT, api_key TEXT)")
|
|
con.executemany("INSERT INTO users(tg_id,token_name,api_key) VALUES(?,?,?)", rows)
|
|
con.commit()
|
|
finally:
|
|
con.close()
|
|
|
|
|
|
def _gw(path, rows):
|
|
con = sqlite3.connect(str(path))
|
|
try:
|
|
con.execute("CREATE TABLE tokens(id INTEGER PRIMARY KEY, name TEXT, key TEXT, "
|
|
"status INTEGER, deleted_at INTEGER)")
|
|
con.executemany("INSERT INTO tokens(id,name,key,status,deleted_at) VALUES(?,?,?,?,?)", rows)
|
|
con.commit()
|
|
finally:
|
|
con.close()
|
|
|
|
|
|
def _one(tmp_path, bot_row=(UID, None, None), gw_rows=()):
|
|
bot = tmp_path / "bot.db"
|
|
gw = tmp_path / "gw.db"
|
|
_bot(bot, [bot_row])
|
|
_gw(gw, gw_rows)
|
|
return bot, gw
|
|
|
|
|
|
def test_canonical_live_key_is_present_without_binding(tmp_path):
|
|
bot, gw = _one(tmp_path, gw_rows=[(1, "tg1001", "sk-main", 1, None)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.MAIN_PRESENT}
|
|
|
|
|
|
def test_confirmed_requires_exact_live_key_and_matching_material(tmp_path):
|
|
bot, gw = _one(tmp_path, bot_row=(UID, "tg1001", "sk-main"),
|
|
gw_rows=[(1, "tg1001", "main", 2, None)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.MAIN_CONFIRMED}
|
|
|
|
|
|
def test_deleted_or_nonlive_canonical_is_not_a_main(tmp_path):
|
|
bot, gw = _one(tmp_path, gw_rows=[(1, "tg1001", "sk-main", 3, None),
|
|
(2, "tg1001", "sk-old", 1, 123)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.NO_MAIN}
|
|
|
|
|
|
def test_secondary_only_is_not_a_confirmed_primary_even_when_bound(tmp_path):
|
|
bot, gw = _one(tmp_path, gw_rows=[(1, "tg1001-k2", "k2", 1, None)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.NO_MAIN}
|
|
|
|
bound = tmp_path / "bound"
|
|
bound.mkdir()
|
|
bot2, gw2 = _one(bound, bot_row=(UID, "tg1001-k2", "sk-k2"),
|
|
gw_rows=[(1, "tg1001-k2", "k2", 1, None)])
|
|
assert aks.main_key_states(bot2, gw2, [UID]) == {UID: aks.NO_MAIN}
|
|
|
|
|
|
def test_pm_binding_is_not_a_primary_key(tmp_path):
|
|
bot, gw = _one(tmp_path, bot_row=(UID, "pm1001", "sk-pm"),
|
|
gw_rows=[(1, "pm1001", "pm", 1, None)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.NO_MAIN}
|
|
|
|
|
|
def test_secondary_pointer_confirms_only_matching_canonical_key(tmp_path):
|
|
bot, gw = _one(tmp_path, bot_row=(UID, "tg1001-k2", "sk-main"),
|
|
gw_rows=[(1, "tg1001", "main", 1, None),
|
|
(2, "tg1001-k2", "secondary", 1, None)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.MAIN_CONFIRMED}
|
|
|
|
|
|
def test_duplicate_live_primary_binding_does_not_confirm(tmp_path):
|
|
bot, gw = _one(tmp_path, bot_row=(UID, "tg1001", "sk-main"),
|
|
gw_rows=[(1, "tg1001", "main", 1, None),
|
|
(2, "tg1001", "main", 1, None)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.MAIN_PRESENT}
|
|
|
|
|
|
def test_foreign_binding_never_confirms(tmp_path):
|
|
bot, gw = _one(tmp_path, bot_row=(UID, "tg2002", "sk-other"),
|
|
gw_rows=[(1, "tg2002", "other", 1, None)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.NO_MAIN}
|
|
|
|
|
|
def test_key_mismatch_does_not_confirm_but_canonical_is_present(tmp_path):
|
|
bot, gw = _one(tmp_path, bot_row=(UID, "tg1001", "sk-wrong"),
|
|
gw_rows=[(1, "tg1001", "right", 1, None)])
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.MAIN_PRESENT}
|
|
|
|
|
|
def test_missing_database_is_unavailable_not_none(tmp_path):
|
|
bot = tmp_path / "bot.db"
|
|
_bot(bot, [(UID, None, None)])
|
|
missing = tmp_path / "missing.db"
|
|
assert aks.main_key_states(bot, missing, [UID]) == {UID: aks.UNAVAILABLE}
|
|
assert not missing.exists()
|
|
|
|
|
|
def test_missing_table_is_unavailable(tmp_path):
|
|
bot = tmp_path / "bot.db"
|
|
_bot(bot, [(UID, None, None)])
|
|
gw = tmp_path / "gw.db"
|
|
sqlite3.connect(str(gw)).close()
|
|
assert aks.main_key_states(bot, gw, [UID]) == {UID: aks.UNAVAILABLE}
|
|
|
|
|
|
def test_ten_uid_batch_has_no_secret_in_result(tmp_path):
|
|
rows = [(uid, None, None) for uid in range(2001, 2011)]
|
|
gw_rows = [(i, "tg%d" % uid, "secret-%d" % uid, 1, None)
|
|
for i, uid in enumerate(range(2001, 2011), 1)]
|
|
bot = tmp_path / "bot.db"
|
|
gw = tmp_path / "gw.db"
|
|
_bot(bot, rows)
|
|
_gw(gw, gw_rows)
|
|
states = aks.main_key_states(bot, gw, list(range(2001, 2011)))
|
|
assert states == {uid: aks.MAIN_PRESENT for uid in range(2001, 2011)}
|
|
assert "secret-" not in repr(states) |