Skip to content

Commit

Permalink
[Cases] Fix export flaky test (#112456)
Browse files Browse the repository at this point in the history
* Refactoring flaky test

* Forcing error in ci

* Removing fast fail

* remove .only

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
jonathan-buttner and kibanamachine authored Sep 20, 2021
1 parent 4cda49f commit 69dd0ee
Showing 1 changed file with 132 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import expect from '@kbn/expect';
import { join } from 'path';
import { SavedObject } from 'kibana/server';
import { ObjectRemover as ActionsRemover } from '../../../../../alerting_api_integration/common/lib';
import {
deleteAllCaseItems,
Expand All @@ -22,15 +23,20 @@ import {
CommentsResponse,
CASES_URL,
CaseType,
CASE_SAVED_OBJECT,
CaseAttributes,
CASE_USER_ACTION_SAVED_OBJECT,
CaseUserActionAttributes,
CASE_COMMENT_SAVED_OBJECT,
CasePostRequest,
} from '../../../../../../plugins/cases/common';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const es = getService('es');

// FLAKY: https://github.com/elastic/kibana/issues/112353
describe.skip('import and export cases', () => {
describe('import and export cases', () => {
const actionsRemover = new ActionsRemover(supertest);

afterEach(async () => {
Expand Down Expand Up @@ -60,42 +66,9 @@ export default ({ getService }: FtrProviderContext): void => {

expect(objects).to.have.length(4);

// should be the case
expect(objects[0].attributes.title).to.eql(caseRequest.title);
expect(objects[0].attributes.description).to.eql(caseRequest.description);
expect(objects[0].attributes.connector.type).to.eql(caseRequest.connector.type);
expect(objects[0].attributes.connector.name).to.eql(caseRequest.connector.name);
expect(objects[0].attributes.connector.fields).to.eql([]);
expect(objects[0].attributes.settings).to.eql(caseRequest.settings);

// should be two user actions
expect(objects[1].attributes.action).to.eql('create');

const parsedCaseNewValue = JSON.parse(objects[1].attributes.new_value);
const {
connector: { id: ignoreParsedId, ...restParsedConnector },
...restParsedCreateCase
} = parsedCaseNewValue;

const {
connector: { id: ignoreConnectorId, ...restConnector },
...restCreateCase
} = caseRequest;

expect(restParsedCreateCase).to.eql({ ...restCreateCase, type: CaseType.individual });
expect(restParsedConnector).to.eql(restConnector);

expect(objects[1].attributes.old_value).to.eql(null);
expect(includesAllRequiredFields(objects[1].attributes.action_field)).to.eql(true);

// should be the comment
expect(objects[2].attributes.comment).to.eql(postCommentUserReq.comment);
expect(objects[2].attributes.type).to.eql(postCommentUserReq.type);

expect(objects[3].attributes.action).to.eql('create');
expect(JSON.parse(objects[3].attributes.new_value)).to.eql(postCommentUserReq);
expect(objects[3].attributes.old_value).to.eql(null);
expect(objects[3].attributes.action_field).to.eql(['comment']);
expectExportToHaveCaseSavedObject(objects, caseRequest);
expectExportToHaveUserActions(objects, caseRequest);
expectExportToHaveAComment(objects);
});

it('imports a case with a comment and user actions', async () => {
Expand Down Expand Up @@ -132,7 +105,7 @@ export default ({ getService }: FtrProviderContext): void => {

expect(userActions).to.have.length(2);
expect(userActions[0].action).to.eql('create');
expect(includesAllRequiredFields(userActions[0].action_field)).to.eql(true);
expect(includesAllCreateCaseActionFields(userActions[0].action_field)).to.eql(true);

expect(userActions[1].action).to.eql('create');
expect(userActions[1].action_field).to.eql(['comment']);
Expand Down Expand Up @@ -172,7 +145,7 @@ export default ({ getService }: FtrProviderContext): void => {

expect(userActions).to.have.length(3);
expect(userActions[0].action).to.eql('create');
expect(includesAllRequiredFields(userActions[0].action_field)).to.eql(true);
expect(includesAllCreateCaseActionFields(userActions[0].action_field)).to.eql(true);

expect(userActions[1].action).to.eql('push-to-service');
expect(userActions[1].action_field).to.eql(['pushed']);
Expand All @@ -195,16 +168,123 @@ const ndjsonToObject = (input: string) => {
return input.split('\n').map((str) => JSON.parse(str));
};

const includesAllRequiredFields = (actionFields: string[]): boolean => {
const requiredFields = [
'description',
'status',
'tags',
'title',
'connector',
'settings',
'owner',
];

return requiredFields.every((field) => actionFields.includes(field));
const expectExportToHaveCaseSavedObject = (
objects: SavedObject[],
caseRequest: CasePostRequest
) => {
const caseSOs = findSavedObjectsByType<CaseAttributes>(objects, CASE_SAVED_OBJECT);
expect(caseSOs.length).to.eql(1);

const createdCaseSO = caseSOs[0];

// should be the case
expect(createdCaseSO.attributes.title).to.eql(caseRequest.title);
expect(createdCaseSO.attributes.description).to.eql(caseRequest.description);
expect(createdCaseSO.attributes.connector.type).to.eql(caseRequest.connector.type);
expect(createdCaseSO.attributes.connector.name).to.eql(caseRequest.connector.name);
expect(createdCaseSO.attributes.connector.fields).to.eql([]);
expect(createdCaseSO.attributes.settings).to.eql(caseRequest.settings);
};

const expectExportToHaveUserActions = (objects: SavedObject[], caseRequest: CasePostRequest) => {
const userActionSOs = findSavedObjectsByType<CaseUserActionAttributes>(
objects,
CASE_USER_ACTION_SAVED_OBJECT
);

expect(userActionSOs.length).to.eql(2);

expectCaseCreateUserAction(userActionSOs, caseRequest);
expectCreateCommentUserAction(userActionSOs);
};

const expectCaseCreateUserAction = (
userActions: Array<SavedObject<CaseUserActionAttributes>>,
caseRequest: CasePostRequest
) => {
const userActionForCaseCreate = findUserActionSavedObject(
userActions,
'create',
createCaseActionFields
);

expect(userActionForCaseCreate?.attributes.action).to.eql('create');

const parsedCaseNewValue = JSON.parse(userActionForCaseCreate?.attributes.new_value as string);
const {
connector: { id: ignoreParsedId, ...restParsedConnector },
...restParsedCreateCase
} = parsedCaseNewValue;

const {
connector: { id: ignoreConnectorId, ...restConnector },
...restCreateCase
} = caseRequest;

expect(restParsedCreateCase).to.eql({ ...restCreateCase, type: CaseType.individual });
expect(restParsedConnector).to.eql(restConnector);

expect(userActionForCaseCreate?.attributes.old_value).to.eql(null);
expect(
includesAllCreateCaseActionFields(userActionForCaseCreate?.attributes.action_field)
).to.eql(true);
};

const expectCreateCommentUserAction = (
userActions: Array<SavedObject<CaseUserActionAttributes>>
) => {
const userActionForComment = findUserActionSavedObject(userActions, 'create', ['comment']);

expect(userActionForComment?.attributes.action).to.eql('create');
expect(JSON.parse(userActionForComment!.attributes.new_value!)).to.eql(postCommentUserReq);
expect(userActionForComment?.attributes.old_value).to.eql(null);
expect(userActionForComment?.attributes.action_field).to.eql(['comment']);
};

const expectExportToHaveAComment = (objects: SavedObject[]) => {
const commentSOs = findSavedObjectsByType<AttributesTypeUser>(objects, CASE_COMMENT_SAVED_OBJECT);

expect(commentSOs.length).to.eql(1);

const commentSO = commentSOs[0];
expect(commentSO.attributes.comment).to.eql(postCommentUserReq.comment);
expect(commentSO.attributes.type).to.eql(postCommentUserReq.type);
};

const createCaseActionFields = [
'description',
'status',
'tags',
'title',
'connector',
'settings',
'owner',
];

const includesAllCreateCaseActionFields = (actionFields?: string[]): boolean => {
return createCaseActionFields.every(
(field) => actionFields != null && actionFields.includes(field)
);
};

const findSavedObjectsByType = <ReturnType>(
savedObjects: SavedObject[],
type: string
): Array<SavedObject<ReturnType>> => {
return (savedObjects.filter((so) => so.type === type) ?? []) as Array<SavedObject<ReturnType>>;
};

const findUserActionSavedObject = (
savedObjects: Array<SavedObject<CaseUserActionAttributes>>,
action: string,
actionFields: string[]
): SavedObject<CaseUserActionAttributes> | undefined => {
return savedObjects.find(
(so) =>
so.attributes.action === action && hasAllStrings(so.attributes.action_field, actionFields)
);
};

const hasAllStrings = (collection: string[], stringsToFind: string[]): boolean => {
return stringsToFind.every((str) => collection.includes(str));
};

0 comments on commit 69dd0ee

Please sign in to comment.