84 lines
3.0 KiB
TypeScript
84 lines
3.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { test } from "node:test";
|
|
import { BadRequestException } from "@nestjs/common";
|
|
import { AdminUploadService } from "../src/modules/admin/admin-upload.service.js";
|
|
|
|
const environmentKeys = [
|
|
"OBJECT_STORAGE_BUCKET",
|
|
"OBJECT_STORAGE_ACCESS_KEY",
|
|
"OBJECT_STORAGE_SECRET_KEY",
|
|
"LOCAL_MEDIA_ROOT",
|
|
"LOCAL_MEDIA_BASE_URL",
|
|
] as const;
|
|
|
|
test("local mode reports configuration and returns a relative local upload URL", async () => {
|
|
const original = saveEnvironment();
|
|
const mediaRoot = await mkdtemp(path.join(tmpdir(), "personal-brand-uploads-"));
|
|
process.env.LOCAL_MEDIA_ROOT = mediaRoot;
|
|
process.env.LOCAL_MEDIA_BASE_URL = "/uploads";
|
|
try {
|
|
const service = new AdminUploadService();
|
|
assert.deepEqual(await service.getConfiguration(), {
|
|
configured: true,
|
|
status: "ok",
|
|
mode: "local",
|
|
});
|
|
const result = await service.presign({
|
|
fileName: "case study.png",
|
|
contentType: "image/png",
|
|
scope: "trust",
|
|
});
|
|
assert.match(result.key, /^\d{4}\/[a-f0-9]{16}\.png$/);
|
|
assert.equal(result.uploadUrl, `/admin/uploads/local?key=${encodeURIComponent(result.key)}`);
|
|
assert.equal(result.publicUrl, `/uploads/${result.key}`);
|
|
assert.equal(result.mode, "local");
|
|
await service.saveLocal(result.key, "image/png", Buffer.from("image-data"));
|
|
assert.equal(await readFile(path.join(mediaRoot, result.key), "utf8"), "image-data");
|
|
} finally {
|
|
await rm(mediaRoot, { recursive: true, force: true });
|
|
restoreEnvironment(original);
|
|
}
|
|
});
|
|
|
|
test("upload service validates media type and traversal keys before persistence", async () => {
|
|
const original = saveEnvironment();
|
|
const mediaRoot = await mkdtemp(path.join(tmpdir(), "personal-brand-uploads-"));
|
|
process.env.LOCAL_MEDIA_ROOT = mediaRoot;
|
|
try {
|
|
const service = new AdminUploadService();
|
|
await assert.rejects(
|
|
() => service.presign({ fileName: "document.pdf", contentType: "application/pdf" }),
|
|
BadRequestException,
|
|
);
|
|
await assert.rejects(
|
|
() => service.saveLocal("../2026/evil.png", "image/png", Buffer.from("evil")),
|
|
BadRequestException,
|
|
);
|
|
await assert.rejects(
|
|
() => service.saveLocal("2026/photo.jpg", "image/png", Buffer.from("mismatch")),
|
|
BadRequestException,
|
|
);
|
|
await assert.rejects(
|
|
() => service.saveLocal("2026/photo.png", "application/pdf", Buffer.from("mismatch")),
|
|
BadRequestException,
|
|
);
|
|
} finally {
|
|
await rm(mediaRoot, { recursive: true, force: true });
|
|
restoreEnvironment(original);
|
|
}
|
|
});
|
|
|
|
function saveEnvironment(): Record<(typeof environmentKeys)[number], string | undefined> {
|
|
return Object.fromEntries(environmentKeys.map((key) => [key, process.env[key]]));
|
|
}
|
|
|
|
function restoreEnvironment(values: Record<string, string | undefined>): void {
|
|
for (const key of environmentKeys) {
|
|
if (values[key] === undefined) delete process.env[key];
|
|
else process.env[key] = values[key];
|
|
}
|
|
}
|