|
| 1 | +const mockGetCloudFrontConfig = jest.fn(); |
| 2 | +const mockGetSignedCookies = jest.fn(); |
| 3 | + |
| 4 | +jest.mock('~/cdn/cloudfront', () => ({ |
| 5 | + getCloudFrontConfig: () => mockGetCloudFrontConfig(), |
| 6 | +})); |
| 7 | + |
| 8 | +jest.mock('@aws-sdk/cloudfront-signer', () => ({ |
| 9 | + getSignedCookies: (params: unknown) => mockGetSignedCookies(params), |
| 10 | +})); |
| 11 | + |
| 12 | +import type { Response } from 'express'; |
| 13 | +import { isCloudFrontCookiesEnabled, setCloudFrontCookies } from '../cloudfront-cookies'; |
| 14 | + |
| 15 | +describe('isCloudFrontCookiesEnabled', () => { |
| 16 | + beforeEach(() => { |
| 17 | + jest.clearAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('returns false when CloudFront config is null', () => { |
| 21 | + mockGetCloudFrontConfig.mockReturnValue(null); |
| 22 | + |
| 23 | + const result = isCloudFrontCookiesEnabled(); |
| 24 | + |
| 25 | + expect(result).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns false when imageSigning is not "cookies"', () => { |
| 29 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 30 | + domain: 'https://cdn.example.com', |
| 31 | + imageSigning: 'none', |
| 32 | + cookieDomain: '.example.com', |
| 33 | + privateKey: 'test-key', |
| 34 | + keyPairId: 'K123', |
| 35 | + }); |
| 36 | + |
| 37 | + const result = isCloudFrontCookiesEnabled(); |
| 38 | + |
| 39 | + expect(result).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns false when signing keys are missing', () => { |
| 43 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 44 | + domain: 'https://cdn.example.com', |
| 45 | + imageSigning: 'cookies', |
| 46 | + cookieDomain: '.example.com', |
| 47 | + privateKey: null, |
| 48 | + keyPairId: null, |
| 49 | + }); |
| 50 | + |
| 51 | + const result = isCloudFrontCookiesEnabled(); |
| 52 | + |
| 53 | + expect(result).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns false when cookieDomain is missing', () => { |
| 57 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 58 | + domain: 'https://cdn.example.com', |
| 59 | + imageSigning: 'cookies', |
| 60 | + privateKey: 'test-key', |
| 61 | + keyPairId: 'K123', |
| 62 | + }); |
| 63 | + |
| 64 | + const result = isCloudFrontCookiesEnabled(); |
| 65 | + |
| 66 | + expect(result).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns true when all conditions are met', () => { |
| 70 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 71 | + domain: 'https://cdn.example.com', |
| 72 | + imageSigning: 'cookies', |
| 73 | + cookieDomain: '.example.com', |
| 74 | + privateKey: 'test-key', |
| 75 | + keyPairId: 'K123', |
| 76 | + }); |
| 77 | + |
| 78 | + const result = isCloudFrontCookiesEnabled(); |
| 79 | + |
| 80 | + expect(result).toBe(true); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('setCloudFrontCookies', () => { |
| 85 | + let mockRes: Partial<Response>; |
| 86 | + let cookieArgs: Array<[string, string, object]>; |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + jest.clearAllMocks(); |
| 90 | + cookieArgs = []; |
| 91 | + mockRes = { |
| 92 | + cookie: jest.fn((name: string, value: string, options: object) => { |
| 93 | + cookieArgs.push([name, value, options]); |
| 94 | + return mockRes as Response; |
| 95 | + }) as unknown as Response['cookie'], |
| 96 | + }; |
| 97 | + }); |
| 98 | + |
| 99 | + it('returns false when CloudFront config is null', () => { |
| 100 | + mockGetCloudFrontConfig.mockReturnValue(null); |
| 101 | + |
| 102 | + const result = setCloudFrontCookies(mockRes as Response); |
| 103 | + |
| 104 | + expect(result).toBe(false); |
| 105 | + expect(mockRes.cookie).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('sets three CloudFront cookies when enabled', () => { |
| 109 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 110 | + domain: 'https://cdn.example.com', |
| 111 | + imageSigning: 'cookies', |
| 112 | + cookieExpiry: 1800, |
| 113 | + cookieDomain: '.example.com', |
| 114 | + privateKey: '-----BEGIN RSA PRIVATE KEY-----\ntest\n-----END RSA PRIVATE KEY-----', |
| 115 | + keyPairId: 'K123ABC', |
| 116 | + }); |
| 117 | + |
| 118 | + mockGetSignedCookies.mockReturnValue({ |
| 119 | + 'CloudFront-Policy': 'policy-value', |
| 120 | + 'CloudFront-Signature': 'signature-value', |
| 121 | + 'CloudFront-Key-Pair-Id': 'K123ABC', |
| 122 | + }); |
| 123 | + |
| 124 | + const result = setCloudFrontCookies(mockRes as Response); |
| 125 | + |
| 126 | + expect(result).toBe(true); |
| 127 | + expect(mockRes.cookie).toHaveBeenCalledTimes(3); |
| 128 | + |
| 129 | + const cookieNames = cookieArgs.map(([name]) => name); |
| 130 | + expect(cookieNames).toContain('CloudFront-Policy'); |
| 131 | + expect(cookieNames).toContain('CloudFront-Signature'); |
| 132 | + expect(cookieNames).toContain('CloudFront-Key-Pair-Id'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('uses cookieDomain from config', () => { |
| 136 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 137 | + domain: 'https://cdn.example.com', |
| 138 | + imageSigning: 'cookies', |
| 139 | + cookieExpiry: 1800, |
| 140 | + cookieDomain: '.example.com', |
| 141 | + privateKey: '-----BEGIN RSA PRIVATE KEY-----\ntest\n-----END RSA PRIVATE KEY-----', |
| 142 | + keyPairId: 'K123ABC', |
| 143 | + }); |
| 144 | + |
| 145 | + mockGetSignedCookies.mockReturnValue({ |
| 146 | + 'CloudFront-Policy': 'policy-value', |
| 147 | + 'CloudFront-Signature': 'signature-value', |
| 148 | + 'CloudFront-Key-Pair-Id': 'K123ABC', |
| 149 | + }); |
| 150 | + |
| 151 | + setCloudFrontCookies(mockRes as Response); |
| 152 | + |
| 153 | + const [, , options] = cookieArgs[0]; |
| 154 | + expect(options).toMatchObject({ |
| 155 | + httpOnly: true, |
| 156 | + secure: true, |
| 157 | + sameSite: 'none', |
| 158 | + domain: '.example.com', |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('builds correct custom policy for wildcard resource', () => { |
| 163 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 164 | + domain: 'https://cdn.example.com', |
| 165 | + imageSigning: 'cookies', |
| 166 | + cookieExpiry: 1800, |
| 167 | + cookieDomain: '.example.com', |
| 168 | + privateKey: '-----BEGIN RSA PRIVATE KEY-----\ntest\n-----END RSA PRIVATE KEY-----', |
| 169 | + keyPairId: 'K123ABC', |
| 170 | + }); |
| 171 | + |
| 172 | + mockGetSignedCookies.mockReturnValue({ |
| 173 | + 'CloudFront-Policy': 'policy-value', |
| 174 | + 'CloudFront-Signature': 'signature-value', |
| 175 | + 'CloudFront-Key-Pair-Id': 'K123ABC', |
| 176 | + }); |
| 177 | + |
| 178 | + setCloudFrontCookies(mockRes as Response); |
| 179 | + |
| 180 | + expect(mockGetSignedCookies).toHaveBeenCalledWith( |
| 181 | + expect.objectContaining({ |
| 182 | + keyPairId: 'K123ABC', |
| 183 | + privateKey: expect.stringContaining('BEGIN RSA PRIVATE KEY'), |
| 184 | + policy: expect.stringContaining('https://cdn.example.com/*'), |
| 185 | + }), |
| 186 | + ); |
| 187 | + }); |
| 188 | + |
| 189 | + it('returns false when cookieDomain is missing', () => { |
| 190 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 191 | + domain: 'https://cdn.example.com', |
| 192 | + imageSigning: 'cookies', |
| 193 | + cookieExpiry: 1800, |
| 194 | + privateKey: '-----BEGIN RSA PRIVATE KEY-----\ntest\n-----END RSA PRIVATE KEY-----', |
| 195 | + keyPairId: 'K123ABC', |
| 196 | + // cookieDomain is missing |
| 197 | + }); |
| 198 | + |
| 199 | + const result = setCloudFrontCookies(mockRes as Response); |
| 200 | + |
| 201 | + expect(result).toBe(false); |
| 202 | + expect(mockRes.cookie).not.toHaveBeenCalled(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('returns false and logs warning on signing error', () => { |
| 206 | + mockGetCloudFrontConfig.mockReturnValue({ |
| 207 | + domain: 'https://cdn.example.com', |
| 208 | + imageSigning: 'cookies', |
| 209 | + cookieExpiry: 1800, |
| 210 | + cookieDomain: '.example.com', |
| 211 | + privateKey: 'invalid-key', |
| 212 | + keyPairId: 'K123ABC', |
| 213 | + }); |
| 214 | + |
| 215 | + mockGetSignedCookies.mockImplementation(() => { |
| 216 | + throw new Error('Invalid private key'); |
| 217 | + }); |
| 218 | + |
| 219 | + const result = setCloudFrontCookies(mockRes as Response); |
| 220 | + |
| 221 | + expect(result).toBe(false); |
| 222 | + expect(mockRes.cookie).not.toHaveBeenCalled(); |
| 223 | + }); |
| 224 | +}); |
0 commit comments