test(agent): 覆盖陪诊员订单归属与角色判定
新增本人订单强制归属、非本人订单拒绝、状态与字段白名单、跳步拦截,以及角色判定(显式管理员优先、员工档案为准、普通账号拒绝、历史管理员兼容)用例。
This commit is contained in:
@@ -12,6 +12,9 @@ import { createEmployeeReviewQueryTool } from "../agent/infra/tools/db/user/empl
|
||||
import { employeeReviewQueryTool } from "../agent/infra/tools/db/admin/employee_review_query.js";
|
||||
import { createEscortRecordQueryTool } from "../agent/infra/tools/db/user/escort_record_query.js";
|
||||
import { escortRecordQueryTool } from "../agent/infra/tools/db/admin/escort_record_query.js";
|
||||
import { createAttendantEscortRecordQueryTool } from "../agent/infra/tools/db/attendant/escort_record_query.js";
|
||||
import { buildAttendantEscortRecordSetTool } from "../agent/infra/tools/db/attendant/escort_record_set.js";
|
||||
import { resolveEscortAdminRole } from "../utils/permissions.js";
|
||||
|
||||
const OWNER_ID = "64b000000000000000000001";
|
||||
const OTHER_ID = "64b000000000000000000002";
|
||||
@@ -62,6 +65,24 @@ function withModel(modelName, stub, fn) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 陪诊员改单模型桩:findOne 返回固定记录,findOneAndUpdate 返回合并后的记录
|
||||
*/
|
||||
function createRecordModelStub(record) {
|
||||
const calls = [];
|
||||
return {
|
||||
calls,
|
||||
findOne(filter) {
|
||||
calls.push({ method: "findOne", filter });
|
||||
return Promise.resolve(record);
|
||||
},
|
||||
findOneAndUpdate(filter, update, options) {
|
||||
calls.push({ method: "findOneAndUpdate", filter, update, options });
|
||||
return Promise.resolve(record ? { ...record, ...update.$set } : null);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test("ObjectId 严格解析拒绝非法输入", () => {
|
||||
assert.equal(parseObjectId(OWNER_ID).toHexString(), OWNER_ID);
|
||||
assert.equal(parseObjectId(null), null);
|
||||
@@ -241,3 +262,150 @@ test("管理端员工评价查询允许按评价人筛选", async () => {
|
||||
assert.equal(stub.calls[0].filter.userId.toHexString(), OTHER_ID);
|
||||
});
|
||||
});
|
||||
|
||||
test("陪诊员端陪诊记录查询强制按本人归属", async () => {
|
||||
const stub = createModelStub([]);
|
||||
await withModel("EscortRecord", stub, async () => {
|
||||
const tool = createAttendantEscortRecordQueryTool({ _id: OWNER_ID });
|
||||
|
||||
// 手机号与陪诊员 ID 属管理端筛选,陪诊员侧不暴露
|
||||
assert.equal("mobile" in tool.schema.shape, false);
|
||||
assert.equal("attendantId" in tool.schema.shape, false);
|
||||
// 日期范围用于查自己的排期,保留
|
||||
assert.equal("dateFrom" in tool.schema.shape, true);
|
||||
|
||||
const result = parseResult(
|
||||
await tool.invoke({ status: "confirmed", attendantId: OTHER_ID, mobile: "13800000000" })
|
||||
);
|
||||
assert.equal(result.success, true);
|
||||
|
||||
for (const call of stub.calls) {
|
||||
assert.equal(call.filter["attendant.id"].toHexString(), OWNER_ID);
|
||||
assert.equal(call.filter.userId, undefined);
|
||||
// 越权参数即使被传入,也会被 schema 剥离
|
||||
assert.equal(call.filter["patient.mobile"], undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("陪诊员端改单拒绝非本人订单", async () => {
|
||||
const record = {
|
||||
_id: OTHER_ID,
|
||||
status: "confirmed",
|
||||
attendant: { id: OTHER_ID },
|
||||
notes: {},
|
||||
};
|
||||
const stub = createRecordModelStub(record);
|
||||
await withModel("EscortRecord", stub, async () => {
|
||||
const tool = buildAttendantEscortRecordSetTool({ attendantId: OWNER_ID });
|
||||
|
||||
const result = parseResult(await tool.invoke({ orderId: OTHER_ID, status: "in_progress" }));
|
||||
assert.equal(result.success, false);
|
||||
assert.match(result.error, /未分配给你/);
|
||||
// 越权时不应进入更新阶段
|
||||
assert.equal(stub.calls.filter((call) => call.method === "findOneAndUpdate").length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
test("陪诊员端改单只允许服务进度与本人备注", async () => {
|
||||
const record = {
|
||||
_id: OWNER_ID,
|
||||
status: "confirmed",
|
||||
attendant: { id: OWNER_ID },
|
||||
notes: {},
|
||||
};
|
||||
const stub = createRecordModelStub(record);
|
||||
await withModel("EscortRecord", stub, async () => {
|
||||
const tool = buildAttendantEscortRecordSetTool({ attendantId: OWNER_ID });
|
||||
|
||||
// 费用字段不暴露、也不允许写入
|
||||
assert.equal("payment" in tool.schema.shape, false);
|
||||
|
||||
const result = parseResult(
|
||||
await tool.invoke({
|
||||
orderId: OWNER_ID,
|
||||
status: "in_progress",
|
||||
notes: { escortNote: "已到达医院", medicalSummary: "完成检查" },
|
||||
})
|
||||
);
|
||||
assert.equal(result.success, true);
|
||||
|
||||
const updateCall = stub.calls.find((call) => call.method === "findOneAndUpdate");
|
||||
assert.ok(updateCall);
|
||||
assert.equal(updateCall.filter["attendant.id"].toHexString(), OWNER_ID);
|
||||
// 并发保护:以读取时的旧状态作为条件
|
||||
assert.equal(updateCall.filter.status, "confirmed");
|
||||
assert.equal(updateCall.update.$set.status, "in_progress");
|
||||
assert.equal(updateCall.update.$set["notes.escortNote"], "已到达医院");
|
||||
assert.equal(updateCall.update.$set["notes.medicalSummary"], "完成检查");
|
||||
assert.equal(updateCall.update.$set.payment, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
test("陪诊员端改单拒绝跳步", async () => {
|
||||
const record = {
|
||||
_id: OWNER_ID,
|
||||
status: "confirmed",
|
||||
attendant: { id: OWNER_ID },
|
||||
notes: {},
|
||||
};
|
||||
const stub = createRecordModelStub(record);
|
||||
await withModel("EscortRecord", stub, async () => {
|
||||
const tool = buildAttendantEscortRecordSetTool({ attendantId: OWNER_ID });
|
||||
|
||||
// confirmed 不能直接跳 completed;cancelled 也不在 schema 枚举内,模型无法提交
|
||||
const skip = parseResult(await tool.invoke({ orderId: OWNER_ID, status: "completed" }));
|
||||
assert.equal(skip.success, false);
|
||||
assert.match(skip.error, /Illegal status transition/);
|
||||
assert.equal(stub.calls.filter((call) => call.method === "findOneAndUpdate").length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
test("管理端 Agent 角色以员工档案为准", async () => {
|
||||
const stub = createModelStub([{ role: "attendant" }]);
|
||||
await withModel("Employee", stub, async () => {
|
||||
const role = await resolveEscortAdminRole({
|
||||
_id: OWNER_ID,
|
||||
app: { "wxapp-escort-admin": { role: ["user"] } },
|
||||
});
|
||||
assert.equal(role, "attendant");
|
||||
|
||||
const filter = stub.calls[0].filter;
|
||||
assert.equal(filter.userId.toHexString(), OWNER_ID);
|
||||
assert.equal(filter.status, "active");
|
||||
});
|
||||
});
|
||||
|
||||
test("显式管理员不依赖员工档案", async () => {
|
||||
const stub = createModelStub([{ role: "attendant" }]);
|
||||
await withModel("Employee", stub, async () => {
|
||||
const admin = await resolveEscortAdminRole({
|
||||
_id: OWNER_ID,
|
||||
app: { "wxapp-escort-admin": { role: ["admin"] } },
|
||||
});
|
||||
assert.equal(admin, "admin");
|
||||
assert.equal(stub.calls.length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
test("普通管理端账号无员工档案时拒绝", async () => {
|
||||
const stub = createModelStub([]);
|
||||
await withModel("Employee", stub, async () => {
|
||||
const outsider = await resolveEscortAdminRole({
|
||||
_id: OWNER_ID,
|
||||
app: { "wxapp-escort-admin": { role: ["user"] } },
|
||||
});
|
||||
assert.equal(outsider, null);
|
||||
});
|
||||
});
|
||||
|
||||
test("历史管理员 role 缺失仍按管理员放行", async () => {
|
||||
const stub = createModelStub([]);
|
||||
await withModel("Employee", stub, async () => {
|
||||
const legacy = await resolveEscortAdminRole({
|
||||
_id: OWNER_ID,
|
||||
app: { "wxapp-escort-admin": { wxopenid: "o123" } },
|
||||
});
|
||||
assert.equal(legacy, "admin");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user