Skip to content

Commit

Permalink
User action tests and fixing migration errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Apr 30, 2021
1 parent 8543b5b commit 8841e65
Show file tree
Hide file tree
Showing 28 changed files with 218 additions and 29 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/common/api/cases/sub_case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as rt from 'io-ts';
import { NumberFromString } from '../saved_object';
import { UserRT } from '../user';
import { CommentResponseRt } from './comment';
import { OWNER_FIELD } from './constants';
import { CasesStatusResponseRt } from './status';
import { CaseStatusRt } from './status';

Expand All @@ -26,6 +27,7 @@ export const SubCaseAttributesRt = rt.intersection([
created_by: rt.union([UserRT, rt.null]),
updated_at: rt.union([rt.string, rt.null]),
updated_by: rt.union([UserRT, rt.null]),
[OWNER_FIELD]: rt.string,
}),
]);

Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cases/common/api/cases/user_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const CaseUserActionBasicRT = rt.type({
action_by: UserRT,
new_value: rt.union([rt.string, rt.null]),
old_value: rt.union([rt.string, rt.null]),
[OWNER_FIELD]: rt.string,
});

const CaseUserActionResponseRT = rt.intersection([
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/common/api/connectors/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import * as rt from 'io-ts';
import { OWNER_FIELD } from '..';
import { OWNER_FIELD } from '../cases/constants';

const ActionTypeRT = rt.union([
rt.literal('append'),
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/cases/server/authorization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CASE_COMMENT_SAVED_OBJECT,
CASE_CONFIGURE_SAVED_OBJECT,
CASE_SAVED_OBJECT,
CASE_USER_ACTION_SAVED_OBJECT,
} from '../../common/constants';
import { Verbs, ReadOperations, WriteOperations, OperationDetails } from './types';

Expand Down Expand Up @@ -231,4 +232,13 @@ export const Operations: Record<ReadOperations | WriteOperations, OperationDetai
docType: 'cases',
savedObjectType: CASE_SAVED_OBJECT,
},
// user actions operations
[ReadOperations.GetUserActions]: {
type: EVENT_TYPES.access,
name: ReadOperations.GetUserActions,
action: 'get-user-actions',
verbs: accessVerbs,
docType: 'user actions',
savedObjectType: CASE_USER_ACTION_SAVED_OBJECT,
},
};
1 change: 1 addition & 0 deletions x-pack/plugins/cases/server/authorization/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export enum ReadOperations {
GetTags = 'getTags',
GetReporters = 'getReporters',
FindConfigurations = 'findConfigurations',
GetUserActions = 'getUserActions',
}

/**
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/cases/server/client/attachments/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async function getSubCase({
subCaseId: newSubCase.id,
fields: ['status', 'sub_case'],
newValue: JSON.stringify({ status: newSubCase.attributes.status }),
owner: newSubCase.attributes.owner,
}),
],
});
Expand Down Expand Up @@ -222,6 +223,7 @@ const addGeneratedAlerts = async (
commentId: newComment.id,
fields: ['comment'],
newValue: JSON.stringify(query),
owner: newComment.attributes.owner,
}),
],
});
Expand Down Expand Up @@ -396,6 +398,7 @@ export const addComment = async (
commentId: newComment.id,
fields: ['comment'],
newValue: JSON.stringify(query),
owner: newComment.attributes.owner,
}),
],
});
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/server/client/attachments/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export async function deleteAll(
subCaseId: subCaseID,
commentId: comment.id,
fields: ['comment'],
owner: comment.attributes.owner,
})
),
});
Expand Down Expand Up @@ -167,6 +168,7 @@ export async function deleteComment(
subCaseId: subCaseID,
commentId: attachmentID,
fields: ['comment'],
owner: myComment.attributes.owner,
}),
],
});
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cases/server/client/attachments/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export async function update(
// myComment.attribute contains also CommentAttributesBasicRt attributes
pick(Object.keys(queryRestAttributes), myComment.attributes)
),
owner: myComment.attributes.owner,
}),
],
});
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cases/server/client/cases/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const create = async (
caseId: newCase.id,
fields: ['description', 'status', 'tags', 'title', 'connector', 'settings', OWNER_FIELD],
newValue: JSON.stringify(query),
owner: newCase.attributes.owner,
}),
],
});
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/cases/server/client/cases/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ export async function deleteCases(ids: string[], clientArgs: CasesClientArgs): P

await userActionService.bulkCreate({
soClient,
actions: ids.map((id) =>
actions: cases.saved_objects.map((caseInfo) =>
buildCaseUserActionItem({
action: 'delete',
actionAt: deleteDate,
actionBy: user,
caseId: id,
caseId: caseInfo.id,
fields: [
'description',
'status',
Expand All @@ -151,6 +151,7 @@ export async function deleteCases(ids: string[], clientArgs: CasesClientArgs): P
'comment',
...(ENABLE_CASE_CONNECTOR ? ['sub_case'] : []),
],
owner: caseInfo.attributes.owner,
})
),
});
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/server/client/cases/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export const push = async (
fields: ['status'],
newValue: CaseStatuses.closed,
oldValue: myCase.attributes.status,
owner: myCase.attributes.owner,
}),
]
: []),
Expand All @@ -226,6 +227,7 @@ export const push = async (
caseId,
fields: ['pushed'],
newValue: JSON.stringify(externalService),
owner: myCase.attributes.owner,
}),
],
}),
Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugins/cases/server/client/sub_cases/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,17 @@ async function deleteSubCase(ids: string[], clientArgs: CasesClientArgs): Promis

await userActionService.bulkCreate({
soClient,
actions: ids.map((id) =>
actions: subCases.saved_objects.map((subCase) =>
buildCaseUserActionItem({
action: 'delete',
actionAt: deleteDate,
actionBy: user,
// if for some reason the sub case didn't have a reference to its parent, we'll still log a user action
// but we won't have the case ID
caseId: subCaseIDToParentID.get(id) ?? '',
subCaseId: id,
caseId: subCaseIDToParentID.get(subCase.id) ?? '',
subCaseId: subCase.id,
fields: ['sub_case', 'comment', 'status'],
owner: subCase.attributes.owner,
})
),
});
Expand Down
12 changes: 11 additions & 1 deletion x-pack/plugins/cases/server/client/user_actions/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { CaseUserActionsResponseRt, CaseUserActionsResponse } from '../../../com
import { createCaseError } from '../../common/error';
import { checkEnabledCaseConnectorOrThrow } from '../../common';
import { CasesClientArgs } from '..';
import { ensureAuthorized } from '../utils';
import { Operations } from '../../authorization';

interface GetParams {
caseId: string;
Expand All @@ -24,7 +26,7 @@ export const get = async (
{ caseId, subCaseId }: GetParams,
clientArgs: CasesClientArgs
): Promise<CaseUserActionsResponse> => {
const { savedObjectsClient, userActionService, logger } = clientArgs;
const { savedObjectsClient, userActionService, logger, authorization, auditLogger } = clientArgs;

try {
checkEnabledCaseConnectorOrThrow(subCaseId);
Expand All @@ -35,6 +37,14 @@ export const get = async (
subCaseId,
});

await ensureAuthorized({
authorization,
auditLogger,
owners: userActions.saved_objects.map((userAction) => userAction.attributes.owner),
savedObjectIDs: userActions.saved_objects.map((userAction) => userAction.id),
operation: Operations.getUserActions,
});

return CaseUserActionsResponseRt.encode(
userActions.saved_objects.reduce<CaseUserActionsResponse>((acc, ua) => {
if (subCaseId == null && ua.references.some((uar) => uar.type === SUB_CASE_SAVED_OBJECT)) {
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/cases/server/services/cases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ interface GetReportersArgs {
const transformNewSubCase = ({
createdAt,
createdBy,
owner,
}: {
createdAt: string;
createdBy: User;
owner: string;
}): SubCaseAttributes => {
return {
closed_at: null,
Expand All @@ -198,6 +200,7 @@ const transformNewSubCase = ({
status: CaseStatuses.open,
updated_at: null,
updated_by: null,
owner,
};
};

Expand Down Expand Up @@ -585,7 +588,8 @@ export class CaseService {
this.log.debug(`Attempting to POST a new sub case`);
return soClient.create<SubCaseAttributes>(
SUB_CASE_SAVED_OBJECT,
transformNewSubCase({ createdAt, createdBy }),
// ENABLE_CASE_CONNECTOR: populate the owner field correctly
transformNewSubCase({ createdAt, createdBy, owner: '' }),
{
references: [
{
Expand Down
21 changes: 20 additions & 1 deletion x-pack/plugins/cases/server/services/user_actions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ export const transformNewUserAction = ({
email,
// eslint-disable-next-line @typescript-eslint/naming-convention
full_name,
owner,
newValue = null,
oldValue = null,
username,
}: {
actionField: UserActionField;
action: UserAction;
actionAt: string;
owner: string;
email?: string | null;
full_name?: string | null;
newValue?: string | null;
Expand All @@ -54,13 +56,15 @@ export const transformNewUserAction = ({
action_by: { email, full_name, username },
new_value: newValue,
old_value: oldValue,
owner,
});

interface BuildCaseUserAction {
action: UserAction;
actionAt: string;
actionBy: User;
caseId: string;
owner: string;
fields: UserActionField | unknown[];
newValue?: string | unknown;
oldValue?: string | unknown;
Expand All @@ -81,11 +85,13 @@ export const buildCommentUserActionItem = ({
newValue,
oldValue,
subCaseId,
owner,
}: BuildCommentUserActionItem): UserActionItem => ({
attributes: transformNewUserAction({
actionField: fields as UserActionField,
action,
actionAt,
owner,
...actionBy,
newValue: newValue as string,
oldValue: oldValue as string,
Expand Down Expand Up @@ -122,11 +128,13 @@ export const buildCaseUserActionItem = ({
newValue,
oldValue,
subCaseId,
owner,
}: BuildCaseUserAction): UserActionItem => ({
attributes: transformNewUserAction({
actionField: fields as UserActionField,
action,
actionAt,
owner,
...actionBy,
newValue: newValue as string,
oldValue: oldValue as string,
Expand Down Expand Up @@ -181,7 +189,14 @@ interface Getters {
getCaseAndSubID: GetCaseAndSubID;
}

const buildGenericCaseUserActions = <T>({
interface OwnerEntity {
owner: string;
}

/**
* The entity associated with the user action must contain an owner field
*/
const buildGenericCaseUserActions = <T extends OwnerEntity>({
actionDate,
actionBy,
originalCases,
Expand Down Expand Up @@ -222,6 +237,7 @@ const buildGenericCaseUserActions = <T>({
fields: [field],
newValue: updatedValue,
oldValue: origValue,
owner: originalItem.attributes.owner,
}),
];
} else if (Array.isArray(origValue) && Array.isArray(updatedValue)) {
Expand All @@ -237,6 +253,7 @@ const buildGenericCaseUserActions = <T>({
subCaseId,
fields: [field],
newValue: compareValues.addedItems.join(', '),
owner: originalItem.attributes.owner,
}),
];
}
Expand All @@ -252,6 +269,7 @@ const buildGenericCaseUserActions = <T>({
subCaseId,
fields: [field],
newValue: compareValues.deletedItems.join(', '),
owner: originalItem.attributes.owner,
}),
];
}
Expand All @@ -271,6 +289,7 @@ const buildGenericCaseUserActions = <T>({
fields: [field],
newValue: JSON.stringify(updatedValue),
oldValue: JSON.stringify(origValue),
owner: originalItem.attributes.owner,
}),
];
}
Expand Down
Loading

0 comments on commit 8841e65

Please sign in to comment.