import { mocked } from 'jest-mock' ; import * as KeyStore from 'oidc-provider/lib/helpers/keystore.js' ; import * as OidcProviderInstance from 'oidc-provider/lib/helpers/weak_cache' ; import { ModuleRef } from '@nestjs/core' ; import { Test , TestingModule } from '@nestjs/testing' ; import { ConfigService } from '@fc/config' ; import { LoggerService } from '@fc/logger' ; import { OidcProviderService } from '@fc/oidc-provider' ; import { OverrideOidcProviderService } from './override-oidc-provider.service' ; jest . mock ( 'oidc-provider/lib/helpers/keystore.js' ) ; describe ( 'OverrideOidcProviderService' , ( ) => { let service ; const configMock = { get : jest . fn ( ) , } ; const loggerMock = { setContext : jest . fn ( ) , debug : jest . fn ( ) , } as unknown as LoggerService ; const providerMock = { } ; const oidcProviderMock = { getProvider : jest . fn ( ) , } ; const moduleRefMock = { get : jest . fn ( ) , } ; const KeyStoreMock = mocked ( KeyStore ) ; const signHsmPubKeyMock = { x : 'foo' , y : 'bar' , } ; beforeEach ( async ( ) => { const module = await Test . createTestingModule ( { providers : [ ConfigService , LoggerService , OverrideOidcProviderService ] , } ) . overrideProvider ( ConfigService ) . useValue ( configMock ) . overrideProvider ( LoggerService ) . useValue ( loggerMock ) . overrideProvider ( ModuleRef ) . useValue ( moduleRefMock ) . compile ( ) ; service = module . get < OverrideOidcProviderService > ( OverrideOidcProviderService ) ; jest . resetAllMocks ( ) ; oidcProviderMock . getProvider . mockReturnValue ( providerMock ) ; configMock . get . mockReturnValue ( { sigHsmPubKey : signHsmPubKeyMock } ) ; } ) ; it ( 'should be defined' , ( ) => { expect ( service ) . toBeDefined ( ) ; } ) ; describe ( 'onApplicationBootstrap' , ( ) => { it ( 'should call internal initializers' , ( ) => { service [ 'overrideJwksResponse' ] = jest . fn ( ) ; service . onApplicationBootstrap ( ) ; expect ( service [ 'overrideJwksResponse' ] ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; } ) ; describe ( 'overrideJwksResponse' , ( ) => { beforeEach ( ( ) => { service [ 'getOidcProviderService' ] = jest . fn ( ) . mockReturnValueOnce ( oidcProviderMock ) ; } ) ; it ( 'should create a Key object from config key' , ( ) => { KeyStoreMock . mockImplementationOnce ( function ( value ) { return value ; } ) ; service [ 'overrideJwksResponse' ] ( ) ; expect ( KeyStoreMock ) . toHaveBeenCalledTimes ( 1 ) ; expect ( KeyStoreMock ) . toHaveBeenCalledWith ( [ { ... signHsmPubKeyMock , d : 'not-a-private' , } , ] ) ; } ) ; it ( 'should affect the keystore to provider instance' , ( ) => { const instanceSpy = OidcProviderInstance ( providerMock ) ; service [ 'overrideJwksResponse' ] ( ) ; expect ( instanceSpy . jwksResponse ) . toEqual ( { keys : [ signHsmPubKeyMock ] } ) ; } ) ; } ) ; describe ( 'getOidcProviderService' , ( ) => { it ( 'should call moduleRef.get with options' , ( ) => { service [ 'getOidcProviderService' ] ( ) ; expect ( moduleRefMock . get ) . toBeCalledTimes ( 1 ) ; expect ( moduleRefMock . get ) . toBeCalledWith ( OidcProviderService , { strict : false , } ) ; } ) ; it ( 'should return result from moduleRef.get' , ( ) => { const mockedReturn = Symbol ( 'someValue' ) ; moduleRefMock . get . mockReturnValueOnce ( mockedReturn ) ; const result = service [ 'getOidcProviderService' ] ( ) ; expect ( result ) . toBe ( mockedReturn ) ; } ) ; } ) ; } ) ;