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

[8.x] [Search] refactor: unify license check utils (#197675) #199994

Merged
merged 1 commit into from
Nov 13, 2024
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
184 changes: 117 additions & 67 deletions x-pack/plugins/enterprise_search/common/utils/licensing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,142 @@
* 2.0.
*/

import type { ILicense } from '@kbn/licensing-plugin/public';
import { licenseMock } from '@kbn/licensing-plugin/common/licensing.mock';

import { hasEnterpriseLicense } from './licensing';
import { License } from '@kbn/licensing-plugin/common/license';

import {
hasEnterpriseLicense,
hasGoldLicense,
hasPlatinumLicense,
isTrialLicense,
} from './licensing';

describe('licensing utils', () => {
const baseLicense: ILicense = {
isActive: true,
type: 'trial',
isAvailable: true,
signature: 'fake',
toJSON: jest.fn(),
getUnavailableReason: jest.fn().mockReturnValue(undefined),
hasAtLeast: jest.fn().mockReturnValue(false),
check: jest.fn().mockReturnValue({ state: 'valid' }),
getFeature: jest.fn().mockReturnValue({ isAvailable: false, isEnabled: false }),
};
describe('hasEnterpriseLicense', () => {
let license: ILicense;
beforeEach(() => {
jest.resetAllMocks();
license = {
...baseLicense,
};
});
it('returns true for active enterprise license', () => {
license.type = 'enterprise';
const basicLicense = licenseMock.createLicense();
const basicExpiredLicense = licenseMock.createLicense({ license: { status: 'expired' } });
const goldLicense = licenseMock.createLicense({ license: { type: 'gold' } });
const goldLicenseExpired = licenseMock.createLicense({
license: { status: 'expired', type: 'gold' },
});
const platinumLicense = licenseMock.createLicense({ license: { type: 'platinum' } });
const platinumLicenseExpired = licenseMock.createLicense({
license: { status: 'expired', type: 'platinum' },
});
const enterpriseLicense = licenseMock.createLicense({ license: { type: 'enterprise' } });
const enterpriseLicenseExpired = licenseMock.createLicense({
license: { status: 'expired', type: 'enterprise' },
});
const trialLicense = licenseMock.createLicense({ license: { type: 'trial' } });
const trialLicenseExpired = licenseMock.createLicense({
license: { status: 'expired', type: 'trial' },
});

expect(hasEnterpriseLicense(license)).toEqual(true);
const errorMessage = 'unavailable';
const errorLicense = new License({ error: errorMessage, signature: '' });
const unavailableLicense = new License({ signature: '' });

beforeEach(() => {
jest.resetAllMocks();
});

describe('hasEnterpriseLicense', () => {
it('returns true for active valid licenses', () => {
expect(hasEnterpriseLicense(enterpriseLicense)).toEqual(true);
expect(hasEnterpriseLicense(trialLicense)).toEqual(true);
});
it('returns true for active trial license', () => {
expect(hasEnterpriseLicense(license)).toEqual(true);
it('returns false for active invalid licenses', () => {
expect(hasEnterpriseLicense(basicLicense)).toEqual(false);
expect(hasEnterpriseLicense(goldLicense)).toEqual(false);
expect(hasEnterpriseLicense(platinumLicense)).toEqual(false);
});
it('returns false for active basic license', () => {
license.type = 'basic';

expect(hasEnterpriseLicense(license)).toEqual(false);
it('returns false for inactive licenses', () => {
expect(hasEnterpriseLicense(trialLicenseExpired)).toEqual(false);
expect(hasEnterpriseLicense(enterpriseLicenseExpired)).toEqual(false);
expect(hasEnterpriseLicense(basicExpiredLicense)).toEqual(false);
});
it('returns false for active gold license', () => {
license.type = 'gold';

expect(hasEnterpriseLicense(license)).toEqual(false);
it('returns false for unavailable license', () => {
expect(hasEnterpriseLicense(errorLicense)).toEqual(false);
expect(hasEnterpriseLicense(unavailableLicense)).toEqual(false);
});
it('returns false for active platinum license', () => {
license.type = 'platinum';

expect(hasEnterpriseLicense(license)).toEqual(false);
it('returns false for null license', () => {
expect(hasEnterpriseLicense(null)).toEqual(false);
});
it('returns false for inactive enterprise license', () => {
license.type = 'enterprise';
license.isActive = false;

expect(hasEnterpriseLicense(license)).toEqual(false);
it('returns false for undefined license', () => {
expect(hasEnterpriseLicense(undefined)).toEqual(false);
});
it('returns false for inactive trial license', () => {
license.isActive = false;
});

expect(hasEnterpriseLicense(license)).toEqual(false);
describe('hasPlatinumLicense', () => {
it('returns true for valid active licenses', () => {
expect(hasPlatinumLicense(platinumLicense)).toEqual(true);
expect(hasPlatinumLicense(enterpriseLicense)).toEqual(true);
expect(hasPlatinumLicense(trialLicense)).toEqual(true);
});
it('returns false for inactive basic license', () => {
license.type = 'basic';
license.isActive = false;

expect(hasEnterpriseLicense(license)).toEqual(false);
it('returns false for invalid active licenses', () => {
expect(hasPlatinumLicense(goldLicense)).toEqual(false);
expect(hasPlatinumLicense(basicLicense)).toEqual(false);
});
it('returns false for inactive gold license', () => {
license.type = 'gold';
license.isActive = false;

expect(hasEnterpriseLicense(license)).toEqual(false);
it('returns false for inactive licenses', () => {
expect(hasPlatinumLicense(platinumLicenseExpired)).toEqual(false);
expect(hasPlatinumLicense(enterpriseLicenseExpired)).toEqual(false);
expect(hasPlatinumLicense(trialLicenseExpired)).toEqual(false);
});
it('returns false for inactive platinum license', () => {
license.type = 'platinum';
license.isActive = false;

expect(hasEnterpriseLicense(license)).toEqual(false);
it('returns false for bad licenses', () => {
expect(hasPlatinumLicense(errorLicense)).toEqual(false);
expect(hasPlatinumLicense(unavailableLicense)).toEqual(false);
});
it('returns false for active license is missing type', () => {
delete license.type;

expect(hasEnterpriseLicense(license)).toEqual(false);
it('returns false for null license', () => {
expect(hasPlatinumLicense(null)).toEqual(false);
});
it('returns false for undefined license', () => {
expect(hasPlatinumLicense(undefined)).toEqual(false);
});
});
describe('hasGoldLicense', () => {
it('returns true for valid active licenses', () => {
expect(hasGoldLicense(goldLicense)).toEqual(true);
expect(hasGoldLicense(platinumLicense)).toEqual(true);
expect(hasGoldLicense(enterpriseLicense)).toEqual(true);
expect(hasGoldLicense(trialLicense)).toEqual(true);
});
it('returns false for invalid active licenses', () => {
expect(hasGoldLicense(basicLicense)).toEqual(false);
});
it('returns false for inactive licenses', () => {
expect(hasGoldLicense(goldLicenseExpired)).toEqual(false);
expect(hasGoldLicense(platinumLicenseExpired)).toEqual(false);
expect(hasGoldLicense(enterpriseLicenseExpired)).toEqual(false);
expect(hasGoldLicense(trialLicenseExpired)).toEqual(false);
});
it('returns false for bad licenses', () => {
expect(hasGoldLicense(errorLicense)).toEqual(false);
expect(hasGoldLicense(unavailableLicense)).toEqual(false);
});
it('returns false for null license', () => {
expect(hasEnterpriseLicense(null)).toEqual(false);
expect(hasGoldLicense(null)).toEqual(false);
});
it('returns false for undefined license', () => {
expect(hasEnterpriseLicense(undefined)).toEqual(false);
expect(hasGoldLicense(undefined)).toEqual(false);
});
});
describe('isTrialLicense', () => {
it('returns true for active trial license', () => {
expect(hasGoldLicense(trialLicense)).toEqual(true);
});
it('returns false for non-trial license', () => {
expect(isTrialLicense(platinumLicense)).toEqual(false);
});
it('returns false for invalid license', () => {
expect(isTrialLicense(trialLicenseExpired)).toEqual(false);
expect(isTrialLicense(errorLicense)).toEqual(false);
expect(isTrialLicense(unavailableLicense)).toEqual(false);
});
it('returns false for null license', () => {
expect(isTrialLicense(null)).toEqual(false);
});
it('returns false for undefined license', () => {
expect(isTrialLicense(undefined)).toEqual(false);
});
});
});
34 changes: 31 additions & 3 deletions x-pack/plugins/enterprise_search/common/utils/licensing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,38 @@

import type { ILicense } from '@kbn/licensing-plugin/public';

/* hasEnterpriseLicense return if the given license is an active `enterprise` or `trial` license
/* hasEnterpriseLicense return if the given license is an active `enterprise` or greater license
*/
export function hasEnterpriseLicense(license: ILicense | null | undefined): boolean {
if (license === undefined || license === null) return false;
const qualifyingLicenses = ['enterprise', 'trial'];
return license.isActive && qualifyingLicenses.includes(license?.type ?? '');
if (!license.isAvailable) return false;
if (!license.isActive) return false;
return license.hasAtLeast('enterprise');
}

/* hasPlatinumLicense return if the given license is an active `platinum` or greater license
*/
export function hasPlatinumLicense(license: ILicense | null | undefined): boolean {
if (license === undefined || license === null) return false;
if (!license.isAvailable) return false;
if (!license.isActive) return false;
return license.hasAtLeast('platinum');
}

/* hasGoldLicense return if the given license is an active `gold` or greater license
*/
export function hasGoldLicense(license: ILicense | null | undefined): boolean {
if (license === undefined || license === null) return false;
if (!license.isAvailable) return false;
if (!license.isActive) return false;
return license.hasAtLeast('gold');
}

/* isTrialLicense returns if the given license is an active `trial` license
*/
export function isTrialLicense(license: ILicense | null | undefined): boolean {
if (license === undefined || license === null) return false;
if (!license.isAvailable) return false;
if (!license.isActive) return false;
return license?.type === 'trial';
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { Observable, Subscription } from 'rxjs';

import { ILicense } from '@kbn/licensing-plugin/public';

import {
hasEnterpriseLicense,
hasGoldLicense,
hasPlatinumLicense,
isTrialLicense,
} from '../../../../common/utils/licensing';

interface LicensingValues {
license: ILicense | null;
licenseSubscription: Subscription | null;
Expand Down Expand Up @@ -50,29 +57,14 @@ export const LicensingLogic = kea<MakeLogicType<LicensingValues, LicensingAction
selectors: {
hasPlatinumLicense: [
(selectors) => [selectors.license],
(license) => {
const qualifyingLicenses = ['platinum', 'enterprise', 'trial'];
return license?.isActive && qualifyingLicenses.includes(license?.type);
},
(license) => hasPlatinumLicense(license),
],
hasEnterpriseLicense: [
(selectors) => [selectors.license],
(license) => {
const qualifyingLicenses = ['enterprise', 'trial'];
return license?.isActive && qualifyingLicenses.includes(license?.type);
},
],
hasGoldLicense: [
(selectors) => [selectors.license],
(license) => {
const qualifyingLicenses = ['gold', 'platinum', 'enterprise', 'trial'];
return license?.isActive && qualifyingLicenses.includes(license?.type);
},
],
isTrial: [
(selectors) => [selectors.license],
(license) => license?.isActive && license?.type === 'trial',
(license) => hasEnterpriseLicense(license),
],
hasGoldLicense: [(selectors) => [selectors.license], (license) => hasGoldLicense(license)],
isTrial: [(selectors) => [selectors.license], (license) => isTrialLicense(license)],
},
events: ({ props, actions, values }) => ({
afterMount: () => {
Expand Down