set-auth-cookie-options.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. const mockCookieSet = vi.hoisted(() => vi.fn());
  3. const mockCookies = vi.hoisted(() => vi.fn());
  4. const mockGetEnvConfig = vi.hoisted(() => vi.fn());
  5. const mockIsDevelopment = vi.hoisted(() => vi.fn(() => false));
  6. vi.mock("next/headers", () => ({
  7. cookies: mockCookies,
  8. headers: vi.fn().mockResolvedValue(new Headers()),
  9. }));
  10. vi.mock("@/lib/config/env.schema", () => ({
  11. getEnvConfig: mockGetEnvConfig,
  12. isDevelopment: mockIsDevelopment,
  13. }));
  14. vi.mock("@/lib/config/config", () => ({ config: { auth: { adminToken: "test" } } }));
  15. vi.mock("@/repository/key", () => ({ validateApiKeyAndGetUser: vi.fn() }));
  16. import { setAuthCookie } from "@/lib/auth";
  17. describe("setAuthCookie options", () => {
  18. beforeEach(() => {
  19. mockCookieSet.mockClear();
  20. mockCookies.mockResolvedValue({ set: mockCookieSet, get: vi.fn(), delete: vi.fn() });
  21. });
  22. describe("when ENABLE_SECURE_COOKIES is true", () => {
  23. beforeEach(() => {
  24. mockGetEnvConfig.mockReturnValue({ ENABLE_SECURE_COOKIES: true });
  25. });
  26. it("sets secure=true", async () => {
  27. await setAuthCookie("test-key-123");
  28. expect(mockCookieSet).toHaveBeenCalledTimes(1);
  29. const [, , options] = mockCookieSet.mock.calls[0];
  30. expect(options.secure).toBe(true);
  31. });
  32. });
  33. describe("when ENABLE_SECURE_COOKIES is false", () => {
  34. beforeEach(() => {
  35. mockGetEnvConfig.mockReturnValue({ ENABLE_SECURE_COOKIES: false });
  36. });
  37. it("sets secure=false", async () => {
  38. await setAuthCookie("test-key-456");
  39. expect(mockCookieSet).toHaveBeenCalledTimes(1);
  40. const [, , options] = mockCookieSet.mock.calls[0];
  41. expect(options.secure).toBe(false);
  42. });
  43. });
  44. describe("invariant cookie options", () => {
  45. beforeEach(() => {
  46. mockGetEnvConfig.mockReturnValue({ ENABLE_SECURE_COOKIES: true });
  47. });
  48. it("always sets httpOnly to true", async () => {
  49. await setAuthCookie("any-key");
  50. const [, , options] = mockCookieSet.mock.calls[0];
  51. expect(options.httpOnly).toBe(true);
  52. });
  53. it("always sets sameSite to lax", async () => {
  54. await setAuthCookie("any-key");
  55. const [, , options] = mockCookieSet.mock.calls[0];
  56. expect(options.sameSite).toBe("lax");
  57. });
  58. it("always sets maxAge to 7 days (604800 seconds)", async () => {
  59. await setAuthCookie("any-key");
  60. const [, , options] = mockCookieSet.mock.calls[0];
  61. expect(options.maxAge).toBe(604800);
  62. });
  63. it("always sets path to /", async () => {
  64. await setAuthCookie("any-key");
  65. const [, , options] = mockCookieSet.mock.calls[0];
  66. expect(options.path).toBe("/");
  67. });
  68. });
  69. describe("cookie name and value", () => {
  70. beforeEach(() => {
  71. mockGetEnvConfig.mockReturnValue({ ENABLE_SECURE_COOKIES: true });
  72. });
  73. it("sets cookie name to auth-token", async () => {
  74. await setAuthCookie("my-secret-key");
  75. const [name] = mockCookieSet.mock.calls[0];
  76. expect(name).toBe("auth-token");
  77. });
  78. it("sets cookie value to the provided keyString", async () => {
  79. await setAuthCookie("my-secret-key");
  80. const [, value] = mockCookieSet.mock.calls[0];
  81. expect(value).toBe("my-secret-key");
  82. });
  83. });
  84. });