-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
674 additions
and
57 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 { SECURITY_SOLUTION_OWNER } from '../../../common'; | ||
import { CaseSeverity, ConnectorTypes } from '../../../common/api'; | ||
import { mockCases } from '../../mocks'; | ||
import { createCasesClientMockArgs } from '../mocks'; | ||
import { create } from './create'; | ||
|
||
describe('create', () => { | ||
const theCase = { | ||
title: 'My Case', | ||
tags: [], | ||
description: 'testing sir', | ||
connector: { | ||
id: '.none', | ||
name: 'None', | ||
type: ConnectorTypes.none, | ||
fields: null, | ||
}, | ||
settings: { syncAlerts: true }, | ||
severity: CaseSeverity.LOW, | ||
owner: SECURITY_SOLUTION_OWNER, | ||
assignees: [{ uid: '1' }], | ||
}; | ||
|
||
const caseSO = mockCases[0]; | ||
|
||
describe('Assignees', () => { | ||
const clientArgs = createCasesClientMockArgs(); | ||
clientArgs.services.caseService.postNewCase.mockResolvedValue(caseSO); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('notifies single assignees', async () => { | ||
await create(theCase, clientArgs); | ||
|
||
expect(clientArgs.services.notificationService.notifyAssignees).toHaveBeenCalledWith({ | ||
assignees: theCase.assignees, | ||
theCase: caseSO, | ||
}); | ||
}); | ||
|
||
it('notifies multiple assignees', async () => { | ||
await create({ ...theCase, assignees: [{ uid: '1' }, { uid: '2' }] }, clientArgs); | ||
|
||
expect(clientArgs.services.notificationService.notifyAssignees).toHaveBeenCalledWith({ | ||
assignees: [{ uid: '1' }, { uid: '2' }], | ||
theCase: caseSO, | ||
}); | ||
}); | ||
|
||
it('does not notify when there are no assignees', async () => { | ||
await create({ ...theCase, assignees: [] }, clientArgs); | ||
|
||
expect(clientArgs.services.notificationService.notifyAssignees).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not notify the current user', async () => { | ||
await create( | ||
{ | ||
...theCase, | ||
assignees: [{ uid: '1' }, { uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0' }], | ||
}, | ||
clientArgs | ||
); | ||
|
||
expect(clientArgs.services.notificationService.notifyAssignees).toHaveBeenCalledWith({ | ||
assignees: [{ uid: '1' }], | ||
theCase: caseSO, | ||
}); | ||
}); | ||
}); | ||
}); |
245 changes: 245 additions & 0 deletions
245
x-pack/plugins/cases/server/client/cases/update.test.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,245 @@ | ||
/* | ||
* 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 { mockCases } from '../../mocks'; | ||
import { createCasesClientMockArgs } from '../mocks'; | ||
import { update } from './update'; | ||
|
||
describe('update', () => { | ||
const cases = { | ||
cases: [ | ||
{ | ||
id: mockCases[0].id, | ||
version: mockCases[0].version ?? '', | ||
assignees: [{ uid: '1' }], | ||
}, | ||
], | ||
}; | ||
|
||
describe('Assignees', () => { | ||
const clientArgs = createCasesClientMockArgs(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
clientArgs.services.caseService.getCases.mockResolvedValue({ saved_objects: mockCases }); | ||
clientArgs.services.caseService.getAllCaseComments.mockResolvedValue({ | ||
saved_objects: [], | ||
total: 0, | ||
per_page: 10, | ||
page: 1, | ||
}); | ||
|
||
clientArgs.services.caseService.patchCases.mockResolvedValue({ | ||
saved_objects: [{ ...mockCases[0], attributes: { assignees: cases.cases[0].assignees } }], | ||
}); | ||
}); | ||
|
||
it('notifies an assignee', async () => { | ||
await update(cases, clientArgs); | ||
|
||
expect(clientArgs.services.notificationService.bulkNotifyAssignees).toHaveBeenCalledWith([ | ||
{ | ||
assignees: [{ uid: '1' }], | ||
theCase: { | ||
...mockCases[0], | ||
attributes: { ...mockCases[0].attributes, assignees: [{ uid: '1' }] }, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('does not notify if the case does not exist', async () => { | ||
expect.assertions(1); | ||
|
||
await expect( | ||
update( | ||
{ | ||
cases: [ | ||
{ | ||
id: 'not-exists', | ||
version: '123', | ||
assignees: [{ uid: '1' }], | ||
}, | ||
], | ||
}, | ||
clientArgs | ||
) | ||
).rejects.toThrow( | ||
'Failed to update case, ids: [{"id":"not-exists","version":"123"}]: Error: These cases not-exists do not exist. Please check you have the correct ids.' | ||
); | ||
}); | ||
|
||
it('does not notify if the case is patched with the same assignee', async () => { | ||
clientArgs.services.caseService.getCases.mockResolvedValue({ | ||
saved_objects: [ | ||
{ | ||
...mockCases[0], | ||
attributes: { ...mockCases[0].attributes, assignees: [{ uid: '1' }] }, | ||
}, | ||
], | ||
}); | ||
|
||
await expect(update(cases, clientArgs)).rejects.toThrow( | ||
'Failed to update case, ids: [{"id":"mock-id-1","version":"WzAsMV0="}]: Error: All update fields are identical to current version.' | ||
); | ||
}); | ||
|
||
it('notifies only new users', async () => { | ||
clientArgs.services.caseService.getCases.mockResolvedValue({ | ||
saved_objects: [ | ||
{ | ||
...mockCases[0], | ||
attributes: { ...mockCases[0].attributes, assignees: [{ uid: '1' }] }, | ||
}, | ||
], | ||
}); | ||
|
||
clientArgs.services.caseService.patchCases.mockResolvedValue({ | ||
saved_objects: [ | ||
{ | ||
...mockCases[0], | ||
attributes: { assignees: [{ uid: '1' }, { uid: '2' }, { uid: '3' }] }, | ||
}, | ||
], | ||
}); | ||
|
||
await update( | ||
{ | ||
cases: [ | ||
{ | ||
id: mockCases[0].id, | ||
version: mockCases[0].version ?? '', | ||
assignees: [{ uid: '1' }, { uid: '2' }, { uid: '3' }], | ||
}, | ||
], | ||
}, | ||
clientArgs | ||
); | ||
|
||
expect(clientArgs.services.notificationService.bulkNotifyAssignees).toHaveBeenCalledWith([ | ||
{ | ||
assignees: [{ uid: '2' }, { uid: '3' }], | ||
theCase: { | ||
...mockCases[0], | ||
attributes: { | ||
...mockCases[0].attributes, | ||
assignees: [{ uid: '1' }, { uid: '2' }, { uid: '3' }], | ||
}, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('does not notify when deleting users', async () => { | ||
clientArgs.services.caseService.getCases.mockResolvedValue({ | ||
saved_objects: [ | ||
{ | ||
...mockCases[0], | ||
attributes: { ...mockCases[0].attributes, assignees: [{ uid: '1' }, { uid: '2' }] }, | ||
}, | ||
], | ||
}); | ||
|
||
clientArgs.services.caseService.patchCases.mockResolvedValue({ | ||
saved_objects: [{ ...mockCases[0], attributes: { assignees: [{ uid: '1' }] } }], | ||
}); | ||
|
||
await update( | ||
{ | ||
cases: [ | ||
{ | ||
id: mockCases[0].id, | ||
version: mockCases[0].version ?? '', | ||
assignees: [{ uid: '1' }], | ||
}, | ||
], | ||
}, | ||
clientArgs | ||
); | ||
|
||
expect(clientArgs.services.notificationService.bulkNotifyAssignees).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not notify the current user', async () => { | ||
clientArgs.services.caseService.getCases.mockResolvedValue({ | ||
saved_objects: [ | ||
{ | ||
...mockCases[0], | ||
attributes: { ...mockCases[0].attributes, assignees: [{ uid: '1' }] }, | ||
}, | ||
], | ||
}); | ||
|
||
clientArgs.services.caseService.patchCases.mockResolvedValue({ | ||
saved_objects: [ | ||
{ | ||
...mockCases[0], | ||
attributes: { | ||
assignees: [{ uid: '2' }, { uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0' }], | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
await update( | ||
{ | ||
cases: [ | ||
{ | ||
id: mockCases[0].id, | ||
version: mockCases[0].version ?? '', | ||
assignees: [{ uid: '2' }, { uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0' }], | ||
}, | ||
], | ||
}, | ||
clientArgs | ||
); | ||
|
||
expect(clientArgs.services.notificationService.bulkNotifyAssignees).toHaveBeenCalledWith([ | ||
{ | ||
assignees: [{ uid: '2' }], | ||
theCase: { | ||
...mockCases[0], | ||
attributes: { | ||
...mockCases[0].attributes, | ||
assignees: [{ uid: '2' }, { uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0' }], | ||
}, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('does not notify when there are no new assignees', async () => { | ||
clientArgs.services.caseService.getCases.mockResolvedValue({ | ||
saved_objects: [ | ||
{ | ||
...mockCases[0], | ||
attributes: { ...mockCases[0].attributes, assignees: [{ uid: '1' }] }, | ||
}, | ||
], | ||
}); | ||
|
||
await update( | ||
{ | ||
cases: [ | ||
{ | ||
id: mockCases[0].id, | ||
version: mockCases[0].version ?? '', | ||
assignees: [{ uid: '1' }, { uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0' }], | ||
}, | ||
], | ||
}, | ||
clientArgs | ||
); | ||
|
||
/** | ||
* Current user is filtered out. Assignee with uid=1 should not be | ||
* notified because it was already assigned to the case. | ||
*/ | ||
expect(clientArgs.services.notificationService.bulkNotifyAssignees).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.