import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock' ; import { muteAlertInstanceRoute } from './mute_instance' ; import { httpServiceMock } from 'src/core/server/mocks' ; import { licenseStateMock } from '../../lib/license_state.mock' ; import { mockHandlerArguments } from './../_mock_handler_arguments' ; import { rulesClientMock } from '../../rules_client.mock' ; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled' ; import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage' ; const rulesClient = rulesClientMock . create ( ) ; jest . mock ( '../../lib/license_api_access.ts' , ( ) => ( { verifyApiAccess : jest . fn ( ) , } ) ) ; jest . mock ( '../../lib/track_legacy_route_usage' , ( ) => ( { trackLegacyRouteUsage : jest . fn ( ) , } ) ) ; beforeEach ( ( ) => { jest . resetAllMocks ( ) ; } ) ; describe ( 'muteAlertInstanceRoute' , ( ) => { it ( 'mutes an alert instance' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; muteAlertInstanceRoute ( router , licenseState ) ; const [ config , handler ] = router . post . mock . calls [ 0 ] ; expect ( config . path ) . toMatchInlineSnapshot ( `"/api/alerts/alert/{alert_id}/alert_instance/{alert_instance_id}/_mute"` ) ; rulesClient . muteInstance . mockResolvedValueOnce ( ) ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { alert_id : '1' , alert_instance_id : '2' , } , } , [ 'noContent' ] ) ; expect ( await handler ( context , req , res ) ) . toEqual ( undefined ) ; expect ( rulesClient . muteInstance ) . toHaveBeenCalledTimes ( 1 ) ; expect ( rulesClient . muteInstance . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
      Array [
        Object {
          "alertId": "1",
          "alertInstanceId": "2",
        },
      ]
    ` ) ; expect ( res . noContent ) . toHaveBeenCalled ( ) ; } ) ; it ( 'ensures the alert type gets validated for the license' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; muteAlertInstanceRoute ( router , licenseState ) ; const [ , handler ] = router . post . mock . calls [ 0 ] ; rulesClient . muteInstance . mockRejectedValue ( new AlertTypeDisabledError ( 'Fail' , 'license_invalid' ) ) ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { } , body : { } } , [ 'ok' , 'forbidden' , ] ) ; await handler ( context , req , res ) ; expect ( res . forbidden ) . toHaveBeenCalledWith ( { body : { message : 'Fail' } } ) ; } ) ; it ( 'should track every call' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; const mockUsageCountersSetup = usageCountersServiceMock . createSetupContract ( ) ; const mockUsageCounter = mockUsageCountersSetup . createUsageCounter ( 'test' ) ; muteAlertInstanceRoute ( router , licenseState , mockUsageCounter ) ; const [ , handler ] = router . post . mock . calls [ 0 ] ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { } , body : { } } , [ 'ok' , ] ) ; await handler ( context , req , res ) ; expect ( trackLegacyRouteUsage ) . toHaveBeenCalledWith ( 'muteInstance' , mockUsageCounter ) ; } ) ; } ) ;