Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] update_trained_models_spaces api tests #150901

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
});
});
};