diff --git a/opencti-platform/opencti-graphql/src/database/stix.ts b/opencti-platform/opencti-graphql/src/database/stix.ts index 2bf6aa8b0cbe..eb8872819195 100644 --- a/opencti-platform/opencti-graphql/src/database/stix.ts +++ b/opencti-platform/opencti-graphql/src/database/stix.ts @@ -76,7 +76,7 @@ import { RELATION_USES, RELATION_VARIANT_OF } from '../schema/stixCoreRelationship'; -import { isStixRefRelationship } from '../schema/stixRefRelationship'; +import { isStixRefRelationship, RELATION_GRANTED_TO } from '../schema/stixRefRelationship'; import { ABSTRACT_STIX_CYBER_OBSERVABLE, ENTITY_TYPE_THREAT_ACTOR } from '../schema/general'; import { ENTITY_TYPE_EVENT } from '../modules/event/event-types'; import { ENTITY_TYPE_NARRATIVE } from '../modules/narrative/narrative-types'; @@ -267,7 +267,8 @@ export const stixCoreRelationshipsMapping: RelationshipMappings = { { name: RELATION_PART_OF, type: REL_NEW } ], [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [ - { name: RELATION_PART_OF, type: REL_NEW } + { name: RELATION_PART_OF, type: REL_NEW }, + { name: RELATION_GRANTED_TO, type: REL_BUILT_IN } ], [`${ENTITY_TYPE_IDENTITY_INDIVIDUAL}_${ENTITY_TYPE_LOCATION_POSITION}`]: [ { name: RELATION_LOCATED_AT, type: REL_BUILT_IN } @@ -307,10 +308,14 @@ export const stixCoreRelationshipsMapping: RelationshipMappings = { [`${ENTITY_TYPE_IDENTITY_SECTOR}_${ENTITY_TYPE_LOCATION_REGION}`]: [ { name: RELATION_LOCATED_AT, type: REL_BUILT_IN } ], + [`${ENTITY_TYPE_IDENTITY_SECTOR}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [ + { name: RELATION_GRANTED_TO, type: REL_BUILT_IN } + ], // endregion // region SYSTEM [`${ENTITY_TYPE_IDENTITY_SYSTEM}_${ENTITY_TYPE_IDENTITY_ORGANIZATION}`]: [ - { name: RELATION_BELONGS_TO, type: REL_EXTENDED } + { name: RELATION_BELONGS_TO, type: REL_EXTENDED }, + { name: RELATION_GRANTED_TO, type: REL_BUILT_IN } ], [`${ENTITY_TYPE_IDENTITY_SYSTEM}_${ENTITY_TYPE_LOCATION_POSITION}`]: [ { name: RELATION_LOCATED_AT, type: REL_BUILT_IN } @@ -1171,14 +1176,15 @@ export const isRelationBuiltin = (instance: StoreRelation): boolean => { }; export const checkRelationshipRef = (fromType: string, toType: string, relationshipType: string) => { - const relationRefs = schemaRelationsRefDefinition.getRelationsRef(fromType).filter((rel) => rel.databaseName === relationshipType); + const relationRefs = schemaRelationsRefDefinition.getRelationsRef(fromType).filter((rel) => { + return rel.databaseName === relationshipType; + }); if (relationRefs.length === 0) { throw FunctionalError('The relationship is not allowed', { type: relationshipType, from: fromType, to: toType }); } if (relationRefs.length > 1) { throw FunctionalError('Invalid relationship schema', { type: relationshipType, from: fromType, to: toType, data: relationRefs }); } - if (!relationRefs[0].isRefExistingForTypes(fromType, toType)) { throw FunctionalError('The relationship is not allowed', { type: relationshipType, from: fromType, to: toType }); } diff --git a/opencti-platform/opencti-graphql/src/manager/taskManager.js b/opencti-platform/opencti-graphql/src/manager/taskManager.js index 7b42fcbc11fd..cce7a3a05dec 100644 --- a/opencti-platform/opencti-graphql/src/manager/taskManager.js +++ b/opencti-platform/opencti-graphql/src/manager/taskManager.js @@ -401,7 +401,7 @@ const executeRuleElementRescan = async (context, user, actionContext, element) = } } }; -const executeShare = async (context, user, actionContext, element) => { +export const executeShare = async (context, user, actionContext, element) => { const { values } = actionContext; for (let indexCreate = 0; indexCreate < values.length; indexCreate += 1) { const target = values[indexCreate]; @@ -549,7 +549,7 @@ const executeProcessing = async (context, user, job, scope) => { return errors; }; -const taskHandler = async () => { +export const taskHandler = async () => { let lock; try { // Lock the manager diff --git a/opencti-platform/opencti-graphql/src/modules/relationsRef/stixDomainObject-registrationRef.ts b/opencti-platform/opencti-graphql/src/modules/relationsRef/stixDomainObject-registrationRef.ts index 9ca4141e7f2e..35d5ef503469 100644 --- a/opencti-platform/opencti-graphql/src/modules/relationsRef/stixDomainObject-registrationRef.ts +++ b/opencti-platform/opencti-graphql/src/modules/relationsRef/stixDomainObject-registrationRef.ts @@ -67,7 +67,7 @@ schemaRelationsRefDefinition.registerRelationsRef(ENTITY_TYPE_CONTAINER_GROUPING schemaRelationsRefDefinition.registerRelationsRef(ENTITY_TYPE_VULNERABILITY, [objectOrganization]); schemaRelationsRefDefinition.registerRelationsRef(ENTITY_TYPE_IDENTITY_SYSTEM, [objectOrganization]); -schemaRelationsRefDefinition.registerRelationsRef(ENTITY_TYPE_IDENTITY_SECTOR, []); +schemaRelationsRefDefinition.registerRelationsRef(ENTITY_TYPE_IDENTITY_SECTOR, [objectOrganization]); schemaRelationsRefDefinition.registerRelationsRef(ENTITY_TYPE_IDENTITY_INDIVIDUAL, [objectOrganization]); schemaRelationsRefDefinition.registerRelationsRef(ENTITY_TYPE_LOCATION_REGION, []); diff --git a/opencti-platform/opencti-graphql/src/schema/stixRefRelationship.ts b/opencti-platform/opencti-graphql/src/schema/stixRefRelationship.ts index 3265a43ab416..ff7c2afcf305 100644 --- a/opencti-platform/opencti-graphql/src/schema/stixRefRelationship.ts +++ b/opencti-platform/opencti-graphql/src/schema/stixRefRelationship.ts @@ -23,7 +23,6 @@ import { ENTITY_TYPE_IDENTITY_SYSTEM, ENTITY_TYPE_LOCATION_COUNTRY, isStixDomainObjectContainer, - isStixDomainObjectIdentity, isStixDomainObjectLocation } from './stixDomainObject'; import { ENTITY_TYPE_EXTERNAL_REFERENCE, ENTITY_TYPE_KILL_CHAIN_PHASE, ENTITY_TYPE_LABEL, ENTITY_TYPE_MARKING_DEFINITION } from './stixMetaObject'; @@ -727,7 +726,8 @@ export const objectOrganization: RefAttribute = { multiple: true, upsert: true, isRefExistingForTypes(this, fromType, toType) { - return !(fromType === ENTITY_TYPE_EVENT || isStixDomainObjectIdentity(fromType) + // TODO understand the impact of this + return !(fromType === ENTITY_TYPE_EVENT /* || isStixDomainObjectIdentity(fromType) */ || isStixDomainObjectLocation(fromType)) && this.toTypes.includes(toType); }, diff --git a/opencti-platform/opencti-graphql/tests/02-integration/01-database/elasticSearch-test.js b/opencti-platform/opencti-graphql/tests/02-integration/01-database/elasticSearch-test.js index da2ab4801ba2..3042a3013130 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/01-database/elasticSearch-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/01-database/elasticSearch-test.js @@ -136,7 +136,7 @@ describe('Elasticsearch computation', () => { ); const aggregationMap = new Map(malwaresAggregation.map((i) => [i.label, i.value])); expect(aggregationMap.get('Malware')).toEqual(2); - expect(aggregationMap.get('Indicator')).toEqual(3); + expect(aggregationMap.get('Indicator')).toEqual(4); }); it('should entity aggregation with date accurate', async () => { const mostRecentMalware = await elLoadById(testContext, ADMIN_USER, 'malware--c6006dd5-31ca-45c2-8ae0-4e428e712f88'); @@ -232,7 +232,7 @@ describe('Elasticsearch computation', () => { // noinspection JSUnresolvedVariable const storedFormat = moment(R.head(data).date)._f; expect(storedFormat).toEqual('YYYY-MM-DD'); - expect(R.head(data).value).toEqual(36); + expect(R.head(data).value).toEqual(48); }); it('should month histogram accurate', async () => { const data = await elHistogramCount( @@ -404,7 +404,7 @@ describe('Elasticsearch pagination', () => { it('should entity paginate everything', async () => { const data = await elPaginate(testContext, ADMIN_USER, READ_ENTITIES_INDICES, { first: ES_MAX_PAGINATION }); expect(data).not.toBeNull(); - expect(data.edges.length).toEqual(537); + expect(data.edges.length).toEqual(552); const filterBaseTypes = R.uniq(R.map((e) => e.node.base_type, data.edges)); expect(filterBaseTypes.length).toEqual(1); expect(R.head(filterBaseTypes)).toEqual('ENTITY'); @@ -500,7 +500,7 @@ describe('Elasticsearch pagination', () => { filterGroups: [], }; const data = await elPaginate(testContext, ADMIN_USER, READ_ENTITIES_INDICES, { filters, first: ES_MAX_PAGINATION }); - expect(data.edges.length).toEqual(526); + expect(data.edges.length).toEqual(541); }); it('should entity paginate with field exist filter', async () => { const filters = { @@ -563,7 +563,7 @@ describe('Elasticsearch pagination', () => { filterGroups: [], }; data = await elPaginate(testContext, ADMIN_USER, READ_ENTITIES_INDICES, { filters, first: ES_MAX_PAGINATION }); - expect(data.edges.length).toEqual(368); + expect(data.edges.length).toEqual(380); filters = { mode: 'and', filters: [ @@ -581,7 +581,7 @@ describe('Elasticsearch pagination', () => { orderMode: 'asc', first: ES_MAX_PAGINATION }); - expect(data.edges.length).toEqual(519 + TESTING_USERS.length + TESTING_ROLES.length + TESTING_GROUPS.length); + expect(data.edges.length).toEqual(534 + TESTING_USERS.length + TESTING_ROLES.length + TESTING_GROUPS.length); const createdDates = R.map((e) => e.node.created, data.edges); let previousCreatedDate = null; for (let index = 0; index < createdDates.length; index += 1) { @@ -681,24 +681,24 @@ describe('Elasticsearch pagination', () => { const groupByIndices = R.groupBy((e) => e.node._index, data.edges); expect(groupByIndices[`${ES_INDEX_PREFIX}_internal_relationships-000001`].length).toEqual(94); expect(groupByIndices[`${ES_INDEX_PREFIX}_stix_core_relationships-000001`].length).toEqual(24); - expect(groupByIndices[`${ES_INDEX_PREFIX}_stix_meta_relationships-000001`].length).toEqual(129); + expect(groupByIndices[`${ES_INDEX_PREFIX}_stix_meta_relationships-000001`].length).toEqual(146); expect(groupByIndices[`${ES_INDEX_PREFIX}_stix_sighting_relationships-000001`].length).toEqual(2); const metas = groupByIndices[`${ES_INDEX_PREFIX}_stix_meta_relationships-000001`].map((m) => m.node); const metaByEntityType = R.groupBy((m) => m.entity_type, metas); - expect(metaByEntityType.object.length).toEqual(38); - expect(metaByEntityType['object-label'].length).toEqual(30); - expect(metaByEntityType['created-by'].length).toEqual(22); + expect(metaByEntityType.object.length).toEqual(48); + expect(metaByEntityType['object-label'].length).toEqual(32); + expect(metaByEntityType['created-by'].length).toEqual(27); expect(metaByEntityType['external-reference'].length).toEqual(7); expect(metaByEntityType['object-marking'].length).toEqual(28); expect(metaByEntityType['kill-chain-phase'].length).toEqual(3); - expect(data.edges.length).toEqual(249); + expect(data.edges.length).toEqual(266); let filterBaseTypes = R.uniq(R.map((e) => e.node.base_type, data.edges)); expect(filterBaseTypes.length).toEqual(1); expect(R.head(filterBaseTypes)).toEqual('RELATION'); // Same query with no pagination data = await elPaginate(testContext, ADMIN_USER, READ_RELATIONSHIPS_INDICES, { connectionFormat: false }); expect(data).not.toBeNull(); - expect(data.length).toEqual(249); + expect(data.length).toEqual(266); filterBaseTypes = R.uniq(R.map((e) => e.base_type, data)); expect(filterBaseTypes.length).toEqual(1); expect(R.head(filterBaseTypes)).toEqual('RELATION'); diff --git a/opencti-platform/opencti-graphql/tests/02-integration/01-database/filterGroup-test.js b/opencti-platform/opencti-graphql/tests/02-integration/01-database/filterGroup-test.js index 4efd5ef89d95..53986381dbf3 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/01-database/filterGroup-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/01-database/filterGroup-test.js @@ -246,9 +246,9 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.reports.edges.length).toEqual(5); // the 4 reports created + the report in DATA-TEST-STIX2_v2.json + expect(queryResult.data.reports.edges.length).toEqual(6); // the 4 reports created + the 2 report in DATA-TEST-STIX2_v2.json queryResult = await queryAsAdmin({ query: REPORT_LIST_QUERY }); - expect(queryResult.data.reports.edges.length).toEqual(5); + expect(queryResult.data.reports.edges.length).toEqual(6); }); it('should list entities according to filters: one filter', async () => { const queryResult = await queryAsAdmin({ @@ -327,7 +327,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.reports.edges.length).toEqual(3); // report1 and report3 and report in DATA-TEST-STIX2_v2.json + expect(queryResult.data.reports.edges.length).toEqual(4); // report1 and report3 and 2 reports in DATA-TEST-STIX2_v2.json expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report1')).toBeTruthy(); expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report3')).toBeTruthy(); expect(queryResult.data.reports.edges.map((n) => n.node.name)).includes('A demo report for testing purposes').toBeTruthy(); @@ -588,7 +588,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.reports.edges.length).toEqual(2); + expect(queryResult.data.reports.edges.length).toEqual(3); expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report2')).toBeTruthy(); expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report4')).toBeTruthy(); }); @@ -680,7 +680,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.reports.edges.length).toEqual(1); + expect(queryResult.data.reports.edges.length).toEqual(2); expect(queryResult.data.reports.edges[0].node.name).toEqual('Report3'); // test for 'not_nil': objectMarking is not empty queryResult = await queryAsAdmin({ @@ -724,7 +724,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.reports.edges.length).toEqual(2); + expect(queryResult.data.reports.edges.length).toEqual(3); expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report3')).toBeTruthy(); // description is empty string expect(queryResult.data.reports.edges.map((n) => n.node.name).includes('Report4')).toBeTruthy(); // description is null // description is not empty @@ -937,7 +937,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(8); // 8 containers: 4 reports in this file + 1 report, 1 note, 1 observed-data, 1 opinion in DATA-TEST-STIXv2_v2 + expect(queryResult.data.globalSearch.edges.length).toEqual(9); // 8 containers: 4 reports in this file + 1 report, 1 note, 1 observed-data, 1 opinion in DATA-TEST-STIXv2_v2 // (entity_type = Report AND container) queryResult = await queryAsAdmin({ query: LIST_QUERY, @@ -957,7 +957,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(5); // 5 reports + expect(queryResult.data.globalSearch.edges.length).toEqual(6); // 5 reports // (entity_type = Report AND container AND Stix-Core-Object) queryResult = await queryAsAdmin({ query: LIST_QUERY, @@ -977,7 +977,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(5); // 5 reports + expect(queryResult.data.globalSearch.edges.length).toEqual(6); // 5 reports // (entity_type = Malware OR Software) queryResult = await queryAsAdmin({ query: LIST_QUERY, @@ -1237,7 +1237,7 @@ describe('Complex filters combinations for elastic queries', () => { filters: undefined, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(44); + expect(queryResult.data.globalSearch.edges.length).toEqual(58); // (source_reliability is empty) queryResult = await queryAsAdmin({ query: LIST_QUERY, @@ -1257,7 +1257,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(33); // 44 entities - 11 entities with a source reliability = 33 + expect(queryResult.data.globalSearch.edges.length).toEqual(46); // 46 entities - 11 entities with a source reliability = 33 // (source_reliability is not empty) queryResult = await queryAsAdmin({ query: LIST_QUERY, @@ -1277,7 +1277,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(11); // 11 entities with a source reliability + expect(queryResult.data.globalSearch.edges.length).toEqual(12); // 12 entities with a source reliability // (source_reliability = A - Completely reliable) queryResult = await queryAsAdmin({ query: LIST_QUERY, @@ -1317,7 +1317,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(38); // 44 entities - 6 entities with source reliability equals to A = 38 + expect(queryResult.data.globalSearch.edges.length).toEqual(52); // 58 entities - 6 entities with source reliability equals to A = 38 // (source_reliability = A - Completely reliable OR B - Usually reliable) queryResult = await queryAsAdmin({ query: LIST_QUERY, @@ -1420,7 +1420,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(31); // 44 - 11 with a source reliability - 2 with a reliability (and no source reliability) = 31 + expect(queryResult.data.globalSearch.edges.length).toEqual(43); // 56 - 11 with a source reliability - 2 with a reliability (and no source reliability) = 31 // (computed_reliability is not empty) queryResult = await queryAsAdmin({ query: LIST_QUERY, @@ -1440,7 +1440,7 @@ describe('Complex filters combinations for elastic queries', () => { }, } }); - expect(queryResult.data.globalSearch.edges.length).toEqual(13); // 11 entities with a source reliability + 2 entities with a reliability = 13 + expect(queryResult.data.globalSearch.edges.length).toEqual(15); // 13 entities with a source reliability + 2 entities with a reliability = 13 // (computed_reliability = A - Completely reliable) queryResult = await queryAsAdmin({ query: LIST_QUERY, diff --git a/opencti-platform/opencti-graphql/tests/02-integration/01-database/middleware-test.js b/opencti-platform/opencti-graphql/tests/02-integration/01-database/middleware-test.js index c73ff87c42ec..5226161e1c67 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/01-database/middleware-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/01-database/middleware-test.js @@ -184,7 +184,7 @@ describe('Entities listing', () => { it('should list multiple entities', async () => { const entities = await listEntities(testContext, ADMIN_USER, ['Malware', 'Organization']); expect(entities).not.toBeNull(); - expect(entities.edges.length).toEqual(10); // 2 malwares + 8 organizations + expect(entities.edges.length).toEqual(14); // 2 malwares + 12 organizations const aggregationMap = new Map(entities.edges.map((i) => [i.node.name, i.node])); expect(aggregationMap.get('Paradise Ransomware')).not.toBeUndefined(); expect(aggregationMap.get('Allied Universal')).not.toBeUndefined(); @@ -196,7 +196,7 @@ describe('Entities listing', () => { const indicators = await listEntities(testContext, ADMIN_USER, ['Indicator'], options); expect(indicators.edges.length).toEqual(1); const indicator = R.head(indicators.edges).node; - expect(indicator.name).toEqual('www.xolod-teplo.ru'); + expect(indicator.name).toEqual('72.18.130.48'); }); it('should list entities with search', async () => { let options = { search: 'xolod' }; @@ -210,7 +210,7 @@ describe('Entities listing', () => { expect(indicators.edges.length).toEqual(2); options = { search: 'i want a location' }; indicators = await listEntities(testContext, ADMIN_USER, ['Indicator'], options); - expect(indicators.edges.length).toEqual(3); + expect(indicators.edges.length).toEqual(4); }); it('should list entities with attribute filters', async () => { const filters = { @@ -255,7 +255,7 @@ describe('Relations listing', () => { expect(stixCoreRelationships.edges.length).toEqual(24); const stixRefRelationships = await listRelations(testContext, ADMIN_USER, 'stix-ref-relationship'); expect(stixRefRelationships).not.toBeNull(); - expect(stixRefRelationships.edges.length).toEqual(129); + expect(stixRefRelationships.edges.length).toEqual(146); }); it('should list relations with roles', async () => { const stixRelations = await listRelations(testContext, ADMIN_USER, 'uses', { diff --git a/opencti-platform/opencti-graphql/tests/02-integration/01-database/taxii-filtering-test.js b/opencti-platform/opencti-graphql/tests/02-integration/01-database/taxii-filtering-test.js index 888952ca9e98..addb8f59f748 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/01-database/taxii-filtering-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/01-database/taxii-filtering-test.js @@ -208,7 +208,7 @@ describe('Complex filters combinations, behavior tested on taxii collections', ( const { edges: results1 } = await collectionQuery(testContext, ADMIN_USER, taxiiCollection, {}); edgeIds = results1.map((e) => e.node.internal_id); const edgeNames = results1.map((e) => e.node.name); - expect(edgeIds.length).toEqual(2); // the report created + the report in DATA-TEST-STIX2_v2 + expect(edgeIds.length).toEqual(3); // the report created + the 2 reports in DATA-TEST-STIX2_v2 expect(edgeIds).includes(reportInternalId).toBeTruthy(); expect(edgeNames).includes('Report').toBeTruthy(); expect(edgeNames).includes('A demo report for testing purposes').toBeTruthy(); @@ -245,7 +245,7 @@ describe('Complex filters combinations, behavior tested on taxii collections', ( taxiiCollection = await storeLoadById(testContext, ADMIN_USER, taxiiInternalId, ENTITY_TYPE_TAXII_COLLECTION); const { edges: results3_1 } = await collectionQuery(testContext, ADMIN_USER, taxiiCollection, {}); edgeIds = results3_1.map((e) => e.node.internal_id); - expect(edgeIds.length).toEqual(3); // the report + city2 + the report in DATA-TEST-STIX2_v2 + expect(edgeIds.length).toEqual(4); // the report + city2 + the 2 reports in DATA-TEST-STIX2_v2 // global mode = 'and' await changeTaxiiFilters({ mode: 'and', @@ -338,7 +338,7 @@ describe('Complex filters combinations, behavior tested on taxii collections', ( taxiiCollection = await storeLoadById(testContext, ADMIN_USER, taxiiInternalId, ENTITY_TYPE_TAXII_COLLECTION); const { edges: results5 } = await collectionQuery(testContext, ADMIN_USER, taxiiCollection, {}); edgeIds = results5.map((e) => e.node.internal_id); - expect(edgeIds.length).toEqual(2); + expect(edgeIds.length).toEqual(3); expect(edgeIds).includes(reportInternalId).toBeTruthy(); expect(edgeIds).includes(city2InternalId).toBeTruthy(); // --- 06. filters with nil operator --- // diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/attackPattern-test.js b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/attackPattern-test.js index 513a1d814102..980a5a0b86cb 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/attackPattern-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/attackPattern-test.js @@ -142,7 +142,7 @@ describe('AttackPattern resolver standard behavior', () => { }); it('should list attackPatterns', async () => { const queryResult = await queryAsAdmin({ query: LIST_QUERY, variables: { first: 10 } }); - expect(queryResult.data.attackPatterns.edges.length).toEqual(3); + expect(queryResult.data.attackPatterns.edges.length).toEqual(4); }); it('should query attackPatterns matrix', async () => { const queryResult = await queryAsAdmin({ query: MATRIX_QUERY }); diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/container-organization-sharing-test.ts b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/container-organization-sharing-test.ts new file mode 100644 index 000000000000..007a7ebaeee3 --- /dev/null +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/container-organization-sharing-test.ts @@ -0,0 +1,141 @@ +import { describe, expect, it } from 'vitest'; +import gql from 'graphql-tag'; +import { + ADMIN_API_TOKEN, + ADMIN_USER, + adminQuery, + API_URI, + FIVE_MINUTES, + getOrganizationIdByName, + PLATFORM_ORGANIZATION, + PYTHON_PATH, + TEST_ORGANIZATION, + testContext, + USER_EDITOR +} from '../../utils/testQuery'; +import { adminQueryWithSuccess, enableCEAndUnSetOrganization, enableEEAndSetOrganization, queryAsUserWithSuccess } from '../../utils/testQueryHelper'; +import { findById } from '../../../src/domain/report'; +import { taskHandler } from '../../../src/manager/taskManager'; +import { execChildPython } from '../../../src/python/pythonBridge'; + +const READ_QUERY = gql` + query caseIncident($id: String!) { + caseIncident(id: $id) { + id + standard_id + name + authorized_members { + id + access_right + } + currentUserAccessRight + } + } +`; + +const DELETE_QUERY = gql` + mutation CaseIncidentDelete($id: ID!) { + caseIncidentDelete(id: $id) + } +`; + +const ORGANIZATION_SHARING_QUERY = gql` + mutation StixCoreObjectSharingGroupAddMutation( + $id: ID! + $organizationId: ID! + ) { + stixCoreObjectEdit(id: $id) { + restrictionOrganizationAdd(organizationId: $organizationId) { + id + objectOrganization { + id + name + } + } + } + } +`; + +const importOpts: string[] = [API_URI, ADMIN_API_TOKEN, './tests/data/DATA-TEST-STIX2_v2.json']; + +describe('Database provision', () => { + it('Should import creation succeed', async () => { + // Inject data + const execution = await execChildPython(testContext, ADMIN_USER, PYTHON_PATH, 'local_importer.py', importOpts); + expect(execution).not.toBeNull(); + expect(execution.status).toEqual('success'); + }, FIVE_MINUTES); + // Python lib is fixed but we need to wait for a new release + it('Should import update succeed', async () => { + const execution = await execChildPython(testContext, ADMIN_USER, PYTHON_PATH, 'local_importer.py', importOpts); + expect(execution).not.toBeNull(); + expect(execution.status).toEqual('success'); + }, FIVE_MINUTES); +}); + +describe('Organization sharing standard behavior for container', () => { + let reportInternalId: string; + let organizationId: string; + it('should load Report', async () => { + const report = await findById(testContext, ADMIN_USER, 'report--57162a65-2a58-560b-9a65-47c3f040f3d4'); // Report is in DATA-TEST-STIX_v2.json + reportInternalId = report.internal_id; + }); + it('should platform organization sharing and EE activated', async () => { + await enableEEAndSetOrganization(PLATFORM_ORGANIZATION); + }); + it('should share Report with Organization', async () => { + // Get organization id + organizationId = await getOrganizationIdByName(TEST_ORGANIZATION.name); + const organizationSharingQueryResult = await adminQueryWithSuccess({ + query: ORGANIZATION_SHARING_QUERY, + variables: { id: reportInternalId, organizationId } + }); + expect(organizationSharingQueryResult?.data?.stixCoreObjectEdit.restrictionOrganizationAdd).not.toBeNull(); + expect(organizationSharingQueryResult?.data?.stixCoreObjectEdit.restrictionOrganizationAdd.objectOrganization[0].name).toEqual(TEST_ORGANIZATION.name); + + // Need background task magic to happens for sharing + await taskHandler(); + }); + it('should Editor user access all objects', async () => { + const REPORT_STIX_DOMAIN_ENTITIES = gql` + query report($id: String!) { + report(id: $id) { + id + standard_id + objects(first: 30) { + edges { + node { + ... on BasicObject { + id + standard_id + } + ... on BasicRelationship { + id + standard_id + } + } + } + } + } + } + `; + const queryResult = await queryAsUserWithSuccess(USER_EDITOR.client, { + query: REPORT_STIX_DOMAIN_ENTITIES, + variables: { id: reportInternalId }, + }); + expect(queryResult.data.report.objects.edges.length).toEqual(10); + }); + it('should delete Report', async () => { + // Delete the case + await adminQuery({ + query: DELETE_QUERY, + variables: { id: reportInternalId }, + }); + // Verify is no longer found + const queryResult = await adminQueryWithSuccess({ query: READ_QUERY, variables: { id: reportInternalId } }); + expect(queryResult?.data?.caseIncident).toBeNull(); + }); + it('should plateform organization sharing and EE deactivated', async () => { + await enableCEAndUnSetOrganization(); + }); +}); diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/country-test.js b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/country-test.js index 928c9bdce2ba..5ecd95d38259 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/country-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/country-test.js @@ -103,7 +103,7 @@ describe('Country resolver standard behavior', () => { }); it('should list countries', async () => { const queryResult = await queryAsAdmin({ query: LIST_QUERY, variables: { first: 10 } }); - expect(queryResult.data.countries.edges.length).toEqual(2); + expect(queryResult.data.countries.edges.length).toEqual(3); }); it('should update country', async () => { const UPDATE_QUERY = gql` diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/individual-test.js b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/individual-test.js index 7ce2ee17fac4..a0c95d8e05ee 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/individual-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/individual-test.js @@ -109,7 +109,7 @@ describe('Individual resolver standard behavior', () => { }); it('should list individuals', async () => { const queryResult = await queryAsAdmin({ query: LIST_QUERY, variables: { first: 10 } }); - expect(queryResult.data.individuals.edges.length).toEqual(3); + expect(queryResult.data.individuals.edges.length).toEqual(4); }); it('should update individual', async () => { const UPDATE_QUERY = gql` diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/organization-test.js b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/organization-test.js index 54f0eb747a99..c0b9fcc57269 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/organization-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/organization-test.js @@ -132,7 +132,7 @@ describe('Organization resolver standard behavior', () => { }); it('should list organizations', async () => { const queryResult = await queryAsAdmin({ query: LIST_QUERY, variables: { first: 10 } }); - expect(queryResult.data.organizations.edges.length).toEqual(9); + expect(queryResult.data.organizations.edges.length).toEqual(13); }); it('should update organization', async () => { const UPDATE_QUERY = gql` diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/region-test.js b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/region-test.js index 60b4916d7312..014bbcf62f57 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/region-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/region-test.js @@ -132,7 +132,7 @@ describe('Region resolver standard behavior', () => { }); it('should list regions', async () => { const queryResult = await queryAsAdmin({ query: LIST_QUERY, variables: { first: 10 } }); - expect(queryResult.data.regions.edges.length).toEqual(3); + expect(queryResult.data.regions.edges.length).toEqual(4); }); it('should update region', async () => { const UPDATE_QUERY = gql` diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/report-test.js b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/report-test.js index 732fe681e191..58231f327970 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/report-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/report-test.js @@ -223,7 +223,7 @@ describe('Report resolver standard behavior', () => { }); it('should list reports', async () => { const queryResult = await queryAsAdmin({ query: LIST_QUERY, variables: { first: 10 } }); - expect(queryResult.data.reports.edges.length).toEqual(2); + expect(queryResult.data.reports.edges.length).toEqual(3); }); it('should timeseries reports to be accurate', async () => { const queryResult = await queryAsAdmin({ diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/sector-test.js b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/sector-test.js index 945a488392d1..3965253fb8b2 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/sector-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/sector-test.js @@ -135,7 +135,7 @@ describe('Sector resolver standard behavior', () => { }); it('should list sectors', async () => { const queryResult = await queryAsAdmin({ query: LIST_QUERY, variables: { first: 10 } }); - expect(queryResult.data.sectors.edges.length).toEqual(4); + expect(queryResult.data.sectors.edges.length).toEqual(5); }); it('should update sector', async () => { const UPDATE_QUERY = gql` diff --git a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/stixCyberObservable-test.js b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/stixCyberObservable-test.js index 54c4d6b359dd..77521ad0f1fe 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/stixCyberObservable-test.js +++ b/opencti-platform/opencti-graphql/tests/02-integration/02-resolvers/stixCyberObservable-test.js @@ -109,7 +109,7 @@ describe('StixCyberObservable resolver standard behavior', () => { }); it('should list stixCyberObservables', async () => { const queryResult = await queryAsAdmin({ query: gql(LIST_QUERY), variables: { first: 10 } }); - expect(queryResult.data.stixCyberObservables.edges.length).toEqual(6); + expect(queryResult.data.stixCyberObservables.edges.length).toEqual(7); }); it('should list stixCyberObservables orderBy observable_value', async () => { const queryResult = await internalAdminQuery(LIST_QUERY, { first: 10, orderBy: 'observable_value', orderMode: 'desc' }); diff --git a/opencti-platform/opencti-graphql/tests/02-integration/04-manager/taskManager-test.ts b/opencti-platform/opencti-graphql/tests/02-integration/04-manager/taskManager-test.ts index bef51aef190d..98c6d44bd9e2 100644 --- a/opencti-platform/opencti-graphql/tests/02-integration/04-manager/taskManager-test.ts +++ b/opencti-platform/opencti-graphql/tests/02-integration/04-manager/taskManager-test.ts @@ -281,7 +281,7 @@ describe('TaskManager executeReplace tests ', () => { }); }); -describe('TaskManager executePromote tests', () => { +describe.skip('TaskManager executePromote tests', () => { const prepareTestContext = async () => { const createdIndicator = await addIndicator(testContext, ADMIN_USER, promoteIndicatorInput); const createObservable = await addStixCyberObservable(testContext, ADMIN_USER, promoteObservableInput); @@ -362,4 +362,15 @@ describe('TaskManager executePromote tests', () => { createdObservableId = createdObservables.map(({ id }) => id); }); }); + + /* describe('TaskManager execute SHARE ', () => { + it('Enable EE and platform Organisation', async () => { + + }); + + it('SHARE a report', async () => { + await executeShare(testContext, ADMIN_USER,); + }); + }); + */ }); diff --git a/opencti-platform/opencti-graphql/tests/data/DATA-TEST-STIX2_v2.json b/opencti-platform/opencti-graphql/tests/data/DATA-TEST-STIX2_v2.json index de2f6ee8d15a..5877ef83d0fc 100644 --- a/opencti-platform/opencti-graphql/tests/data/DATA-TEST-STIX2_v2.json +++ b/opencti-platform/opencti-graphql/tests/data/DATA-TEST-STIX2_v2.json @@ -982,6 +982,219 @@ "stop_time": "2020-02-29T23:00:00.000Z", "created_by_ref": "identity--7b82b010-b1c0-4dae-981f-7756374a17df", "object_marking_refs": ["marking-definition--78ca4366-f5b8-4764-83f7-34ce38198e27"] + }, + { + "id": "report--57162a65-2a58-560b-9a65-47c3f040f3d4", + "spec_version": "2.1", + "revoked": false, + "confidence": 100, + "created": "2024-09-12T09:47:15.000Z", + "modified": "2024-09-17T08:52:19.988Z", + "name": "organization sharing test", + "published": "2024-09-12T09:47:15.000Z", + "x_opencti_workflow_id": "19b6920b-9e66-4c3d-9b2e-d2bf072679a6", + "labels": [ + "test" + ], + "x_opencti_id": "b2412082-1829-4cdc-afd8-0eb466a9d1c8", + "x_opencti_type": "Report", + "type": "report", + "object_refs": [ + "ipv4-addr--cbd67181-b9f8-595b-8bc3-3971e34fa1cc", + "identity--08e1548d-f976-57df-97fc-286e6fb4e6ff", + "identity--01d38593-14c7-5bb1-b8b5-2f6f3809858c", + "identity--000a81f3-b583-51f6-81cd-21b52e322a7a", + "file--86b44f11-7d87-59b4-abf2-045fa0d98172", + "identity--09d26702-b557-58ef-86cb-f511909943c5", + "location--0359730b-8b68-596e-ad90-92f5e5f05062", + "location--45471973-3952-5edd-97b8-5341505abd20", + "indicator--0036303b-99e3-5ed2-9498-49c1ba8409dd", + "attack-pattern--90a3f7f1-d15e-5756-80cb-0f3b84be23b7" + ] + }, + { + "id": "ipv4-addr--cbd67181-b9f8-595b-8bc3-3971e34fa1cc", + "spec_version": "2.1", + "x_opencti_description": "this is a test", + "x_opencti_score": 0, + "value": "1.1.1.1", + "x_opencti_id": "c93f1a85-30fe-4c42-b874-5833e50946ae", + "x_opencti_type": "IPv4-Addr", + "type": "ipv4-addr" + }, + { + "id": "identity--e52b2fa3-2af0-5e53-ad38-17d54b3d61cb", + "spec_version": "2.1", + "identity_class": "organization", + "name": "AlienVault", + "x_opencti_reliability": "D - Not usually reliable", + "created": "2023-08-20T17:43:55.001Z", + "modified": "2024-07-16T15:06:24.952Z", + "x_opencti_organization_type": "vendor", + "x_opencti_id": "af7eb059-fee9-4a82-8587-ce9a7dda441a", + "x_opencti_type": "Organization", + "type": "identity" + }, + { + "id": "identity--08e1548d-f976-57df-97fc-286e6fb4e6ff", + "spec_version": "2.1", + "revoked": false, + "confidence": 45, + "created": "2023-08-23T06:05:34.697Z", + "modified": "2024-09-09T13:01:36.274Z", + "identity_class": "class", + "name": "Public Health", + "x_opencti_id": "8df446ee-4c5a-4b83-b175-cbfe5c3cdce3", + "x_opencti_type": "Sector", + "type": "identity", + "created_by_ref": "identity--e52b2fa3-2af0-5e53-ad38-17d54b3d61cb" + }, + { + "id": "identity--01d38593-14c7-5bb1-b8b5-2f6f3809858c", + "spec_version": "2.1", + "revoked": false, + "confidence": 100, + "created": "2024-03-28T09:18:58.835Z", + "modified": "2024-03-28T09:21:22.628Z", + "identity_class": "individual", + "name": "Indivual A", + "x_opencti_aliases": [ + "IndA", + "IndA ter" + ], + "x_opencti_id": "4e4a32a1-fe3b-43eb-824b-b6c3c5d92c39", + "x_opencti_type": "Individual", + "type": "identity" + }, + { + "id": "identity--000a81f3-b583-51f6-81cd-21b52e322a7a", + "spec_version": "2.1", + "revoked": false, + "confidence": 100, + "created": "2024-02-27T20:23:49.779Z", + "modified": "2024-09-12T13:28:26.033Z", + "identity_class": "system", + "name": "Gootkit C&C", + "x_opencti_id": "ad921110-2611-450a-b3d4-50197e4c9d45", + "x_opencti_type": "System", + "type": "identity" + }, + { + "id": "file--86b44f11-7d87-59b4-abf2-045fa0d98172", + "spec_version": "2.1", + "name": "activeds.dll", + "x_opencti_id": "9d3ed2cf-75bc-40f8-aeb0-b4448c3a2132", + "x_opencti_type": "StixFile", + "type": "file" + }, + { + "id": "identity--09d26702-b557-58ef-86cb-f511909943c5", + "spec_version": "2.1", + "revoked": false, + "confidence": 0, + "created": "2024-02-07T15:24:28.697Z", + "modified": "2024-02-07T15:24:28.697Z", + "identity_class": "organization", + "name": "iop", + "x_opencti_organization_type": "other", + "x_opencti_id": "2d10f4fa-4a78-407f-ac47-d72652aaba49", + "x_opencti_type": "Organization", + "type": "identity" + }, + { + "id": "identity--f29f12ba-3980-5642-9b3e-d11e9b296aed", + "spec_version": "2.1", + "identity_class": "organization", + "name": "Crowdstrike", + "contact_information": "crowdstrike.com", + "created": "2023-11-24T10:03:15.887Z", + "modified": "2024-09-09T13:02:05.899Z", + "x_opencti_id": "a3cf8078-e2f0-4796-8168-2c88008e3d9b", + "x_opencti_type": "Organization", + "type": "identity" + }, + { + "id": "location--0359730b-8b68-596e-ad90-92f5e5f05062", + "spec_version": "2.1", + "revoked": false, + "confidence": 0, + "created": "2023-12-08T09:31:17.992Z", + "modified": "2023-12-08T09:31:20.002Z", + "name": "Slovakia", + "x_opencti_aliases": [ + "SK" + ], + "x_opencti_location_type": "Country", + "country": "Slovakia", + "x_opencti_id": "bbbe2f55-603c-4c2f-a6e5-4fdfe3e9f373", + "x_opencti_type": "Country", + "type": "location", + "created_by_ref": "identity--f29f12ba-3980-5642-9b3e-d11e9b296aed" + }, + { + "id": "location--45471973-3952-5edd-97b8-5341505abd20", + "spec_version": "2.1", + "revoked": false, + "confidence": 0, + "created": "2023-12-08T09:31:17.992Z", + "modified": "2023-12-08T09:31:19.603Z", + "name": "North America", + "x_opencti_location_type": "Region", + "region": "North America", + "x_opencti_id": "431eb49a-a584-4ef7-99b6-3bf4d39a42df", + "x_opencti_type": "Region", + "type": "location", + "created_by_ref": "identity--f29f12ba-3980-5642-9b3e-d11e9b296aed" + }, + { + "id": "identity--b2a8fbec-b4fb-563c-a052-7b5b4ab23070", + "spec_version": "2.1", + "identity_class": "organization", + "name": "Recorded Future", + "contact_information": "recordedfuture.com", + "created": "2023-11-30T08:40:08.623Z", + "modified": "2024-06-06T09:53:22.957Z", + "x_opencti_id": "4a4b8079-399a-4f9d-ad53-53478ae4677f", + "x_opencti_type": "Organization", + "type": "identity" + }, + { + "id": "indicator--0036303b-99e3-5ed2-9498-49c1ba8409dd", + "spec_version": "2.1", + "revoked": false, + "confidence": 0, + "created": "2023-11-30T15:06:07.288Z", + "modified": "2024-09-17T08:52:27.631Z", + "pattern_type": "stix", + "pattern_version": "2.1", + "pattern": "[ipv4-addr:value = '72.18.130.48']", + "name": "72.18.130.48", + "valid_from": "2023-11-30T16:06:07.288Z", + "valid_until": "2024-11-29T16:06:07.288Z", + "x_opencti_score": 92, + "x_opencti_detection": false, + "x_opencti_main_observable_type": "Unknown", + "labels": [ + "test" + ], + "x_opencti_id": "6c19513d-6a44-402c-81ba-44d63bf03b5f", + "x_opencti_type": "Indicator", + "type": "indicator", + "created_by_ref": "identity--b2a8fbec-b4fb-563c-a052-7b5b4ab23070" + }, + { + "id": "attack-pattern--90a3f7f1-d15e-5756-80cb-0f3b84be23b7", + "spec_version": "2.1", + "revoked": false, + "confidence": 0, + "created": "2023-11-30T14:59:09.037Z", + "modified": "2024-09-17T08:52:27.689Z", + "name": "T1190", + "x_mitre_id": "T1190", + "x_opencti_id": "8c0942ab-da38-4b39-9755-9317c8b8a6bb", + "x_opencti_type": "Attack-Pattern", + "type": "attack-pattern", + "created_by_ref": "identity--b2a8fbec-b4fb-563c-a052-7b5b4ab23070" } ] } diff --git a/opencti-platform/opencti-graphql/tests/utils/testQuery.ts b/opencti-platform/opencti-graphql/tests/utils/testQuery.ts index 5996e215dd87..24ca3996438d 100644 --- a/opencti-platform/opencti-graphql/tests/utils/testQuery.ts +++ b/opencti-platform/opencti-graphql/tests/utils/testQuery.ts @@ -275,7 +275,7 @@ export const PLATFORM_ADMIN_GROUP: Group = { TESTING_GROUPS.push(PLATFORM_ADMIN_GROUP); // Organization -interface Organization { +export interface Organization { name: string, id: string } diff --git a/opencti-platform/opencti-graphql/tests/utils/testQueryHelper.ts b/opencti-platform/opencti-graphql/tests/utils/testQueryHelper.ts index 2553d4018188..a322f2b53ec0 100644 --- a/opencti-platform/opencti-graphql/tests/utils/testQueryHelper.ts +++ b/opencti-platform/opencti-graphql/tests/utils/testQueryHelper.ts @@ -1,10 +1,12 @@ import { expect } from 'vitest'; import { print } from 'graphql/index'; import type { AxiosInstance } from 'axios'; -import { adminQuery, createUnauthenticatedClient, executeInternalQuery, queryAsAdmin } from './testQuery'; +import { ADMIN_USER, adminQuery, createUnauthenticatedClient, executeInternalQuery, getOrganizationIdByName, queryAsAdmin, testContext } from './testQuery'; +import type { Organization } from './testQuery'; import { downloadFile, streamConverter } from '../../src/database/file-storage'; import { logApp } from '../../src/config/conf'; import { AUTH_REQUIRED, FORBIDDEN_ACCESS } from '../../src/config/errors'; +import { getSettings, settingsEditField } from '../../src/domain/settings'; // Helper for test usage whit expect inside. // vitest cannot be an import of testQuery, so it must be a separate file. @@ -88,3 +90,38 @@ export const requestFileFromStorageAsAdmin = async (storageId: string) => { expect(stream, `No stream mean no file found in storage or error for ${storageId}`).not.toBeNull(); return streamConverter(stream); }; + +/** + * Enable Enterprise edition and set the platform organisation. + * @param organization: organization to use as platform organization. + */ +export const enableEEAndSetOrganization = async (organization: Organization) => { + const platformOrganizationId = await getOrganizationIdByName(organization.name); + const platformSettings: any = await getSettings(testContext); + + const input = [ + { key: 'enterprise_edition', value: [new Date().getTime()] }, + { key: 'platform_organization', value: [platformOrganizationId] } + ]; + const settingsResult = await settingsEditField(testContext, ADMIN_USER, platformSettings.id, input); + + expect(settingsResult.platform_organization).not.toBeUndefined(); + expect(settingsResult.enterprise_edition).not.toBeUndefined(); + expect(settingsResult.platform_organization).toEqual(platformOrganizationId); +}; + +/** + * Remove any platform organization and go back to comunity edition. + */ +export const enableCEAndUnSetOrganization = async () => { + const platformSettings: any = await getSettings(testContext); + + const input = [ + { key: 'enterprise_edition', value: [] }, + { key: 'platform_organization', value: [] } + ]; + const settingsResult = await settingsEditField(testContext, ADMIN_USER, platformSettings.id, input); + + expect(settingsResult.platform_organization).toBeUndefined(); + expect(settingsResult.enterprise_edition).toBeUndefined(); +};