Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Nov 2, 2022
1 parent 2a61a1f commit 0f90bb7
Show file tree
Hide file tree
Showing 17 changed files with 674 additions and 57 deletions.
21 changes: 19 additions & 2 deletions x-pack/plugins/cases/server/attachment_framework/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ import type {
CommentRequestPersistableStateType,
} from '../../common/api';
import { ExternalReferenceStorageType } from '../../common/api';
import { ExternalReferenceAttachmentTypeRegistry } from './external_reference_registry';
import { PersistableStateAttachmentTypeRegistry } from './persistable_state_registry';
import type { PersistableStateAttachmentTypeSetup, PersistableStateAttachmentState } from './types';
import type {
PersistableStateAttachmentTypeSetup,
PersistableStateAttachmentState,
ExternalReferenceAttachmentType,
} from './types';

export const getPersistableAttachment = (): PersistableStateAttachmentTypeSetup => ({
id: '.test',
Expand All @@ -42,6 +47,10 @@ export const getPersistableAttachment = (): PersistableStateAttachmentTypeSetup
}),
});

export const getExternalReferenceAttachment = (): ExternalReferenceAttachmentType => ({
id: '.test',
});

export const externalReferenceAttachmentSO = {
type: CommentType.externalReference as const,
externalReferenceId: 'my-id',
Expand Down Expand Up @@ -130,10 +139,18 @@ export const externalReferenceAttachmentSOAttributesWithoutRefs = omit(
'externalReferenceId'
);

export const getPersistableStateAttachmentTypeRegistry =
export const createPersistableStateAttachmentTypeRegistryMock =
(): PersistableStateAttachmentTypeRegistry => {
const persistableStateAttachmentTypeRegistry = new PersistableStateAttachmentTypeRegistry();
persistableStateAttachmentTypeRegistry.register(getPersistableAttachment());

return persistableStateAttachmentTypeRegistry;
};

export const createExternalReferenceAttachmentTypeRegistryMock =
(): ExternalReferenceAttachmentTypeRegistry => {
const externalReferenceAttachmentTypeRegistry = new ExternalReferenceAttachmentTypeRegistry();
externalReferenceAttachmentTypeRegistry.register(getExternalReferenceAttachment());

return externalReferenceAttachmentTypeRegistry;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { CommentType, SECURITY_SOLUTION_OWNER } from '../../common';
import {
getPersistableStateAttachmentTypeRegistry,
createPersistableStateAttachmentTypeRegistryMock,
persistableStateAttachment,
persistableStateAttachmentAttributes,
} from './mocks';
Expand All @@ -19,7 +19,7 @@ import {
} from './so_references';

describe('Persistable state SO references', () => {
const persistableStateAttachmentTypeRegistry = getPersistableStateAttachmentTypeRegistry();
const persistableStateAttachmentTypeRegistry = createPersistableStateAttachmentTypeRegistryMock();
const references = [
{
id: 'testRef',
Expand Down
80 changes: 80 additions & 0 deletions x-pack/plugins/cases/server/client/cases/create.test.ts
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 x-pack/plugins/cases/server/client/cases/update.test.ts
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();
});
});
});
Loading

0 comments on commit 0f90bb7

Please sign in to comment.