feat: 增加模板版本独立预览入口
This commit is contained in:
1 parent
413413efda
commit
6edde5a8d6
15 files changed
+781
-14
No files matched your search
@@ -11,6 +11,7 @@ import { AdminTemplateController } from "../src/modules/workspace/admin-template
|
||||
import { AdminCapabilityController } from "../src/modules/workspace/admin-capability.controller.js";
|
||||
import { AdminOfferingController } from "../src/modules/workspace/admin-offering.controller.js";
|
||||
import { AdminInquiryController } from "../src/modules/workspace/admin-inquiry.controller.js";
|
||||
import { AdminTemplatePackageController } from "../src/modules/admin/admin-template-package.controller.js";
|
||||
|
||||
const expectedRoutes = new Set([
|
||||
"GET /admin/workspace",
|
||||
@@ -38,6 +39,12 @@ const expectedRoutes = new Set([
|
||||
"GET /admin/uploads/config",
|
||||
"POST /admin/uploads/presign",
|
||||
"PUT /admin/uploads/local",
|
||||
"GET /admin/templates",
|
||||
"POST /admin/templates/import",
|
||||
"POST /admin/templates/:id/publish",
|
||||
"POST /admin/templates/:id/retire",
|
||||
"GET /admin/templates/:id/audit",
|
||||
"GET /admin/templates/:id/preview",
|
||||
]);
|
||||
|
||||
test("refactored admin controllers preserve their declared API routes", () => {
|
||||
@@ -49,6 +56,7 @@ test("refactored admin controllers preserve their declared API routes", () => {
|
||||
AdminInquiryController,
|
||||
AdminUploadController,
|
||||
AdminLocalUploadController,
|
||||
AdminTemplatePackageController,
|
||||
];
|
||||
const scanner = new MetadataScanner();
|
||||
const routes = controllers.flatMap((controller) =>
|
||||
|
||||
@@ -41,14 +41,14 @@ function multipartArchive(archive) {
|
||||
};
|
||||
}
|
||||
|
||||
function createTemplateArchive() {
|
||||
function createTemplateArchive(template = "acheng-campus-food") {
|
||||
const output = mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-pack-"));
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
path.resolve(process.cwd(), "../../scripts/template-pack.cjs"),
|
||||
"--template",
|
||||
"acheng-campus-food",
|
||||
template,
|
||||
"--out",
|
||||
output,
|
||||
],
|
||||
@@ -154,6 +154,143 @@ test("模板目录只允许平台模板管理员访问,并支持安全、幂
|
||||
assert.equal(JSON.parse(invalidRetire.body).changed, false);
|
||||
});
|
||||
|
||||
test("平台模板管理员可以按行业预览版本,且预览不写入品牌数据", async () => {
|
||||
const missing = await request("/admin/templates/preview-version/preview?industry=personal");
|
||||
assert.equal(missing.statusCode, 401);
|
||||
const customer = await request("/admin/templates/preview-version/preview?industry=personal", {
|
||||
headers: { authorization: `Bearer ${customerToken}` },
|
||||
});
|
||||
assert.equal(customer.statusCode, 403);
|
||||
const invalid = await request("/admin/templates/preview-version/preview?industry=personal", {
|
||||
headers: { authorization: "Bearer invalid-preview-token" },
|
||||
});
|
||||
assert.equal(invalid.statusCode, 401);
|
||||
const ordinaryUser = await prisma.adminUser.create({
|
||||
data: {
|
||||
username: `preview-viewer-${randomUUID()}`,
|
||||
passwordHash: "unused",
|
||||
displayName: "品牌管理员",
|
||||
platformTemplateRole: "none",
|
||||
},
|
||||
});
|
||||
try {
|
||||
const ordinary = await request("/admin/templates/preview-version/preview?industry=personal", {
|
||||
headers: {
|
||||
authorization: `Bearer ${signIdentity({ subject: ordinaryUser.id, brandId, role: "admin" })}`,
|
||||
},
|
||||
});
|
||||
assert.equal(ordinary.statusCode, 403);
|
||||
} finally {
|
||||
await prisma.adminUser.delete({ where: { id: ordinaryUser.id } });
|
||||
}
|
||||
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||
});
|
||||
assert.equal(login.statusCode, 201);
|
||||
const headers = { authorization: `Bearer ${JSON.parse(login.body).token}` };
|
||||
const archive = createTemplateArchive("atelier-solopreneur");
|
||||
const multipart = multipartArchive(archive);
|
||||
const imported = await request("/admin/templates/import", {
|
||||
method: "POST",
|
||||
headers: { ...headers, ...multipart.headers },
|
||||
rawPayload: multipart.payload,
|
||||
});
|
||||
assert.equal(imported.statusCode, 201);
|
||||
const versionId = JSON.parse(imported.body).version.id;
|
||||
|
||||
const businessCounts = () =>
|
||||
Promise.all([
|
||||
prisma.brandProfile.count(),
|
||||
prisma.offering.count(),
|
||||
prisma.trustContent.count(),
|
||||
prisma.inquiryForm.count(),
|
||||
prisma.brandRelease.count(),
|
||||
prisma.customerUser.count(),
|
||||
prisma.inquirySubmission.count(),
|
||||
prisma.brandTemplateInstallation.count(),
|
||||
]);
|
||||
const before = await businessCounts();
|
||||
const preview = await request(`/admin/templates/${versionId}/preview?industry=personal`, {
|
||||
headers,
|
||||
});
|
||||
assert.equal(preview.statusCode, 200, preview.body);
|
||||
const body = JSON.parse(preview.body);
|
||||
assert.equal(body.template.id, versionId);
|
||||
assert.equal(body.industry.key, "personal");
|
||||
assert.equal(body.workspace.publication.status, "draft");
|
||||
assert.match(body.workspace.brand.id, /^template-preview:/);
|
||||
assert.equal(body.workspace.offerings.length > 0, true);
|
||||
assert.equal(body.workspace.inquiryForms.length, 1);
|
||||
assert.equal(body.workspace.brand.config.visualTemplateKey, "atelier-solopreneur");
|
||||
assert.equal(body.template.pages.includes("profile"), true);
|
||||
assert.equal(body.template.pages.includes("inquiry"), true);
|
||||
const stored = await prisma.templatePackageVersion.findUniqueOrThrow({
|
||||
where: { id: versionId },
|
||||
});
|
||||
const failures = [
|
||||
[{ ...stored.manifestSnapshot, sampleContent: {} }, /缺少必需数据/],
|
||||
[{ ...stored.manifestSnapshot, requiredCapabilities: ["unknown"] }, /未注册的 capability/],
|
||||
[{ ...stored.manifestSnapshot, visualTemplateKey: "unknown" }, /renderer/],
|
||||
[{ ...stored.manifestSnapshot, runtimeContract: "9.0" }, /契约/],
|
||||
[{ ...stored.manifestSnapshot, dataSlots: [null] }, /契约/],
|
||||
[{ ...stored.manifestSnapshot, navigation: [null] }, /契约/],
|
||||
[{ ...stored.manifestSnapshot, trustContentTypes: {} }, /契约/],
|
||||
[{ ...stored.manifestSnapshot, templateFields: [null] }, /契约/],
|
||||
[{ ...stored.manifestSnapshot, admin: { sections: [{ fields: [null] }] } }, /契约/],
|
||||
];
|
||||
try {
|
||||
for (const [manifestSnapshot, reason] of failures) {
|
||||
await prisma.templatePackageVersion.update({
|
||||
where: { id: versionId },
|
||||
data: { manifestSnapshot },
|
||||
});
|
||||
const rejected = await request(`/admin/templates/${versionId}/preview?industry=personal`, {
|
||||
headers,
|
||||
});
|
||||
assert.equal(rejected.statusCode, 400, rejected.body);
|
||||
assert.match(JSON.parse(rejected.body).error.message, reason);
|
||||
}
|
||||
} finally {
|
||||
await prisma.templatePackageVersion.update({
|
||||
where: { id: versionId },
|
||||
data: { manifestSnapshot: stored.manifestSnapshot },
|
||||
});
|
||||
}
|
||||
const noIndustry = await request(`/admin/templates/${versionId}/preview`, { headers });
|
||||
assert.equal(noIndustry.statusCode, 400);
|
||||
const absent = await request("/admin/templates/missing/preview?industry=personal", { headers });
|
||||
assert.equal(absent.statusCode, 400);
|
||||
|
||||
const incompatible = await request(`/admin/templates/${versionId}/preview?industry=doors`, {
|
||||
headers,
|
||||
});
|
||||
assert.equal(incompatible.statusCode, 400);
|
||||
assert.match(JSON.parse(incompatible.body).error.message, /不支持所选行业/);
|
||||
assert.deepEqual(await businessCounts(), before);
|
||||
|
||||
const retired = await request(`/admin/templates/${versionId}/retire`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
});
|
||||
assert.equal(retired.statusCode, 409);
|
||||
const published = await request(`/admin/templates/${versionId}/publish`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
});
|
||||
assert.equal(published.statusCode, 201);
|
||||
const retiredAfterPublish = await request(`/admin/templates/${versionId}/retire`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
});
|
||||
assert.equal(retiredAfterPublish.statusCode, 201);
|
||||
const retiredPreview = await request(`/admin/templates/${versionId}/preview?industry=personal`, {
|
||||
headers,
|
||||
});
|
||||
assert.equal(retiredPreview.statusCode, 409);
|
||||
});
|
||||
|
||||
test("admin routes remain guarded when an API prefix is configured", async () => {
|
||||
process.env.API_PREFIX = "api";
|
||||
try {
|
||||
@@ -3657,7 +3794,7 @@ test("atelier template imports selected package and preserves draft publication
|
||||
payload: { templateKey: "personal" },
|
||||
});
|
||||
assert.equal(installation.visualTemplate.key, "atelier-solopreneur");
|
||||
assert.equal(installation.visualTemplate.tokens.colors.accent, "#183329");
|
||||
assert.equal(installation.visualTemplate.tokens.colors.accent, "#1f3a5f");
|
||||
const preview = await json("/admin/workspace/sample-content/preview", { headers });
|
||||
assert.equal(preview.counts.fields, 5);
|
||||
await json("/admin/workspace/sample-content/import", { method: "POST", headers });
|
||||
|
||||
Reference in new issue
Block a user