import expect from '@kbn/expect' ; import { FtrProviderContext } from '../../../ftr_provider_context' ; import { USER } from '../../../../functional/services/ml/security_common' ; import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api' ; import { DATA_FRAME_TASK_STATE } from '../../../../../plugins/ml/common/constants/data_frame_analytics' ; export default ( { getService } ) => { const esArchiver = getService ( 'esArchiver' ) ; const supertest = getService ( 'supertestWithoutAuth' ) ; const ml = getService ( 'ml' ) ; const jobId = `bm_${ Date . now ( ) }` ; const analyticsId = `${ jobId }_1` ; let destinationIndex ; describe ( 'POST data_frame/analytics/{analyticsId}/_stop' , ( ) => { before ( async ( ) => { await esArchiver . loadIfNeeded ( 'x-pack/test/functional/es_archives/ml/bm_classification' ) ; await ml . testResources . setKibanaTimeZoneToUTC ( ) ; const slowRunningConfig = ml . commonConfig . getDFABmClassificationJobConfig ( analyticsId ) ; destinationIndex = slowRunningConfig . dest . index ; await ml . api . createDataFrameAnalyticsJob ( slowRunningConfig ) ; await ml . api . runDFAJob ( analyticsId ) ; } ) ; after ( async ( ) => { await ml . api . deleteDataFrameAnalyticsJobES ( analyticsId ) ; await ml . api . cleanMlIndices ( ) ; await ml . api . deleteIndices ( destinationIndex ) ; } ) ; describe ( 'StopsDataFrameAnalyticsJob' , ( ) => { it ( 'should stop analytics job for specified id when job exists' , async ( ) => { const { body } = await supertest . post ( `/api/ml/data_frame/analytics/${ analyticsId }/_stop` ) . auth ( USER . ML_POWERUSER , ml . securityCommon . getPasswordForUser ( USER . ML_POWERUSER ) ) . set ( COMMON_REQUEST_HEADERS ) . expect ( 200 ) ; expect ( body ) . not . to . be ( undefined ) ; expect ( body . stopped ) . to . be ( true ) ; await ml . api . waitForAnalyticsState ( analyticsId , DATA_FRAME_TASK_STATE . STOPPED , 5000 ) ; } ) ; it ( 'should show 404 error if job does not exist' , async ( ) => { const id = `${ jobId }_invalid` ; const message = `No known job with id '${ id }'` ; const { body } = await supertest . post ( `/api/ml/data_frame/analytics/${ id }/_stop` ) . auth ( USER . ML_POWERUSER , ml . securityCommon . getPasswordForUser ( USER . ML_POWERUSER ) ) . set ( COMMON_REQUEST_HEADERS ) . expect ( 404 ) ; expect ( body . error ) . to . eql ( 'Not Found' ) ; expect ( body . message ) . to . eql ( message ) ; } ) ; it ( 'should not allow to stop analytics job for unauthorized user' , async ( ) => { const { body } = await supertest . post ( `/api/ml/data_frame/analytics/${ analyticsId }/_stop` ) . auth ( USER . ML_UNAUTHORIZED , ml . securityCommon . getPasswordForUser ( USER . ML_UNAUTHORIZED ) ) . set ( COMMON_REQUEST_HEADERS ) . expect ( 403 ) ; expect ( body . error ) . to . eql ( 'Forbidden' ) ; expect ( body . message ) . to . eql ( 'Forbidden' ) ; } ) ; it ( 'should not allow to stop analytics job for user with view only permission' , async ( ) => { const { body } = await supertest . post ( `/api/ml/data_frame/analytics/${ analyticsId }/_stop` ) . auth ( USER . ML_VIEWER , ml . securityCommon . getPasswordForUser ( USER . ML_VIEWER ) ) . set ( COMMON_REQUEST_HEADERS ) . expect ( 403 ) ; expect ( body . error ) . to . eql ( 'Forbidden' ) ; expect ( body . message ) . to . eql ( 'Forbidden' ) ; } ) ; } ) ; } ) ; } ;