|
| 1 | +import { authProviderFactory } from '../authProvider'; |
| 2 | +import { Backoff } from '../../utils/Backoff'; |
| 3 | +import { loggerMock } from '../../logger/__tests__/sdkLogger.mock'; |
| 4 | +import { makeJwtCredential } from '../../__tests__/testUtils/jwt'; |
| 5 | + |
| 6 | +// Speed up backoff for tests |
| 7 | +Backoff.__TEST__BASE_MILLIS = 10; |
| 8 | +Backoff.__TEST__MAX_MILLIS = 50; |
| 9 | + |
| 10 | +function mockSplitHttpClient() { |
| 11 | + return jest.fn(() => Promise.resolve({ |
| 12 | + ok: true, |
| 13 | + status: 200, |
| 14 | + json: () => Promise.resolve(makeJwtCredential()), |
| 15 | + text: () => Promise.resolve('') |
| 16 | + })); |
| 17 | +} |
| 18 | + |
| 19 | +function networkError(statusCode?: number) { |
| 20 | + const err: any = new Error('fetch failed'); |
| 21 | + err.statusCode = statusCode; |
| 22 | + return err; |
| 23 | +} |
| 24 | + |
| 25 | +const mockSettings = { |
| 26 | + urls: { auth: 'https://auth.split.io/api' }, |
| 27 | + log: loggerMock, |
| 28 | +} as any; |
| 29 | + |
| 30 | +const mockTelemetryTracker = { trackHttp: jest.fn(() => jest.fn()) } as any; |
| 31 | + |
| 32 | +describe('authProviderFactory', () => { |
| 33 | + |
| 34 | + test('credential() fetches and caches token', async () => { |
| 35 | + const splitHttpClient = mockSplitHttpClient(); |
| 36 | + const provider = authProviderFactory(mockSettings, splitHttpClient, mockTelemetryTracker); |
| 37 | + |
| 38 | + const cred = await provider.credential(); |
| 39 | + expect(cred.token).toContain('.'); |
| 40 | + expect(splitHttpClient).toHaveBeenCalledTimes(1); |
| 41 | + |
| 42 | + // Second call returns cached |
| 43 | + const cred2 = await provider.credential(); |
| 44 | + expect(cred2).toBe(cred); |
| 45 | + expect(splitHttpClient).toHaveBeenCalledTimes(1); |
| 46 | + }); |
| 47 | + |
| 48 | + test('credential() deduplicates concurrent calls', async () => { |
| 49 | + const splitHttpClient = mockSplitHttpClient(); |
| 50 | + const provider = authProviderFactory(mockSettings, splitHttpClient, mockTelemetryTracker); |
| 51 | + |
| 52 | + const [cred1, cred2] = await Promise.all([provider.credential(), provider.credential()]); |
| 53 | + expect(cred1).toBe(cred2); |
| 54 | + expect(splitHttpClient).toHaveBeenCalledTimes(1); |
| 55 | + }); |
| 56 | + |
| 57 | + test('invalidate() clears cache, next call fetches fresh', async () => { |
| 58 | + const splitHttpClient = mockSplitHttpClient(); |
| 59 | + const provider = authProviderFactory(mockSettings, splitHttpClient, mockTelemetryTracker); |
| 60 | + |
| 61 | + await provider.credential(); |
| 62 | + provider.invalidate(); |
| 63 | + |
| 64 | + await provider.credential(); |
| 65 | + expect(splitHttpClient).toHaveBeenCalledTimes(2); |
| 66 | + }); |
| 67 | + |
| 68 | + test('credential() refetches when token is expired', async () => { |
| 69 | + const splitHttpClient = mockSplitHttpClient(); |
| 70 | + const provider = authProviderFactory(mockSettings, splitHttpClient, mockTelemetryTracker); |
| 71 | + |
| 72 | + await provider.credential(); |
| 73 | + expect(splitHttpClient).toHaveBeenCalledTimes(1); |
| 74 | + |
| 75 | + provider.invalidate(); |
| 76 | + await provider.credential(); |
| 77 | + expect(splitHttpClient).toHaveBeenCalledTimes(2); |
| 78 | + }); |
| 79 | + |
| 80 | + test('4xx errors reject immediately without retry', async () => { |
| 81 | + const splitHttpClient = jest.fn(() => Promise.reject(networkError(401))); |
| 82 | + const provider = authProviderFactory(mockSettings, splitHttpClient as any, mockTelemetryTracker); |
| 83 | + |
| 84 | + await expect(provider.credential()).rejects.toThrow('fetch failed'); |
| 85 | + expect(splitHttpClient).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + test('retries on non-4xx errors with backoff', async () => { |
| 89 | + let callCount = 0; |
| 90 | + const splitHttpClient = jest.fn(() => { |
| 91 | + callCount++; |
| 92 | + if (callCount < 3) return Promise.reject(networkError()); |
| 93 | + return Promise.resolve({ |
| 94 | + ok: true, status: 200, |
| 95 | + json: () => Promise.resolve(makeJwtCredential()), |
| 96 | + text: () => Promise.resolve('') |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + const provider = authProviderFactory(mockSettings, splitHttpClient as any, mockTelemetryTracker); |
| 101 | + const cred = await provider.credential(); |
| 102 | + |
| 103 | + expect(cred.token).toContain('.'); |
| 104 | + expect(splitHttpClient).toHaveBeenCalledTimes(3); |
| 105 | + }); |
| 106 | + |
| 107 | + test('stop() does not throw in any state', async () => { |
| 108 | + const splitHttpClient = mockSplitHttpClient(); |
| 109 | + const provider = authProviderFactory(mockSettings, splitHttpClient, mockTelemetryTracker); |
| 110 | + |
| 111 | + // Before any credential() call |
| 112 | + expect(() => provider.stop()).not.toThrow(); |
| 113 | + |
| 114 | + // After credential is cached |
| 115 | + await provider.credential(); |
| 116 | + expect(() => provider.stop()).not.toThrow(); |
| 117 | + |
| 118 | + // After invalidate |
| 119 | + provider.invalidate(); |
| 120 | + expect(() => provider.stop()).not.toThrow(); |
| 121 | + |
| 122 | + // While fetch is in-flight |
| 123 | + const splitHttpClient2 = jest.fn(() => new Promise(() => {})); // never resolves |
| 124 | + const provider2 = authProviderFactory(mockSettings, splitHttpClient2 as any, mockTelemetryTracker); |
| 125 | + provider2.credential(); |
| 126 | + expect(() => provider2.stop()).not.toThrow(); |
| 127 | + }); |
| 128 | + |
| 129 | + test('stop() prevents in-flight request from rejecting or rescheduling', async () => { |
| 130 | + let rejectFetch: (err: any) => void; |
| 131 | + const splitHttpClient = jest.fn(() => new Promise((_, reject) => { rejectFetch = reject; })); |
| 132 | + const provider = authProviderFactory(mockSettings, splitHttpClient as any, mockTelemetryTracker); |
| 133 | + |
| 134 | + const promise = provider.credential(); |
| 135 | + provider.stop(); |
| 136 | + |
| 137 | + // Simulate the in-flight fetch failing after stop |
| 138 | + rejectFetch!(networkError()); |
| 139 | + |
| 140 | + // Should resolve (not reject) with last cached credential (undefined in this case), and no retry scheduled |
| 141 | + const result = await promise; |
| 142 | + expect(result).toEqual(undefined); |
| 143 | + expect(splitHttpClient).toHaveBeenCalledTimes(1); |
| 144 | + }); |
| 145 | + |
| 146 | + test('stop() cancels pending retries', async () => { |
| 147 | + const splitHttpClient = jest.fn(() => Promise.reject(networkError())); |
| 148 | + const provider = authProviderFactory(mockSettings, splitHttpClient as any, mockTelemetryTracker); |
| 149 | + |
| 150 | + const promise = provider.credential(); |
| 151 | + // Let first fetch fail and backoff schedule |
| 152 | + await new Promise(r => setTimeout(r, 5)); |
| 153 | + |
| 154 | + provider.stop(); |
| 155 | + |
| 156 | + // Promise should never resolve/reject after stop (pending timeout cleared) |
| 157 | + const result = await Promise.race([ |
| 158 | + promise.then(() => 'resolved').catch(() => 'rejected'), |
| 159 | + new Promise(r => setTimeout(() => r('timeout'), 100)) |
| 160 | + ]); |
| 161 | + expect(result).toBe('timeout'); |
| 162 | + }); |
| 163 | +}); |
0 commit comments