fix: 校验咨询表单关联业务并修正发布清单文案
- 关联的业务提供项必须存在且属于当前品牌,非法 id 返回 400 而不是数据库外键错误 - targetType 由 offeringId 推导,允许传 null 改回品牌通用入口 - 业务被停用时在发布准备度里点名说明,避免静默卡住发布 - 发布清单条目改为中性描述,是否达标交给状态标签表达
This commit is contained in:
1 parent
47d6309cba
commit
7b1e9eee47
6 files changed
+223
-25
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { after, before, test } from "node:test";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { createApp } from "../dist/main.js";
|
||||
@@ -2353,6 +2354,155 @@ test("admin can create and version an inquiry form", async () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("admin workspace data stays inside the acting brand", async () => {
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||
});
|
||||
const token = JSON.parse(login.body).data?.token ?? JSON.parse(login.body).token;
|
||||
const actingBrandId = readIdentity(token).brandId;
|
||||
const otherBrand = await prisma.brandProfile.findFirst({
|
||||
where: { id: { not: actingBrandId } },
|
||||
});
|
||||
assert.ok(otherBrand);
|
||||
|
||||
const switchResponse = await request(`/admin/brands/${otherBrand.id}/switch`, {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(switchResponse.statusCode, 201);
|
||||
const switchedToken = JSON.parse(switchResponse.body).token;
|
||||
assert.equal(readIdentity(switchedToken).brandId, otherBrand.id);
|
||||
|
||||
const workspace = await request("/admin/workspace", {
|
||||
headers: { authorization: `Bearer ${switchedToken}` },
|
||||
});
|
||||
assert.equal(workspace.statusCode, 200);
|
||||
const offeringBrandIds = [
|
||||
...new Set(JSON.parse(workspace.body).offerings.map((item) => item.brandId)),
|
||||
];
|
||||
assert.deepEqual(offeringBrandIds, [otherBrand.id]);
|
||||
|
||||
// 公开工作空间不带 brandSlug 时会回退到默认品牌;后台下拉框若直接用它会串到别的品牌,
|
||||
// 这里把这个差异固定下来,防止再次被接回 Admin。
|
||||
const publicWorkspace = await request("/public/workspace");
|
||||
const publicOfferingIds = JSON.parse(publicWorkspace.body).offerings.map((item) => item.id);
|
||||
const foreignIds = JSON.parse(workspace.body)
|
||||
.offerings.map((item) => item.id)
|
||||
.filter((id) => publicOfferingIds.includes(id));
|
||||
assert.deepEqual(foreignIds, []);
|
||||
});
|
||||
|
||||
test("inquiry form offering link is validated against the brand", async () => {
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||
});
|
||||
const token = JSON.parse(login.body).data?.token ?? JSON.parse(login.body).token;
|
||||
const headers = { authorization: `Bearer ${token}` };
|
||||
const offering = await prisma.offering.findFirst({ where: { brandId, isEnabled: true } });
|
||||
assert.ok(offering);
|
||||
const otherBrandOffering = await prisma.offering.findFirst({
|
||||
where: { brandId: { not: brandId } },
|
||||
});
|
||||
assert.ok(otherBrandOffering);
|
||||
const fields = [{ key: "project", type: "textarea", label: "项目背景", required: true }];
|
||||
|
||||
// 不存在的业务 id:必须是可读的 400,而不是数据库外键错误。
|
||||
const unknown = await request("/admin/workspace/forms", {
|
||||
method: "POST",
|
||||
headers,
|
||||
payload: { name: "非法关联表单", offeringId: randomUUID(), fieldsSnapshot: fields },
|
||||
});
|
||||
assert.equal(unknown.statusCode, 400);
|
||||
assert.match(errorMessage(unknown.body), /关联的业务提供项不存在/);
|
||||
|
||||
// 其他品牌的业务 id:品牌隔离必须拦住。
|
||||
const crossBrand = await request("/admin/workspace/forms", {
|
||||
method: "POST",
|
||||
headers,
|
||||
payload: {
|
||||
name: "跨品牌关联表单",
|
||||
offeringId: otherBrandOffering.id,
|
||||
fieldsSnapshot: fields,
|
||||
},
|
||||
});
|
||||
assert.equal(crossBrand.statusCode, 400);
|
||||
assert.match(errorMessage(crossBrand.body), /不属于当前品牌/);
|
||||
assert.equal(await prisma.inquiryForm.count({ where: { brandId, name: "跨品牌关联表单" } }), 0);
|
||||
|
||||
// 合法的关联:targetType 由后端推导为 offering,写入后仍然只能被本品牌看到。
|
||||
const created = await request("/admin/workspace/forms", {
|
||||
method: "POST",
|
||||
headers,
|
||||
payload: { name: "业务专属表单", offeringId: offering.id, fieldsSnapshot: fields },
|
||||
});
|
||||
assert.equal(created.statusCode, 201);
|
||||
const form = JSON.parse(created.body);
|
||||
assert.equal(form.offeringId, offering.id);
|
||||
assert.equal(form.targetType, "offering");
|
||||
try {
|
||||
const hijack = await request(`/admin/workspace/forms/${form.id}`, {
|
||||
method: "PATCH",
|
||||
headers,
|
||||
payload: { offeringId: otherBrandOffering.id },
|
||||
});
|
||||
assert.equal(hijack.statusCode, 400);
|
||||
const unchanged = await prisma.inquiryForm.findUnique({ where: { id: form.id } });
|
||||
assert.equal(unchanged.offeringId, offering.id);
|
||||
|
||||
// 解除关联:显式传 null 才能回到品牌通用入口。
|
||||
const unlinked = await request(`/admin/workspace/forms/${form.id}`, {
|
||||
method: "PATCH",
|
||||
headers,
|
||||
payload: { offeringId: null },
|
||||
});
|
||||
assert.equal(unlinked.statusCode, 200);
|
||||
const unlinkedBody = JSON.parse(unlinked.body);
|
||||
assert.equal(unlinkedBody.offeringId, null);
|
||||
assert.equal(unlinkedBody.targetType, "brand");
|
||||
} finally {
|
||||
await prisma.inquiryForm.delete({ where: { id: form.id } });
|
||||
}
|
||||
});
|
||||
|
||||
test("release readiness explains a form whose offering was disabled", async () => {
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||
});
|
||||
const token = JSON.parse(login.body).data?.token ?? JSON.parse(login.body).token;
|
||||
const headers = { authorization: `Bearer ${token}` };
|
||||
const offering = await prisma.offering.findFirst({ where: { brandId, isEnabled: true } });
|
||||
assert.ok(offering);
|
||||
const form = await prisma.inquiryForm.create({
|
||||
data: {
|
||||
brandId,
|
||||
name: "随业务停用的表单",
|
||||
offeringId: offering.id,
|
||||
targetType: "offering",
|
||||
purpose: "inquiry",
|
||||
submissionAction: "create-inquiry",
|
||||
fieldsSnapshot: [{ key: "project", type: "textarea", label: "项目背景", required: true }],
|
||||
isEnabled: true,
|
||||
},
|
||||
});
|
||||
const originalFormEnabled = form.isEnabled;
|
||||
await prisma.offering.update({ where: { id: offering.id }, data: { isEnabled: false } });
|
||||
try {
|
||||
const readiness = await request("/admin/releases/readiness", { headers });
|
||||
assert.equal(readiness.statusCode, 200);
|
||||
const formsCheck = JSON.parse(readiness.body).checks.find((check) => check.key === "forms");
|
||||
assert.equal(formsCheck.passed, false);
|
||||
assert.match(formsCheck.help, /随业务停用的表单/);
|
||||
assert.match(formsCheck.help, /关联的业务提供项已停用/);
|
||||
} finally {
|
||||
await prisma.offering.update({ where: { id: offering.id }, data: { isEnabled: true } });
|
||||
await prisma.inquiryForm.deleteMany({ where: { id: form.id } });
|
||||
}
|
||||
assert.equal(originalFormEnabled, true);
|
||||
});
|
||||
|
||||
test("admin rejects invalid dynamic inquiry field definitions", async () => {
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
|
||||
Reference in new issue
Block a user