test_authorization.py 936 B

123456789101112131415161718192021222324
  1. from data_query_agent.models import IncomingMessage
  2. from data_query_agent.service import DataQueryService
  3. def message(*, chat_id: str = "allowed", chat_type: str = "group", mentioned: bool = True) -> IncomingMessage:
  4. return IncomingMessage(
  5. message_id="m1",
  6. chat_id=chat_id,
  7. chat_type=chat_type,
  8. sender_open_id="any-group-member",
  9. text="查询昨天 DAU",
  10. message_type="text",
  11. mentioned_bot=mentioned,
  12. )
  13. def test_authorizes_any_member_who_mentions_bot_in_allowed_group() -> None:
  14. service = object.__new__(DataQueryService)
  15. service.settings = type("Settings", (), {"allowed_chat_ids": frozenset({"allowed"})})()
  16. assert service._authorized(message()) is True
  17. assert service._authorized(message(chat_id="other")) is False
  18. assert service._authorized(message(mentioned=False)) is False
  19. assert service._authorized(message(chat_type="p2p")) is False