ff7e9cb1d9
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>
207 lines
6.8 KiB
Python
207 lines
6.8 KiB
Python
"""Tests for GET /api/departments, /api/staff, /api/accounts.
|
|
|
|
Seeds a small org (3 departments, 3 staff, 2 wechat + 1 whatsapp accounts) and
|
|
asserts shape + filters + pagination across all three endpoints.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.db.models import Department, Staff, WechatAccount, WhatsappAccount
|
|
|
|
|
|
async def _seed(session: AsyncSession) -> None:
|
|
session.add_all(
|
|
[
|
|
Department(dept_id="root", parent_id=None, name="Root", path="root"),
|
|
Department(
|
|
dept_id="d1", parent_id="root", name="Sales", path="root/d1"
|
|
),
|
|
Department(
|
|
dept_id="d2", parent_id="root", name="Support", path="root/d2"
|
|
),
|
|
Staff(
|
|
user_id="u1",
|
|
name="Alice",
|
|
dept_id="d1",
|
|
is_on_job=True,
|
|
phone="111",
|
|
),
|
|
Staff(
|
|
user_id="u2",
|
|
name="Bob",
|
|
dept_id="d2",
|
|
is_on_job=False,
|
|
),
|
|
Staff(user_id="u3", name="Carol", dept_id="d1", is_on_job=True),
|
|
WechatAccount(
|
|
wx_id="wx_a",
|
|
nick_name="WxOne",
|
|
user_id="u1",
|
|
user_name="Alice",
|
|
phone="111",
|
|
contacts_num=10,
|
|
group_num=2,
|
|
is_on_job=True,
|
|
),
|
|
WechatAccount(
|
|
wx_id="wx_b",
|
|
nick_name="WxTwo",
|
|
user_id="u2",
|
|
contacts_num=5,
|
|
is_on_job=False,
|
|
),
|
|
WhatsappAccount(
|
|
uid="wa_c@s.whatsapp.net",
|
|
nick_name="WaOne",
|
|
user_id="u3",
|
|
user_name="Carol",
|
|
phone="333",
|
|
contacts_num=7,
|
|
group_num=1,
|
|
is_on_job=True,
|
|
),
|
|
]
|
|
)
|
|
await session.commit()
|
|
|
|
|
|
async def test_departments_returns_all_ordered_by_path(make_client):
|
|
"""``GET /api/departments`` -> plain list, ordered by ``path``."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/departments")
|
|
assert r.status_code == 200
|
|
items = r.json()
|
|
assert isinstance(items, list)
|
|
assert len(items) == 3
|
|
# Path order: root, root/d1, root/d2
|
|
assert [it["dept_id"] for it in items] == ["root", "d1", "d2"]
|
|
# Shape
|
|
for key in ("dept_id", "parent_id", "name", "path"):
|
|
assert key in items[0]
|
|
|
|
|
|
async def test_staff_default_page_returns_all(make_client):
|
|
"""``GET /api/staff`` -> 3 rows, page 1 / page_size 20."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/staff")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["page"] == 1
|
|
assert body["page_size"] == 20
|
|
assert body["total"] == 3
|
|
assert len(body["items"]) == 3
|
|
for key in ("user_id", "name", "phone", "sale_phone", "dept_id", "is_on_job"):
|
|
assert key in body["items"][0]
|
|
|
|
|
|
async def test_staff_filter_by_department_id(make_client):
|
|
"""``?department_id=d1`` -> 2 staff (Alice + Carol)."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/staff", params={"department_id": "d1"})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["total"] == 2
|
|
assert {it["user_id"] for it in body["items"]} == {"u1", "u3"}
|
|
|
|
|
|
async def test_staff_filter_by_is_on_job(make_client):
|
|
"""``?is_on_job=true`` -> 2 on-job staff (Alice + Carol)."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/staff", params={"is_on_job": "true"})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["total"] == 2
|
|
assert all(it["is_on_job"] is True for it in body["items"])
|
|
|
|
|
|
async def test_staff_filters_compose(make_client):
|
|
"""``department_id=d1 & is_on_job=true`` AND together -> still 2."""
|
|
client = await make_client(_seed)
|
|
r = await client.get(
|
|
"/api/staff", params={"department_id": "d1", "is_on_job": "true"}
|
|
)
|
|
assert r.status_code == 200
|
|
assert r.json()["total"] == 2
|
|
|
|
|
|
async def test_staff_pagination(make_client):
|
|
"""``page_size=1&page=2`` -> 1 item; total still 3."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/staff", params={"page": 2, "page_size": 1})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["total"] == 3
|
|
assert len(body["items"]) == 1
|
|
|
|
|
|
async def test_accounts_default_returns_both_channels(make_client):
|
|
"""``GET /api/accounts`` with no channel -> 3 rows across both tables."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/accounts")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["total"] == 3
|
|
# Sorted by (channel, account_id): wechat first, then whatsapp.
|
|
ids = [(it["channel"], it["account_id"]) for it in body["items"]]
|
|
assert ids == [
|
|
("wechat", "wx_a"),
|
|
("wechat", "wx_b"),
|
|
("whatsapp", "wa_c@s.whatsapp.net"),
|
|
]
|
|
# Unified shape exposes channel + key fields.
|
|
item = body["items"][0]
|
|
for key in (
|
|
"channel",
|
|
"account_id",
|
|
"nickname",
|
|
"user_id",
|
|
"user_name",
|
|
"phone",
|
|
"contacts_num",
|
|
"group_num",
|
|
"is_on_job",
|
|
):
|
|
assert key in item
|
|
assert item["channel"] == "wechat"
|
|
assert item["nickname"] == "WxOne"
|
|
assert item["user_name"] == "Alice"
|
|
|
|
|
|
async def test_accounts_filter_channel_wechat(make_client):
|
|
"""``?channel=wechat`` -> only the 2 wechat accounts."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/accounts", params={"channel": "wechat"})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["total"] == 2
|
|
assert all(it["channel"] == "wechat" for it in body["items"])
|
|
|
|
|
|
async def test_accounts_filter_channel_whatsapp(make_client):
|
|
"""``?channel=whatsapp`` -> the single whatsapp account."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/accounts", params={"channel": "whatsapp"})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["total"] == 1
|
|
assert body["items"][0]["account_id"] == "wa_c@s.whatsapp.net"
|
|
assert body["items"][0]["user_name"] == "Carol"
|
|
|
|
|
|
async def test_accounts_pagination(make_client):
|
|
"""``page_size=2&page=1`` -> 2 items, total 3; page 2 -> 1 item."""
|
|
client = await make_client(_seed)
|
|
r1 = await client.get("/api/accounts", params={"page": 1, "page_size": 2})
|
|
assert r1.status_code == 200
|
|
b1 = r1.json()
|
|
assert b1["total"] == 3
|
|
assert len(b1["items"]) == 2
|
|
|
|
r2 = await client.get("/api/accounts", params={"page": 2, "page_size": 2})
|
|
assert r2.status_code == 200
|
|
b2 = r2.json()
|
|
assert len(b2["items"]) == 1
|
|
assert b2["items"][0]["account_id"] == "wa_c@s.whatsapp.net"
|