Skip to content

Commit

Permalink
Addressing PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Jan 18, 2022
1 parent cdf5016 commit 261a2a6
Show file tree
Hide file tree
Showing 7 changed files with 477 additions and 105 deletions.
9 changes: 9 additions & 0 deletions x-pack/plugins/cases/common/utils/user_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ export function isUpdateConnector(action?: string, actionFields?: string[]): boo
export function isPush(action?: string, actionFields?: string[]): boolean {
return action === 'push-to-service' && actionFields != null && actionFields.includes('pushed');
}

export function isCreateComment(action?: string, actionFields?: string[]): boolean {
return (
action === 'create' &&
actionFields !== null &&
actionFields !== undefined &&
actionFields.includes('comment')
);
}
195 changes: 102 additions & 93 deletions x-pack/plugins/cases/public/components/user_action_tree/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,121 +249,130 @@ describe('User action tree helpers', () => {
});
});

describe.each([
['getRuleId', getRuleId],
['getRuleName', getRuleName],
])('%s', (name, funcToExec) => {
it('returns the first entry in the comment field', () => {
const comment = {
rule: {
id: ['1', '2'],
name: ['1', '2'],
},
} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toEqual('1');
});
describe('rule getters', () => {
describe.each([
['getRuleId', getRuleId],
['getRuleName', getRuleName],
])('%s null checks', (name, funcToExec) => {
it('returns null if the comment field is an empty string', () => {
const comment = {
rule: {
id: '',
name: '',
},
} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toBeNull();
});

it('returns null if the comment field is an empty string', () => {
const comment = {
rule: {
id: '',
name: '',
},
} as unknown as CommentRequestAlertType;
it('returns null if the comment field is an empty string in an array', () => {
const comment = {
rule: {
id: [''],
name: [''],
},
} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toBeNull();
});
expect(funcToExec(comment)).toBeNull();
});

it('returns null if the comment field is an empty string in an array', () => {
const comment = {
rule: {
id: [''],
name: [''],
},
} as unknown as CommentRequestAlertType;
it('returns null if the comment does not have a rule field', () => {
const comment = {} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toBeNull();
});
expect(funcToExec(comment)).toBeNull();
});

it('returns null if the comment does not have a rule field', () => {
const comment = {} as unknown as CommentRequestAlertType;
it('returns null if the signals and alert field is an empty string', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '', name: '' } },
kibana: { alert: { rule: { uuid: '', name: '' } } },
} as unknown as Ecs;

expect(funcToExec(comment)).toBeNull();
expect(funcToExec(comment, alert)).toBeNull();
});
});

it('returns signal field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = { signal: { rule: { id: '1', name: '1' } } } as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});
describe.each([
['getRuleId', getRuleId, '1'],
['getRuleName', getRuleName, 'Rule name1'],
])('%s', (name, funcToExec, expectedResult) => {
it('returns the first entry in the comment field', () => {
const comment = {
rule: {
id: ['1', '2'],
name: ['Rule name1', 'Rule name2'],
},
} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toEqual(expectedResult);
});

it('returns kibana alert field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = { kibana: { alert: { rule: { uuid: '1', name: '1' } } } } as unknown as Ecs;
it('returns signal field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = { signal: { rule: { id: '1', name: 'Rule name1' } } } as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});
expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns signal field even when kibana alert field is defined', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: 'signal', name: 'signal' } },
kibana: { alert: { rule: { uuid: '1', name: '1' } } },
} as unknown as Ecs;
it('returns kibana alert field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
kibana: { alert: { rule: { uuid: '1', name: 'Rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('signal');
});
expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns the first entry in the signals field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: ['signal1', 'signal2'], name: ['signal1', 'signal2'] } },
kibana: { alert: { rule: { uuid: '1', name: '1' } } },
} as unknown as Ecs;
it('returns signal field even when kibana alert field is defined', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '1', name: 'Rule name1' } },
kibana: { alert: { rule: { uuid: 'rule id1', name: 'other rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('signal1');
});
expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns the alert field if the signals field is an empty string', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '', name: '' } },
kibana: { alert: { rule: { uuid: '1', name: '1' } } },
} as unknown as Ecs;
it('returns the first entry in the signals field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '1', name: 'Rule name1' } },
kibana: { alert: { rule: { uuid: 'rule id1', name: 'other rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});
expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns the alert field if the signals field is an empty string in an array', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: [''], name: [''] } },
kibana: { alert: { rule: { uuid: '1', name: '1' } } },
} as unknown as Ecs;
it('returns the alert field if the signals field is an empty string', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '', name: '' } },
kibana: { alert: { rule: { uuid: '1', name: 'Rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});
expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns the alert field first item if the signals field is an empty string in an array', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: [''], name: [''] } },
kibana: { alert: { rule: { uuid: ['1', '2'], name: ['1', '2'] } } },
} as unknown as Ecs;
it('returns the alert field if the signals field is an empty string in an array', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: [''], name: [''] } },
kibana: { alert: { rule: { uuid: '1', name: 'Rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});
expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns null if the signals and alert field is an empty string', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '', name: '' } },
kibana: { alert: { rule: { uuid: '', name: '' } } },
} as unknown as Ecs;
it('returns the alert field first item if the signals field is an empty string in an array', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: [''], name: [''] } },
kibana: { alert: { rule: { uuid: ['1', '2'], name: ['Rule name1', 'Rule name2'] } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toBeNull();
expect(funcToExec(comment, alert)).toEqual(expectedResult);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,7 @@ export interface Alert {
}

export const getFirstItem = (items?: string | string[] | null): string | null => {
if (items == null) {
return null;
}

return Array.isArray(items) ? (items.length > 0 ? items[0] : null) : items;
return Array.isArray(items) ? items[0] : items ?? null;
};

export const getRuleId = (comment: CommentRequestAlertType, alertData?: Ecs): string | null =>
Expand Down
Loading

0 comments on commit 261a2a6

Please sign in to comment.