import type { ObjectType } from '@kbn/config-schema' ; import type { IRouter , RequestHandler , RequestHandlerContext , RouteConfig } from 'src/core/server' ; import { kibanaResponseFactory } from 'src/core/server' ; import { httpServerMock } from 'src/core/server/mocks' ; import { routeDefinitionParamsMock } from './index.mock' ; import { defineVerifyRoute } from './verify' ; describe ( 'Configure routes' , ( ) => { let router ; let mockRouteParams ; let mockContext ; beforeEach ( ( ) => { mockRouteParams = routeDefinitionParamsMock . create ( ) ; router = mockRouteParams . router ; mockContext = { } as unknown as RequestHandlerContext ; defineVerifyRoute ( mockRouteParams ) ; } ) ; describe ( '#verify' , ( ) => { let routeHandler ; let routeConfig ; beforeEach ( ( ) => { const [ verifyRouteConfig , verifyRouteHandler ] = router . post . mock . calls . find ( ( [ { path } ] ) => path === '/internal/interactive_setup/verify' ) ! ; routeConfig = verifyRouteConfig ; routeHandler = verifyRouteHandler ; } ) ; it ( 'correctly defines route.' , ( ) => { expect ( routeConfig . options ) . toEqual ( { authRequired : false } ) ; const bodySchema = ( routeConfig . validate as any ) . body as ObjectType ; expect ( ( ) => bodySchema . validate ( { } ) ) . toThrowErrorMatchingInlineSnapshot ( `"[code]: expected value of type [string] but got [undefined]"` ) ; expect ( bodySchema . validate ( { code : '123456' } ) ) . toMatchInlineSnapshot ( `
        Object {
          "code": "123456",
        }
      ` ) ; } ) ; it ( 'fails if verification code is invalid.' , async ( ) => { mockRouteParams . verificationCode . verify . mockReturnValue ( false ) ; const mockRequest = httpServerMock . createKibanaRequest ( { body : { code : '123456' } , } ) ; await expect ( routeHandler ( mockContext , mockRequest , kibanaResponseFactory ) ) . resolves . toEqual ( expect . objectContaining ( { status : 403 , } ) ) ; } ) ; it ( 'succeeds if verification code is valid.' , async ( ) => { mockRouteParams . verificationCode . verify . mockReturnValue ( true ) ; const mockRequest = httpServerMock . createKibanaRequest ( { body : { code : '123456' } , } ) ; await expect ( routeHandler ( mockContext , mockRequest , kibanaResponseFactory ) ) . resolves . toEqual ( expect . objectContaining ( { status : 204 , } ) ) ; } ) ; } ) ; } ) ;