Skip to content

Commit 72d4d20

Browse files
committed
[backend] add stix filtering tests
1 parent 661ad74 commit 72d4d20

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

opencti-platform/opencti-graphql/tests/03-integration/01-database/filters-test.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { isEmptyField } from '../../../src/database/utils';
55
import { ENTITY_TYPE_INTRUSION_SET } from '../../../src/schema/stixDomainObject';
66
import { isStixMatchFilterGroup_MockableForUnitTests } from '../../../src/utils/filtering/filtering-stix/stix-filtering';
77

8+
// -- File to test stix filtering (filters on events: in the context of playbooks, streams, triggers)
9+
// -- with different keys, operators, modes, combinations
10+
811
const WHITE_TLP = { standard_id: 'marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9', internal_id: null };
912

1013
const applyFilters = async (filters, user = ADMIN_USER) => {
@@ -131,7 +134,7 @@ describe('Filters testing', () => {
131134
// assignee_filter
132135

133136
it('Should labels filters correctly applied', async () => {
134-
// With eq on marking
137+
// With eq
135138
const filters = {
136139
mode: 'and',
137140
filters: [{
@@ -144,7 +147,7 @@ describe('Filters testing', () => {
144147
};
145148
const filteredObjects = await applyFilters(filters);
146149
expect(filteredObjects.length).toBe(2);
147-
// With _not_eq
150+
// With not_eq
148151
const filtersNot = {
149152
mode: 'and',
150153
filters: [{
@@ -158,6 +161,73 @@ describe('Filters testing', () => {
158161
const filteredObjectsNot = await applyFilters(filtersNot);
159162
expect(stixBundle.objects.length - filteredObjects.length).toBe(filteredObjectsNot.length);
160163
});
164+
it('Should labels filters correctly applied with only_eq_to operator', async () => {
165+
// With only_eq_to
166+
let filters = {
167+
mode: 'and',
168+
filters: [{
169+
key: ['objectLabel'],
170+
values: ['identity'],
171+
operator: 'only_eq_to',
172+
mode: 'or',
173+
}],
174+
filterGroups: [],
175+
};
176+
let filteredObjects = await applyFilters(filters);
177+
expect(filteredObjects.length).toBe(10);
178+
// With not_only_eq_to
179+
let filtersNot = {
180+
mode: 'and',
181+
filters: [{
182+
key: ['objectLabel'],
183+
values: ['attack-pattern'],
184+
operator: 'not_only_eq_to',
185+
mode: 'or',
186+
}],
187+
filterGroups: [],
188+
};
189+
let filteredObjectsNot = await applyFilters(filtersNot);
190+
expect(stixBundle.objects.length - filteredObjects.length).toBe(filteredObjectsNot.length);
191+
// With only_eq_to & AND local mode
192+
filters = {
193+
mode: 'and',
194+
filters: [{
195+
key: ['objectLabel'],
196+
values: ['identity', 'organization'],
197+
operator: 'only_eq_to',
198+
mode: 'and',
199+
}],
200+
filterGroups: [],
201+
};
202+
filteredObjects = await applyFilters(filters);
203+
// With not_only_eq_to & AND local mode
204+
filtersNot = {
205+
mode: 'and',
206+
filters: [{
207+
key: ['objectLabel'],
208+
values: ['identity', 'organization'],
209+
operator: 'not_only_eq_to',
210+
mode: 'and',
211+
}],
212+
filterGroups: [],
213+
};
214+
filteredObjectsNot = await applyFilters(filtersNot);
215+
expect(stixBundle.objects.length - filteredObjects.length).toBe(filteredObjectsNot.length);
216+
expect(filteredObjects.map((n) => n.node.name).includes('ANSSI')).toBeFalsy();
217+
// With only_eq_to & OR local mode
218+
filters = {
219+
mode: 'and',
220+
filters: [{
221+
key: ['objectLabel'],
222+
values: ['identity', 'organization'],
223+
operator: 'only_eq_to',
224+
mode: 'or',
225+
}],
226+
filterGroups: [],
227+
};
228+
filteredObjects = await applyFilters(filters);
229+
expect(filteredObjects.length).toBe(10);
230+
});
161231

162232
// revoked
163233

opencti-platform/opencti-graphql/tests/data/DATA-TEST-STIX2_v2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"spec_version": "2.1",
99
"name": "ANSSI",
1010
"identity_class": "organization",
11-
"labels": ["identity"],
11+
"labels": ["identity", "organization"],
1212
"created": "2020-02-23T23:40:53.575Z",
1313
"modified": "2020-02-27T08:45:39.351Z",
1414
"x_opencti_organization_type": "CSIRT",

0 commit comments

Comments
 (0)