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