mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-06 17:38:35 +00:00
style: trim pairing sender-id comments
maintainer edit: remove redundant explanatory comments from the focused sender-id normalization tests and store change without changing behavior.
This commit is contained in:
parent
d7f868b832
commit
c55f7ec5bb
@ -44,10 +44,7 @@ def _load() -> dict[str, Any]:
|
|||||||
logger.warning("Corrupted pairing store, resetting")
|
logger.warning("Corrupted pairing store, resetting")
|
||||||
return {"approved": {}, "pending": {}}
|
return {"approved": {}, "pending": {}}
|
||||||
|
|
||||||
# Convert approved lists to sets for O(1) lookup. Sender IDs are normalized
|
# Convert approved lists to str sets for O(1) lookup.
|
||||||
# to str so lookups match is_approved()/revoke(), which coerce with str():
|
|
||||||
# IDs may be numeric (e.g. Telegram/QQ) in code or in a hand-edited
|
|
||||||
# pairing.json, and an int entry would never match the str() lookup.
|
|
||||||
for channel, users in data.get("approved", {}).items():
|
for channel, users in data.get("approved", {}).items():
|
||||||
data["approved"][channel] = {str(u) for u in users}
|
data["approved"][channel] = {str(u) for u in users}
|
||||||
return data
|
return data
|
||||||
@ -113,9 +110,6 @@ def approve_code(code: str) -> tuple[str, str] | None:
|
|||||||
if info is None:
|
if info is None:
|
||||||
return None
|
return None
|
||||||
channel = info["channel"]
|
channel = info["channel"]
|
||||||
# Coerce to str: a hand-edited pending entry may carry a numeric
|
|
||||||
# sender_id, which would otherwise add an int to the (str-normalized)
|
|
||||||
# approved set and break _save()'s sorted() on the mixed-type set.
|
|
||||||
sender_id = str(info["sender_id"])
|
sender_id = str(info["sender_id"])
|
||||||
data.setdefault("approved", {}).setdefault(channel, set()).add(sender_id)
|
data.setdefault("approved", {}).setdefault(channel, set()).add(sender_id)
|
||||||
_save(data)
|
_save(data)
|
||||||
|
|||||||
@ -175,25 +175,16 @@ class TestHandlePairingCommand:
|
|||||||
|
|
||||||
|
|
||||||
class TestNonStringSenderId:
|
class TestNonStringSenderId:
|
||||||
"""Sender IDs may be numeric (e.g. Telegram/QQ). The store normalizes them
|
|
||||||
to str so writes/reads/removals stay consistent with is_approved()."""
|
|
||||||
|
|
||||||
def test_numeric_sender_id_round_trip(self) -> None:
|
def test_numeric_sender_id_round_trip(self) -> None:
|
||||||
code = store.generate_code("telegram", 12345)
|
code = store.generate_code("telegram", 12345)
|
||||||
assert store.approve_code(code) == ("telegram", "12345")
|
assert store.approve_code(code) == ("telegram", "12345")
|
||||||
# Approved regardless of whether the caller passes int or str.
|
|
||||||
assert store.is_approved("telegram", 12345) is True
|
assert store.is_approved("telegram", 12345) is True
|
||||||
assert store.is_approved("telegram", "12345") is True
|
assert store.is_approved("telegram", "12345") is True
|
||||||
assert store.get_approved("telegram") == ["12345"]
|
assert store.get_approved("telegram") == ["12345"]
|
||||||
# Revoke also works with a numeric id.
|
|
||||||
assert store.revoke("telegram", 12345) is True
|
assert store.revoke("telegram", 12345) is True
|
||||||
assert store.is_approved("telegram", "12345") is False
|
assert store.is_approved("telegram", "12345") is False
|
||||||
|
|
||||||
def test_hand_edited_numeric_pending_does_not_corrupt_approved_set(self) -> None:
|
def test_hand_edited_numeric_pending_does_not_corrupt_approved_set(self) -> None:
|
||||||
# A hand-edited *pending* entry may carry a numeric sender_id. Approving
|
|
||||||
# it must coerce to str so the approved set stays homogeneously str —
|
|
||||||
# otherwise the next _save()'s sorted() on a mixed int/str set raises
|
|
||||||
# TypeError (in fact approve_code()'s own _save() would already raise).
|
|
||||||
store._store_path().write_text(
|
store._store_path().write_text(
|
||||||
'{"approved": {"telegram": ["111"]}, '
|
'{"approved": {"telegram": ["111"]}, '
|
||||||
'"pending": {"ABCD-EFGH": {"channel": "telegram", "sender_id": 222, '
|
'"pending": {"ABCD-EFGH": {"channel": "telegram", "sender_id": 222, '
|
||||||
@ -202,13 +193,10 @@ class TestNonStringSenderId:
|
|||||||
)
|
)
|
||||||
assert store.approve_code("ABCD-EFGH") == ("telegram", "222")
|
assert store.approve_code("ABCD-EFGH") == ("telegram", "222")
|
||||||
assert store.is_approved("telegram", 222) is True
|
assert store.is_approved("telegram", 222) is True
|
||||||
# A subsequent write must not raise on a mixed-type set.
|
|
||||||
store.generate_code("telegram", 333)
|
store.generate_code("telegram", 333)
|
||||||
assert store.get_approved("telegram") == ["111", "222"]
|
assert store.get_approved("telegram") == ["111", "222"]
|
||||||
|
|
||||||
def test_numeric_id_in_hand_edited_store(self) -> None:
|
def test_numeric_id_in_hand_edited_store(self) -> None:
|
||||||
# Operators may edit pairing.json directly; a numeric entry must still
|
|
||||||
# match the str() lookup that is_approved() performs.
|
|
||||||
store._store_path().write_text(
|
store._store_path().write_text(
|
||||||
'{"approved": {"telegram": [12345]}, "pending": {}}',
|
'{"approved": {"telegram": [12345]}, "pending": {}}',
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user