import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock' ; import { findAlertRoute } from './find' ; import { httpServiceMock } from 'src/core/server/mocks' ; import { licenseStateMock } from '../../lib/license_state.mock' ; import { verifyApiAccess } from '../../lib/license_api_access' ; import { mockHandlerArguments } from './../_mock_handler_arguments' ; import { rulesClientMock } from '../../rules_client.mock' ; import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage' ; import { trackLegacyTerminology } from '../lib/track_legacy_terminology' ; const rulesClient = rulesClientMock . create ( ) ; jest . mock ( '../../lib/license_api_access.ts' , ( ) => ( { verifyApiAccess : jest . fn ( ) , } ) ) ; jest . mock ( '../../lib/track_legacy_route_usage' , ( ) => ( { trackLegacyRouteUsage : jest . fn ( ) , } ) ) ; jest . mock ( '../lib/track_legacy_terminology' , ( ) => ( { trackLegacyTerminology : jest . fn ( ) , } ) ) ; beforeEach ( ( ) => { jest . resetAllMocks ( ) ; } ) ; describe ( 'findAlertRoute' , ( ) => { it ( 'finds alerts with proper parameters' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; findAlertRoute ( router , licenseState ) ; const [ config , handler ] = router . get . mock . calls [ 0 ] ; expect ( config . path ) . toMatchInlineSnapshot ( `"/api/alerts/_find"` ) ; const findResult = { page : 1 , perPage : 1 , total : 0 , data : [ ] , } ; rulesClient . find . mockResolvedValueOnce ( findResult ) ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { query : { per_page : 1 , page : 1 , default_search_operator : 'OR' , } , } , [ 'ok' ] ) ; expect ( await handler ( context , req , res ) ) . toMatchInlineSnapshot ( `
      Object {
        "body": Object {
          "data": Array [],
          "page": 1,
          "perPage": 1,
          "total": 0,
        },
      }
    ` ) ; expect ( rulesClient . find ) . toHaveBeenCalledTimes ( 1 ) ; expect ( rulesClient . find . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
      Array [
        Object {
          "options": Object {
            "defaultSearchOperator": "OR",
            "page": 1,
            "perPage": 1,
          },
        },
      ]
    ` ) ; expect ( res . ok ) . toHaveBeenCalledWith ( { body : findResult , } ) ; } ) ; it ( 'ensures the license allows finding alerts' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; findAlertRoute ( router , licenseState ) ; const [ , handler ] = router . get . mock . calls [ 0 ] ; rulesClient . find . mockResolvedValueOnce ( { page : 1 , perPage : 1 , total : 0 , data : [ ] , } ) ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { query : { per_page : 1 , page : 1 , default_search_operator : 'OR' , } , } ) ; await handler ( context , req , res ) ; expect ( verifyApiAccess ) . toHaveBeenCalledWith ( licenseState ) ; } ) ; it ( 'ensures the license check prevents finding alerts' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; ( verifyApiAccess as jest . Mock ) . mockImplementation ( ( ) => { throw new Error ( 'OMG' ) ; } ) ; findAlertRoute ( router , licenseState ) ; const [ , handler ] = router . get . mock . calls [ 0 ] ; const [ context , req , res ] = mockHandlerArguments ( { } , { query : { per_page : 1 , page : 1 , default_search_operator : 'OR' , } , } , [ 'ok' ] ) ; expect ( handler ( context , req , res ) ) . rejects . toMatchInlineSnapshot ( `[Error: OMG]` ) ; expect ( verifyApiAccess ) . toHaveBeenCalledWith ( licenseState ) ; } ) ; it ( 'should track every call' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; const mockUsageCountersSetup = usageCountersServiceMock . createSetupContract ( ) ; const mockUsageCounter = mockUsageCountersSetup . createUsageCounter ( 'test' ) ; findAlertRoute ( router , licenseState , mockUsageCounter ) ; const [ , handler ] = router . get . mock . calls [ 0 ] ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { } , query : { } } , [ 'ok' , ] ) ; await handler ( context , req , res ) ; expect ( trackLegacyRouteUsage ) . toHaveBeenCalledWith ( 'find' , mockUsageCounter ) ; } ) ; it ( 'should track calls with deprecated param values' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; const mockUsageCountersSetup = usageCountersServiceMock . createSetupContract ( ) ; const mockUsageCounter = mockUsageCountersSetup . createUsageCounter ( 'test' ) ; findAlertRoute ( router , licenseState , mockUsageCounter ) ; const [ , handler ] = router . get . mock . calls [ 0 ] ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { } , query : { search_fields : [ 'alertTypeId:1' , 'message:foo' ] , search : 'alertTypeId:2' , sort_field : 'alertTypeId' , } , } , [ 'ok' ] ) ; await handler ( context , req , res ) ; expect ( trackLegacyTerminology ) . toHaveBeenCalledTimes ( 1 ) ; expect ( ( trackLegacyTerminology as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ) . toStrictEqual ( [ 'alertTypeId:2' , [ 'alertTypeId:1' , 'message:foo' ] , 'alertTypeId' , ] ) ; } ) ; it ( 'should track calls to deprecated functionality' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; const mockUsageCountersSetup = usageCountersServiceMock . createSetupContract ( ) ; const mockUsageCounter = mockUsageCountersSetup . createUsageCounter ( 'test' ) ; findAlertRoute ( router , licenseState , mockUsageCounter ) ; const [ , handler ] = router . get . mock . calls [ 0 ] ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { } , query : { fields : [ 'foo' , 'bar' ] , } , } , [ 'ok' ] ) ; await handler ( context , req , res ) ; expect ( mockUsageCounter . incrementCounter ) . toHaveBeenCalledWith ( { counterName : `legacyAlertingFieldsUsage` , counterType : 'alertingFieldsUsage' , incrementBy : 1 , } ) ; } ) ; } ) ;