59 lines
2.9 KiB
TypeScript
59 lines
2.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = { session: vi.fn(), auth: vi.fn() };
|
|
vi.mock("server-only", () => ({}));
|
|
vi.mock("@/db", () => ({ getDb: () => ({}) }));
|
|
vi.mock("next/headers", () => ({ headers: async () => new Headers() }));
|
|
vi.mock("better-auth", () => ({ betterAuth: mocks.auth }));
|
|
vi.mock("better-auth/adapters/drizzle", () => ({ drizzleAdapter: () => ({}) }));
|
|
vi.mock("better-auth/next-js", () => ({ nextCookies: () => ({}) }));
|
|
vi.mock("better-auth/plugins", () => ({ admin: (options: unknown) => options, captcha: () => ({}) }));
|
|
|
|
const envNames = ["NODE_ENV", "DATABASE_URL", "BETTER_AUTH_URL", "BETTER_AUTH_SECRET", "BUZZ_DEMO_MODE"] as const;
|
|
const testEnv = process.env as Record<string, string | undefined>;
|
|
const originalEnv = envNames.map((name) => testEnv[name]);
|
|
|
|
beforeEach(() => {
|
|
vi.resetModules?.();
|
|
testEnv.NODE_ENV = "development";
|
|
testEnv.DATABASE_URL = "postgresql://test.invalid/test";
|
|
testEnv.BETTER_AUTH_URL = "https://guide.example.test";
|
|
testEnv.BETTER_AUTH_SECRET = "a-test-secret-with-more-than-32-characters";
|
|
mocks.auth.mockReturnValue({ api: { getSession: mocks.session } });
|
|
mocks.session.mockResolvedValue(null);
|
|
});
|
|
afterEach(() => {
|
|
envNames.forEach((name, index) => {
|
|
const value = originalEnv[index];
|
|
if (value === undefined) delete testEnv[name];
|
|
else testEnv[name] = value;
|
|
});
|
|
});
|
|
|
|
describe("actual administrator session boundary", () => {
|
|
it("does not grant access when the old demo flag is enabled", async () => {
|
|
testEnv.BUZZ_DEMO_MODE = "true";
|
|
const { getAdminSession, requireAdmin } = await import("./server");
|
|
expect(await getAdminSession()).toBeNull();
|
|
await expect(requireAdmin()).rejects.toMatchObject({ status: 401 });
|
|
});
|
|
it("rejects unprivileged users and revoked sessions", async () => {
|
|
const { requireAdmin } = await import("./server");
|
|
const user = { id: "admin", email: "[email protected]", emailVerified: true, role: "admin" };
|
|
mocks.session.mockResolvedValue({ user, session: { id: "session" } });
|
|
await expect(requireAdmin()).resolves.toMatchObject({ user: { id: "admin" } });
|
|
mocks.session.mockResolvedValue({ user: { ...user, role: "user" }, session: { id: "session" } });
|
|
await expect(requireAdmin()).rejects.toMatchObject({ status: 401 });
|
|
mocks.session.mockResolvedValue(null);
|
|
await expect(requireAdmin()).rejects.toMatchObject({ status: 401 });
|
|
});
|
|
it("defaults accounts to user and requires six-character new passwords", async () => {
|
|
(await import("./server")).getAuth();
|
|
const options = mocks.auth.mock.calls.at(-1)![0];
|
|
expect(options.emailAndPassword).toMatchObject({ disableSignUp: false, minPasswordLength: 6 });
|
|
expect(options.plugins).toContainEqual({ defaultRole: "user" });
|
|
expect(options.rateLimit.customStorage.consume).toBeTypeOf("function");
|
|
expect(options.databaseHooks).toBeUndefined();
|
|
});
|
|
});
|