import { Test , TestingModule } from '@nestjs/testing' ; import { LoggerService } from '@fc/logger' ; import { OidcProviderGrantSaveException } from '../exceptions' ; import { OidcProviderGrantService } from './oidc-provider-grant.service' ; describe ( 'OidcProviderGrantService' , ( ) => { let service ; const interactionIdMock = 'interactionIdMockValue' ; const clientIdMock = 'clientIdMockValue' ; const loggerServiceMock = { setContext : jest . fn ( ) , } as unknown as LoggerService ; const reqMock = Symbol ( 'req' ) ; const resMock = Symbol ( 'res' ) ; let grantMock ; beforeEach ( async ( ) => { jest . resetAllMocks ( ) ; jest . restoreAllMocks ( ) ; const module = await Test . createTestingModule ( { providers : [ LoggerService , OidcProviderGrantService ] , } ) . overrideProvider ( LoggerService ) . useValue ( loggerServiceMock ) . compile ( ) ; service = module . get < OidcProviderGrantService > ( OidcProviderGrantService ) ; } ) ; describe ( 'generateGrant' , ( ) => { let providerMock ; const interactionDetailsMock = { params : { client_id : clientIdMock , scope : 'nautilus u571' , } , } ; beforeEach ( async ( ) => { grantMock = { accountId : '' , clientId : '' , addOIDCScope : jest . fn ( ) , } ; providerMock = { Grant : jest . fn ( ) . mockImplementation ( ( ) => { return grantMock ; } ) , interactionDetails : jest . fn ( ) . mockReturnValueOnce ( interactionDetailsMock ) , } ; } ) ; it ( 'should be defined' , ( ) => { expect ( service ) . toBeDefined ( ) ; expect ( loggerServiceMock . setContext ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should call interactionDetail once with req and res' , async ( ) => { await service . generateGrant ( providerMock , reqMock , resMock , interactionIdMock ) ; expect ( providerMock . interactionDetails ) . toHaveBeenCalledTimes ( 1 ) ; expect ( providerMock . interactionDetails ) . toHaveBeenCalledWith ( reqMock , resMock ) ; } ) ; it ( 'should call Grant constructor once and set interactionId and clientId' , async ( ) => { await service . generateGrant ( providerMock , reqMock , resMock , interactionIdMock ) ; expect ( providerMock . Grant ) . toHaveBeenCalledTimes ( 1 ) ; expect ( providerMock . Grant ) . toHaveBeenCalledTimes ( 1 ) ; expect ( providerMock . Grant ) . toHaveBeenCalledWith ( ) ; expect ( grantMock . accountId ) . toEqual ( interactionIdMock ) ; expect ( grantMock . clientId ) . toEqual ( clientIdMock ) ; } ) ; it ( 'should call grant.addOidcScope for each scope' , async ( ) => { const scopesMock = [ 'nautilus' , 'u571' ] ; await service . generateGrant ( providerMock , reqMock , resMock , interactionIdMock ) ; expect ( grantMock . addOIDCScope ) . toHaveBeenNthCalledWith ( 1 , scopesMock [ 0 ] ) ; expect ( grantMock . addOIDCScope ) . toHaveBeenNthCalledWith ( 2 , scopesMock [ 1 ] ) ; } ) ; } ) ; describe ( 'saveGrant' , ( ) => { beforeEach ( ( ) => { grantMock = { save : jest . fn ( ) , } ; } ) ; it ( 'should save grant' , async ( ) => { const grantSaveMock = 'grantSaveMockValue' ; grantMock . save . mockResolvedValueOnce ( grantSaveMock ) ; const result = await service . saveGrant ( grantMock ) ; expect ( result ) . toEqual ( grantSaveMock ) ; expect ( grantMock . save ) . toHaveBeenCalledTimes ( 1 ) ; expect ( grantMock . save ) . toHaveBeenCalledWith ( ) ; } ) ; it ( 'should throw an error if grant failed to save' , async ( ) => { const errorMock = new Error ( 'Unknown Error' ) ; grantMock . save . mockRejectedValueOnce ( errorMock ) ; await expect ( service . saveGrant ( grantMock ) ) . rejects . toThrow ( OidcProviderGrantSaveException ) ; } ) ; } ) ; } ) ;