Files
sass-mohfarawati/tests/integration/media-service.test.ts
T
moh dc21c33867 ADDED - Admin SEO page, robots/sitemap hardening and media/maintenance security fixes
SEO
- New Settings > SEO admin page (seo_settings in app_config): indexing switch,
  Google/Bing verification, X handle, JSON-LD identity (Person/Organization,
  sameAs), per-locale keywords, readiness checklist and open links for
  sitemap.xml / robots.txt / manifest.
- robots.txt is now dynamic: disallows admin, api, success and coming-soon
  paths; blocks everything while indexing is off or maintenance is on.
- sitemap.xml carries hreflang alternates per URL, lists only categories with
  published projects, and is empty while hidden.
- Metadata: robots + verification meta, og:locale in de_DE/en_US/ar_AR form,
  alternateLocale, twitter site/creator, project cover as OG image with
  article type, noindex on /success and /coming-soon.
- JSON-LD: WebSite + publisher graph on all public pages, CreativeWork per
  project (view-mode independent).

Security
- Maintenance bypass now requires a correctly signed admin cookie; the
  middleware previously only checked the cookie existed. Token helpers moved
  to lib/admin-session-token.ts (shared by proxy.ts and lib/admin-auth.ts).
- Media uploads: magic-byte validation against the declared type, SVG
  sanitization (script/handlers/foreignObject/javascript: rejected), upload
  folder sanitized, kind inferred from the real file.
- Media route: fixed prefix-based path check that accepted sibling
  directories, unknown extensions return 404, nosniff header, CSP sandbox on
  SVG, gif content type added.
- External media URLs: protocol-relative (//host) URLs rejected.

Portfolio
- Project and category slugs share /portfolio/[slug]; saving now rejects a
  slug already used on the other side instead of silently shadowing it.

Tooling/docs
- Lint: ignore scripts/legacy-prisma-seed.cjs, drop unused import.
- New docs/SEO.md; FEATURES, ARCHITECTURE (Drizzle instead of Prisma), admin
  spec and CLAUDE.md updated.
- Tests for all of the above (unit + integration); suite green.
2026-09-20 21:36:16 +02:00

124 lines
4.4 KiB
TypeScript

import { readFile } from "fs/promises";
import { describe, expect, it } from "vitest";
import { eq } from "drizzle-orm";
import { resolveMediaSelection } from "@/lib/media-service";
import { resolveMediaUploadPath } from "@/lib/media-storage";
import { db } from "@/lib/db";
import { mediaAsset } from "@/lib/db/schema";
import { createMediaAsset } from "@/tests/helpers/factories";
import { canManageUploads } from "@/tests/helpers/fs-capability";
describe("resolveMediaSelection — library mode", () => {
it("returns the referenced asset", async () => {
const asset = await createMediaAsset({ url: "https://cdn/lib.png" });
const result = await resolveMediaSelection({
media: { mode: "library", assetId: asset.id, url: "", label: "", kind: "IMAGE" },
uploadFile: null,
folder: "covers",
fallbackLabel: "Cover",
required: false,
});
expect(result.assetId).toBe(asset.id);
expect(result.url).toBe("https://cdn/lib.png");
});
it("throws when the referenced asset is missing", async () => {
await expect(
resolveMediaSelection({
media: { mode: "library", assetId: "nope", url: "", label: "", kind: "IMAGE" },
uploadFile: null,
folder: "covers",
fallbackLabel: "Cover",
required: true,
}),
).rejects.toThrow(/not found/i);
});
});
describe("resolveMediaSelection — external mode", () => {
it("creates a new external asset from the url", async () => {
const result = await resolveMediaSelection({
media: { mode: "external", assetId: "", url: "https://cdn/new/photo.png", label: "Photo", kind: "IMAGE" },
uploadFile: null,
folder: "covers",
fallbackLabel: "Cover",
required: true,
});
expect(result.createdAssetId).toBeTruthy();
expect(result.url).toBe("https://cdn/new/photo.png");
const stored = await db.query.mediaAsset.findFirst({ where: eq(mediaAsset.id, result.assetId!) });
expect(stored?.source).toBe("EXTERNAL");
expect(stored?.fileName).toBe("photo.png");
expect(stored?.label).toBe("Photo");
});
it("returns empty selection for a not-required empty url", async () => {
const result = await resolveMediaSelection({
media: { mode: "external", assetId: "", url: "", label: "", kind: "IMAGE" },
uploadFile: null,
folder: "covers",
fallbackLabel: "Cover",
required: false,
});
expect(result.assetId).toBeNull();
expect(result.url).toBe("");
});
});
describe("resolveMediaSelection — missing configuration", () => {
it("throws when required and no media object is present", async () => {
await expect(
resolveMediaSelection({ media: undefined, uploadFile: null, folder: "covers", fallbackLabel: "L", required: true }),
).rejects.toThrow(/missing/i);
});
it("returns empty selection when not required and no media object is present", async () => {
const result = await resolveMediaSelection({
media: undefined,
uploadFile: null,
folder: "covers",
fallbackLabel: "L",
required: false,
});
expect(result.assetId).toBeNull();
});
it("throws for a required upload with no file", async () => {
await expect(
resolveMediaSelection({
media: { mode: "upload", assetId: "", url: "", label: "", kind: "IMAGE" },
uploadFile: null,
folder: "covers",
fallbackLabel: "L",
required: true,
}),
).rejects.toThrow(/required/i);
});
});
describe("resolveMediaSelection — upload mode (filesystem)", () => {
it.skipIf(!canManageUploads)("saves the file and creates an UPLOAD asset", async () => {
const file = new File([new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])], "shot.png", { type: "image/png" });
const result = await resolveMediaSelection({
media: { mode: "upload", assetId: "", url: "", label: "Shot", kind: "IMAGE" },
uploadFile: file,
folder: "tests",
fallbackLabel: "L",
required: true,
});
expect(result.uploadedUrl).toBeTruthy();
const stored = await db.query.mediaAsset.findFirst({ where: eq(mediaAsset.id, result.assetId!) });
expect(stored?.source).toBe("UPLOAD");
// File actually written to disk
const bytes = await readFile(resolveMediaUploadPath(result.url));
expect(bytes.length).toBeGreaterThan(0);
// cleanup
const { removeManagedMediaFile } = await import("@/lib/media-storage");
await removeManagedMediaFile(result.url);
});
});