Files
buzz-sheet/app/api/auth/[...all]/route.test.ts
T

44 lines
1.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const handler = vi.fn(async () => Response.json({ passed: true }));
vi.mock("server-only", () => ({}));
vi.mock("better-auth/next-js", () => ({ toNextJsHandler: () => ({ GET: handler }) }));
vi.mock("@/lib/auth/server", () => ({
getAuth: () => ({ handler }),
}));
const { GET, POST } = await import("./route");
beforeEach(() => {
vi.clearAllMocks();
process.env.BETTER_AUTH_URL = "https://guide.sudloh.com";
});
describe("Better Auth route", () => {
it("lets Better Auth serve the local browser session", async () => {
const response = await GET(new Request("https://guide.sudloh.com/api/auth/get-session"));
expect(response.status).toBe(200);
expect(await response.json()).toEqual({ passed: true });
expect(handler).toHaveBeenCalledOnce();
});
it("passes mutations to Better Auth for local session checks", async () => {
const response = await POST(new Request("https://guide.sudloh.com/api/auth/admin/create-user", {
method: "POST", headers: { Origin: "https://guide.sudloh.com", "Content-Type": "application/json" },
body: JSON.stringify({ password: "example-password" }),
}));
expect(response.status).toBe(200);
expect(handler).toHaveBeenCalledOnce();
});
it("passes local profile changes to Better Auth", async () => {
const response = await POST(new Request("https://guide.sudloh.com/api/auth/change-email", {
method: "POST", headers: { Origin: "https://guide.sudloh.com", "Content-Type": "application/json" },
body: JSON.stringify({ newEmail: "[email protected]" }),
}));
expect(response.status).toBe(200);
expect(handler).toHaveBeenCalledOnce();
});
});