import { mocked } from 'jest-mock' ; import { Test , TestingModule } from '@nestjs/testing' ; import { validateDto } from '@fc/common' ; import { EidasIdentityDto } from '@fc/core-fcp/dto' ; import { LoggerService } from '@fc/logger' ; import { CoreFcpEidasIdentityCheckHandler } from './core-fcp.eidas.identity-check' ; jest . mock ( '@fc/common' , ( ) => ( { ... ( jest . requireActual ( '@fc/common' ) as any ) , validateDto : jest . fn ( ) , } ) ) ; describe ( 'CoreFcpEidasIdentityCheckHandler' , ( ) => { let service ; const loggerServiceMock = { setContext : jest . fn ( ) , debug : jest . fn ( ) , } as unknown as LoggerService ; const identityMock = { sub : '1' , given_name : 'given_nameValue' , family_name : 'family_nameValue' , email : 'emailValue' , } ; let validationDtoMock ; beforeEach ( async ( ) => { jest . resetAllMocks ( ) ; jest . restoreAllMocks ( ) ; const module = await Test . createTestingModule ( { providers : [ LoggerService , CoreFcpEidasIdentityCheckHandler ] , } ) . overrideProvider ( LoggerService ) . useValue ( loggerServiceMock ) . compile ( ) ; service = module . get < CoreFcpEidasIdentityCheckHandler > ( CoreFcpEidasIdentityCheckHandler ) ; validationDtoMock = mocked ( validateDto ) ; } ) ; it ( 'should be defined' , ( ) => { expect ( service ) . toBeDefined ( ) ; expect ( loggerServiceMock . setContext ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should log when check identity' , async ( ) => { validationDtoMock . mockResolvedValueOnce ( [ ] ) ; await service . handle ( identityMock ) ; expect ( loggerServiceMock . debug ) . toHaveBeenCalledTimes ( 1 ) ; expect ( loggerServiceMock . debug ) . toHaveBeenCalledWith ( 'Identity Check: ##### core-fcp-eidas-identity-check' ) ; } ) ; it ( 'should successfully check identity' , async ( ) => { validationDtoMock . mockResolvedValueOnce ( [ ] ) ; const results = await service . handle ( identityMock ) ; expect ( results ) . toStrictEqual ( [ ] ) ; } ) ; it ( 'should successfully check identity with DTO' , async ( ) => { validationDtoMock . mockResolvedValueOnce ( [ ] ) ; await service . handle ( identityMock ) ; expect ( validationDtoMock ) . toHaveBeenCalledTimes ( 1 ) ; expect ( validationDtoMock ) . toHaveBeenCalledWith ( identityMock , EidasIdentityDto , { whitelist : true , forbidNonWhitelisted : true , forbidUnknownValues : true , } , { excludeExtraneousValues : true , } ) ; } ) ; it ( 'should failed check identity with DTO' , async ( ) => { const errorMock = new Error ( 'Unknown Error' ) ; validationDtoMock . mockResolvedValueOnce ( [ errorMock ] ) ; const results = await service . handle ( identityMock ) ; expect ( results ) . toStrictEqual ( [ errorMock ] ) ; } ) ; } ) ;