|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import { |
| 6 | + clearSailPointTokenStateForTests, |
| 7 | + getSailPointAccessToken, |
| 8 | + getSailPointTokenStateForTests, |
| 9 | + resolveSailPointHosts, |
| 10 | + sailpointFetch, |
| 11 | +} from '@/lib/internal/sailpoint/client' |
| 12 | + |
| 13 | +const mockFetch = vi.fn<typeof fetch>() |
| 14 | + |
| 15 | +function tokenResponse(token: string, expiresIn = 3600): Response { |
| 16 | + return Response.json({ access_token: token, expires_in: expiresIn }) |
| 17 | +} |
| 18 | + |
| 19 | +describe('SailPoint client', () => { |
| 20 | + beforeEach(() => { |
| 21 | + clearSailPointTokenStateForTests() |
| 22 | + mockFetch.mockReset() |
| 23 | + vi.stubGlobal('fetch', mockFetch) |
| 24 | + }) |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + vi.useRealTimers() |
| 28 | + vi.unstubAllGlobals() |
| 29 | + }) |
| 30 | + |
| 31 | + it('accepts only commercial and government tenant hosts', () => { |
| 32 | + expect(resolveSailPointHosts('acme').host).toBe('acme.api.identitynow.com') |
| 33 | + expect(resolveSailPointHosts('https://agency.api.identitynowgov.com').host).toBe( |
| 34 | + 'agency.api.identitynowgov.com' |
| 35 | + ) |
| 36 | + expect(() => resolveSailPointHosts('acme.api.identitynow.com.evil.test')).toThrow( |
| 37 | + 'not an allowed' |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + it('isolates cache entries by the exact credential secret', async () => { |
| 42 | + mockFetch |
| 43 | + .mockResolvedValueOnce(tokenResponse('first')) |
| 44 | + .mockResolvedValueOnce(tokenResponse('second')) |
| 45 | + |
| 46 | + const common = { tenant: 'acme', clientId: 'client' } |
| 47 | + expect(await getSailPointAccessToken({ ...common, clientSecret: 'one' })).toBe('first') |
| 48 | + expect(await getSailPointAccessToken({ ...common, clientSecret: 'two' })).toBe('second') |
| 49 | + expect(mockFetch).toHaveBeenCalledTimes(2) |
| 50 | + }) |
| 51 | + |
| 52 | + it('single-flights concurrent exchanges for the same credentials', async () => { |
| 53 | + let release: ((response: Response) => void) | undefined |
| 54 | + mockFetch.mockImplementationOnce( |
| 55 | + () => |
| 56 | + new Promise<Response>((resolve) => { |
| 57 | + release = resolve |
| 58 | + }) |
| 59 | + ) |
| 60 | + const credentials = { tenant: 'acme', clientId: 'client', clientSecret: 'secret' } |
| 61 | + const first = getSailPointAccessToken(credentials) |
| 62 | + const second = getSailPointAccessToken(credentials) |
| 63 | + expect(mockFetch).toHaveBeenCalledTimes(1) |
| 64 | + release?.(tokenResponse('shared')) |
| 65 | + await expect(Promise.all([first, second])).resolves.toEqual(['shared', 'shared']) |
| 66 | + }) |
| 67 | + |
| 68 | + it('expires cached tokens before their provider expiry', async () => { |
| 69 | + vi.useFakeTimers() |
| 70 | + vi.setSystemTime(new Date('2026-01-01T00:00:00.000Z')) |
| 71 | + mockFetch |
| 72 | + .mockResolvedValueOnce(tokenResponse('old', 100)) |
| 73 | + .mockResolvedValueOnce(tokenResponse('new', 100)) |
| 74 | + const credentials = { tenant: 'acme', clientId: 'client', clientSecret: 'secret' } |
| 75 | + |
| 76 | + expect(await getSailPointAccessToken(credentials)).toBe('old') |
| 77 | + vi.setSystemTime(new Date('2026-01-01T00:01:31.000Z')) |
| 78 | + expect(await getSailPointAccessToken(credentials)).toBe('new') |
| 79 | + }) |
| 80 | + |
| 81 | + it('evicts the oldest token when the bounded cache is full', async () => { |
| 82 | + mockFetch.mockImplementation(async () => tokenResponse('token')) |
| 83 | + for (let index = 0; index < 101; index += 1) { |
| 84 | + await getSailPointAccessToken({ |
| 85 | + tenant: 'acme', |
| 86 | + clientId: `client-${index}`, |
| 87 | + clientSecret: 'secret', |
| 88 | + }) |
| 89 | + } |
| 90 | + expect(getSailPointTokenStateForTests()).toEqual({ cacheSize: 100, exchangeSize: 0 }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('rejects provider responses larger than the shared JSON cap', async () => { |
| 94 | + mockFetch.mockResolvedValueOnce(tokenResponse('token')).mockResolvedValueOnce( |
| 95 | + new Response('{}', { |
| 96 | + status: 200, |
| 97 | + headers: { 'content-length': String(10 * 1024 * 1024 + 1) }, |
| 98 | + }) |
| 99 | + ) |
| 100 | + const credentials = { tenant: 'acme', clientId: 'client', clientSecret: 'secret' } |
| 101 | + |
| 102 | + await expect( |
| 103 | + sailpointFetch(credentials, (hosts) => ({ |
| 104 | + url: `${hosts.apiBaseUrl}/identities/v1`, |
| 105 | + init: { method: 'GET' }, |
| 106 | + })) |
| 107 | + ).rejects.toThrow(/maximum|limit|exceeds/i) |
| 108 | + }) |
| 109 | +}) |
0 commit comments