feat: 完成模板目录导入与生命周期管理
This commit is contained in:
1 parent
553a07c4ec
commit
413413efda
25 files changed
+1313
-24
No files matched your search
@@ -1,5 +1,9 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdtempSync, readFileSync, readdirSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { after, before, test } from "node:test";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { getVisualTemplateManifest } from "@personal-brand/config";
|
||||
@@ -16,12 +20,140 @@ let customerToken;
|
||||
const createdBookingIds = [];
|
||||
|
||||
before(async () => {
|
||||
process.env.TEMPLATE_ARTIFACT_ROOT = mkdtempSync(
|
||||
path.join(os.tmpdir(), "personal-brand-template-artifacts-"),
|
||||
);
|
||||
await prisma.customerUser.create({ data: { brandId, openid } });
|
||||
customerToken = signIdentity({ subject: openid, brandId, role: "customer" });
|
||||
app = await createApp();
|
||||
await app.init();
|
||||
});
|
||||
|
||||
function multipartArchive(archive) {
|
||||
const boundary = "----personal-brand-template-test";
|
||||
const prefix = Buffer.from(
|
||||
`--${boundary}\r\nContent-Disposition: form-data; name="archive"; filename="template.tar.gz"\r\nContent-Type: application/gzip\r\n\r\n`,
|
||||
);
|
||||
const suffix = Buffer.from(`\r\n--${boundary}--\r\n`);
|
||||
return {
|
||||
payload: Buffer.concat([prefix, archive, suffix]),
|
||||
headers: { "content-type": `multipart/form-data; boundary=${boundary}` },
|
||||
};
|
||||
}
|
||||
|
||||
function createTemplateArchive() {
|
||||
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",
|
||||
"--out",
|
||||
output,
|
||||
],
|
||||
{ cwd: path.resolve(process.cwd(), "../.."), stdio: "ignore" },
|
||||
);
|
||||
const filename = readdirSync(output).find((item) => item.endsWith(".tar.gz"));
|
||||
assert.ok(filename);
|
||||
return readFileSync(path.join(output, filename));
|
||||
}
|
||||
|
||||
test("模板目录只允许平台模板管理员访问,并支持安全、幂等导入", async () => {
|
||||
const missing = await request("/admin/templates");
|
||||
assert.equal(missing.statusCode, 401);
|
||||
const customer = await request("/admin/templates", {
|
||||
headers: { authorization: `Bearer ${customerToken}` },
|
||||
});
|
||||
assert.equal(customer.statusCode, 403);
|
||||
const ordinaryUser = await prisma.adminUser.create({
|
||||
data: {
|
||||
username: `template-viewer-${Date.now()}`,
|
||||
passwordHash: "not-used-by-this-test",
|
||||
displayName: "普通管理员",
|
||||
platformTemplateRole: "none",
|
||||
mustChangePassword: false,
|
||||
},
|
||||
});
|
||||
try {
|
||||
const ordinary = await request("/admin/templates", {
|
||||
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 empty = await request("/admin/templates", { headers });
|
||||
assert.equal(empty.statusCode, 200);
|
||||
assert.deepEqual(JSON.parse(empty.body).items, []);
|
||||
assert.equal(JSON.parse(empty.body).total, 0);
|
||||
|
||||
const archive = createTemplateArchive();
|
||||
const imported = await request("/admin/templates/import", {
|
||||
method: "POST",
|
||||
headers: { ...headers, ...multipartArchive(archive).headers },
|
||||
rawPayload: multipartArchive(archive).payload,
|
||||
});
|
||||
assert.equal(imported.statusCode, 201);
|
||||
assert.equal(JSON.parse(imported.body).imported, true);
|
||||
const repeated = await request("/admin/templates/import", {
|
||||
method: "POST",
|
||||
headers: { ...headers, ...multipartArchive(archive).headers },
|
||||
rawPayload: multipartArchive(archive).payload,
|
||||
});
|
||||
assert.equal(repeated.statusCode, 201);
|
||||
assert.equal(JSON.parse(repeated.body).idempotent, true);
|
||||
const listed = await request("/admin/templates", { headers });
|
||||
const listedBody = JSON.parse(listed.body);
|
||||
assert.equal(listedBody.items.length, 1);
|
||||
assert.equal(listedBody.items[0].status, "imported");
|
||||
const versionId = listedBody.items[0].id;
|
||||
const publishMissing = await request(`/admin/templates/${versionId}/publish`, { method: "POST" });
|
||||
assert.equal(publishMissing.statusCode, 401);
|
||||
const publishCustomer = await request(`/admin/templates/${versionId}/publish`, {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${customerToken}` },
|
||||
});
|
||||
assert.equal(publishCustomer.statusCode, 403);
|
||||
const published = await request(`/admin/templates/${versionId}/publish`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
});
|
||||
assert.equal(published.statusCode, 201);
|
||||
assert.equal(JSON.parse(published.body).version.status, "published");
|
||||
const repeatedPublish = await request(`/admin/templates/${versionId}/publish`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
});
|
||||
assert.equal(JSON.parse(repeatedPublish.body).changed, false);
|
||||
const audit = await request(`/admin/templates/${versionId}/audit`, { headers });
|
||||
assert.equal(audit.statusCode, 200);
|
||||
assert.deepEqual(
|
||||
JSON.parse(audit.body).map((item) => item.action),
|
||||
["imported", "published"],
|
||||
);
|
||||
const retired = await request(`/admin/templates/${versionId}/retire`, {
|
||||
method: "POST",
|
||||
headers: { ...headers, "x-request-id": "template-retire-test" },
|
||||
});
|
||||
assert.equal(retired.statusCode, 201);
|
||||
assert.equal(JSON.parse(retired.body).version.status, "retired");
|
||||
const invalidRetire = await request(`/admin/templates/${versionId}/retire`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
});
|
||||
assert.equal(invalidRetire.statusCode, 201);
|
||||
assert.equal(JSON.parse(invalidRetire.body).changed, false);
|
||||
});
|
||||
|
||||
test("admin routes remain guarded when an API prefix is configured", async () => {
|
||||
process.env.API_PREFIX = "api";
|
||||
try {
|
||||
@@ -1126,10 +1258,16 @@ function errorMessage(body) {
|
||||
}
|
||||
|
||||
async function request(url, options = {}) {
|
||||
const { rawPayload, ...injectOptions } = options;
|
||||
return app
|
||||
.getHttpAdapter()
|
||||
.getInstance()
|
||||
.inject({ method: "GET", url, ...options });
|
||||
.inject({
|
||||
method: "GET",
|
||||
url,
|
||||
...injectOptions,
|
||||
...(rawPayload ? { payload: rawPayload } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
async function publishReleaseForTests(note = "集成测试发布") {
|
||||
|
||||
Reference in new issue
Block a user