-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcookie.spec.ts
85 lines (70 loc) · 2.74 KB
/
cookie.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { describe, it, expect } from '@jest/globals';
// Mock at the top of the file
jest.mock('../src/env-variables');
describe('cookie.ts', () => {
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();
// Reset modules
jest.resetModules();
});
describe('getCookieOptions', () => {
it('should return the default cookie options', async () => {
const { getCookieOptions } = await import('../src/cookie');
const options = getCookieOptions();
expect(options).toEqual(
expect.objectContaining({
path: '/',
httpOnly: true,
secure: false,
sameSite: 'lax',
maxAge: 400 * 24 * 60 * 60,
domain: 'example.com',
}),
);
});
it('should return the cookie options with custom values', async () => {
// Import the mocked module
const envVars = await import('../src/env-variables');
// Set the mock values
Object.defineProperty(envVars, 'WORKOS_COOKIE_MAX_AGE', { value: '1000' });
Object.defineProperty(envVars, 'WORKOS_COOKIE_DOMAIN', { value: 'foobar.com' });
const { getCookieOptions } = await import('../src/cookie');
const options = getCookieOptions('http://example.com');
expect(options).toEqual(
expect.objectContaining({
secure: false,
maxAge: 1000,
domain: 'foobar.com',
}),
);
Object.defineProperty(envVars, 'WORKOS_COOKIE_DOMAIN', { value: '' });
const options2 = getCookieOptions('http://example.com');
expect(options2).toEqual(
expect.objectContaining({
secure: false,
maxAge: 1000,
domain: '',
}),
);
const options3 = getCookieOptions('https://example.com', true);
expect(options3).toEqual(expect.stringContaining('Domain='));
});
it('should return the cookie options with expired set to true', async () => {
const { getCookieOptions } = await import('../src/cookie');
const options = getCookieOptions('http://example.com', false, true);
expect(options).toEqual(expect.objectContaining({ maxAge: 0 }));
});
it('should return the cookie options as a string', async () => {
const { getCookieOptions } = await import('../src/cookie');
const options = getCookieOptions('http://example.com', true, false);
expect(options).toEqual(
expect.stringContaining('Path=/; HttpOnly; Secure=false; SameSite="Lax"; Max-Age=34560000; Domain=example.com'),
);
const options2 = getCookieOptions('https://example.com', true, true);
expect(options2).toEqual(
expect.stringContaining('Path=/; HttpOnly; Secure=true; SameSite="Lax"; Max-Age=0; Domain=example.com'),
);
});
});
});