|
| 1 | +import ImageKit from '@imagekit/nodejs'; |
| 2 | + |
| 3 | +const client = new ImageKit({ |
| 4 | + privateAPIKey: 'private_key_test', |
| 5 | + password: 'My Password', |
| 6 | + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', |
| 7 | +}); |
| 8 | + |
| 9 | +describe('Helper Authentication Parameters', function () { |
| 10 | + it('should return correct authentication parameters with provided token and expire', function () { |
| 11 | + const authenticationParameters = client.helper.getAuthenticationParameters('your_token', 1582269249); |
| 12 | + |
| 13 | + expect(authenticationParameters).toEqual({ |
| 14 | + token: 'your_token', |
| 15 | + expire: 1582269249, |
| 16 | + signature: 'e71bcd6031016b060d349d212e23e85c791decdd', |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should return authentication parameters with required properties when no params provided', function () { |
| 21 | + const authenticationParameters = client.helper.getAuthenticationParameters(); |
| 22 | + |
| 23 | + expect(authenticationParameters).toHaveProperty('token'); |
| 24 | + expect(authenticationParameters).toHaveProperty('expire'); |
| 25 | + expect(authenticationParameters).toHaveProperty('signature'); |
| 26 | + |
| 27 | + // Token should be a UUID (36 characters with dashes) |
| 28 | + expect(authenticationParameters.token).toMatch( |
| 29 | + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, |
| 30 | + ); |
| 31 | + |
| 32 | + // Expire should be a number greater than current time |
| 33 | + expect(typeof authenticationParameters.expire).toBe('number'); |
| 34 | + expect(authenticationParameters.expire).toBeGreaterThan(Math.floor(Date.now() / 1000)); |
| 35 | + |
| 36 | + // Signature should be a hex string (40 characters for HMAC-SHA1) |
| 37 | + expect(authenticationParameters.signature).toMatch(/^[a-f0-9]{40}$/); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should handle edge case with expire time 0', function () { |
| 41 | + // When expire is 0, it's falsy, so the method uses default expire time |
| 42 | + const authenticationParameters = client.helper.getAuthenticationParameters('test-token', 0); |
| 43 | + |
| 44 | + expect(authenticationParameters.token).toBe('test-token'); |
| 45 | + // Since 0 is falsy, it should use the default expire (30 minutes from now) |
| 46 | + const expectedExpire = Math.floor(Date.now() / 1000) + 60 * 30; |
| 47 | + expect(authenticationParameters.expire).toBeCloseTo(expectedExpire, -1); |
| 48 | + expect(authenticationParameters.signature).toMatch(/^[a-f0-9]{40}$/); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle empty string token', function () { |
| 52 | + // When token is empty string, it's falsy, so the method generates a UUID |
| 53 | + const authenticationParameters = client.helper.getAuthenticationParameters('', 1582269249); |
| 54 | + |
| 55 | + // Since '' is falsy, it should generate a UUID |
| 56 | + expect(authenticationParameters.token).toMatch( |
| 57 | + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, |
| 58 | + ); |
| 59 | + expect(authenticationParameters.expire).toBe(1582269249); |
| 60 | + expect(authenticationParameters.signature).toMatch(/^[a-f0-9]{40}$/); |
| 61 | + }); |
| 62 | +}); |
0 commit comments