|
| 1 | +import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'; |
| 2 | +import { AuthenticationManager, AuthenticationOptions } from '../../src/authenticationManager'; |
| 3 | +import * as openidClient from 'openid-client'; |
| 4 | +import * as jwtDecodeModule from 'jwt-decode'; |
| 5 | +import * as grpc from '@grpc/grpc-js'; |
| 6 | + |
| 7 | +vi.mock('openid-client'); |
| 8 | +vi.mock('jwt-decode'); |
| 9 | + |
| 10 | +const mockDiscovery = { issuer: 'https://issuer.example.com' } as any; |
| 11 | +const mockToken = { |
| 12 | + access_token: 'mock_access_token', |
| 13 | + token_type: 'Bearer', |
| 14 | + refresh_token: 'mock_refresh_token', |
| 15 | +} as any; |
| 16 | +const mockDecoded = { exp: Math.floor(Date.now() / 1000) + 3600 }; |
| 17 | + |
| 18 | +const options: AuthenticationOptions = { |
| 19 | + clientId: 'client-id', |
| 20 | + clientSecret: 'client-secret', |
| 21 | + issuerUrl: 'https://issuer.example.com', |
| 22 | + scope: 'manage:classify', |
| 23 | +}; |
| 24 | + |
| 25 | +describe('AuthenticationManager', () => { |
| 26 | + let manager: AuthenticationManager; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + vi.resetAllMocks(); |
| 30 | + (openidClient.discovery as any).mockResolvedValue(mockDiscovery); |
| 31 | + (openidClient.clientCredentialsGrant as any).mockResolvedValue(mockToken); |
| 32 | + (openidClient.refreshTokenGrant as any).mockResolvedValue(mockToken); |
| 33 | + (jwtDecodeModule.jwtDecode as any).mockReturnValue(mockDecoded); |
| 34 | + manager = new AuthenticationManager(options); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should discover OIDC configuration and acquire token on first use', async () => { |
| 38 | + await manager.getAuthenticationHeader(); |
| 39 | + expect(openidClient.discovery).toHaveBeenCalledWith( |
| 40 | + new URL(options.issuerUrl), |
| 41 | + options.clientId, |
| 42 | + options.clientSecret, |
| 43 | + ); |
| 44 | + expect(openidClient.clientCredentialsGrant).toHaveBeenCalledWith( |
| 45 | + mockDiscovery, |
| 46 | + { audience: 'crisp-athena-dev', scope: options.scope } |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return a Bearer token in the authentication header', async () => { |
| 51 | + const header = await manager.getAuthenticationHeader(); |
| 52 | + expect(header).toBe('Bearer mock_access_token'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should append Authorization to grpc.Metadata', async () => { |
| 56 | + const metadata = new grpc.Metadata(); |
| 57 | + await manager.appendAuthorizationToMetadata(metadata); |
| 58 | + expect(metadata.get('Authorization')).toEqual(['Bearer mock_access_token']); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should refresh token if expired and refresh_token is present', async () => { |
| 62 | + // Simulate token already acquired and expired |
| 63 | + await manager.getAuthenticationHeader(); |
| 64 | + (manager as any).tokenExpiration = new Date(Date.now() - 1000); // expired |
| 65 | + (manager as any).token.refresh_token = 'mock_refresh_token'; |
| 66 | + (openidClient.refreshTokenGrant as any).mockResolvedValue({ |
| 67 | + ...mockToken, |
| 68 | + access_token: 'new_access_token', |
| 69 | + }); |
| 70 | + (jwtDecodeModule.jwtDecode as any).mockReturnValue({ exp: Math.floor(Date.now() / 1000) + 3600 }); |
| 71 | + |
| 72 | + const header = await manager.getAuthenticationHeader(); |
| 73 | + expect(openidClient.refreshTokenGrant).toHaveBeenCalled(); |
| 74 | + expect(header).toBe('Bearer new_access_token'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should reacquire token if expired and no refresh_token', async () => { |
| 78 | + await manager.getAuthenticationHeader(); |
| 79 | + (manager as any).tokenExpiration = new Date(Date.now() - 1000); // expired |
| 80 | + (manager as any).token.refresh_token = undefined; |
| 81 | + (openidClient.clientCredentialsGrant as any).mockResolvedValue({ |
| 82 | + ...mockToken, |
| 83 | + access_token: 'reacquired_access_token', |
| 84 | + }); |
| 85 | + (jwtDecodeModule.jwtDecode as any).mockReturnValue({ exp: Math.floor(Date.now() / 1000) + 3600 }); |
| 86 | + |
| 87 | + const header = await manager.getAuthenticationHeader(); |
| 88 | + expect(openidClient.clientCredentialsGrant).toHaveBeenCalled(); |
| 89 | + expect(header).toBe('Bearer reacquired_access_token'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should reacquire token if refresh fails', async () => { |
| 93 | + await manager.getAuthenticationHeader(); |
| 94 | + (manager as any).tokenExpiration = new Date(Date.now() - 1000); // expired |
| 95 | + (manager as any).token.refresh_token = 'mock_refresh_token'; |
| 96 | + (openidClient.refreshTokenGrant as any).mockRejectedValue(new Error('refresh failed')); |
| 97 | + (openidClient.clientCredentialsGrant as any).mockResolvedValue({ |
| 98 | + ...mockToken, |
| 99 | + access_token: 'fallback_access_token', |
| 100 | + }); |
| 101 | + (jwtDecodeModule.jwtDecode as any).mockReturnValue({ exp: Math.floor(Date.now() / 1000) + 3600 }); |
| 102 | + |
| 103 | + const header = await manager.getAuthenticationHeader(); |
| 104 | + expect(openidClient.refreshTokenGrant).toHaveBeenCalled(); |
| 105 | + expect(openidClient.clientCredentialsGrant).toHaveBeenCalled(); |
| 106 | + expect(header).toBe('Bearer fallback_access_token'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should only discover once', async () => { |
| 110 | + await manager.getAuthenticationHeader(); |
| 111 | + await manager.getAuthenticationHeader(); |
| 112 | + expect(openidClient.discovery).toHaveBeenCalledTimes(1); |
| 113 | + }); |
| 114 | +}); |
0 commit comments