-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend] avoid useless operations during batched replace (#6297)
Co-authored-by: Laurent Bonnet <laurent.bonnet@filigran.io>
- Loading branch information
1 parent
d857cc0
commit 9c63947
Showing
4 changed files
with
135 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
opencti-platform/opencti-graphql/tests/02-integration/04-manager/taskManager-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { afterAll, describe, expect, it } from 'vitest'; | ||
import { executeReplace } from '../../../src/manager/taskManager'; | ||
import type { AuthContext } from '../../../src/types/user'; | ||
import { ADMIN_USER } from '../../utils/testQuery'; | ||
import { MARKING_TLP_CLEAR, MARKING_TLP_AMBER } from '../../../src/schema/identifier'; | ||
import { addReport, findById as findReportById } from '../../../src/domain/report'; | ||
import { findById } from '../../../src/domain/markingDefinition'; | ||
import { stixDomainObjectDelete } from '../../../src/domain/stixDomainObject'; | ||
|
||
describe('TaskManager executeReplace tests ', () => { | ||
const adminContext: AuthContext = { user: ADMIN_USER, tracing: undefined, source: 'taskManager-integration-test', otp_mandatory: false }; | ||
const reportsId: string[] = []; | ||
afterAll(async () => { | ||
expect(reportsId.length).toBe(3); | ||
for (let index = 0; index < reportsId.length; index += 1) { | ||
await stixDomainObjectDelete(adminContext, adminContext.user, reportsId[index]); | ||
const report = await findReportById(adminContext, adminContext.user, reportsId[index]); | ||
expect(report).toBeUndefined(); | ||
} | ||
}); | ||
it('REPLACE report marking with different marking', async () => { | ||
const reportInput = { | ||
name: 'test report marking with different marking', | ||
objectMarking: [MARKING_TLP_CLEAR], | ||
}; | ||
const report = await addReport(adminContext, adminContext.user, reportInput); | ||
|
||
expect(report.id).toBeDefined(); | ||
reportsId.push(report.id); | ||
const reportId = report.id; | ||
const marking = await findById(adminContext, adminContext.user, MARKING_TLP_AMBER); | ||
|
||
const actionContext = { | ||
field: 'object-marking', | ||
type: 'RELATION', | ||
values: [marking.id] | ||
}; | ||
|
||
await executeReplace(adminContext, adminContext.user, actionContext, report); | ||
const reportAfterReplace = await findReportById(adminContext, adminContext.user, reportId); | ||
|
||
const markings = reportAfterReplace['object-marking']; | ||
expect(markings?.length).toEqual(1); | ||
if (markings) { | ||
const markingEntity = await findById(adminContext, adminContext.user, markings[0]); | ||
expect(markingEntity.standard_id).toEqual(MARKING_TLP_AMBER); | ||
} | ||
}); | ||
it('REPLACE report marking with same marking', async () => { | ||
const reportInput = { | ||
name: 'test report marking with same marking', | ||
objectMarking: [MARKING_TLP_CLEAR], | ||
}; | ||
const report = await addReport(adminContext, adminContext.user, reportInput); | ||
expect(report.id).toBeDefined(); | ||
reportsId.push(report.id); | ||
const reportId = report.id; | ||
|
||
const marking = await findById(adminContext, adminContext.user, MARKING_TLP_CLEAR); | ||
|
||
const actionContext = { | ||
field: 'object-marking', | ||
type: 'RELATION', | ||
values: [marking.id] | ||
}; | ||
|
||
await executeReplace(adminContext, adminContext.user, actionContext, report); | ||
const reportAfterReplace = await findReportById(adminContext, adminContext.user, reportId); | ||
|
||
const markings = reportAfterReplace['object-marking']; | ||
expect(markings?.length).toEqual(1); | ||
if (markings) { | ||
const markingEntity = await findById(adminContext, adminContext.user, markings[0]); | ||
expect(markingEntity.standard_id).toEqual(MARKING_TLP_CLEAR); | ||
} | ||
}); | ||
it('REPLACE report no marking with marking', async () => { | ||
const reportInput = { | ||
name: 'test report no marking with marking', | ||
objectMarking: [], | ||
}; | ||
const report = await addReport(adminContext, adminContext.user, reportInput); | ||
expect(report.id).toBeDefined(); | ||
reportsId.push(report.id); | ||
const reportId = report.id; | ||
|
||
const marking = await findById(adminContext, adminContext.user, MARKING_TLP_CLEAR); | ||
|
||
const actionContext = { | ||
field: 'object-marking', | ||
type: 'RELATION', | ||
values: [marking.id] | ||
}; | ||
|
||
await executeReplace(adminContext, adminContext.user, actionContext, report); | ||
const reportAfterReplace = await findReportById(adminContext, adminContext.user, reportId); | ||
|
||
const markings = reportAfterReplace['object-marking']; | ||
expect(markings?.length).toEqual(1); | ||
if (markings) { | ||
const markingEntity = await findById(adminContext, adminContext.user, markings[0]); | ||
expect(markingEntity.standard_id).toEqual(MARKING_TLP_CLEAR); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters