import * as fetchAny from 'jest-fetch-mock' ; import { createServer } from '@auth/index' ; import * as authService from '@auth/features/authenticate/service' ; const fetch = fetchAny as fetchAny . FetchMock ; describe ( 'authenticate handler receives a request' , ( ) => { let server ; beforeEach ( async ( ) => { server = await createServer ( ) ; } ) ; describe ( 'user management service says credentials are not valid' , ( ) => { it ( 'returns a 401 response to client' , async ( ) => { fetch . mockReject ( new Error ( ) ) ; const res = await server . server . inject ( { method : 'POST' , url : '/authenticateSystemClient' , payload : { client_id : '123' , client_secret : '456' } } ) ; expect ( res . statusCode ) . toBe ( 401 ) ; } ) ; } ) ; describe ( 'user management service says credentials are valid' , ( ) => { it ( 'returns a token to the client' , async ( ) => { jest . spyOn ( authService , 'createToken' ) . mockReturnValue ( Promise . resolve ( '789' ) ) ; fetch . mockResponse ( JSON . stringify ( { systemId : '1' , status : 'active' , scope : [ 'nationalId' ] } ) ) ; const res = await server . server . inject ( { method : 'POST' , url : '/authenticateSystemClient' , payload : { client_id : '123' , client_secret : '456' } } ) ; expect ( JSON . parse ( res . payload ) . token ) . toBe ( '789' ) ; } ) ; } ) ; } ) ;