Files
yunke/yunke-server/tests/test_api_groups.py
T

258 lines
9.0 KiB
Python

"""Tests for GET /api/groups and /api/groups/{room_id}/members.
Seeds 2 groups (one wechat, one whatsapp) with one member each on the wechat
group, plus a second wechat group owned by another account, then asserts list
filters + member-list shape + 404 for unknown room.
"""
from __future__ import annotations
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.models import ChatGroup, GroupMember
async def _seed(session: AsyncSession) -> None:
session.add_all(
[
ChatGroup(
room_id="room_a@chatroom",
channel="wechat",
room_name="Team A",
owner_account_id="wx_owner_1",
owner_account_nickname="WxOne",
owner_user_id="u_wx_1",
member_count=2,
message_count=10,
last_chat_time=2000,
),
ChatGroup(
room_id="room_b@chatroom",
channel="wechat",
room_name="Team B",
owner_account_id="wx_owner_2",
owner_user_id="u_wx_2",
member_count=1,
last_chat_time=1000,
),
ChatGroup(
room_id="wa_room_x@s.whatsapp.net",
channel="whatsapp",
room_name="WA Team",
owner_account_id="wa_owner_1",
owner_user_id="u_wa_1",
member_count=3,
last_chat_time=None,
),
GroupMember(
channel="wechat",
room_id="room_a@chatroom",
member_id="mem1",
nickname="Alice",
display_name="Alice",
),
GroupMember(
channel="wechat",
room_id="room_a@chatroom",
member_id="mem2",
nickname="Bob",
display_name="Bob",
),
]
)
await session.commit()
async def test_default_list_returns_all_ordered_desc(make_client):
"""No filters -> 3 groups, ordered by ``last_chat_time desc`` (nulls last)."""
client = await make_client(_seed)
r = await client.get("/api/groups")
assert r.status_code == 200
body = r.json()
assert body["total"] == 3
# 2000, 1000, null -> [room_a, room_b, wa_room_x]
assert [it["room_id"] for it in body["items"]] == [
"room_a@chatroom",
"room_b@chatroom",
"wa_room_x@s.whatsapp.net",
]
for key in (
"room_id",
"channel",
"room_name",
"owner_account_id",
"owner_account_nickname",
"member_count",
"message_count",
"last_chat_time",
"raw_json",
):
assert key in body["items"][0]
async def test_raw_json_serializes_verbatim(make_client):
"""``raw_json`` round-trips the verbatim upstream chatroom record.
Seeded blobs must come back unmolested (every camelCase key preserved) so
the all-fields drawer can render every column the 云客 群管理 page shows.
Rows without a blob serialize as JSON null.
"""
async def seed(session: AsyncSession) -> None:
await _seed(session)
session.add(
ChatGroup(
room_id="room_raw@chatroom",
channel="wechat",
room_name="Raw Team",
owner_account_id="wx_owner_1",
member_count=4,
last_chat_time=9000,
raw_json={
"chatRoomId": "room_raw@chatroom",
"chatRoomName": "Raw Team",
"memberCount": 4,
"memberIds": "wx_a;wx_b;wx_c;wx_d",
"headImageUrl": "https://oss/raw.jpg",
"wechatId": "wx_owner_1",
"wechatNickName": "WxOne",
"userName": "owner_login",
"userPhone": "13800000000",
"userId": "u_owner",
"messageCount": 42,
"ownMessageCount": 7,
"lossMembers": 1,
"addMembers": 2,
"lastChatTime": 9000,
"updateTime": 9999,
},
)
)
await session.commit()
client = await make_client(seed)
r = await client.get("/api/groups", params={"owner_account_id": "wx_owner_1"})
assert r.status_code == 200
by_id = {it["room_id"]: it for it in r.json()["items"]}
raw = by_id["room_raw@chatroom"]["raw_json"]
assert raw == {
"chatRoomId": "room_raw@chatroom",
"chatRoomName": "Raw Team",
"memberCount": 4,
"memberIds": "wx_a;wx_b;wx_c;wx_d",
"headImageUrl": "https://oss/raw.jpg",
"wechatId": "wx_owner_1",
"wechatNickName": "WxOne",
"userName": "owner_login",
"userPhone": "13800000000",
"userId": "u_owner",
"messageCount": 42,
"ownMessageCount": 7,
"lossMembers": 1,
"addMembers": 2,
"lastChatTime": 9000,
"updateTime": 9999,
}
# Seeded without an explicit raw_json -> JSON null (not omitted, not error).
assert by_id["room_a@chatroom"]["raw_json"] is None
async def test_filter_by_channel_whatsapp(make_client):
"""``?channel=whatsapp`` -> the single WA group."""
client = await make_client(_seed)
r = await client.get("/api/groups", params={"channel": "whatsapp"})
assert r.status_code == 200
body = r.json()
assert body["total"] == 1
assert body["items"][0]["room_id"] == "wa_room_x@s.whatsapp.net"
async def test_filter_by_owner_account_id(make_client):
"""``?owner_account_id=wx_owner_1`` -> only Team A."""
client = await make_client(_seed)
r = await client.get("/api/groups", params={"owner_account_id": "wx_owner_1"})
assert r.status_code == 200
body = r.json()
assert body["total"] == 1
assert body["items"][0]["room_id"] == "room_a@chatroom"
async def test_filter_by_user_id(make_client):
"""``?user_id=u_wx_1`` -> only Team A (param maps to owner_user_id column)."""
client = await make_client(_seed)
r = await client.get("/api/groups", params={"user_id": "u_wx_1"})
assert r.status_code == 200
body = r.json()
assert body["total"] == 1
assert body["items"][0]["room_id"] == "room_a@chatroom"
# Sanity: a different user_id matches a different single group.
r2 = await client.get("/api/groups", params={"user_id": "u_wa_1"})
assert r2.status_code == 200
body2 = r2.json()
assert body2["total"] == 1
assert body2["items"][0]["room_id"] == "wa_room_x@s.whatsapp.net"
# Sanity: an unknown user_id matches nothing.
r3 = await client.get("/api/groups", params={"user_id": "nobody"})
assert r3.status_code == 200
assert r3.json()["total"] == 0
async def test_pagination(make_client):
"""``page_size=1`` -> 1 item, total 3."""
client = await make_client(_seed)
r = await client.get("/api/groups", params={"page": 1, "page_size": 1})
assert r.status_code == 200
body = r.json()
assert body["total"] == 3
assert len(body["items"]) == 1
async def test_members_returns_list(make_client):
"""``/api/groups/room_a@chatroom/members`` -> 2 members (plain list)."""
client = await make_client(_seed)
r = await client.get("/api/groups/room_a@chatroom/members")
assert r.status_code == 200
items = r.json()
assert isinstance(items, list)
assert len(items) == 2
assert {it["member_id"] for it in items} == {"mem1", "mem2"}
for key in ("channel", "room_id", "member_id", "nickname", "display_name", "avatar"):
assert key in items[0]
async def test_members_unknown_room_404(make_client):
"""Unknown room_id -> 404 (distinguishes missing vs empty)."""
client = await make_client(_seed)
r = await client.get("/api/groups/nope@chatroom/members")
assert r.status_code == 404
async def test_members_empty_room_is_200(make_client):
"""Known room with no seeded members -> 200 + empty list (not 404)."""
client = await make_client(_seed)
r = await client.get("/api/groups/room_b@chatroom/members")
assert r.status_code == 200
assert r.json() == []
# ---------------------------------------------------------------------------
# room_id 精确过滤(2026-08-20:群聊页群名单行直查,替代超上限的大页拉取)
# ---------------------------------------------------------------------------
async def test_room_id_filter_returns_single_group(make_client):
async def seed(session):
session.add_all([
ChatGroup(room_id="r1@chatroom", channel="wechat", room_name="群一", owner_account_id="o1"),
ChatGroup(room_id="r2@chatroom", channel="wechat", room_name="群二", owner_account_id="o1"),
])
await session.commit()
client = await make_client(seed)
r = await client.get("/api/groups", params={"room_id": "r2@chatroom"})
assert r.status_code == 200
body = r.json()
assert body["total"] == 1
assert body["items"][0]["room_name"] == "群二"