[ML] update_trained_models_spaces api tests (#150901)

Adds tests for the `update_trained_models_spaces` endpoint
This commit is contained in:
James Gowdy 2023-02-10 16:53:10 +00:00 committed by GitHub
parent caee8cbe8e
commit 648786e532
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 103 additions and 4 deletions

View file

@ -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'));
});
}

View file

@ -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,

View file

@ -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]);
});
});
};