[Defend Workflows] Reputation services checkbox tests (#162495)

Test coverage for https://github.com/elastic/kibana/pull/161617
This commit is contained in:
Konrad Szwarc 2023-07-26 10:35:36 +02:00 committed by GitHub
parent 2f975eb708
commit a32ed1d14a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 54 deletions

View file

@ -50,6 +50,7 @@ import { guidedOnboardingMock } from '@kbn/guided-onboarding-plugin/public/mocks
import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks';
import { of } from 'rxjs';
import { UpsellingService } from '../upsellings';
import { cloudMock } from '@kbn/cloud-plugin/public/mocks';
import { NavigationProvider } from '@kbn/security-solution-navigation';
const mockUiSettings: Record<string, unknown> = {
@ -119,6 +120,7 @@ export const createStartServicesMock = (
const triggersActionsUi = triggersActionsUiMock.createStart();
const cloudExperiments = cloudExperimentsMock.createStartMock();
const guidedOnboarding = guidedOnboardingMock.createStart();
const cloud = cloudMock.createStart();
return {
...core,
@ -199,6 +201,10 @@ export const createStartServicesMock = (
triggersActionsUi,
cloudExperiments,
guidedOnboarding,
cloud: {
...cloud,
isCloudEnabled: false,
},
isSidebarEnabled$: of(true),
upselling: new UpsellingService(),
customDataService,

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { expectIsViewOnly, getPolicySettingsFormTestSubjects, exactMatchText } from '../../mocks';
import { expectIsViewOnly, getPolicySettingsFormTestSubjects } from '../../mocks';
import type { AppContextTestRender } from '../../../../../../../common/mock/endpoint';
import { createAppRootMockRenderer } from '../../../../../../../common/mock/endpoint';
import { FleetPackagePolicyGenerator } from '../../../../../../../../common/endpoint/data_generators/fleet_package_policy_generator';
@ -31,9 +31,11 @@ describe('Policy Behaviour Protection Card', () => {
let formProps: BehaviourProtectionCardProps;
let render: () => ReturnType<AppContextTestRender['render']>;
let renderResult: ReturnType<typeof render>;
let startServices: AppContextTestRender['startServices'];
beforeEach(() => {
const mockedContext = createAppRootMockRenderer();
startServices = mockedContext.startServices;
formProps = {
policy: new FleetPackagePolicyGenerator('seed').generateEndpointPackagePolicy().inputs[0]
@ -48,12 +50,20 @@ describe('Policy Behaviour Protection Card', () => {
});
it('should render the card with expected components', () => {
const { getByTestId } = render();
const { getByTestId, queryByTestId } = render();
expect(getByTestId(testSubj.enableDisableSwitch));
expect(getByTestId(testSubj.protectionPreventRadio));
expect(getByTestId(testSubj.notifyUserCheckbox));
expect(getByTestId(testSubj.rulesCallout));
expect(queryByTestId(testSubj.reputationServiceCheckbox)).not.toBeInTheDocument();
});
it('should show reputation service section for cloud user', () => {
startServices.cloud!.isCloudEnabled = true;
const { getByTestId } = render();
expect(getByTestId(testSubj.reputationServiceCheckbox));
});
it('should show supported OS values', () => {
@ -84,79 +94,93 @@ describe('Policy Behaviour Protection Card', () => {
});
describe('and displayed in View mode', () => {
const cardTextContent = (
args: {
enabled?: boolean;
reputationServices?: boolean;
notifyUser?: boolean;
prebuiltRules?: boolean;
} = {}
) => {
const defaults = {
enabled: true,
reputationServices: false,
notifyUser: true,
prebuiltRules: false,
};
const config = { ...defaults, ...args };
return [
'Type',
'Malicious behavior',
'Operating system',
'Windows, Mac, Linux ',
`Malicious behavior protections ${config.enabled ? 'enabled' : 'disabled'}`,
'Protection level',
'Prevent',
...(config.reputationServices
? ['Reputation serviceInfo', "Don't use reputation service"]
: []),
'User notification',
'Agent version 7.15+',
...(config.notifyUser
? ['Notify user', 'Notification message', '—']
: ["Don't notify user"]),
...(config.prebuiltRules
? [
'View related detection rules. ',
'Prebuilt rules are tagged “Elastic” on the Detection Rules page.',
]
: ['View related detection rules.']),
].join('');
};
beforeEach(() => {
formProps.mode = 'view';
});
it('should display correctly when overall card is enabled', () => {
const { getByTestId } = render();
expectIsViewOnly(getByTestId(testSubj.card));
expect(getByTestId(testSubj.card)).toHaveTextContent(cardTextContent());
});
it('should display correctly when overall card is enabled for cloud user', () => {
startServices.cloud!.isCloudEnabled = true;
const { getByTestId } = render();
expectIsViewOnly(getByTestId(testSubj.card));
expect(getByTestId(testSubj.card)).toHaveTextContent(
'Type' +
'Malicious behavior' +
'Operating system' +
'Windows, Mac, Linux ' +
'Malicious behavior protections enabled' +
'Protection level' +
'Prevent' +
'User notification' +
'Agent version 7.15+' +
'Notify user' +
'Notification message' +
'—' +
'View related detection rules.'
cardTextContent({ reputationServices: true })
);
});
it('should display correctly when overall card is disabled', () => {
set(formProps.policy, 'windows.behavior_protection.mode', ProtectionModes.off);
const { getByTestId } = render();
expectIsViewOnly(getByTestId(testSubj.card));
expect(getByTestId(testSubj.card)).toHaveTextContent(
exactMatchText(
'Type' +
'Malicious behavior' +
'Operating system' +
'Windows, Mac, Linux ' +
'Malicious behavior protections disabled' +
'Protection level' +
'Prevent' +
'User notification' +
'Agent version 7.15+' +
'Notify user' +
'Notification message' +
'—' +
'View related detection rules. Prebuilt rules are tagged “Elastic” on the Detection Rules page.'
)
cardTextContent({ enabled: false, prebuiltRules: true })
);
});
it('should display correctly when overall card is disabled for cloud user', () => {
startServices.cloud!.isCloudEnabled = true;
set(formProps.policy, 'windows.behavior_protection.mode', ProtectionModes.off);
const { getByTestId } = render();
expectIsViewOnly(getByTestId(testSubj.card));
expect(getByTestId(testSubj.card)).toHaveTextContent(
cardTextContent({
enabled: false,
reputationServices: true,
prebuiltRules: true,
})
);
});
it('should display user notification disabled', () => {
set(formProps.policy, 'windows.popup.behavior_protection.enabled', false);
const { getByTestId } = render();
expectIsViewOnly(getByTestId(testSubj.card));
expect(getByTestId(testSubj.card)).toHaveTextContent(
exactMatchText(
'Type' +
'Malicious behavior' +
'Operating system' +
'Windows, Mac, Linux ' +
'Malicious behavior protections enabled' +
'Protection level' +
'Prevent' +
'User notification' +
'Agent version 7.15+' +
"Don't notify user" +
'View related detection rules. Prebuilt rules are tagged “Elastic” on the Detection Rules page.'
)
);
expect(getByTestId(testSubj.card)).toHaveTextContent(cardTextContent({ notifyUser: false }));
});
});
});

View file

@ -92,6 +92,7 @@ export const BehaviourProtectionCard = memo<BehaviourProtectionCardProps>(
onChange={onChange}
mode={mode}
protection={protection}
data-test-subj={getTestId('reputationService')}
/>
<NotifyUserOption

View file

@ -44,7 +44,7 @@ export const getPolicySettingsFormTestSubjects = (
const antivirusTestSubj = genTestSubj.withPrefix('antivirusRegistration');
const attackSurfaceTestSubj = genTestSubj.withPrefix('attackSurface');
const testSubj = {
return {
form: genTestSubj(),
malware: {
@ -95,6 +95,7 @@ export const getPolicySettingsFormTestSubjects = (
enableDisableSwitch: behaviourTestSubj('enableDisableSwitch'),
protectionPreventRadio: behaviourTestSubj('protectionLevel-preventRadio'),
protectionDetectRadio: behaviourTestSubj('protectionLevel-detectRadio'),
reputationServiceCheckbox: behaviourTestSubj('reputationService-checkbox'),
notifyUserCheckbox: behaviourTestSubj('notifyUser-checkbox'),
osValuesContainer: behaviourTestSubj('osValues'),
rulesCallout: behaviourTestSubj('rulesCallout'),
@ -164,8 +165,6 @@ export const getPolicySettingsFormTestSubjects = (
},
},
};
return testSubj;
};
export const expectIsViewOnly = (ele: HTMLElement): void => {
@ -193,6 +192,7 @@ export const exactMatchText = (text: string): RegExp => {
* @param policy
* @param turnOff
* @param includePopup
* @param includeBlocklist
*/
export const setMalwareMode = (
policy: PolicyConfig,