Skip to content

Commit

Permalink
renamed param on that indicates fleet extension state
Browse files Browse the repository at this point in the history
added unit test for CspPolicyTemplateForm component
  • Loading branch information
alexreal1314 committed Feb 20, 2025
1 parent 8f832f2 commit fda66d9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
const handleExtensionViewOnChange = useCallback<
PackagePolicyEditExtensionComponentProps['onChange']
>(
({ isValid, updatedPolicy, fetchedPackagePolicies }) => {
({ isValid, updatedPolicy, isLoadedPackagePolicies }) => {
updatePackagePolicy(updatedPolicy);
setIsFetchedPackagePolicies(fetchedPackagePolicies);
setIsFetchedPackagePolicies(isLoadedPackagePolicies);
setFormState((prevState) => {
if (prevState === 'VALID' && !isValid) {
return 'INVALID';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface PackagePolicyEditExtensionComponentProps {
isValid: boolean;
/** The updated Integration Policy to be merged back and included in the API call */
updatedPolicy: Partial<NewPackagePolicy>;
fetchedPackagePolicies?: boolean;
isLoadedPackagePolicies?: boolean;
}) => void;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ describe('<CspPolicyTemplateForm />', () => {
edit = false,
packageInfo = {} as PackageInfo,
isAgentlessEnabled,
integrationToEnable,
}: {
edit?: boolean;
newPolicy: NewPackagePolicy;
packageInfo?: PackageInfo;
onChange?: jest.Mock<void, [NewPackagePolicy]>;
isAgentlessEnabled?: boolean;
integrationToEnable?: string;
}) => {
const { AppWrapper: FleetAppWrapper } = createFleetTestRendererMock();
return (
Expand All @@ -143,6 +145,7 @@ describe('<CspPolicyTemplateForm />', () => {
packageInfo={packageInfo}
isEditPage={true}
isAgentlessEnabled={isAgentlessEnabled}
integrationToEnable={integrationToEnable}
/>
)}
{!edit && (
Expand All @@ -152,6 +155,7 @@ describe('<CspPolicyTemplateForm />', () => {
packageInfo={packageInfo}
isEditPage={false}
isAgentlessEnabled={isAgentlessEnabled}
integrationToEnable={integrationToEnable}
/>
)}
</TestProvider>
Expand Down Expand Up @@ -425,6 +429,89 @@ describe('<CspPolicyTemplateForm />', () => {
});
});

it('KSPM - calls onChange with isLoadedPackagePolicies the second time after increment of package version', () => {
const policy = getMockPolicyK8s();

// enable all inputs of a policy template, same as fleet does
policy.inputs = policy.inputs.map((input) => ({
...input,
enabled: input.policy_template === 'kspm',
}));
policy.name = 'cloud_security_posture-1';

(useParams as jest.Mock).mockReturnValue({
integration: 'kspm',
});

(useCspSetupStatusApi as jest.Mock).mockImplementation(() =>
createReactQueryResponseWithRefetch({
status: 'success',
data: {
kspm: { status: 'not-deployed', healthyAgents: 0, installedPackagePolicies: 1 },
},
})
);

(usePackagePolicyList as jest.Mock).mockImplementation(() =>
createReactQueryResponseWithRefetch({
status: 'success',
data: {
items: [getPosturePolicy(getMockPolicyEKS(), CLOUDBEAT_EKS)],
},
})
);

render(
<WrappedComponent
newPolicy={policy}
packageInfo={{ name: 'kspm' } as PackageInfo}
onChange={onChange}
integrationToEnable="kspm"
/>
);

// 1st call happens on mount and selects the default policy template enabled input
expect(onChange).nthCalledWith(1, {
isLoadedPackagePolicies: undefined,
isValid: true,
updatedPolicy: {
...getMockPolicyK8s(),
name: 'cloud_security_posture-1',
},
});

// 2nd call happens on mount and increments kspm template enabled input
expect(onChange).nthCalledWith(2, {
isLoadedPackagePolicies: undefined,
isValid: true,
updatedPolicy: {
...getMockPolicyK8s(),
inputs: policy.inputs.map((input) => ({
...input,
enabled: input.type === 'cloudbeat/cis_k8s',
})),
name: 'cloud_security_posture-1',
},
});

/*
3rd call happens when policies are fetched and the package version is incremented
in that case isLoadedPackagePolicies is set to 'true'
*/
expect(onChange).nthCalledWith(3, {
isLoadedPackagePolicies: true,
isValid: true,
updatedPolicy: {
...getMockPolicyK8s(),
inputs: policy.inputs.map((input) => ({
...input,
enabled: input.policy_template === 'kspm',
})),
name: 'kspm-1',
},
});
});

it('selects default VULN_MGMT input selector', () => {
const policy = getMockPolicyVulnMgmtAWS();
// enable all inputs of a policy template, same as fleet does
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const AwsAccountTypeSelect = ({
}: {
input: Extract<NewPackagePolicyPostureInput, { type: 'cloudbeat/cis_aws' }>;
newPolicy: NewPackagePolicy;
updatePolicy: (updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
updatePolicy: (updatedPolicy: NewPackagePolicy, isLoadedPackagePolicies?: boolean) => void;
packageInfo: PackageInfo;
disabled: boolean;
}) => {
Expand Down Expand Up @@ -303,7 +303,7 @@ const GcpAccountTypeSelect = ({
}: {
input: Extract<NewPackagePolicyPostureInput, { type: 'cloudbeat/cis_gcp' }>;
newPolicy: NewPackagePolicy;
updatePolicy: (updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
updatePolicy: (updatedPolicy: NewPackagePolicy, isLoadedPackagePolicies?: boolean) => void;
packageInfo: PackageInfo;
disabled: boolean;
}) => {
Expand Down Expand Up @@ -443,7 +443,7 @@ const AzureAccountTypeSelect = ({
}: {
input: Extract<NewPackagePolicyPostureInput, { type: 'cloudbeat/cis_azure' }>;
newPolicy: NewPackagePolicy;
updatePolicy: (updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
updatePolicy: (updatedPolicy: NewPackagePolicy, isLoadedPackagePolicies?: boolean) => void;
disabled: boolean;
packageInfo: PackageInfo;
setupTechnology: SetupTechnology;
Expand Down Expand Up @@ -548,7 +548,7 @@ const useEnsureDefaultNamespace = ({
}: {
newPolicy: NewPackagePolicy;
input: NewPackagePolicyPostureInput;
updatePolicy: (policy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
updatePolicy: (policy: NewPackagePolicy, isLoadedPackagePolicies?: boolean) => void;
}) => {
useEffect(() => {
if (newPolicy.namespace === POSTURE_NAMESPACE) return;
Expand All @@ -572,7 +572,7 @@ const usePolicyTemplateInitialName = ({
integration: CloudSecurityPolicyTemplate | undefined;
newPolicy: NewPackagePolicy;
packagePolicyList: PackagePolicy[] | undefined;
updatePolicy: (policy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
updatePolicy: (policy: NewPackagePolicy, isLoadedPackagePolicies?: boolean) => void;
setCanFetchIntegration: (canFetch: boolean) => void;
}) => {
useEffect(() => {
Expand Down Expand Up @@ -628,7 +628,7 @@ const useCloudFormationTemplate = ({
}: {
packageInfo: PackageInfo;
newPolicy: NewPackagePolicy;
updatePolicy: (policy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => void;
updatePolicy: (policy: NewPackagePolicy, isLoadedPackagePolicies?: boolean) => void;
}) => {
useEffect(() => {
const templateUrl = getVulnMgmtCloudFormationDefaultValue(packageInfo);
Expand Down Expand Up @@ -743,8 +743,8 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
};

const updatePolicy = useCallback(
(updatedPolicy: NewPackagePolicy, fetchedPackagePolicies?: boolean) => {
onChange({ isValid, updatedPolicy, fetchedPackagePolicies });
(updatedPolicy: NewPackagePolicy, isLoadedPackagePolicies?: boolean) => {
onChange({ isValid, updatedPolicy, isLoadedPackagePolicies });
},
[onChange, isValid]
);
Expand Down

0 comments on commit fda66d9

Please sign in to comment.