import { updateRuleApiKeyRoute } from './update_rule_api_key' ; 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' ; const rulesClient = rulesClientMock . create ( ) ; jest . mock ( '../lib/license_api_access.ts' , ( ) => ( { verifyApiAccess : jest . fn ( ) , } ) ) ; beforeEach ( ( ) => { jest . resetAllMocks ( ) ; } ) ; describe ( 'updateRuleApiKeyRoute' , ( ) => { it ( 'updates api key for a rule' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; updateRuleApiKeyRoute ( router , licenseState ) ; const [ config , handler ] = router . post . mock . calls [ 0 ] ; expect ( config . path ) . toMatchInlineSnapshot ( `"/internal/alerting/rule/{id}/_update_api_key"` ) ; rulesClient . updateApiKey . mockResolvedValueOnce ( ) ; const [ context , req , res ] = mockHandlerArguments ( { rulesClient } , { params : { id : '1' , } , } , [ 'noContent' ] ) ; expect ( await handler ( context , req , res ) ) . toEqual ( undefined ) ; expect ( rulesClient . updateApiKey ) . toHaveBeenCalledTimes ( 1 ) ; expect ( rulesClient . updateApiKey . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
      Array [
        Object {
          "id": "1",
        },
      ]
    ` ) ; expect ( res . noContent ) . toHaveBeenCalled ( ) ; } ) ; it ( 'ensures the rule type gets validated for the license' , async ( ) => { const licenseState = licenseStateMock . create ( ) ; const router = httpServiceMock . createRouter ( ) ; updateRuleApiKeyRoute ( router , licenseState ) ; const [ , handler ] = router . post . mock . calls [ 0 ] ; rulesClient . updateApiKey . 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' } } ) ; } ) ; } ) ;