feat: 增加模板预览短期隔离上下文

This commit is contained in:
que01 committed 2026-10-16 19:46:38 +08:00
1 parent b60174885e
commit d38dbe02aa
8 files changed
+190 -22

No files matched your search

+52
View File
@@ -298,6 +298,58 @@ test("平台模板管理员可以按行业预览版本,且预览不写入品
fixtureSource: "previewFixture",
});
assert.ok(event.actor.id);
// Preview session 只复用同一管理员、同一版本和行业的已构建投影;复用不产生第二条预览审计。
assert.match(body.preview.session.token, /^[a-f0-9]{64}$/);
const anotherTemplateAdmin = await prisma.adminUser.create({
data: {
username: `preview-session-${randomUUID()}`,
passwordHash: "unused",
displayName: "另一位模板管理员",
platformTemplateRole: "template_admin",
},
});
const anotherHeaders = {
authorization: `Bearer ${signIdentity({
subject: anotherTemplateAdmin.id,
brandId,
role: "admin",
})}`,
};
try {
const auditCount = await prisma.templatePackageAuditLog.count({
where: { templatePackageVersionId: versionId, action: "previewed" },
});
const mismatchedActor = await request(
`/admin/templates/${versionId}/preview?industry=personal&session=${body.preview.session.token}`,
{ headers: anotherHeaders },
);
assert.equal(mismatchedActor.statusCode, 400, mismatchedActor.body);
assert.match(JSON.parse(mismatchedActor.body).error.message, /不匹配/);
const reused = await request(
`/admin/templates/${versionId}/preview?industry=personal&session=${body.preview.session.token}`,
{ headers },
);
assert.equal(reused.statusCode, 200, reused.body);
const reusedBody = JSON.parse(reused.body);
assert.equal(reusedBody.preview.session.token, body.preview.session.token);
assert.deepEqual(reusedBody.workspace.brand, body.workspace.brand);
assert.equal(
await prisma.templatePackageAuditLog.count({
where: { templatePackageVersionId: versionId, action: "previewed" },
}),
auditCount,
);
const unknownSession = await request(
`/admin/templates/${versionId}/preview?industry=personal&session=${"0".repeat(64)}`,
{ headers },
);
assert.equal(unknownSession.statusCode, 400, unknownSession.body);
assert.match(JSON.parse(unknownSession.body).error.message, /不存在或已过期/);
} finally {
await prisma.adminUser.delete({ where: { id: anotherTemplateAdmin.id } });
}
const stored = await prisma.templatePackageVersion.findUniqueOrThrow({
where: { id: versionId },
});