57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
from types import SimpleNamespace
|
|
|
|
|
|
class FakeClient:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def api(self, name, data):
|
|
self.calls.append((name, data))
|
|
if name == "chatters.PullChattersByIdsRequest":
|
|
assert data["notSubscribeChatters"] is True
|
|
people = {
|
|
"u1": {"id": "u1", "name": "张三"},
|
|
"u3": {"id": "u3", "name": "王五"},
|
|
}
|
|
return {
|
|
"chatters": {
|
|
user_id: people[user_id]
|
|
for user_id in data["chatterIds"]
|
|
if user_id in people
|
|
}
|
|
}
|
|
assert name == "chats.PullChatChattersRequest"
|
|
assert data["chatId"] == "group-a"
|
|
assert data["needChatters"] is True
|
|
if "cursor" not in data:
|
|
return {
|
|
"chatters": {"u1": {"id": "u1", "name": "张三"}},
|
|
"nextCursor": 2,
|
|
}
|
|
assert data["cursor"] == 2
|
|
return {"chatters": {"u2": {"id": "u2", "name": "李四"}}}
|
|
|
|
|
|
def test_fetch_contacts_scopes_members_to_relevant_chats():
|
|
from larksniffer.contacts import fetch_contacts
|
|
|
|
client = FakeClient()
|
|
rows = [
|
|
SimpleNamespace(chat_id="group-a", chat_type=2, sender_id="u1"),
|
|
SimpleNamespace(chat_id="private-b", chat_type=1, sender_id="u3"),
|
|
]
|
|
|
|
contacts = fetch_contacts(client, rows)
|
|
|
|
assert contacts == {
|
|
"group-a": {"u1": "张三", "u2": "李四"},
|
|
"private-b": {"u3": "王五"},
|
|
}
|
|
member_calls = [
|
|
data
|
|
for name, data in client.calls
|
|
if name == "chats.PullChatChattersRequest"
|
|
]
|
|
assert len(member_calls) == 2
|
|
assert all(data["chatId"] == "group-a" for data in member_calls)
|