|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | + |
| 3 | +import { |
| 4 | + getCookieOptions, |
| 5 | + removeTrailingSlash, |
| 6 | + TWENTY_NINE_DAYS, |
| 7 | + MAX_COOKIE_LENGTH, |
| 8 | +} from "./getCookieOptions"; |
| 9 | + |
| 10 | +describe("getCookieOptions", () => { |
| 11 | + it("returns the default configuration when env is provided", () => { |
| 12 | + const result = getCookieOptions(undefined, { |
| 13 | + NODE_ENV: "production", |
| 14 | + KINDE_COOKIE_DOMAIN: "example.com/", |
| 15 | + }); |
| 16 | + |
| 17 | + expect(result).toMatchObject({ |
| 18 | + maxAge: TWENTY_NINE_DAYS, |
| 19 | + domain: "example.com", |
| 20 | + maxCookieLength: MAX_COOKIE_LENGTH, |
| 21 | + sameSite: "lax", |
| 22 | + httpOnly: true, |
| 23 | + path: "/", |
| 24 | + secure: true, |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("allows consumers to override default options", () => { |
| 29 | + const result = getCookieOptions( |
| 30 | + { |
| 31 | + secure: false, |
| 32 | + sameSite: "none", |
| 33 | + path: "/custom", |
| 34 | + maxAge: 60, |
| 35 | + customOption: "value", |
| 36 | + }, |
| 37 | + { |
| 38 | + NODE_ENV: "production", |
| 39 | + KINDE_COOKIE_DOMAIN: "example.com", |
| 40 | + }, |
| 41 | + ); |
| 42 | + |
| 43 | + expect(result.secure).toBe(false); |
| 44 | + expect(result.sameSite).toBe("none"); |
| 45 | + expect(result.path).toBe("/custom"); |
| 46 | + expect(result.maxAge).toBe(60); |
| 47 | + expect(result.customOption).toBe("value"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("falls back to runtime environment variables when env param is omitted", () => { |
| 51 | + const previousNodeEnv = process.env.NODE_ENV; |
| 52 | + const previousCookieDomain = process.env.KINDE_COOKIE_DOMAIN; |
| 53 | + |
| 54 | + process.env.NODE_ENV = "production"; |
| 55 | + process.env.KINDE_COOKIE_DOMAIN = "runtime-domain.io/"; |
| 56 | + |
| 57 | + const result = getCookieOptions(); |
| 58 | + |
| 59 | + expect(result.domain).toBe("runtime-domain.io"); |
| 60 | + expect(result.secure).toBe(true); |
| 61 | + |
| 62 | + if (previousNodeEnv === undefined) { |
| 63 | + delete process.env.NODE_ENV; |
| 64 | + } else { |
| 65 | + process.env.NODE_ENV = previousNodeEnv; |
| 66 | + } |
| 67 | + |
| 68 | + if (previousCookieDomain === undefined) { |
| 69 | + delete process.env.KINDE_COOKIE_DOMAIN; |
| 70 | + } else { |
| 71 | + process.env.KINDE_COOKIE_DOMAIN = previousCookieDomain; |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it("warns when NODE_ENV is missing and secure option is not provided", () => { |
| 76 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 77 | + |
| 78 | + const result = getCookieOptions({}, {}); |
| 79 | + |
| 80 | + expect(result.secure).toBe(false); |
| 81 | + expect(warnSpy).toHaveBeenCalledWith( |
| 82 | + "getCookieOptions: NODE_ENV not set; defaulting secure cookie flag to false. Provide env or override secure to suppress this warning.", |
| 83 | + ); |
| 84 | + |
| 85 | + warnSpy.mockRestore(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("warns when KINDE_COOKIE_DOMAIN resolves to an empty string", () => { |
| 89 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 90 | + |
| 91 | + const result = getCookieOptions( |
| 92 | + {}, |
| 93 | + { NODE_ENV: "development", KINDE_COOKIE_DOMAIN: " " }, |
| 94 | + ); |
| 95 | + |
| 96 | + expect(result.domain).toBeUndefined(); |
| 97 | + expect(warnSpy).toHaveBeenCalledWith( |
| 98 | + "getCookieOptions: KINDE_COOKIE_DOMAIN is empty after trimming and will be ignored.", |
| 99 | + ); |
| 100 | + |
| 101 | + warnSpy.mockRestore(); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe("removeTrailingSlash", () => { |
| 106 | + it("removes trailing slashes and trims whitespace", () => { |
| 107 | + expect(removeTrailingSlash("example.com/")).toBe("example.com"); |
| 108 | + expect(removeTrailingSlash(" example.com/ ")).toBe("example.com"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("returns the original string when there is no trailing slash", () => { |
| 112 | + expect(removeTrailingSlash("example.com")).toBe("example.com"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("returns undefined for nullish values", () => { |
| 116 | + expect(removeTrailingSlash(undefined)).toBeUndefined(); |
| 117 | + expect(removeTrailingSlash(null)).toBeUndefined(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("returns undefined for whitespace-only strings", () => { |
| 121 | + expect(removeTrailingSlash(" ")).toBeUndefined(); |
| 122 | + }); |
| 123 | +}); |
0 commit comments