Skip to content

Commit

Permalink
[ML] Fix navigation for the Basic licence (#139469)
Browse files Browse the repository at this point in the history
* fix redirect to the data vis

* update jest tests

* remove window.location.href from the clone action

* assert the redirect

* update power user tests

* use plugin id const, return promise reject
  • Loading branch information
darnautov committed Aug 26, 2022
1 parent a1e0b98 commit 62578bd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 23 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/ml/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const renderApp = (

appMountParams.onAppLeave((actions) => actions.default());

const mlLicense = setLicenseCache(deps.licensing, [
const mlLicense = setLicenseCache(deps.licensing, coreStart.application, [
() =>
ReactDOM.render(
<App coreStart={coreStart} deps={deps} appMountParams={appMountParams} />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getToastNotificationService,
toastNotificationServiceProvider,
} from '../../../services/toast_notification_service';
import { getToastNotifications } from '../../../util/dependency_cache';
import { getApplication, getToastNotifications } from '../../../util/dependency_cache';
import { ml } from '../../../services/ml_api_service';
import { stringMatch } from '../../../util/string_utils';
import { getDataViewNames } from '../../../util/index_utils';
Expand All @@ -22,6 +22,8 @@ import { JOB_ACTION } from '../../../../../common/constants/job_actions';
import { parseInterval } from '../../../../../common/util/parse_interval';
import { mlCalendarService } from '../../../services/calendar_service';
import { isPopulatedObject } from '@kbn/ml-is-populated-object';
import { ML_PAGES } from '../../../../../common/constants/locator';
import { PLUGIN_ID } from '../../../../../common/constants/app';

export function loadFullJob(jobId) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -287,7 +289,7 @@ export async function cloneJob(jobId) {
);
}

window.location.href = '#/jobs/new_job';
getApplication().navigateToApp(PLUGIN_ID, { path: ML_PAGES.ANOMALY_DETECTION_CREATE_JOB });
} catch (error) {
getToastNotificationService().displayErrorToast(
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { LicensingPluginSetup } from '@kbn/licensing-plugin/public';
import type { LicensingPluginSetup } from '@kbn/licensing-plugin/public';
import type { CoreStart } from '@kbn/core/public';
import { MlLicense } from '../../../common/license';
import { MlClientLicense } from './ml_client_license';

Expand All @@ -16,13 +17,16 @@ let mlLicense: MlClientLicense | null = null;
*
* @export
* @param {LicensingPluginSetup} licensingSetup
* @param application
* @param postInitFunctions
* @returns {MlClientLicense}
*/
export function setLicenseCache(
licensingSetup: LicensingPluginSetup,
application: CoreStart['application'],
postInitFunctions?: Array<(lic: MlLicense) => void>
) {
mlLicense = new MlClientLicense();
mlLicense = new MlClientLicense(application);
mlLicense.setup(licensingSetup.license$, postInitFunctions);
return mlLicense;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import { Observable, Subject } from 'rxjs';
import { ILicense } from '@kbn/licensing-plugin/common/types';

import { MlClientLicense } from './ml_client_license';
import { applicationServiceMock } from '@kbn/core/public/application/application_service.mock';

describe('MlClientLicense', () => {
const startApplicationContractMock = applicationServiceMock.createStartContract();

test('should miss the license update when initialized without postInitFunction', () => {
const mlLicense = new MlClientLicense();
const mlLicense = new MlClientLicense(startApplicationContractMock);

// upon instantiation the full license doesn't get set
expect(mlLicense.isFullLicense()).toBe(false);
Expand All @@ -35,7 +38,7 @@ describe('MlClientLicense', () => {
});

test('should not miss the license update when initialized with postInitFunction', (done) => {
const mlLicense = new MlClientLicense();
const mlLicense = new MlClientLicense(startApplicationContractMock);

// upon instantiation the full license doesn't get set
expect(mlLicense.isFullLicense()).toBe(false);
Expand Down
35 changes: 21 additions & 14 deletions x-pack/plugins/ml/public/application/license/ml_client_license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,36 @@
* 2.0.
*/

import type { CoreStart } from '@kbn/core/public';
import { ML_PAGES } from '../../../common/constants/locator';
import { MlLicense } from '../../../common/license';
import { showExpiredLicenseWarning } from './expired_warning';
import { PLUGIN_ID } from '../../../common/constants/app';

export class MlClientLicense extends MlLicense {
fullLicenseResolver() {
constructor(private application: CoreStart['application']) {
super();
}

private redirectToKibana() {
this.application.navigateToApp('home');
return Promise.reject();
}

private redirectToBasic() {
this.application.navigateToApp(PLUGIN_ID, { path: ML_PAGES.DATA_VISUALIZER });
return Promise.reject();
}

fullLicenseResolver(): Promise<void> {
if (this.isMlEnabled() === false || this.isMinimumLicense() === false) {
// ML is not enabled or the license isn't at least basic
return redirectToKibana();
return this.redirectToKibana();
}

if (this.isFullLicense() === false) {
// ML is enabled, but only with a basic or gold license
return redirectToBasic();
return this.redirectToBasic();
}

// ML is enabled
Expand All @@ -30,7 +47,7 @@ export class MlClientLicense extends MlLicense {
basicLicenseResolver() {
if (this.isMlEnabled() === false || this.isMinimumLicense() === false) {
// ML is not enabled or the license isn't at least basic
return redirectToKibana();
return this.redirectToKibana();
}

// ML is enabled
Expand All @@ -40,13 +57,3 @@ export class MlClientLicense extends MlLicense {
return Promise.resolve();
}
}

function redirectToKibana() {
window.location.href = '/';
return Promise.reject();
}

function redirectToBasic() {
window.location.href = '#/datavisualizer';
return Promise.reject();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../ftr_provider_context';

import { USER } from '../../../../functional/services/ml/security_common';

export default function ({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const ml = getService('ml');
const browser = getService('browser');

const testUsers = [
{ user: USER.ML_POWERUSER, discoverAvailable: true },
Expand Down Expand Up @@ -57,6 +58,10 @@ export default function ({ getService }: FtrProviderContext) {
await ml.testExecution.logTestStep('should load the ML app');
await ml.navigation.navigateToMl();

await ml.testExecution.logTestStep('should redirect to the "Data Visualizer" page');
const browserURl = await browser.getCurrentUrl();
expect(browserURl).to.contain('/ml/datavisualizer');

await ml.testExecution.logTestStep('should display the disabled "Overview" tab');
await ml.navigation.assertOverviewTabEnabled(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* 2.0.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../ftr_provider_context';

import { USER } from '../../../../functional/services/ml/security_common';

export default function ({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const ml = getService('ml');
const browser = getService('browser');

const testUsers = [
{ user: USER.ML_VIEWER, discoverAvailable: true },
Expand Down Expand Up @@ -57,6 +58,10 @@ export default function ({ getService }: FtrProviderContext) {
await ml.testExecution.logTestStep('should load the ML app');
await ml.navigation.navigateToMl();

await ml.testExecution.logTestStep('should redirect to the "Data Visualizer" page');
const browserURl = await browser.getCurrentUrl();
expect(browserURl).to.contain('/ml/datavisualizer');

await ml.testExecution.logTestStep('should display the disabled "Overview" tab');
await ml.navigation.assertOverviewTabEnabled(false);

Expand Down

0 comments on commit 62578bd

Please sign in to comment.