import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock' ; import { getAlertRoute } from './get' ; 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 { Alert } from '../../../common' ; 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 ( 'getAlertRoute' , ( ) => { const mockedAlert = { id : '1' , alertTypeId : '1' , schedule : { interval : '10s' } , params : { bar : true , } , createdAt : new Date ( ) , updatedAt : new Date ( ) , actions : [ { group : 'default' , id : '2' , actionTypeId : 'test' , params : { foo : true , } , } , ] , consumer : 'bar' , name : 'abc' , tags : [ 'foo' ] , enabled : true , muteAll : false , notifyWhen : 'onActionGroupChange' , createdBy : '' , updatedBy : '' , apiKey : '' , apiKeyOwner : '' , throttle : '30s' , mutedInstanceIds : [ ] , executionStatus : { status : 'unknown' , lastExecutionDate : new Date ( '2020-08-20T19:23:38Z' ) , } , } ; it ( 'gets an alert with proper parameters' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; getAlertRoute ( router , licenseState ) ; const [ config , handler ] = router . get . mock . calls [ 0 ] ; expect ( config . path ) . toMatchInlineSnapshot ( `"/api/alerts/alert/{id}"` ) ; rulesClient . get . mockResolvedValueOnce ( mockedAlert ) ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { id : '1' } , } , [ 'ok' ] ) ; await handler ( context , req , res ) ; expect ( rulesClient . get ) . toHaveBeenCalledTimes ( 1 ) ; expect ( rulesClient . get . mock . calls [ 0 ] [ 0 ] . id ) . toEqual ( '1' ) ; expect ( res . ok ) . toHaveBeenCalledWith ( { body : mockedAlert , } ) ; } ) ; it ( 'ensures the license allows getting alerts' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; getAlertRoute ( router , licenseState ) ; const [ , handler ] = router . get . mock . calls [ 0 ] ; rulesClient . get . mockResolvedValueOnce ( mockedAlert ) ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { id : '1' } , } , [ 'ok' ] ) ; await handler ( context , req , res ) ; expect ( verifyApiAccess ) . toHaveBeenCalledWith ( licenseState ) ; } ) ; it ( 'ensures the license check prevents getting alerts' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; ( verifyApiAccess as jest . Mock ) . mockImplementation ( ( ) => { throw new Error ( 'OMG' ) ; } ) ; getAlertRoute ( router , licenseState ) ; const [ , handler ] = router . get . mock . calls [ 0 ] ; rulesClient . get . mockResolvedValueOnce ( mockedAlert ) ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { id : '1' } , } , [ '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' ) ; getAlertRoute ( router , licenseState , mockUsageCounter ) ; const [ , handler ] = router . get . mock . calls [ 0 ] ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { id : '1' } } , [ 'ok' , ] ) ; await handler ( context , req , res ) ; expect ( trackLegacyRouteUsage ) . toHaveBeenCalledWith ( 'get' , mockUsageCounter ) ; } ) ; } ) ;