test : just complete
CI / Verify (push) Successful in 1m30s
CI / Build immutable images and deploy (push) Successful in 2m34s

This commit is contained in:
2026-09-22 18:49:54 +07:00 Unverified
parent 1d36167011
commit 41da2478ac
6 changed files with 48 additions and 29 deletions
+18 -8
View File
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({ session: vi.fn(), auth: vi.fn() }));
const mocks = { session: vi.fn(), auth: vi.fn() };
vi.mock("server-only", () => ({}));
vi.mock("@/db", () => ({ getDb: () => ({}) }));
vi.mock("next/headers", () => ({ headers: async () => new Headers() }));
@@ -9,20 +9,30 @@ 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();
vi.stubEnv("NODE_ENV", "development");
vi.stubEnv("DATABASE_URL", "postgresql://test.invalid/test");
vi.stubEnv("BETTER_AUTH_URL", "https://guide.example.test");
vi.stubEnv("BETTER_AUTH_SECRET", "a-test-secret-with-more-than-32-characters");
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(() => vi.unstubAllEnvs());
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 () => {
vi.stubEnv("BUZZ_DEMO_MODE", "true");
testEnv.BUZZ_DEMO_MODE = "true";
const { getAdminSession, requireAdmin } = await import("./server");
expect(await getAdminSession()).toBeNull();
await expect(requireAdmin()).rejects.toMatchObject({ status: 401 });