Skip to content

Commit

Permalink
[backend] fix flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyCloarec committed Sep 25, 2024
1 parent 0c0ecc7 commit 455eaf0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ test('Add and remove observable from Observables tab of a Report as Admin user',
await page.getByRole('textbox', { name: 'Search these results...' }).click();
await page.getByRole('textbox', { name: 'Search these results...' }).pressSequentially('8.8.8.8', { delay: 100 });
await page.getByRole('textbox', { name: 'Search these results...' }).press('Enter');
await search.addSearch('8.8.8.8');
await expect(containerAddObservablesPage.getObservable('IPv4 address 8.8.8.8')).toBeVisible();
await containerAddObservablesPage.getObservable('IPv4 address 8.8.8.8').click();
await containerAddObservablesPage.getCloseObservablesListButton().click();
Expand Down
26 changes: 15 additions & 11 deletions opencti-platform/opencti-graphql/src/database/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3687,6 +3687,7 @@ export const elIndexElements = async (context, user, indexingType, elements) =>
// If we are in a draft, relations from and to need to be elements that are also in draft.
const draftContext = inDraftContext(user);
if (draftContext) {
if (elements.some((e) => isInternalObject(e.from.entity_type) || !isInternalRelationship(e.from.entity_type))) throw new Error('Cannot index internal element in draft context');
for (let i = 0; i < elements.length; i += 1) {
const element = elements[i];
if (element.base_type === BASE_TYPE_RELATION) {
Expand All @@ -3696,18 +3697,14 @@ export const elIndexElements = async (context, user, indexingType, elements) =>
element.from = draftFrom;
element.fromId = draftFrom.id;
} else {
element.from._index = !isInternalObject(element.from.entity_type) && !isInternalRelationship(element.from.entity_type)
? INDEX_DRAFT
: element.from._index;
element.from._index = INDEX_DRAFT;
}
if (!elements.some((e) => e.internal_id === to.internal_id)) {
const draftTo = await getElementDraftVersion(context, user, to);
element.to = draftTo;
element.toId = draftTo.id;
} else {
element.to._index = !isInternalObject(element.to.entity_type) && !isInternalRelationship(element.to.entity_type)
? INDEX_DRAFT
: element.to._index;
element.to._index = INDEX_DRAFT;
}
}
}
Expand All @@ -3717,10 +3714,17 @@ export const elIndexElements = async (context, user, indexingType, elements) =>
// 00. Relations must be transformed before indexing.
const transformedElements = await prepareIndexing(elements);
// 01. Bulk the indexing of row elements
const body = transformedElements.flatMap((doc) => [
{ index: { _index: doc._index, _id: doc.internal_id, retry_on_conflict: ES_RETRY_ON_CONFLICT } },
R.pipe(R.dissoc('_index'))(doc),
]);
const body = transformedElements.flatMap((doc) => {
const elementDoc = doc;
if (draftContext) {
elementDoc._index = INDEX_DRAFT;
elementDoc.draft_ids = [draftContext];
}
return [
{ index: { _index: doc._index, _id: doc.internal_id, retry_on_conflict: ES_RETRY_ON_CONFLICT } },
R.pipe(R.dissoc('_index'))(doc),
];
});
if (body.length > 0) {
meterManager.directBulk(body.length, { type: indexingType });
await elBulk({ refresh: true, timeout: BULK_TIMEOUT, body });
Expand Down Expand Up @@ -3787,7 +3791,7 @@ export const elIndexElements = async (context, user, indexingType, elements) =>
return { ...entity, id: entityId, data: { script: { source, params } } };
});
const bodyUpdate = elementsToUpdate.flatMap((doc) => [
{ update: { _index: doc._index, _id: doc.id, retry_on_conflict: ES_RETRY_ON_CONFLICT } },
{ update: { _index: doc._index, _id: doc._id, retry_on_conflict: ES_RETRY_ON_CONFLICT } },
R.dissoc('_index', doc.data),
]);
if (bodyUpdate.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,22 @@ const LIST_DRAFT_WORKSPACES_QUERY = gql`
}
`;

const MODIFY_USER_DRAFT_WORKSPACE_QUERY = gql`
mutation MeUserWorkspaceModify($input: [EditInput]!) {
meEdit(input: $input) {
id
workspace_context
}
}
`;
// const MODIFY_USER_DRAFT_WORKSPACE_QUERY = gql`
// mutation MeUserWorkspaceModify($input: [EditInput]!) {
// meEdit(input: $input) {
// id
// workspace_context
// }
// }
// `;
//
// const modifyUserWorkspace = async (draftId: string) => {
// const meUserModifyResult = await queryAsAdmin({
// query: MODIFY_USER_DRAFT_WORKSPACE_QUERY,
// variables: { input: { key: 'workspace_context', value: draftId } },
// });
// expect(meUserModifyResult.data?.meEdit.workspace_context).toEqual(draftId);
// };

describe('Drafts workspace resolver testing', () => {
let addedDraftId = '';
Expand Down Expand Up @@ -104,17 +112,6 @@ describe('Drafts workspace resolver testing', () => {
});

it('create entity in draft context', async () => {
const meUserModifyResult = await queryAsAdmin({
query: MODIFY_USER_DRAFT_WORKSPACE_QUERY,
variables: { input: { key: 'workspace_context', value: addedDraftId } },
});
expect(meUserModifyResult.data?.meEdit.workspace_context).toEqual(addedDraftId);

const meUserModifyBackResult = await queryAsAdmin({
query: MODIFY_USER_DRAFT_WORKSPACE_QUERY,
variables: { input: { key: 'workspace_context', value: '' } },
});
expect(meUserModifyBackResult.data?.meEdit.workspace_context).toEqual('');
});

it('should delete a draft by its ID', async () => {
Expand Down

0 comments on commit 455eaf0

Please sign in to comment.