-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Security Solution][Endpoint] Isolate Action should only be available to Platinum+ licenses #102374
Merged
paul-tavares
merged 8 commits into
elastic:master
from
paul-tavares:task/olm-1304-isolate-action-is-platinum-only
Jun 16, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
70d7732
Isolate action should only be available for platinum license
paul-tavares f7f93ae
Moved `useLicense` hook mock into `__mocks__`
paul-tavares 71ddad3
Merge remote-tracking branch 'upstream/master' into task/olm-1304-iso…
paul-tavares c3540cc
Tests for actions menu button to cover license level
paul-tavares b977353
Fix test (maybe)
paul-tavares 5c9905f
Merge remote-tracking branch 'upstream/master' into task/olm-1304-iso…
paul-tavares 87ea5cb
code review suggestion
paul-tavares 33c69f4
Fix test - again
paul-tavares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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 { LicenseService } from './license'; | ||
|
||
export const createLicenseServiceMock = (): jest.Mocked<LicenseService> => { | ||
return ({ | ||
start: jest.fn(), | ||
stop: jest.fn(), | ||
getLicenseInformation: jest.fn(), | ||
getLicenseInformation$: jest.fn(), | ||
isAtLeast: jest.fn(), | ||
isGoldPlus: jest.fn().mockReturnValue(true), | ||
isPlatinumPlus: jest.fn().mockReturnValue(true), | ||
isEnterprise: jest.fn().mockReturnValue(true), | ||
} as unknown) as jest.Mocked<LicenseService>; | ||
}; |
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/security_solution/public/common/hooks/__mocks__/use_license.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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 { createLicenseServiceMock } from '../../../../common/license/mocks'; | ||
|
||
export const licenseService = createLicenseServiceMock(); | ||
export const useLicense = () => licenseService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LicenseService is a concrete class. It should not need to be mocked. Best practice is to init the real class, and inject any needed/mocked state as necessary into the instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simpler approach can be using a spy for useLicense and return the mocked value for the specific describe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by concrete class?
In order to do this better, we would need to mock the observables that come out of kibana and that this class takes in during
.start()
. We can track that as tech. debt if needed - but I'm not trying to testLicenseService
class here (if that was the case - then yes, totally agree 😄 ).I'm just trying to mock the instance created by the
useLicense
hook. The issue is that the hook instantiates and uses aLicenseService
instance that is also exported. That exported instance is then used during the UI'sPlugin.start()
to give it the licensing observable. Because of this, its difficult to avoid the mocking of the hook. the implementation approach would have to be changed to using a React context provider - similar to how we have others context providers defined and then we could instantiate and use a custom LicenseService instance in bootstrapping the UI app.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, so part of it is how the frontend is set up here.
Since the
frontend/plugin.tsx
is starting the service withlicenseService.start(plugins.licensing.license$);
wherelicenseService
is the imported instance. You don't have great control to supplant with your own instance, or passing in an observable you control (replacingplugins.licensing.license$
).It's still not good to mock value classes (sorry this is what I meant, not concrete), specifically when they are not under test. But the singleton you don't control makes it hard.
Unless you can think of a sane way to replace what's in
plugins.licensing.license$
. That's really all the control you need to set the 'license state' for a given test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, another thing I don't like about this mocking of the singleton is that it creates state between tests - I just got a failure here in CI that I think was caused by that.
I'm going to create a refactor issue to revisit this.