Files
wpep/.jjconflict-side-0/tools/gp/webdav_test.ts
T

150 lines
5.1 KiB
TypeScript

import { assert, assertEquals, assertRejects } from "@std/assert";
import { clean_local_files, sync_webdav, upload_webdav } from "./webdav.ts";
function propfindResponse(
entries: Array<{
href: string;
collection?: boolean;
}>,
): Response {
const responses = entries.map(({ href, collection }) =>
`<d:response><d:href>${href}</d:href><d:propstat><d:prop>$
${collection ? "<d:resourcetype><d:collection/></d:resourcetype>" : ""}
</d:prop></d:propstat></d:response>`
).join("");
return new Response(
`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:">${responses}</d:multistatus>`,
{ status: 207 },
);
}
Deno.test("WebDAV sync maps assets and cache into their local roots", async () => {
const base = "https://dav.example.test/remote.php/dav/files/user";
const root = await Deno.makeTempDir();
const assetsRoot = `${root}/assets`;
const cacheRoot = `${root}/publication`;
const seen: string[] = [];
const originalFetch = globalThis.fetch;
globalThis.fetch = async (input, init) => {
const url = String(input);
const method = (init as RequestInit | undefined)?.method ?? "GET";
seen.push(`${method} ${url}`);
if (method === "PROPFIND" && url.endsWith("/gp_upload/assets")) {
return propfindResponse([
{ href: `${base}/gp_upload/assets/` },
{ href: `${base}/gp_upload/assets/pictures/`, collection: true },
{
href:
"https://evil.example.test/remote.php/dav/files/user/gp_upload/assets/outside.png",
},
]);
}
if (method === "PROPFIND" && url.endsWith("/gp_upload/cache/26.11.0")) {
return propfindResponse([
{ href: `${base}/gp_upload/cache/26.11.0/` },
{ href: `${base}/gp_upload/cache/26.11.0/.cache.toml` },
{
href: `${base}/gp_upload/cache/26.11.0/.local-cache/`,
collection: true,
},
]);
}
if (method === "PROPFIND" && url.endsWith("/gp_upload/cache")) {
return propfindResponse([
{ href: `${base}/gp_upload/cache/` },
{ href: `${base}/gp_upload/cache/26.11.0/`, collection: true },
]);
}
if (method === "PROPFIND" && url.endsWith("/assets/pictures")) {
return propfindResponse([
{ href: `${base}/gp_upload/assets/pictures/` },
{ href: `${base}/gp_upload/assets/pictures/rsf.png` },
]);
}
if (method === "PROPFIND" && url.endsWith("/.local-cache")) {
return propfindResponse([
{ href: `${base}/gp_upload/cache/26.11.0/.local-cache/` },
{
href: `${base}/gp_upload/cache/26.11.0/.local-cache/a.png`,
},
]);
}
if (method === "GET") return new Response("ok");
throw new Error(`Unexpected WebDAV request: ${method} ${url}`);
};
try {
await sync_webdav({
url: base,
"remote-dir": "gp_upload",
type: "all",
localRoots: [
{ local: assetsRoot, remote: "assets" },
{ local: cacheRoot, remote: "cache/26.11.0" },
],
});
assertEquals(
await Deno.readTextFile(`${assetsRoot}/pictures/rsf.png`),
"ok",
);
assertEquals(await Deno.readTextFile(`${cacheRoot}/.cache.toml`), "ok");
assertEquals(
await Deno.readTextFile(`${cacheRoot}/.local-cache/a.png`),
"ok",
);
assertEquals(
seen.some((request) => request.includes("outside.png")),
false,
);
} finally {
globalThis.fetch = originalFetch;
await Deno.remove(root, { recursive: true });
}
});
Deno.test("WebDAV upload creates remote collections and uploads only cache files", async () => {
const root = await Deno.makeTempDir();
const publication = `${root}/publication`;
await Deno.mkdir(`${publication}/.local-cache`, { recursive: true });
await Deno.writeTextFile(`${publication}/.cache.toml`, '"a" = "b"');
await Deno.writeTextFile(`${publication}/.local-cache/a.png`, "data");
await Deno.writeTextFile(`${publication}/article.md`, "must not upload");
const methods: string[] = [];
const uploads: string[] = [];
const originalFetch = globalThis.fetch;
globalThis.fetch = async (input, init) => {
const method = (init as RequestInit | undefined)?.method ?? "GET";
methods.push(method);
if (method === "PUT") uploads.push(String(input));
return new Response(null, { status: 201 });
};
try {
await upload_webdav({
url: "https://dav.example.test/root",
type: "cache",
localRoots: [
{ local: publication, remote: "cache/test-publication" },
],
});
assert(methods.includes("MKCOL"));
assert(methods.includes("PUT"));
assertEquals(uploads.length, 2);
assertEquals(uploads.some((url) => url.endsWith("article.md")), false);
} finally {
globalThis.fetch = originalFetch;
await Deno.remove(root, { recursive: true });
}
});
Deno.test("WebDAV commands reject invalid types and unsafe remote roots", async () => {
await assertRejects(
() => sync_webdav({ url: "https://dav.example.test", type: "other" }),
Error,
"--type must be all, cache, or assets",
);
await assertRejects(
() => clean_local_files({ type: "invalid" }),
Error,
"clean --type must be all, cache, or assets",
);
});