feat: backend read api for all entities (messages/groups/calls/sms/org/sync)
Adds the read-only API surface the upcoming Vue frontend needs to browse all
mirrored entities. Follows the existing customer-router pattern: Annotated
deps, Page[T] envelope, select+where+order_by, count() for total.
Endpoints (all GET):
* /api/departments -> list[DepartmentOut] (path-ordered, plain list)
* /api/staff -> Page[StaffOut] (department_id, is_on_job filters)
* /api/accounts -> Page[AccountOut] (channel filter; unified
wechat+whatsapp projection built in memory)
* /api/messages -> Page[MessageOut] (channel/owner/talker/room/
[start,end] filters; ordered timestamp asc)
* /api/groups -> Page[GroupOut] (channel/owner filters)
* /api/groups/{id}/members -> list[GroupMemberOut] (404 on unknown room)
* /api/calls -> Page[PhoneCallOut] (channel/user/time window;
ordered start_call_time desc)
* /api/sms -> Page[SmsOut] (user/time window; create_time desc)
* /api/sync/status -> {states, recent_runs} monitor payload
Schemas (Pydantic v2, from_attributes=True): MessageOut, GroupOut,
GroupMemberOut, PhoneCallOut, SmsOut, DepartmentOut, StaffOut, AccountOut
(unified across two tables), SyncStateOut, SyncRunOut, SyncStatusOut.
Only human-relevant fields per entity are exposed.
Tests: per-router TDD via httpx ASGITransport against an in-memory SQLite
override of get_session. A shared make_client factory in conftest.py builds
the seeded client, deduping the ~40-line fixture across the six new files.
41 new tests cover shape + every documented filter + pagination + ordering
(nulls-last verified on all desc/asc time columns).
Suite: 226 -> 267 passing. ruff check clean.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
"""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",
|
||||
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",
|
||||
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",
|
||||
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",
|
||||
):
|
||||
assert key in body["items"][0]
|
||||
|
||||
|
||||
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_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() == []
|
||||
Reference in New Issue
Block a user