feat: 分离模板演示数据并校验预览资源
This commit is contained in:
1 parent
6edde5a8d6
commit
b60174885e
31 files changed
+1785
-69
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { test } from "node:test";
|
||||
import "reflect-metadata";
|
||||
import { ConflictException } from "@nestjs/common";
|
||||
import { AdminTemplatePreviewAssetsService } from "../src/modules/admin/admin-template-preview-assets.service.js";
|
||||
|
||||
const digest = (value: Buffer | string) => createHash("sha256").update(value).digest("hex");
|
||||
|
||||
function archiveFixture(corruptImage = false) {
|
||||
const root = mkdtempSync(path.join(os.tmpdir(), "template-preview-assets-"));
|
||||
const directory = path.join(root, "fixture");
|
||||
mkdirSync(path.join(directory, "miniapp/assets"), { recursive: true });
|
||||
const png = readFileSync(
|
||||
"../../packages/template-atelier-solopreneur/miniapp/assets/tab/home.png",
|
||||
);
|
||||
const contents: Record<string, Buffer> = {
|
||||
"miniapp/assets/icon.png": png,
|
||||
"miniapp/assets/bad.jpg": Buffer.from("This is not an image"),
|
||||
"miniapp/assets/large.png": Buffer.alloc(2 * 1024 * 1024 + 1),
|
||||
"miniapp/assets/script.svg": Buffer.from("<svg onload='alert(1)'/>"),
|
||||
};
|
||||
const files = Object.entries(contents).map(([file, content]) => {
|
||||
writeFileSync(path.join(directory, file), content);
|
||||
return {
|
||||
bytes: content.length,
|
||||
path: file,
|
||||
sha256: corruptImage ? "0".repeat(64) : digest(content),
|
||||
};
|
||||
});
|
||||
const contentChecksum = digest(JSON.stringify(files));
|
||||
writeFileSync(path.join(directory, "checksums.json"), JSON.stringify({ contentChecksum, files }));
|
||||
const artifactPath = path.join(root, "fixture.tar.gz");
|
||||
execFileSync("tar", ["-czf", artifactPath, "-C", root, "fixture"]);
|
||||
return {
|
||||
root,
|
||||
version: { artifactPath, contentChecksum, archiveChecksum: digest(readFileSync(artifactPath)) },
|
||||
};
|
||||
}
|
||||
|
||||
test("preview resources verify raster content and diagnose invalid or oversized assets", async () => {
|
||||
const fixture = archiveFixture();
|
||||
try {
|
||||
const result = await new AdminTemplatePreviewAssetsService().read(fixture.version);
|
||||
assert.deepEqual(Object.keys(result.files), ["/assets/icon.png"]);
|
||||
assert.match(result.files["/assets/icon.png"]!.dataUrl, /^data:image\/png;base64,/);
|
||||
assert.deepEqual(
|
||||
result.diagnostics.map((item) => item.code),
|
||||
["PREVIEW_ASSET_TYPE", "PREVIEW_ASSET_LIMIT"],
|
||||
);
|
||||
} finally {
|
||||
rmSync(fixture.root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("preview resources reject image checksum drift and unavailable archives", async () => {
|
||||
const fixture = archiveFixture(true);
|
||||
try {
|
||||
const service = new AdminTemplatePreviewAssetsService();
|
||||
await assert.rejects(service.read(fixture.version), ConflictException);
|
||||
await assert.rejects(
|
||||
service.read({ ...fixture.version, artifactPath: path.join(fixture.root, "missing.tar.gz") }),
|
||||
ConflictException,
|
||||
);
|
||||
} finally {
|
||||
rmSync(fixture.root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user