Skip to content

Commit

Permalink
Fix tests and types
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Nov 5, 2023
1 parent fc78f28 commit 2740099
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 80 deletions.
16 changes: 9 additions & 7 deletions x-pack/plugins/cases/server/client/attachments/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ export async function deleteComment(
const attachmentRequestAttributes = decodeOrThrow(AttachmentRequestRt)(attachment.attributes);

await userActionService.creator.createUserAction({
type: UserActionTypes.comment,
action: UserActionActions.delete,
caseId: id,
attachmentId: attachmentID,
payload: { attachment: attachmentRequestAttributes },
user,
owner: attachment.attributes.owner,
userAction: {
type: UserActionTypes.comment,
action: UserActionActions.delete,
caseId: id,
attachmentId: attachmentID,
payload: { attachment: attachmentRequestAttributes },
user,
owner: attachment.attributes.owner,
},
});

await handleAlerts({ alertsService, attachments: [attachment.attributes], caseId: id });
Expand Down
24 changes: 14 additions & 10 deletions x-pack/plugins/cases/server/client/cases/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,13 @@ export const push = async (

if (shouldMarkAsClosed) {
await userActionService.creator.createUserAction({
type: UserActionTypes.status,
payload: { status: CaseStatuses.closed },
user,
caseId,
owner: myCase.attributes.owner,
userAction: {
type: UserActionTypes.status,
payload: { status: CaseStatuses.closed },
user,
caseId,
owner: myCase.attributes.owner,
},
refresh: false,
});

Expand All @@ -275,11 +277,13 @@ export const push = async (
}

await userActionService.creator.createUserAction({
type: UserActionTypes.pushed,
payload: { externalService },
user,
caseId,
owner: myCase.attributes.owner,
userAction: {
type: UserActionTypes.pushed,
payload: { externalService },
user,
caseId,
owner: myCase.attributes.owner,
},
});

/* End of update case with push information */
Expand Down
34 changes: 19 additions & 15 deletions x-pack/plugins/cases/server/common/models/case_with_comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,15 @@ export class CaseCommentModel {
const { id, version, ...queryRestAttributes } = updateRequest;

await this.params.services.userActionService.creator.createUserAction({
type: UserActionTypes.comment,
action: UserActionActions.update,
caseId: this.caseInfo.id,
attachmentId: comment.id,
payload: { attachment: queryRestAttributes },
user: this.params.user,
owner,
userAction: {
type: UserActionTypes.comment,
action: UserActionActions.update,
caseId: this.caseInfo.id,
attachmentId: comment.id,
payload: { attachment: queryRestAttributes },
user: this.params.user,
owner,
},
});
}

Expand Down Expand Up @@ -403,15 +405,17 @@ export class CaseCommentModel {
req: AttachmentRequest
) {
await this.params.services.userActionService.creator.createUserAction({
type: UserActionTypes.comment,
action: UserActionActions.create,
caseId: this.caseInfo.id,
attachmentId: comment.id,
payload: {
attachment: req,
userAction: {
type: UserActionTypes.comment,
action: UserActionActions.create,
caseId: this.caseInfo.id,
attachmentId: comment.id,
payload: {
attachment: req,
},
user: this.params.user,
owner: comment.attributes.owner,
},
user: this.params.user,
owner: comment.attributes.owner,
});
}

Expand Down
11 changes: 7 additions & 4 deletions x-pack/plugins/cases/server/services/attachments/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import {
persistableStateAttachmentAttributes,
persistableStateAttachmentAttributesWithoutInjectedId,
} from '../../attachment_framework/mocks';
import { createAlertAttachment, createErrorSO, createUserAttachment } from './test_utils';
import { createAlertAttachment, createUserAttachment } from './test_utils';
import { AttachmentType } from '../../../common/types/domain';
import { createSOFindResponse } from '../test_utils';
import { createErrorSO, createSOFindResponse } from '../test_utils';
import { CASE_COMMENT_SAVED_OBJECT } from '../../../common';

describe('AttachmentService', () => {
const unsecuredSavedObjectsClient = savedObjectsClientMock.create();
Expand Down Expand Up @@ -135,9 +136,10 @@ describe('AttachmentService', () => {
it('returns error objects unmodified', async () => {
const userAttachment = createUserAttachment({ foo: 'bar' });

const errorResponseObj = createErrorSO();
const errorResponseObj = createErrorSO(CASE_COMMENT_SAVED_OBJECT);

unsecuredSavedObjectsClient.bulkCreate.mockResolvedValue({
// @ts-expect-error: SO client types are wrong
saved_objects: [errorResponseObj, userAttachment],
});

Expand Down Expand Up @@ -411,9 +413,10 @@ describe('AttachmentService', () => {
it('returns error objects unmodified', async () => {
const userAttachment = createUserAttachment({ foo: 'bar' });

const errorResponseObj = createErrorSO();
const errorResponseObj = createErrorSO(CASE_COMMENT_SAVED_OBJECT);

unsecuredSavedObjectsClient.bulkUpdate.mockResolvedValue({
// @ts-expect-error: SO client types are wrong
saved_objects: [errorResponseObj, userAttachment],
});

Expand Down
88 changes: 54 additions & 34 deletions x-pack/plugins/cases/server/services/user_actions/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ describe('CaseUserActionService', () => {
describe('create case', () => {
it('creates a create case user action', async () => {
await service.creator.createUserAction({
...commonArgs,
payload: casePayload,
type: UserActionTypes.create_case,
userAction: {
...commonArgs,
payload: casePayload,
type: UserActionTypes.create_case,
},
});

expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledWith(
Expand Down Expand Up @@ -159,9 +161,11 @@ describe('CaseUserActionService', () => {

it('logs a create case user action', async () => {
await service.creator.createUserAction({
...commonArgs,
payload: casePayload,
type: UserActionTypes.create_case,
userAction: {
...commonArgs,
payload: casePayload,
type: UserActionTypes.create_case,
},
});

expect(mockAuditLogger.log).toBeCalledTimes(1);
Expand Down Expand Up @@ -193,9 +197,11 @@ describe('CaseUserActionService', () => {
describe('status', () => {
it('creates an update status user action', async () => {
await service.creator.createUserAction({
...commonArgs,
payload: { status: CaseStatuses.closed },
type: UserActionTypes.status,
userAction: {
...commonArgs,
payload: { status: CaseStatuses.closed },
type: UserActionTypes.status,
},
});

expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledWith(
Expand All @@ -218,9 +224,11 @@ describe('CaseUserActionService', () => {

it('logs an update status user action', async () => {
await service.creator.createUserAction({
...commonArgs,
payload: { status: CaseStatuses.closed },
type: UserActionTypes.status,
userAction: {
...commonArgs,
payload: { status: CaseStatuses.closed },
type: UserActionTypes.status,
},
});

expect(mockAuditLogger.log).toBeCalledTimes(1);
Expand Down Expand Up @@ -253,9 +261,11 @@ describe('CaseUserActionService', () => {
describe('severity', () => {
it('creates an update severity user action', async () => {
await service.creator.createUserAction({
...commonArgs,
payload: { severity: CaseSeverity.MEDIUM },
type: UserActionTypes.severity,
userAction: {
...commonArgs,
payload: { severity: CaseSeverity.MEDIUM },
type: UserActionTypes.severity,
},
});

expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledWith(
Expand All @@ -278,9 +288,11 @@ describe('CaseUserActionService', () => {

it('logs an update severity user action', async () => {
await service.creator.createUserAction({
...commonArgs,
payload: { severity: CaseSeverity.MEDIUM },
type: UserActionTypes.severity,
userAction: {
...commonArgs,
payload: { severity: CaseSeverity.MEDIUM },
type: UserActionTypes.severity,
},
});

expect(mockAuditLogger.log).toBeCalledTimes(1);
Expand Down Expand Up @@ -313,9 +325,11 @@ describe('CaseUserActionService', () => {
describe('push', () => {
it('creates a push user action', async () => {
await service.creator.createUserAction({
...commonArgs,
payload: { externalService },
type: UserActionTypes.pushed,
userAction: {
...commonArgs,
payload: { externalService },
type: UserActionTypes.pushed,
},
});

expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledWith(
Expand Down Expand Up @@ -357,9 +371,11 @@ describe('CaseUserActionService', () => {

it('logs a push user action', async () => {
await service.creator.createUserAction({
...commonArgs,
payload: { externalService },
type: UserActionTypes.pushed,
userAction: {
...commonArgs,
payload: { externalService },
type: UserActionTypes.pushed,
},
});

expect(mockAuditLogger.log).toBeCalledTimes(1);
Expand Down Expand Up @@ -396,11 +412,13 @@ describe('CaseUserActionService', () => {
[UserActionActions.update],
])('creates a comment user action of action: %s', async (action) => {
await service.creator.createUserAction({
...commonArgs,
type: UserActionTypes.comment,
action,
attachmentId: 'test-id',
payload: { attachment: comment },
userAction: {
...commonArgs,
type: UserActionTypes.comment,
action,
attachmentId: 'test-id',
payload: { attachment: comment },
},
});

expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledWith(
Expand Down Expand Up @@ -438,11 +456,13 @@ describe('CaseUserActionService', () => {
[UserActionActions.update],
])('logs a comment user action of action: %s', async (action) => {
await service.creator.createUserAction({
...commonArgs,
type: UserActionTypes.comment,
action,
attachmentId: 'test-id',
payload: { attachment: comment },
userAction: {
...commonArgs,
type: UserActionTypes.comment,
action,
attachmentId: 'test-id',
payload: { attachment: comment },
},
});

expect(mockAuditLogger.log).toBeCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { set, unset } from 'lodash';
import { createConnectorObject } from '../../test_utils';
import { UserActionPersister } from './create';
import { createUserActionSO } from '../test_utils';
import type { BulkCreateAttachmentUserAction, CreateUserActionClient } from '../types';
import type { BulkCreateAttachmentUserAction, CreateUserActionArgs } from '../types';
import type { UserActionPersistedAttributes } from '../../../common/types/user_actions';
import {
getAssigneesAddedRemovedUserActions,
Expand Down Expand Up @@ -67,14 +67,16 @@ describe('UserActionPersister', () => {

const getRequest = () =>
({
action: 'update' as const,
type: 'connector' as const,
caseId: 'test',
payload: { connector: createConnectorObject().connector },
connectorId: '1',
owner: 'cases',
user: { email: '', full_name: '', username: '' },
} as CreateUserActionClient<'connector'>);
userAction: {
action: 'update' as const,
type: 'connector' as const,
caseId: 'test',
payload: { connector: createConnectorObject().connector },
connectorId: '1',
owner: 'cases',
user: { email: '', full_name: '', username: '' },
},
} as CreateUserActionArgs<'connector'>);

const getBulkCreateAttachmentRequest = (): BulkCreateAttachmentUserAction => ({
caseId: 'test',
Expand Down Expand Up @@ -107,7 +109,7 @@ describe('UserActionPersister', () => {

it('throws if fields is omitted', async () => {
const req = getRequest();
unset(req, 'payload.connector.fields');
unset(req, 'userAction.payload.connector.fields');

await expect(persister.createUserAction(req)).rejects.toThrow(
'Invalid value "undefined" supplied to "payload,connector,fields"'
Expand Down

0 comments on commit 2740099

Please sign in to comment.