Skip to content

Commit 57e1235

Browse files
committed
[backend] add only_eq_to filter tests
1 parent f16da4e commit 57e1235

File tree

2 files changed

+117
-5
lines changed

2 files changed

+117
-5
lines changed

opencti-platform/opencti-graphql/src/database/engine.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,9 +2564,6 @@ export const buildLocalMustFilter = (validFilter: any) => {
25642564
},
25652565
});
25662566
} else if (operator === 'only_eq_to') {
2567-
if (arrayKeys.length > 1) {
2568-
throw UnsupportedError('Filter with `only_eq_to` operator must have only one field', { keys: arrayKeys });
2569-
}
25702567
valuesFiltering.push({
25712568
script: {
25722569
script: {

opencti-platform/opencti-graphql/tests/02-dataInjection/01-dataCount/filterGroup-test.js

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ describe('Complex filters combinations for elastic queries', () => {
126126
let marking1Id;
127127
let marking2StixId;
128128
let marking2Id;
129+
let marking3StixId;
130+
let marking3Id;
129131
let locationInternalId;
130132
let intrusionSetInternalId;
131133
it('should testing environment created', async () => {
@@ -149,12 +151,20 @@ describe('Complex filters combinations for elastic queries', () => {
149151
definition: 'TEST:2',
150152
x_opencti_order: 2,
151153
};
154+
const marking3Input = {
155+
definition_type: 'TEST',
156+
definition: 'TEST:3',
157+
x_opencti_order: 3,
158+
};
152159
const marking1 = await addAllowedMarkingDefinition(testContext, ADMIN_USER, marking1Input);
153160
marking1StixId = marking1.standard_id;
154161
marking1Id = marking1.id;
155162
const marking2 = await addAllowedMarkingDefinition(testContext, ADMIN_USER, marking2Input);
156163
marking2StixId = marking2.standard_id;
157164
marking2Id = marking2.id;
165+
const marking3 = await addAllowedMarkingDefinition(testContext, ADMIN_USER, marking3Input);
166+
marking3StixId = marking3.standard_id;
167+
marking3Id = marking3.id;
158168
// Create the reports
159169
const REPORT1 = {
160170
input: {
@@ -194,7 +204,7 @@ describe('Complex filters combinations for elastic queries', () => {
194204
description: '', // empty string
195205
stix_id: report4StixId,
196206
published: '2023-09-15T00:51:35.000Z',
197-
objectMarking: [marking2StixId],
207+
objectMarking: [marking2StixId, marking1StixId, marking3StixId],
198208
confidence: 40,
199209
},
200210
};
@@ -204,7 +214,7 @@ describe('Complex filters combinations for elastic queries', () => {
204214
description: null,
205215
stix_id: report5StixId,
206216
published: '2025-09-15T00:51:35.000Z',
207-
report_types: ['threat-report', 'internal-report', 'global-report'],
217+
report_types: ['threat-report', 'internal-report'],
208218
objectMarking: [],
209219
confidence: 11,
210220
},
@@ -2398,6 +2408,107 @@ describe('Complex filters combinations for elastic queries', () => {
23982408
});
23992409
expect(queryResult.data.reports.edges.length).toEqual(4); // the reports published in the last 3 years: report1, report2, report4, report5
24002410
});
2411+
it('should list entities according filters with only_eq_to operator', async () => {
2412+
// only_eq_to operator with several filter keys: not supported
2413+
let queryResult = await queryAsAdmin({
2414+
query: REPORT_LIST_QUERY,
2415+
variables: {
2416+
first: 10,
2417+
filters: {
2418+
mode: 'and',
2419+
filters: [{
2420+
key: ['report_types', 'name'],
2421+
values: ['internal-report'],
2422+
operator: 'only_eq_to',
2423+
mode: 'or',
2424+
}],
2425+
filterGroups: [],
2426+
},
2427+
},
2428+
});
2429+
expect(queryResult.errors[0].message).toEqual('Filter must have only one field');
2430+
// report_types only_eq_to internal-report
2431+
queryResult = await queryAsAdmin({
2432+
query: REPORT_LIST_QUERY,
2433+
variables: {
2434+
first: 10,
2435+
filters: {
2436+
mode: 'and',
2437+
filters: [{
2438+
key: 'report_types',
2439+
values: ['internal-report'],
2440+
operator: 'only_eq_to',
2441+
mode: 'or',
2442+
}],
2443+
filterGroups: [],
2444+
},
2445+
},
2446+
});
2447+
expect(queryResult.data.reports.edges.length).toEqual(1);
2448+
expect(queryResult.data.reports.edges[0].node.name).toEqual('Report3');
2449+
// marking only_eq_to marking2 (to test only_eq_to with a relation ref filter)
2450+
queryResult = await queryAsAdmin({
2451+
query: REPORT_LIST_QUERY,
2452+
variables: {
2453+
first: 10,
2454+
filters: {
2455+
mode: 'and',
2456+
filters: [{
2457+
key: 'objectMarking',
2458+
values: [marking2Id],
2459+
operator: 'only_eq_to',
2460+
mode: 'or',
2461+
}],
2462+
filterGroups: [],
2463+
},
2464+
},
2465+
});
2466+
expect(queryResult.data.reports.edges.length).toEqual(2);
2467+
expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report2')).toBeTruthy();
2468+
expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report4')).toBeTruthy();
2469+
// report_types only_eq_to internal-report OR threat-report
2470+
// It corresponds to the reports with report_types exactly equal to ['internal-report'] or to ['threat-report']
2471+
queryResult = await queryAsAdmin({
2472+
query: REPORT_LIST_QUERY,
2473+
variables: {
2474+
first: 10,
2475+
filters: {
2476+
mode: 'and',
2477+
filters: [{
2478+
key: 'report_types',
2479+
values: ['internal-report', 'threat-report'],
2480+
operator: 'only_eq_to',
2481+
mode: 'or',
2482+
}],
2483+
filterGroups: [],
2484+
},
2485+
},
2486+
});
2487+
expect(queryResult.data.reports.edges.length).toEqual(3);
2488+
expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report3')).toBeTruthy();
2489+
expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report1')).toBeTruthy();
2490+
expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('A demo report for testing purposes')).toBeTruthy();
2491+
// marking only_eq_to marking1 AND marking2
2492+
// It corresponds to the reports with exactly 2 markings, one being marking1 and the other being marking2
2493+
queryResult = await queryAsAdmin({
2494+
query: REPORT_LIST_QUERY,
2495+
variables: {
2496+
first: 10,
2497+
filters: {
2498+
mode: 'and',
2499+
filters: [{
2500+
key: 'objectMarking',
2501+
values: [marking1Id, marking2Id],
2502+
operator: 'only_eq_to',
2503+
mode: 'and',
2504+
}],
2505+
filterGroups: [],
2506+
},
2507+
},
2508+
});
2509+
expect(queryResult.data.reports.edges.length).toEqual(1);
2510+
expect(queryResult.data.reports.edges[0].node.name).toEqual('Report1');
2511+
});
24012512
it('should test environment deleted', async () => {
24022513
const DELETE_REPORT_QUERY = gql`
24032514
mutation reportDelete($id: ID!) {
@@ -2442,6 +2553,10 @@ describe('Complex filters combinations for elastic queries', () => {
24422553
query: DELETE_MARKING_QUERY,
24432554
variables: { id: marking2Id },
24442555
});
2556+
await queryAsAdmin({
2557+
query: DELETE_MARKING_QUERY,
2558+
variables: { id: marking3Id },
2559+
});
24452560
// Verify is no longer found
24462561
let queryResult;
24472562
queryResult = await queryAsAdmin({ query: READ_REPORT_QUERY, variables: { id: report1StixId } });

0 commit comments

Comments
 (0)