|
| 1 | +import { PMAuthConfigProvider } from './auth-config-provider.js'; |
| 2 | +import { HttpException } from '@nestjs/common'; |
| 3 | +import { |
| 4 | + DiscoveryService, |
| 5 | + EnvAuthConfigService, |
| 6 | +} from '@openmfp/portal-server-lib'; |
| 7 | +import type { Request } from 'express'; |
| 8 | +import { mock } from 'jest-mock-extended'; |
| 9 | + |
| 10 | +describe('PMAuthConfigProvider', () => { |
| 11 | + let provider: PMAuthConfigProvider; |
| 12 | + let discoveryService: jest.Mocked<DiscoveryService>; |
| 13 | + let envAuthConfigService: jest.Mocked<EnvAuthConfigService>; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + discoveryService = mock<DiscoveryService>(); |
| 17 | + envAuthConfigService = mock<EnvAuthConfigService>(); |
| 18 | + provider = new PMAuthConfigProvider(discoveryService, envAuthConfigService); |
| 19 | + jest.resetModules(); |
| 20 | + process.env = { |
| 21 | + AUTH_SERVER_URL_DEFAULT: 'authUrl', |
| 22 | + TOKEN_URL_DEFAULT: 'tokenUrl', |
| 23 | + BASE_DOMAINS_DEFAULT: 'example.com', |
| 24 | + OIDC_CLIENT_ID_DEFAULT: 'client123', |
| 25 | + OIDC_CLIENT_SECRET_DEFAULT: 'secret123', |
| 26 | + }; |
| 27 | + }); |
| 28 | + |
| 29 | + it('should delegate to EnvAuthConfigService if available', async () => { |
| 30 | + const req = { hostname: 'foo.example.com' } as Request; |
| 31 | + const expected = { |
| 32 | + idpName: 'idp', |
| 33 | + baseDomain: 'example.com', |
| 34 | + oauthServerUrl: 'url', |
| 35 | + oauthTokenUrl: 'token', |
| 36 | + clientId: 'cid', |
| 37 | + clientSecret: 'sec', |
| 38 | + }; |
| 39 | + envAuthConfigService.getAuthConfig.mockResolvedValue(expected); |
| 40 | + |
| 41 | + const result = await provider.getAuthConfig(req); |
| 42 | + |
| 43 | + expect(result).toEqual(expected); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should fall back to default configuration if EnvAuthConfigService throws', async () => { |
| 47 | + const req = { hostname: 'foo.example.com' } as Request; |
| 48 | + envAuthConfigService.getAuthConfig.mockRejectedValue(new Error('fail')); |
| 49 | + discoveryService.getOIDC.mockResolvedValue({ |
| 50 | + authorization_endpoint: 'authUrl', |
| 51 | + token_endpoint: 'tokenUrl', |
| 52 | + }); |
| 53 | + |
| 54 | + const result = await provider.getAuthConfig(req); |
| 55 | + |
| 56 | + expect(result).toMatchObject({ |
| 57 | + baseDomain: 'example.com', |
| 58 | + oauthServerUrl: 'authUrl', |
| 59 | + oauthTokenUrl: 'tokenUrl', |
| 60 | + clientId: 'client123', |
| 61 | + clientSecret: 'secret123', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should throw if default configuration incomplete', async () => { |
| 66 | + const req = { hostname: 'foo.example.com' } as Request; |
| 67 | + envAuthConfigService.getAuthConfig.mockRejectedValue(new Error('fail')); |
| 68 | + discoveryService.getOIDC.mockResolvedValue(null); |
| 69 | + process.env = {}; |
| 70 | + |
| 71 | + await expect(provider.getAuthConfig(req)).rejects.toThrow(HttpException); |
| 72 | + }); |
| 73 | + |
| 74 | + it('getDomain should return organization and baseDomain', () => { |
| 75 | + const req = { hostname: 'foo.example.com' } as Request; |
| 76 | + const result = provider.getDomain(req); |
| 77 | + expect(result).toEqual({ |
| 78 | + organization: 'foo', |
| 79 | + baseDomain: 'example.com', |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('getDomain should return clientId if hostname equals baseDomain', () => { |
| 84 | + const req = { hostname: 'example.com' } as Request; |
| 85 | + const result = provider.getDomain(req); |
| 86 | + expect(result).toEqual({ |
| 87 | + organization: 'client123', |
| 88 | + baseDomain: 'example.com', |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments