From da60218f780f31e98237f4bc9415dfc7e193cb31 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 10 Feb 2023 13:12:37 -0500 Subject: [PATCH] [8.7] [ML] update_trained_models_spaces api tests (#150901) (#150925) # Backport This will backport the following commits from `main` to `8.7`: - [[ML] update_trained_models_spaces api tests (#150901)](https://github.com/elastic/kibana/pull/150901) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: James Gowdy --- .../apis/ml/saved_objects/index.ts | 1 + .../ml/saved_objects/update_jobs_spaces.ts | 8 +- .../update_trained_model_spaces.ts | 98 +++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 x-pack/test/api_integration/apis/ml/saved_objects/update_trained_model_spaces.ts diff --git a/x-pack/test/api_integration/apis/ml/saved_objects/index.ts b/x-pack/test/api_integration/apis/ml/saved_objects/index.ts index 5139a121e07d10..32c83cb6241635 100644 --- a/x-pack/test/api_integration/apis/ml/saved_objects/index.ts +++ b/x-pack/test/api_integration/apis/ml/saved_objects/index.ts @@ -18,6 +18,7 @@ export default function ({ loadTestFile }: FtrProviderContext) { loadTestFile(require.resolve('./sync_jobs')); loadTestFile(require.resolve('./sync_trained_models')); loadTestFile(require.resolve('./update_jobs_spaces')); + loadTestFile(require.resolve('./update_trained_model_spaces')); loadTestFile(require.resolve('./remove_from_current_space')); }); } diff --git a/x-pack/test/api_integration/apis/ml/saved_objects/update_jobs_spaces.ts b/x-pack/test/api_integration/apis/ml/saved_objects/update_jobs_spaces.ts index 079b1b32f10b9d..452bce090eacff 100644 --- a/x-pack/test/api_integration/apis/ml/saved_objects/update_jobs_spaces.ts +++ b/x-pack/test/api_integration/apis/ml/saved_objects/update_jobs_spaces.ts @@ -70,8 +70,8 @@ export default ({ getService }: FtrProviderContext) => { }); it('should assign AD job to space for user with access to that space', async () => { - await ml.api.assertJobSpaces(adJobId, 'anomaly-detector', [defaultSpaceId]); const jobType = 'anomaly-detector'; + await ml.api.assertJobSpaces(adJobId, jobType, [defaultSpaceId]); const body = await runRequest( { jobType, @@ -88,8 +88,8 @@ export default ({ getService }: FtrProviderContext) => { }); it('should assign DFA job to space for user with access to that space', async () => { - await ml.api.assertJobSpaces(dfaJobId, 'data-frame-analytics', [defaultSpaceId]); const jobType = 'data-frame-analytics'; + await ml.api.assertJobSpaces(dfaJobId, jobType, [defaultSpaceId]); const body = await runRequest( { jobType, @@ -106,8 +106,8 @@ export default ({ getService }: FtrProviderContext) => { }); it('should fail to update AD job spaces for space the user has no access to', async () => { - await ml.api.assertJobSpaces(adJobId, 'anomaly-detector', [defaultSpaceId]); const jobType = 'anomaly-detector'; + await ml.api.assertJobSpaces(adJobId, jobType, [defaultSpaceId]); const body = await runRequest( { jobType, @@ -124,8 +124,8 @@ export default ({ getService }: FtrProviderContext) => { }); it('should fail to update DFA job spaces for space the user has no access to', async () => { - await ml.api.assertJobSpaces(dfaJobId, 'data-frame-analytics', [defaultSpaceId]); const jobType = 'data-frame-analytics'; + await ml.api.assertJobSpaces(dfaJobId, jobType, [defaultSpaceId]); const body = await runRequest( { jobType, diff --git a/x-pack/test/api_integration/apis/ml/saved_objects/update_trained_model_spaces.ts b/x-pack/test/api_integration/apis/ml/saved_objects/update_trained_model_spaces.ts new file mode 100644 index 00000000000000..d8c764bf6535a2 --- /dev/null +++ b/x-pack/test/api_integration/apis/ml/saved_objects/update_trained_model_spaces.ts @@ -0,0 +1,98 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +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'; + +export default ({ getService }: FtrProviderContext) => { + const ml = getService('ml'); + const spacesService = getService('spaces'); + const supertest = getService('supertestWithoutAuth'); + + const trainedModelId = 'trained_model'; + const idSpace1 = 'space1'; + const idSpace2 = 'space2'; + const defaultSpaceId = 'default'; + + async function runRequest( + requestBody: { + modelIds: string[]; + spacesToAdd: string[]; + spacesToRemove: string[]; + }, + expectedStatusCode: number, + user: USER + ) { + const { body, status } = await supertest + .post(`/api/ml/saved_objects/update_trained_models_spaces`) + .auth(user, ml.securityCommon.getPasswordForUser(user)) + .set(COMMON_REQUEST_HEADERS) + .send(requestBody); + ml.api.assertResponseStatusCode(expectedStatusCode, status, body); + + return body; + } + + describe('POST saved_objects/update_trained_models_spaces', () => { + before(async () => { + await spacesService.create({ id: idSpace1, name: 'space_one', disabledFeatures: [] }); + await spacesService.create({ id: idSpace2, name: 'space_two', disabledFeatures: [] }); + + await ml.testResources.setKibanaTimeZoneToUTC(); + }); + + beforeEach(async () => { + // Create trained model + const trainedModelConfig = ml.api.createTestTrainedModelConfig(trainedModelId, 'regression'); + await ml.api.createTrainedModel(trainedModelId, trainedModelConfig.body); + }); + + afterEach(async () => { + await ml.api.cleanMlIndices(); + await ml.testResources.cleanMLSavedObjects(); + }); + + after(async () => { + await spacesService.delete(idSpace1); + await spacesService.delete(idSpace2); + }); + + it('should assign trained model to space for user with access to that space', async () => { + await ml.api.assertTrainedModelSpaces(trainedModelId, [defaultSpaceId]); + const body = await runRequest( + { + modelIds: [trainedModelId], + spacesToAdd: [idSpace1], + spacesToRemove: [defaultSpaceId], + }, + 200, + USER.ML_POWERUSER_SPACE1 + ); + + expect(body).to.eql({ [trainedModelId]: { type: 'trained-model', success: true } }); + await ml.api.assertTrainedModelSpaces(trainedModelId, [idSpace1]); + }); + + it('should fail to update trained model spaces for space the user has no access to', async () => { + await ml.api.assertTrainedModelSpaces(trainedModelId, [defaultSpaceId]); + const body = await runRequest( + { + modelIds: [trainedModelId], + spacesToAdd: [idSpace2], + spacesToRemove: [], + }, + 200, + USER.ML_POWERUSER_SPACE1 + ); + + expect(body[trainedModelId]).to.have.property('success', false); + await ml.api.assertTrainedModelSpaces(trainedModelId, [defaultSpaceId]); + }); + }); +};