|
| 1 | +import type { Request, Response } from 'express'; |
| 2 | +import { |
| 3 | + SESSION_COOKIE_NAME, |
| 4 | + DEVICE_TOKEN_COOKIE_NAME, |
| 5 | + getSessionCookieConfig, |
| 6 | + getDeviceTokenCookieConfig, |
| 7 | + setSessionCookie, |
| 8 | + clearSessionCookie, |
| 9 | + setDeviceTokenCookie, |
| 10 | + clearDeviceTokenCookie, |
| 11 | + parseCookieValue, |
| 12 | + getDeviceTokenFromRequest, |
| 13 | + getSessionTokenFromRequest, |
| 14 | +} from '../cookie'; |
| 15 | +import type { AuthSettings } from '../../types'; |
| 16 | + |
| 17 | +describe('cookie utilities', () => { |
| 18 | + describe('getSessionCookieConfig', () => { |
| 19 | + it('returns default config when no authSettings provided', () => { |
| 20 | + const config = getSessionCookieConfig(); |
| 21 | + expect(config).toEqual({ |
| 22 | + secure: false, // NODE_ENV is 'test' |
| 23 | + sameSite: 'lax', |
| 24 | + domain: undefined, |
| 25 | + httpOnly: true, |
| 26 | + maxAge: 86400, |
| 27 | + path: '/', |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('uses authSettings values when provided', () => { |
| 32 | + const authSettings: AuthSettings = { |
| 33 | + cookieSecure: true, |
| 34 | + cookieSamesite: 'strict', |
| 35 | + cookieDomain: '.example.com', |
| 36 | + cookieHttponly: false, |
| 37 | + cookieMaxAge: '3600', |
| 38 | + cookiePath: '/api', |
| 39 | + }; |
| 40 | + const config = getSessionCookieConfig(authSettings); |
| 41 | + expect(config).toEqual({ |
| 42 | + secure: true, |
| 43 | + sameSite: 'strict', |
| 44 | + domain: '.example.com', |
| 45 | + httpOnly: false, |
| 46 | + maxAge: 3600, |
| 47 | + path: '/api', |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('uses rememberMeDuration when rememberMe is true', () => { |
| 52 | + const authSettings: AuthSettings = { |
| 53 | + cookieMaxAge: '3600', |
| 54 | + rememberMeDuration: '2592000', // 30 days |
| 55 | + }; |
| 56 | + const config = getSessionCookieConfig(authSettings, true); |
| 57 | + expect(config.maxAge).toBe(2592000); |
| 58 | + }); |
| 59 | + |
| 60 | + it('uses cookieMaxAge when rememberMe is false', () => { |
| 61 | + const authSettings: AuthSettings = { |
| 62 | + cookieMaxAge: '3600', |
| 63 | + rememberMeDuration: '2592000', |
| 64 | + }; |
| 65 | + const config = getSessionCookieConfig(authSettings, false); |
| 66 | + expect(config.maxAge).toBe(3600); |
| 67 | + }); |
| 68 | + |
| 69 | + it('falls back to cookieMaxAge when rememberMeDuration is not set', () => { |
| 70 | + const authSettings: AuthSettings = { |
| 71 | + cookieMaxAge: '7200', |
| 72 | + }; |
| 73 | + const config = getSessionCookieConfig(authSettings, true); |
| 74 | + expect(config.maxAge).toBe(7200); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('getDeviceTokenCookieConfig', () => { |
| 79 | + it('returns config with 90 day maxAge', () => { |
| 80 | + const config = getDeviceTokenCookieConfig(); |
| 81 | + expect(config.maxAge).toBe(90 * 24 * 60 * 60); |
| 82 | + expect(config.httpOnly).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it('uses authSettings for other cookie options', () => { |
| 86 | + const authSettings: AuthSettings = { |
| 87 | + cookieSecure: true, |
| 88 | + cookieDomain: '.example.com', |
| 89 | + }; |
| 90 | + const config = getDeviceTokenCookieConfig(authSettings); |
| 91 | + expect(config.secure).toBe(true); |
| 92 | + expect(config.domain).toBe('.example.com'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('setSessionCookie', () => { |
| 97 | + it('sets cookie with correct options', () => { |
| 98 | + const mockRes = { |
| 99 | + cookie: jest.fn(), |
| 100 | + } as unknown as Response; |
| 101 | + |
| 102 | + const config = { |
| 103 | + secure: true, |
| 104 | + sameSite: 'lax' as const, |
| 105 | + domain: '.example.com', |
| 106 | + httpOnly: true, |
| 107 | + maxAge: 3600, |
| 108 | + path: '/', |
| 109 | + }; |
| 110 | + |
| 111 | + setSessionCookie(mockRes, 'test-token', config); |
| 112 | + |
| 113 | + expect(mockRes.cookie).toHaveBeenCalledWith( |
| 114 | + SESSION_COOKIE_NAME, |
| 115 | + 'test-token', |
| 116 | + { |
| 117 | + secure: true, |
| 118 | + sameSite: 'lax', |
| 119 | + domain: '.example.com', |
| 120 | + httpOnly: true, |
| 121 | + maxAge: 3600000, // converted to milliseconds |
| 122 | + path: '/', |
| 123 | + } |
| 124 | + ); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('clearSessionCookie', () => { |
| 129 | + it('clears cookie with correct options', () => { |
| 130 | + const mockRes = { |
| 131 | + clearCookie: jest.fn(), |
| 132 | + } as unknown as Response; |
| 133 | + |
| 134 | + const config = { |
| 135 | + secure: true, |
| 136 | + sameSite: 'lax' as const, |
| 137 | + domain: '.example.com', |
| 138 | + httpOnly: true, |
| 139 | + maxAge: 3600, |
| 140 | + path: '/', |
| 141 | + }; |
| 142 | + |
| 143 | + clearSessionCookie(mockRes, config); |
| 144 | + |
| 145 | + expect(mockRes.clearCookie).toHaveBeenCalledWith( |
| 146 | + SESSION_COOKIE_NAME, |
| 147 | + { |
| 148 | + secure: true, |
| 149 | + sameSite: 'lax', |
| 150 | + domain: '.example.com', |
| 151 | + httpOnly: true, |
| 152 | + path: '/', |
| 153 | + } |
| 154 | + ); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('setDeviceTokenCookie', () => { |
| 159 | + it('sets device token cookie', () => { |
| 160 | + const mockRes = { |
| 161 | + cookie: jest.fn(), |
| 162 | + } as unknown as Response; |
| 163 | + |
| 164 | + const config: Parameters<typeof setDeviceTokenCookie>[2] = { |
| 165 | + secure: true, |
| 166 | + sameSite: 'lax', |
| 167 | + httpOnly: true, |
| 168 | + maxAge: 7776000, |
| 169 | + path: '/', |
| 170 | + }; |
| 171 | + |
| 172 | + setDeviceTokenCookie(mockRes, 'device-123', config); |
| 173 | + |
| 174 | + expect(mockRes.cookie).toHaveBeenCalledWith( |
| 175 | + DEVICE_TOKEN_COOKIE_NAME, |
| 176 | + 'device-123', |
| 177 | + expect.objectContaining({ |
| 178 | + maxAge: 7776000000, |
| 179 | + }) |
| 180 | + ); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('clearDeviceTokenCookie', () => { |
| 185 | + it('clears device token cookie', () => { |
| 186 | + const mockRes = { |
| 187 | + clearCookie: jest.fn(), |
| 188 | + } as unknown as Response; |
| 189 | + |
| 190 | + const config: Parameters<typeof clearDeviceTokenCookie>[1] = { |
| 191 | + secure: false, |
| 192 | + sameSite: 'lax', |
| 193 | + httpOnly: true, |
| 194 | + maxAge: 7776000, |
| 195 | + path: '/', |
| 196 | + }; |
| 197 | + |
| 198 | + clearDeviceTokenCookie(mockRes, config); |
| 199 | + |
| 200 | + expect(mockRes.clearCookie).toHaveBeenCalledWith( |
| 201 | + DEVICE_TOKEN_COOKIE_NAME, |
| 202 | + expect.objectContaining({ |
| 203 | + httpOnly: true, |
| 204 | + path: '/', |
| 205 | + }) |
| 206 | + ); |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + describe('parseCookieValue', () => { |
| 211 | + it('parses cookie value from header', () => { |
| 212 | + const mockReq = { |
| 213 | + headers: { |
| 214 | + cookie: 'foo=bar; constructive_session=test-token; baz=qux', |
| 215 | + }, |
| 216 | + } as unknown as Request; |
| 217 | + |
| 218 | + const value = parseCookieValue(mockReq, 'constructive_session'); |
| 219 | + expect(value).toBe('test-token'); |
| 220 | + }); |
| 221 | + |
| 222 | + it('returns undefined when cookie not found', () => { |
| 223 | + const mockReq = { |
| 224 | + headers: { |
| 225 | + cookie: 'foo=bar', |
| 226 | + }, |
| 227 | + } as unknown as Request; |
| 228 | + |
| 229 | + const value = parseCookieValue(mockReq, 'constructive_session'); |
| 230 | + expect(value).toBeUndefined(); |
| 231 | + }); |
| 232 | + |
| 233 | + it('returns undefined when no cookie header', () => { |
| 234 | + const mockReq = { |
| 235 | + headers: {}, |
| 236 | + } as unknown as Request; |
| 237 | + |
| 238 | + const value = parseCookieValue(mockReq, 'constructive_session'); |
| 239 | + expect(value).toBeUndefined(); |
| 240 | + }); |
| 241 | + |
| 242 | + it('decodes URL-encoded cookie values', () => { |
| 243 | + const mockReq = { |
| 244 | + headers: { |
| 245 | + cookie: 'token=hello%20world', |
| 246 | + }, |
| 247 | + } as unknown as Request; |
| 248 | + |
| 249 | + const value = parseCookieValue(mockReq, 'token'); |
| 250 | + expect(value).toBe('hello world'); |
| 251 | + }); |
| 252 | + }); |
| 253 | + |
| 254 | + describe('getDeviceTokenFromRequest', () => { |
| 255 | + it('extracts device token from cookie', () => { |
| 256 | + const mockReq = { |
| 257 | + headers: { |
| 258 | + cookie: `${DEVICE_TOKEN_COOKIE_NAME}=device-abc123`, |
| 259 | + }, |
| 260 | + } as unknown as Request; |
| 261 | + |
| 262 | + const token = getDeviceTokenFromRequest(mockReq); |
| 263 | + expect(token).toBe('device-abc123'); |
| 264 | + }); |
| 265 | + }); |
| 266 | + |
| 267 | + describe('getSessionTokenFromRequest', () => { |
| 268 | + it('extracts session token from cookie', () => { |
| 269 | + const mockReq = { |
| 270 | + headers: { |
| 271 | + cookie: `${SESSION_COOKIE_NAME}=session-xyz789`, |
| 272 | + }, |
| 273 | + } as unknown as Request; |
| 274 | + |
| 275 | + const token = getSessionTokenFromRequest(mockReq); |
| 276 | + expect(token).toBe('session-xyz789'); |
| 277 | + }); |
| 278 | + }); |
| 279 | +}); |
0 commit comments