From b39984196d70766a99aecfeb804734405e55ccb8 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Thu, 25 Feb 2021 18:24:39 +0000 Subject: [PATCH] [ML] Adding ml_capabilities api tests (#92786) * [ML] Adding ml_capabilities api tests * updating test text * changes based on review --- x-pack/test/api_integration/apis/ml/index.ts | 1 + .../apis/ml/system/capabilities.ts | 124 ++++++++++ .../api_integration/apis/ml/system/index.ts | 15 ++ .../apis/ml/system/space_capabilities.ts | 222 ++++++++++++++++++ 4 files changed, 362 insertions(+) create mode 100644 x-pack/test/api_integration/apis/ml/system/capabilities.ts create mode 100644 x-pack/test/api_integration/apis/ml/system/index.ts create mode 100644 x-pack/test/api_integration/apis/ml/system/space_capabilities.ts diff --git a/x-pack/test/api_integration/apis/ml/index.ts b/x-pack/test/api_integration/apis/ml/index.ts index f03756a2885bb..41e94d69d2e9b 100644 --- a/x-pack/test/api_integration/apis/ml/index.ts +++ b/x-pack/test/api_integration/apis/ml/index.ts @@ -70,5 +70,6 @@ export default function ({ getService, loadTestFile }: FtrProviderContext) { loadTestFile(require.resolve('./calendars')); loadTestFile(require.resolve('./annotations')); loadTestFile(require.resolve('./saved_objects')); + loadTestFile(require.resolve('./system')); }); } diff --git a/x-pack/test/api_integration/apis/ml/system/capabilities.ts b/x-pack/test/api_integration/apis/ml/system/capabilities.ts new file mode 100644 index 0000000000000..d8ab2a30ef7fb --- /dev/null +++ b/x-pack/test/api_integration/apis/ml/system/capabilities.ts @@ -0,0 +1,124 @@ +/* + * 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 { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api'; +import { USER } from '../../../../functional/services/ml/security_common'; +import { MlCapabilitiesResponse } from '../../../../../plugins/ml/common/types/capabilities'; + +export default ({ getService }: FtrProviderContext) => { + const supertest = getService('supertestWithoutAuth'); + const ml = getService('ml'); + + async function runRequest(user: USER): Promise { + const { body } = await supertest + .get(`/api/ml/ml_capabilities`) + .auth(user, ml.securityCommon.getPasswordForUser(user)) + .set(COMMON_REQUEST_HEADERS) + .expect(200); + + return body; + } + + describe('ml_capabilities', () => { + describe('get capabilities', function () { + it('should be enabled in space', async () => { + const { mlFeatureEnabledInSpace } = await runRequest(USER.ML_POWERUSER); + expect(mlFeatureEnabledInSpace).to.eql(true); + }); + + it('should have upgradeInProgress false', async () => { + const { upgradeInProgress } = await runRequest(USER.ML_POWERUSER); + expect(upgradeInProgress).to.eql(false); + }); + + it('should have full license', async () => { + const { isPlatinumOrTrialLicense } = await runRequest(USER.ML_POWERUSER); + expect(isPlatinumOrTrialLicense).to.eql(true); + }); + + it('should have the right number of capabilities', async () => { + const { capabilities } = await runRequest(USER.ML_POWERUSER); + expect(Object.keys(capabilities).length).to.eql(29); + }); + + it('should get viewer capabilities', async () => { + const { capabilities } = await runRequest(USER.ML_VIEWER); + + expect(capabilities).to.eql({ + canCreateJob: false, + canDeleteJob: false, + canOpenJob: false, + canCloseJob: false, + canUpdateJob: false, + canForecastJob: false, + canCreateDatafeed: false, + canDeleteDatafeed: false, + canStartStopDatafeed: false, + canUpdateDatafeed: false, + canPreviewDatafeed: false, + canGetFilters: false, + canCreateCalendar: false, + canDeleteCalendar: false, + canCreateFilter: false, + canDeleteFilter: false, + canCreateDataFrameAnalytics: false, + canDeleteDataFrameAnalytics: false, + canStartStopDataFrameAnalytics: false, + canCreateMlAlerts: false, + canAccessML: true, + canGetJobs: true, + canGetDatafeeds: true, + canGetCalendars: true, + canFindFileStructure: true, + canGetDataFrameAnalytics: true, + canGetAnnotations: true, + canCreateAnnotation: true, + canDeleteAnnotation: true, + }); + }); + + it('should get power user capabilities', async () => { + const { capabilities } = await runRequest(USER.ML_POWERUSER); + + expect(capabilities).to.eql({ + canCreateJob: true, + canDeleteJob: true, + canOpenJob: true, + canCloseJob: true, + canUpdateJob: true, + canForecastJob: true, + canCreateDatafeed: true, + canDeleteDatafeed: true, + canStartStopDatafeed: true, + canUpdateDatafeed: true, + canPreviewDatafeed: true, + canGetFilters: true, + canCreateCalendar: true, + canDeleteCalendar: true, + canCreateFilter: true, + canDeleteFilter: true, + canCreateDataFrameAnalytics: true, + canDeleteDataFrameAnalytics: true, + canStartStopDataFrameAnalytics: true, + canCreateMlAlerts: true, + canAccessML: true, + canGetJobs: true, + canGetDatafeeds: true, + canGetCalendars: true, + canFindFileStructure: true, + canGetDataFrameAnalytics: true, + canGetAnnotations: true, + canCreateAnnotation: true, + canDeleteAnnotation: true, + }); + }); + }); + }); +}; diff --git a/x-pack/test/api_integration/apis/ml/system/index.ts b/x-pack/test/api_integration/apis/ml/system/index.ts new file mode 100644 index 0000000000000..68ffd5fa267e9 --- /dev/null +++ b/x-pack/test/api_integration/apis/ml/system/index.ts @@ -0,0 +1,15 @@ +/* + * 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 { FtrProviderContext } from '../../../ftr_provider_context'; + +export default function ({ loadTestFile }: FtrProviderContext) { + describe('system', function () { + loadTestFile(require.resolve('./capabilities')); + loadTestFile(require.resolve('./space_capabilities')); + }); +} diff --git a/x-pack/test/api_integration/apis/ml/system/space_capabilities.ts b/x-pack/test/api_integration/apis/ml/system/space_capabilities.ts new file mode 100644 index 0000000000000..cd922bf4bae92 --- /dev/null +++ b/x-pack/test/api_integration/apis/ml/system/space_capabilities.ts @@ -0,0 +1,222 @@ +/* + * 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 { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api'; +import { USER } from '../../../../functional/services/ml/security_common'; +import { MlCapabilitiesResponse } from '../../../../../plugins/ml/common/types/capabilities'; + +const idSpaceWithMl = 'space_with_ml'; +const idSpaceNoMl = 'space_no_ml'; + +export default ({ getService }: FtrProviderContext) => { + const supertest = getService('supertestWithoutAuth'); + const spacesService = getService('spaces'); + const ml = getService('ml'); + + async function runRequest(user: USER, space?: string): Promise { + const { body } = await supertest + .get(`${space ? `/s/${space}` : ''}/api/ml/ml_capabilities`) + .auth(user, ml.securityCommon.getPasswordForUser(user)) + .set(COMMON_REQUEST_HEADERS) + .expect(200); + + return body; + } + + describe('ml_capabilities in spaces', () => { + before(async () => { + await spacesService.create({ id: idSpaceWithMl, name: 'space_one', disabledFeatures: [] }); + await spacesService.create({ id: idSpaceNoMl, name: 'space_two', disabledFeatures: ['ml'] }); + }); + + after(async () => { + await spacesService.delete(idSpaceWithMl); + await spacesService.delete(idSpaceNoMl); + }); + + describe('get capabilities', function () { + it('should be enabled in space - space with ML', async () => { + const { mlFeatureEnabledInSpace } = await runRequest(USER.ML_POWERUSER, idSpaceWithMl); + expect(mlFeatureEnabledInSpace).to.eql(true); + }); + it('should not be enabled in space - space without ML', async () => { + const { mlFeatureEnabledInSpace } = await runRequest(USER.ML_POWERUSER, idSpaceNoMl); + expect(mlFeatureEnabledInSpace).to.eql(false); + }); + + it('should have upgradeInProgress false - space with ML', async () => { + const { upgradeInProgress } = await runRequest(USER.ML_POWERUSER, idSpaceWithMl); + expect(upgradeInProgress).to.eql(false); + }); + it('should have upgradeInProgress false - space without ML', async () => { + const { upgradeInProgress } = await runRequest(USER.ML_POWERUSER, idSpaceNoMl); + expect(upgradeInProgress).to.eql(false); + }); + + it('should have full license - space with ML', async () => { + const { isPlatinumOrTrialLicense } = await runRequest(USER.ML_POWERUSER, idSpaceWithMl); + expect(isPlatinumOrTrialLicense).to.eql(true); + }); + it('should have full license - space without ML', async () => { + const { isPlatinumOrTrialLicense } = await runRequest(USER.ML_POWERUSER, idSpaceNoMl); + expect(isPlatinumOrTrialLicense).to.eql(true); + }); + + it('should have the right number of capabilities - space with ML', async () => { + const { capabilities } = await runRequest(USER.ML_POWERUSER, idSpaceWithMl); + expect(Object.keys(capabilities).length).to.eql(29); + }); + it('should have the right number of capabilities - space without ML', async () => { + const { capabilities } = await runRequest(USER.ML_POWERUSER, idSpaceNoMl); + expect(Object.keys(capabilities).length).to.eql(29); + }); + + it('should get viewer capabilities - space with ML', async () => { + const { capabilities } = await runRequest(USER.ML_VIEWER, idSpaceWithMl); + expect(capabilities).to.eql({ + canCreateJob: false, + canDeleteJob: false, + canOpenJob: false, + canCloseJob: false, + canUpdateJob: false, + canForecastJob: false, + canCreateDatafeed: false, + canDeleteDatafeed: false, + canStartStopDatafeed: false, + canUpdateDatafeed: false, + canPreviewDatafeed: false, + canGetFilters: false, + canCreateCalendar: false, + canDeleteCalendar: false, + canCreateFilter: false, + canDeleteFilter: false, + canCreateDataFrameAnalytics: false, + canDeleteDataFrameAnalytics: false, + canStartStopDataFrameAnalytics: false, + canCreateMlAlerts: false, + canAccessML: true, + canGetJobs: true, + canGetDatafeeds: true, + canGetCalendars: true, + canFindFileStructure: true, + canGetDataFrameAnalytics: true, + canGetAnnotations: true, + canCreateAnnotation: true, + canDeleteAnnotation: true, + }); + }); + + it('should get viewer capabilities - space without ML', async () => { + const { capabilities } = await runRequest(USER.ML_VIEWER, idSpaceNoMl); + expect(capabilities).to.eql({ + canCreateJob: false, + canDeleteJob: false, + canOpenJob: false, + canCloseJob: false, + canUpdateJob: false, + canForecastJob: false, + canCreateDatafeed: false, + canDeleteDatafeed: false, + canStartStopDatafeed: false, + canUpdateDatafeed: false, + canPreviewDatafeed: false, + canGetFilters: false, + canCreateCalendar: false, + canDeleteCalendar: false, + canCreateFilter: false, + canDeleteFilter: false, + canCreateDataFrameAnalytics: false, + canDeleteDataFrameAnalytics: false, + canStartStopDataFrameAnalytics: false, + canCreateMlAlerts: false, + canAccessML: false, + canGetJobs: false, + canGetDatafeeds: false, + canGetCalendars: false, + canFindFileStructure: false, + canGetDataFrameAnalytics: false, + canGetAnnotations: false, + canCreateAnnotation: false, + canDeleteAnnotation: false, + }); + }); + + it('should get power user capabilities - space with ML', async () => { + const { capabilities } = await runRequest(USER.ML_POWERUSER, idSpaceWithMl); + expect(capabilities).to.eql({ + canCreateJob: true, + canDeleteJob: true, + canOpenJob: true, + canCloseJob: true, + canUpdateJob: true, + canForecastJob: true, + canCreateDatafeed: true, + canDeleteDatafeed: true, + canStartStopDatafeed: true, + canUpdateDatafeed: true, + canPreviewDatafeed: true, + canGetFilters: true, + canCreateCalendar: true, + canDeleteCalendar: true, + canCreateFilter: true, + canDeleteFilter: true, + canCreateDataFrameAnalytics: true, + canDeleteDataFrameAnalytics: true, + canStartStopDataFrameAnalytics: true, + canCreateMlAlerts: true, + canAccessML: true, + canGetJobs: true, + canGetDatafeeds: true, + canGetCalendars: true, + canFindFileStructure: true, + canGetDataFrameAnalytics: true, + canGetAnnotations: true, + canCreateAnnotation: true, + canDeleteAnnotation: true, + }); + }); + + it('should get power user capabilities - space without ML', async () => { + const { capabilities } = await runRequest(USER.ML_POWERUSER, idSpaceNoMl); + expect(capabilities).to.eql({ + canCreateJob: false, + canDeleteJob: false, + canOpenJob: false, + canCloseJob: false, + canUpdateJob: false, + canForecastJob: false, + canCreateDatafeed: false, + canDeleteDatafeed: false, + canStartStopDatafeed: false, + canUpdateDatafeed: false, + canPreviewDatafeed: false, + canGetFilters: false, + canCreateCalendar: false, + canDeleteCalendar: false, + canCreateFilter: false, + canDeleteFilter: false, + canCreateDataFrameAnalytics: false, + canDeleteDataFrameAnalytics: false, + canStartStopDataFrameAnalytics: false, + canCreateMlAlerts: false, + canAccessML: false, + canGetJobs: false, + canGetDatafeeds: false, + canGetCalendars: false, + canFindFileStructure: false, + canGetDataFrameAnalytics: false, + canGetAnnotations: false, + canCreateAnnotation: false, + canDeleteAnnotation: false, + }); + }); + }); + }); +};