Skip to content

Commit

Permalink
added negate filter and test
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanSh committed Feb 21, 2023
1 parent 43e7c3a commit 68d4b95
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,52 @@ describe('useNavigateFindings', () => {
const push = jest.fn();
(useHistory as jest.Mock).mockReturnValueOnce({ push });

const filter = { foo: 1 };
const { result } = renderHook(() => useNavigateFindings());

act(() => {
result.current({ foo: 1 });
});

expect(push).toHaveBeenCalledWith({
pathname: '/cloud_security_posture/findings/default',
search:
"cspq=(filters:!((meta:(alias:!n,disabled:!f,key:foo,negate:!f,type:phrase),query:(match_phrase:(foo:1)))),query:(language:kuery,query:''))",
});
expect(push).toHaveBeenCalledTimes(1);
});

it('creates a URL to findings page with correct path and negated filter', () => {
const push = jest.fn();
(useHistory as jest.Mock).mockReturnValueOnce({ push });

const { result } = renderHook(() => useNavigateFindings());

act(() => {
result.current({ filter });
result.current({ foo: { value: 1, negate: true } });
});

expect(push).toHaveBeenCalledWith({
pathname: '/cloud_security_posture/findings/default',
search:
"cspq=(filters:!((meta:(alias:!n,disabled:!f,key:filter,negate:!f,params:(query:(foo:1)),type:phrase),query:(match_phrase:(filter:(foo:1))))),query:(language:kuery,query:''))",
"cspq=(filters:!((meta:(alias:!n,disabled:!f,key:foo,negate:!t,type:phrase),query:(match_phrase:(foo:1)))),query:(language:kuery,query:''))",
});
expect(push).toHaveBeenCalledTimes(1);
});

it('creates a URL to findings resource page with correct path and filter', () => {
const push = jest.fn();
(useHistory as jest.Mock).mockReturnValueOnce({ push });

const filter = { foo: 1 };

const { result } = renderHook(() => useNavigateFindingsByResource());

act(() => {
result.current({ filter });
result.current({ foo: 1 });
});

expect(push).toHaveBeenCalledWith({
pathname: '/cloud_security_posture/findings/resource',
search:
"cspq=(filters:!((meta:(alias:!n,disabled:!f,key:filter,negate:!f,params:(query:(foo:1)),type:phrase),query:(match_phrase:(filter:(foo:1))))),query:(language:kuery,query:''))",
"cspq=(filters:!((meta:(alias:!n,disabled:!f,key:foo,negate:!f,type:phrase),query:(match_phrase:(foo:1)))),query:(language:kuery,query:''))",
});
expect(push).toHaveBeenCalledTimes(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,45 @@ import { findingsNavigation } from '../navigation/constants';
import { encodeQuery } from '../navigation/query_utils';
import { useKibana } from './use_kibana';

const createFilter = (key: string, value: string, negate = false): Filter => ({
meta: {
alias: null,
negate,
disabled: false,
type: 'phrase',
key,
params: { query: value },
},
query: { match_phrase: { [key]: value } },
});
interface NegatedValue {
value: string | number;
negate: boolean;
}

type FilterValue = string | number | NegatedValue;

export type NavFilter = Record<string, FilterValue>;

const createFilter = (key: string, filterValue: FilterValue): Filter => {
let negate = false;
let value = filterValue;
if (typeof filterValue === 'object') {
negate = filterValue.negate;
value = filterValue.value;
}

return {
meta: {
alias: null,
negate,
disabled: false,
type: 'phrase',
key,
},
query: { match_phrase: { [key]: value } },
};
};

const useNavigate = (pathname: string) => {
const history = useHistory();
const { services } = useKibana();

return useCallback(
(filterParams: Record<string, any> = {}) => {
const filters = Object.entries(filterParams).map(([key, value]) => createFilter(key, value));
(filterParams: NavFilter = {}) => {
const filters = Object.entries(filterParams).map(([key, filterValue]) =>
createFilter(key, filterValue)
);

history.push({
pathname,
search: encodeQuery({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
} from '../../../../common/types';
import { RisksTable } from '../compliance_charts/risks_table';
import {
NavFilter,
useNavigateFindings,
useNavigateFindingsByResource,
} from '../../../common/hooks/use_navigate_findings';
Expand All @@ -37,14 +38,12 @@ export const dashboardColumnsGrow: Record<string, EuiFlexItemProps['grow']> = {
third: 8,
};

export const getPolicyTemplateQuery = (policyTemplate: PosturePolicyTemplate) => {
if (policyTemplate === CSPM_POLICY_TEMPLATE)
export const getPolicyTemplateQuery = (policyTemplate: PosturePolicyTemplate): NavFilter => {
if (policyTemplate === CSPM_POLICY_TEMPLATE) {
return { 'rule.benchmark.posture_type': CSPM_POLICY_TEMPLATE };
// TODO: FIX POSTURE_TYPE
if (policyTemplate === KSPM_POLICY_TEMPLATE)
return { 'rule.benchmark.posture_type': KSPM_POLICY_TEMPLATE };
}

return {};
return { 'rule.benchmark.posture_type': { value: CSPM_POLICY_TEMPLATE, negate: true } };
};

export const SummarySection = ({
Expand Down

0 comments on commit 68d4b95

Please sign in to comment.