Skip to content

Commit

Permalink
[8.x] [Search] refactor: unify license check utils (#197675) (#199994)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[Search] refactor: unify license check utils
(#197675)](#197675)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Rodney
Norris","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-13T12:19:23Z","message":"[Search]
refactor: unify license check utils
(#197675)","sha":"5a138e392ff794561dc9a18d9a631be181543dca","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Search","backport:prev-minor","v8.17.0"],"title":"[Search]
refactor: unify license check
utils","number":197675,"url":"https://github.com/elastic/kibana/pull/197675","mergeCommit":{"message":"[Search]
refactor: unify license check utils
(#197675)","sha":"5a138e392ff794561dc9a18d9a631be181543dca"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197675","number":197675,"mergeCommit":{"message":"[Search]
refactor: unify license check utils
(#197675)","sha":"5a138e392ff794561dc9a18d9a631be181543dca"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Rodney Norris <[email protected]>
  • Loading branch information
kibanamachine and TattdCodeMonkey authored Nov 13, 2024
1 parent 890db71 commit 6e12600
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 89 deletions.
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

0 comments on commit 6e12600

Please sign in to comment.