Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[Security Solution] [Exceptions] Fix edit the exception while adding …
Browse files Browse the repository at this point in the history
…new comment. (elastic#145575)

## Summary

Address elastic#144523

1. Fix type schema check to use `updateExceptionListItemSchema` instead
of `exceptionListItemSchema` as the comments array could be empty when
the user adds the Exceptions and in Editing with adding a new comment(s)
it will contain only (comment and id) in the newly created comment

2. Add `removeCreatedAtCreatedByFromCommentsOnUpdate` to remove the
`createdAt`, and `createdBy` from the updated exception Item if a
comment was added before to fix the `400 Schema validations` as per the
below Schema

The
`packages/kbn-securitysolution-io-ts-list-types/src/common/update_comment/index.ts`
  
  ```
      export const updateComment = t.intersection([
        t.exact(
          t.type({
            comment: NonEmptyString,
          })
        ),
        t.exact(
          t.partial({
            id,
          })
        ),
      ]);
  ```
3. Moving tests and mocks to the `hooks` package as well add new tests
for the new `removeCreatedAtCreatedByFromCommentsOnUpdate`

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
WafaaNasr and kibanamachine authored Nov 18, 2022
1 parent 193b4ec commit 2bd2226
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import {
CommentsArray,
EntriesArray,
Entry,
EntryMatch,
EntryNested,
OsTypeArray,
} from '@kbn/securitysolution-io-ts-list-types';

export const DATE_NOW = '2020-04-20T15:25:31.830Z';
export const USER = 'some user';
export const ELASTIC_USER = 'elastic';
Expand All @@ -18,3 +27,73 @@ export const TYPE = 'ip';

export const VERSION = 1;
export const IMMUTABLE = false;
// Exception List specific
export const ID = 'uuid_here';
export const NAMESPACE_TYPE = 'single';
export const OS_TYPES: OsTypeArray = ['windows'];
export const TAGS = [];
export const UPDATED_COMMENTS = [
{
comment: 'old comment',
id: 'old_created_id',
},
{
comment: 'new comment',
id: 'new_id',
},
];
export const COMMENTS = [];
export const ENTRIES: EntriesArray = [
{
entries: [{ field: 'nested.field', operator: 'included', type: 'match', value: 'some value' }],
field: 'some.parentField',
type: 'nested',
},
{ field: 'some.not.nested.field', operator: 'included', type: 'match', value: 'some value' },
];
export const ITEM_ID = 'some-list-item-id';
export const ITEM_TYPE = 'simple';
export const LIST_ITEM_ID = 'some-list-item-id';
// ENTRIES_WITH_IDS should only be used to mock out functionality of a collection of transforms
// that are UI specific and useful for UI concerns that are inserted between the
// API and the actual user interface. In some ways these might be viewed as
// technical debt or to compensate for the differences and preferences
// of how ReactJS might prefer data vs. how we want to model data.
export const ENTRIES_WITH_IDS: EntriesArray = [
{
entries: [
{
field: 'nested.field',
id: '123',
operator: 'included',
type: 'match',
value: 'some value',
} as EntryMatch & { id: string },
],
field: 'some.parentField',
id: '123',
type: 'nested',
} as EntryNested & { id: string },
{
field: 'some.not.nested.field',
id: '123',
operator: 'included',
type: 'match',
value: 'some value',
} as Entry & { id: string },
];

export const COMMENTS_WITH_CREATEDAT_CREATEDBY: CommentsArray = [
{
comment: 'old comment',
id: 'old_created_id',
created_at: '2022-01-08T15:25:31.830Z',
created_by: 'elastic',
},
{
comment: 'new comment',
id: 'new_id',
created_at: '2022-05-14T15:25:31.830Z',
created_by: 'elastic',
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { CreateExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';

import {
COMMENTS,
DESCRIPTION,
ENTRIES,
ITEM_ID,
ITEM_TYPE,
LIST_ID,
META,
NAME,
NAMESPACE_TYPE,
OS_TYPES,
TAGS,
} from '../constants.mock';

export const getCreateExceptionListItemSchemaMock = (): CreateExceptionListItemSchema => ({
comments: COMMENTS,
description: DESCRIPTION,
entries: ENTRIES,
item_id: undefined,
list_id: LIST_ID,
meta: META,
name: NAME,
namespace_type: NAMESPACE_TYPE,
os_types: OS_TYPES,
tags: TAGS,
type: ITEM_TYPE,
});

/**
* Useful for end to end testing
*/
export const getCreateExceptionListItemMinimalSchemaMock = (): CreateExceptionListItemSchema => ({
description: DESCRIPTION,
entries: ENTRIES,
item_id: ITEM_ID,
list_id: LIST_ID,
name: NAME,
os_types: OS_TYPES,
type: ITEM_TYPE,
});

/**
* Useful for end to end testing
*/
export const getCreateExceptionListItemMinimalSchemaMockWithoutId =
(): CreateExceptionListItemSchema => ({
description: DESCRIPTION,
entries: ENTRIES,
list_id: LIST_ID,
name: NAME,
os_types: OS_TYPES,
type: ITEM_TYPE,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { UpdateExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';

import {
DESCRIPTION,
ENTRIES,
ID,
ITEM_ID,
ITEM_TYPE,
LIST_ITEM_ID,
META,
NAME,
NAMESPACE_TYPE,
OS_TYPES,
TAGS,
UPDATED_COMMENTS,
} from '../constants.mock';

export const getUpdateExceptionListItemSchemaMock = (): UpdateExceptionListItemSchema => ({
_version: undefined,
comments: UPDATED_COMMENTS,
description: DESCRIPTION,
entries: ENTRIES,
id: ID,
item_id: LIST_ITEM_ID,
meta: META,
name: NAME,
namespace_type: NAMESPACE_TYPE,
os_types: ['linux'],
tags: TAGS,
type: ITEM_TYPE,
});

/**
* Useful for end to end tests and other mechanisms which want to fill in the values
* after doing a get of the structure.
*/
export const getUpdateMinimalExceptionListItemSchemaMock = (): UpdateExceptionListItemSchema => ({
description: DESCRIPTION,
entries: ENTRIES,
item_id: ITEM_ID,
name: NAME,
os_types: OS_TYPES,
type: ITEM_TYPE,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { ExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';

import {
COMMENTS,
DATE_NOW,
DESCRIPTION,
ELASTIC_USER,
ENTRIES,
ITEM_ID,
ITEM_TYPE,
LIST_ID,
META,
NAME,
NAMESPACE_TYPE,
OS_TYPES,
TIE_BREAKER,
USER,
} from '../constants.mock';

export const getExceptionListItemSchemaMock = (
overrides?: Partial<ExceptionListItemSchema>
): ExceptionListItemSchema => ({
_version: undefined,
comments: COMMENTS,
created_at: DATE_NOW,
created_by: USER,
description: DESCRIPTION,
entries: ENTRIES,
id: '1',
item_id: 'endpoint_list_item',
list_id: 'endpoint_list_id',
meta: META,
name: NAME,
namespace_type: NAMESPACE_TYPE,
os_types: [],
tags: ['user added string for a tag', 'malware'],
tie_breaker_id: TIE_BREAKER,
type: ITEM_TYPE,
updated_at: DATE_NOW,
updated_by: USER,
...(overrides || {}),
});

/**
* This is useful for end to end tests where we remove the auto generated parts for comparisons
* such as created_at, updated_at, and id.
*/
export const getExceptionListItemResponseMockWithoutAutoGeneratedValues =
(): Partial<ExceptionListItemSchema> => ({
comments: [],
created_by: ELASTIC_USER,
description: DESCRIPTION,
entries: ENTRIES,
item_id: ITEM_ID,
list_id: LIST_ID,
name: NAME,
namespace_type: 'single',
os_types: OS_TYPES,
tags: [],
type: ITEM_TYPE,
updated_by: ELASTIC_USER,
});
Loading

0 comments on commit 2bd2226

Please sign in to comment.