b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createPublicPluginApiRouteHandler } from "../../../src/astro/public-plugin-api-routes.js";
|
|
|
|
function createRuntime(meta: { public: boolean } | null) {
|
|
const result = { success: true, data: { ok: true } };
|
|
const handlePluginApiRoute = vi.fn(async () => result);
|
|
const getPluginRouteMeta = vi.fn(() => meta);
|
|
|
|
return {
|
|
runtime: {
|
|
getPluginRouteMeta,
|
|
handlePluginApiRoute,
|
|
},
|
|
getPluginRouteMeta,
|
|
handlePluginApiRoute,
|
|
result,
|
|
};
|
|
}
|
|
|
|
describe("createPublicPluginApiRouteHandler", () => {
|
|
it("delegates to the runtime when the plugin route is public", async () => {
|
|
const { runtime, getPluginRouteMeta, handlePluginApiRoute, result } = createRuntime({
|
|
public: true,
|
|
});
|
|
const request = new Request("https://example.com/_emdash/api/plugins/demo/definition", {
|
|
method: "POST",
|
|
body: "{}",
|
|
});
|
|
|
|
const handler = createPublicPluginApiRouteHandler(runtime);
|
|
const actual = await handler("demo", "POST", "/definition", request);
|
|
|
|
expect(getPluginRouteMeta).toHaveBeenCalledWith("demo", "/definition");
|
|
expect(handlePluginApiRoute).toHaveBeenCalledWith("demo", "POST", "/definition", request);
|
|
expect(actual).toBe(result);
|
|
});
|
|
|
|
it("returns not found without invoking private plugin routes", async () => {
|
|
const { runtime, handlePluginApiRoute } = createRuntime({ public: false });
|
|
const handler = createPublicPluginApiRouteHandler(runtime);
|
|
|
|
const result = await handler(
|
|
"demo",
|
|
"POST",
|
|
"/admin",
|
|
new Request("https://example.com/_emdash/api/plugins/demo/admin"),
|
|
);
|
|
|
|
expect(handlePluginApiRoute).not.toHaveBeenCalled();
|
|
expect(result).toEqual({
|
|
success: false,
|
|
error: {
|
|
code: "NOT_FOUND",
|
|
message: "Plugin route not found",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("returns not found without invoking missing plugin routes", async () => {
|
|
const { runtime, handlePluginApiRoute } = createRuntime(null);
|
|
const handler = createPublicPluginApiRouteHandler(runtime);
|
|
|
|
const result = await handler(
|
|
"demo",
|
|
"POST",
|
|
"/missing",
|
|
new Request("https://example.com/_emdash/api/plugins/demo/missing"),
|
|
);
|
|
|
|
expect(handlePluginApiRoute).not.toHaveBeenCalled();
|
|
expect(result.error?.code).toBe("NOT_FOUND");
|
|
});
|
|
});
|