|
| 1 | +import { mock } from 'jest-mock-extended'; |
| 2 | +import type express from 'express'; |
| 3 | +import { SamlService } from '@/sso/saml/saml.service.ee'; |
| 4 | +import { mockInstance } from '../../../shared/mocking'; |
| 5 | +import { UrlService } from '@/services/url.service'; |
| 6 | +import { Logger } from '@/Logger'; |
| 7 | +import type { IdentityProviderInstance, ServiceProviderInstance } from 'samlify'; |
| 8 | +import * as samlHelpers from '@/sso/saml/samlHelpers'; |
| 9 | + |
| 10 | +describe('SamlService', () => { |
| 11 | + const logger = mockInstance(Logger); |
| 12 | + const urlService = mockInstance(UrlService); |
| 13 | + const samlService = new SamlService(logger, urlService); |
| 14 | + |
| 15 | + describe('getAttributesFromLoginResponse', () => { |
| 16 | + test('throws when any attribute is missing', async () => { |
| 17 | + // |
| 18 | + // ARRANGE |
| 19 | + // |
| 20 | + jest |
| 21 | + .spyOn(samlService, 'getIdentityProviderInstance') |
| 22 | + .mockReturnValue(mock<IdentityProviderInstance>()); |
| 23 | + |
| 24 | + const serviceProviderInstance = mock<ServiceProviderInstance>(); |
| 25 | + serviceProviderInstance.parseLoginResponse.mockResolvedValue({ |
| 26 | + samlContent: '', |
| 27 | + extract: {}, |
| 28 | + }); |
| 29 | + jest |
| 30 | + .spyOn(samlService, 'getServiceProviderInstance') |
| 31 | + .mockReturnValue(serviceProviderInstance); |
| 32 | + |
| 33 | + jest.spyOn(samlHelpers, 'getMappedSamlAttributesFromFlowResult').mockReturnValue({ |
| 34 | + attributes: {} as never, |
| 35 | + missingAttributes: [ |
| 36 | + 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress', |
| 37 | + 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/firstname', |
| 38 | + 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/lastname', |
| 39 | + 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn', |
| 40 | + ], |
| 41 | + }); |
| 42 | + |
| 43 | + // |
| 44 | + // ACT & ASSERT |
| 45 | + // |
| 46 | + await expect( |
| 47 | + samlService.getAttributesFromLoginResponse(mock<express.Request>(), 'post'), |
| 48 | + ).rejects.toThrowError( |
| 49 | + 'SAML Authentication failed. Invalid SAML response (missing attributes: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/firstname, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/lastname, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn).', |
| 50 | + ); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments