|
| 1 | +import type { Request, Response, NextFunction } from 'express'; |
| 2 | +import { DEVICE_TOKEN_COOKIE_NAME } from '../cookie'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Test the device token reading functionality in auth middleware. |
| 6 | + * |
| 7 | + * The actual createAuthenticateMiddleware requires database connections, |
| 8 | + * so we test the device token parsing logic in isolation. |
| 9 | + */ |
| 10 | + |
| 11 | +/** Cookie parsing function - mirrors the implementation in auth.ts */ |
| 12 | +const parseCookieToken = (req: Request, cookieName: string): string | undefined => { |
| 13 | + const header = req.headers.cookie; |
| 14 | + if (!header) return undefined; |
| 15 | + const match = header.split(';').find((c) => c.trim().startsWith(`${cookieName}=`)); |
| 16 | + return match ? decodeURIComponent(match.split('=')[1].trim()) : undefined; |
| 17 | +}; |
| 18 | + |
| 19 | +describe('auth middleware device token handling', () => { |
| 20 | + const createMockRequest = (cookies?: string): Partial<Request> => ({ |
| 21 | + headers: cookies ? { cookie: cookies } : {}, |
| 22 | + }); |
| 23 | + |
| 24 | + describe('device token cookie parsing', () => { |
| 25 | + it('should extract device token from cookie header', () => { |
| 26 | + const req = createMockRequest(`${DEVICE_TOKEN_COOKIE_NAME}=device-abc123`); |
| 27 | + const deviceToken = parseCookieToken(req as Request, DEVICE_TOKEN_COOKIE_NAME); |
| 28 | + expect(deviceToken).toBe('device-abc123'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return undefined when device token cookie is not present', () => { |
| 32 | + const req = createMockRequest('other_cookie=value'); |
| 33 | + const deviceToken = parseCookieToken(req as Request, DEVICE_TOKEN_COOKIE_NAME); |
| 34 | + expect(deviceToken).toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return undefined when no cookies are present', () => { |
| 38 | + const req = createMockRequest(); |
| 39 | + const deviceToken = parseCookieToken(req as Request, DEVICE_TOKEN_COOKIE_NAME); |
| 40 | + expect(deviceToken).toBeUndefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle multiple cookies and extract device token', () => { |
| 44 | + const req = createMockRequest( |
| 45 | + `session=abc; ${DEVICE_TOKEN_COOKIE_NAME}=device-xyz789; csrf=token123` |
| 46 | + ); |
| 47 | + const deviceToken = parseCookieToken(req as Request, DEVICE_TOKEN_COOKIE_NAME); |
| 48 | + expect(deviceToken).toBe('device-xyz789'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should decode URL-encoded device token values', () => { |
| 52 | + const req = createMockRequest(`${DEVICE_TOKEN_COOKIE_NAME}=device%2Ftoken%3D123`); |
| 53 | + const deviceToken = parseCookieToken(req as Request, DEVICE_TOKEN_COOKIE_NAME); |
| 54 | + expect(deviceToken).toBe('device/token=123'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle device token with special characters', () => { |
| 58 | + const req = createMockRequest(`${DEVICE_TOKEN_COOKIE_NAME}=abc-123_XYZ.test`); |
| 59 | + const deviceToken = parseCookieToken(req as Request, DEVICE_TOKEN_COOKIE_NAME); |
| 60 | + expect(deviceToken).toBe('abc-123_XYZ.test'); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('device token attachment to request', () => { |
| 65 | + it('should set deviceToken on request when cookie is present', () => { |
| 66 | + const req = createMockRequest(`${DEVICE_TOKEN_COOKIE_NAME}=device-token-value`) as Request; |
| 67 | + |
| 68 | + // Simulate what auth middleware does |
| 69 | + const deviceToken = parseCookieToken(req, DEVICE_TOKEN_COOKIE_NAME); |
| 70 | + if (deviceToken) { |
| 71 | + (req as any).deviceToken = deviceToken; |
| 72 | + } |
| 73 | + |
| 74 | + expect((req as any).deviceToken).toBe('device-token-value'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should not set deviceToken when cookie is absent', () => { |
| 78 | + const req = createMockRequest('other=value') as Request; |
| 79 | + |
| 80 | + const deviceToken = parseCookieToken(req, DEVICE_TOKEN_COOKIE_NAME); |
| 81 | + if (deviceToken) { |
| 82 | + (req as any).deviceToken = deviceToken; |
| 83 | + } |
| 84 | + |
| 85 | + expect((req as any).deviceToken).toBeUndefined(); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments