-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcookie.test.ts
More file actions
147 lines (123 loc) · 4.23 KB
/
cookie.test.ts
File metadata and controls
147 lines (123 loc) · 4.23 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {
buildCookieOptions,
buildDeviceCookieOptions,
setSessionCookie,
clearSessionCookie,
setDeviceTokenCookie,
SESSION_COOKIE_NAME,
DEVICE_COOKIE_NAME,
AuthCookieSettings,
} from '../cookie';
describe('cookie helpers', () => {
describe('buildCookieOptions', () => {
it('should return default options when no settings provided', () => {
const options = buildCookieOptions();
expect(options.httpOnly).toBe(true);
expect(options.sameSite).toBe('lax');
expect(options.path).toBe('/');
expect(options.maxAge).toBe(3600 * 1000); // 1 hour in ms
});
it('should use settings when provided', () => {
const settings: AuthCookieSettings = {
cookieSecure: true,
cookieSameSite: 'strict',
cookieDomain: '.example.com',
cookiePath: '/api',
defaultSessionDuration: '2 hours',
};
const options = buildCookieOptions(settings);
expect(options.secure).toBe(true);
expect(options.sameSite).toBe('strict');
expect(options.domain).toBe('.example.com');
expect(options.path).toBe('/api');
expect(options.maxAge).toBe(2 * 3600 * 1000); // 2 hours in ms
});
it('should use rememberMeDuration when rememberMe is true', () => {
const settings: AuthCookieSettings = {
defaultSessionDuration: '1 hour',
rememberMeDuration: '30 days',
};
const options = buildCookieOptions(settings, true);
expect(options.maxAge).toBe(30 * 24 * 3600 * 1000); // 30 days in ms
});
it('should parse various interval formats', () => {
expect(buildCookieOptions({ defaultSessionDuration: '30 seconds' }).maxAge).toBe(30 * 1000);
expect(buildCookieOptions({ defaultSessionDuration: '5 minutes' }).maxAge).toBe(5 * 60 * 1000);
expect(buildCookieOptions({ defaultSessionDuration: '2 hours' }).maxAge).toBe(2 * 3600 * 1000);
expect(buildCookieOptions({ defaultSessionDuration: '7 days' }).maxAge).toBe(7 * 86400 * 1000);
expect(buildCookieOptions({ defaultSessionDuration: '1 week' }).maxAge).toBe(604800 * 1000);
});
});
describe('buildDeviceCookieOptions', () => {
it('should return long-lived cookie options', () => {
const options = buildDeviceCookieOptions();
expect(options.httpOnly).toBe(true);
expect(options.sameSite).toBe('lax');
expect(options.maxAge).toBe(90 * 24 * 3600 * 1000); // 90 days in ms
});
});
describe('setSessionCookie', () => {
it('should call res.cookie with correct arguments', () => {
const mockRes = {
cookie: jest.fn(),
} as any;
setSessionCookie(mockRes, 'test-token', undefined, false);
expect(mockRes.cookie).toHaveBeenCalledWith(
SESSION_COOKIE_NAME,
'test-token',
expect.objectContaining({
httpOnly: true,
sameSite: 'lax',
})
);
});
it('should use rememberMe duration when true', () => {
const mockRes = {
cookie: jest.fn(),
} as any;
const settings: AuthCookieSettings = {
rememberMeDuration: '30 days',
};
setSessionCookie(mockRes, 'test-token', settings, true);
expect(mockRes.cookie).toHaveBeenCalledWith(
SESSION_COOKIE_NAME,
'test-token',
expect.objectContaining({
maxAge: 30 * 24 * 3600 * 1000,
})
);
});
});
describe('clearSessionCookie', () => {
it('should call res.clearCookie with correct arguments', () => {
const mockRes = {
clearCookie: jest.fn(),
} as any;
clearSessionCookie(mockRes);
expect(mockRes.clearCookie).toHaveBeenCalledWith(
SESSION_COOKIE_NAME,
expect.objectContaining({
httpOnly: true,
sameSite: 'lax',
path: '/',
})
);
});
});
describe('setDeviceTokenCookie', () => {
it('should call res.cookie with long-lived options', () => {
const mockRes = {
cookie: jest.fn(),
} as any;
setDeviceTokenCookie(mockRes, 'device-token-123');
expect(mockRes.cookie).toHaveBeenCalledWith(
DEVICE_COOKIE_NAME,
'device-token-123',
expect.objectContaining({
httpOnly: true,
maxAge: 90 * 24 * 3600 * 1000,
})
);
});
});
});