93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import worker from "../src/index";
|
|
import type { Env } from "../src/env";
|
|
|
|
interface OpenAPISchemaResponse {
|
|
openapi: string;
|
|
info: {
|
|
title: string;
|
|
};
|
|
paths: Record<string, unknown>;
|
|
}
|
|
|
|
const env: Env = {
|
|
BETTER_AUTH_URL: "http://localhost:8788",
|
|
TRUSTED_ORIGINS: "http://localhost:8787",
|
|
};
|
|
|
|
describe("cfw-auth worker", () => {
|
|
it("does not expose a custom health endpoint", async () => {
|
|
const response = await worker.fetch(new Request("http://auth.local/healthz"), env);
|
|
expect(response.status).toBe(404);
|
|
});
|
|
|
|
it("does not expose a custom session wrapper", async () => {
|
|
const response = await worker.fetch(new Request("http://auth.local/internal/session"), env);
|
|
expect(response.status).toBe(404);
|
|
});
|
|
|
|
it("exposes Better Auth OpenAPI reference", async () => {
|
|
const response = await worker.fetch(new Request("http://auth.local/api/auth/reference"), env);
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get("content-type") ?? "").toContain("text/html");
|
|
});
|
|
|
|
it("exposes Better Auth OpenAPI schema", async () => {
|
|
const response = await worker.fetch(
|
|
new Request("http://auth.local/api/auth/open-api/generate-schema"),
|
|
env,
|
|
);
|
|
const schema = (await response.json()) as OpenAPISchemaResponse;
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get("content-type") ?? "").toContain("application/json");
|
|
expect(schema).toMatchObject({
|
|
openapi: expect.any(String),
|
|
info: {
|
|
title: expect.any(String),
|
|
},
|
|
});
|
|
expect(Object.keys(schema.paths)).toEqual(expect.arrayContaining(["/sign-in/email"]));
|
|
});
|
|
|
|
it("applies configured CORS origin for auth routes", async () => {
|
|
const response = await worker.fetch(
|
|
new Request("http://auth.local/api/auth/reference", {
|
|
headers: {
|
|
Origin: "http://localhost:8787",
|
|
},
|
|
}),
|
|
env,
|
|
);
|
|
expect(response.headers.get("access-control-allow-origin")).toBe("http://localhost:8787");
|
|
});
|
|
|
|
it("handles password reset requests without exposing account existence", async () => {
|
|
const response = await worker.fetch(
|
|
new Request("http://auth.local/api/auth/request-password-reset", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Origin: "http://localhost:8787",
|
|
},
|
|
body: JSON.stringify({
|
|
email: "missing@example.com",
|
|
redirectTo: "http://localhost:8787/reset-password",
|
|
}),
|
|
}),
|
|
env,
|
|
);
|
|
expect([200, 400, 403]).toContain(response.status);
|
|
});
|
|
|
|
it("does not route non-GET/POST auth requests to Better Auth", async () => {
|
|
const response = await worker.fetch(
|
|
new Request("http://auth.local/api/auth/session", {
|
|
method: "PUT",
|
|
}),
|
|
env,
|
|
);
|
|
expect(response.status).toBe(404);
|
|
});
|
|
});
|