Skip to content

Commit

Permalink
[backend] add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SouadHadjiat committed Nov 14, 2024
1 parent 385b8eb commit 69b1553
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface HistoryData extends BasicStoreEntity {
* @param context
* @param events
*/
const resolveGrantedRefsIds = async (context: AuthContext, events: Array<SseEvent<StreamDataEvent>>) => {
export const resolveGrantedRefsIds = async (context: AuthContext, events: Array<SseEvent<StreamDataEvent>>) => {
const grantedRefsToResolve: StixId[] = [];
events.forEach((event) => {
const stix = event.data.data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { describe, expect, it } from 'vitest';
import { resolveGrantedRefsIds } from '../../../src/manager/historyManager';
import { testContext } from '../../utils/testQuery';

const eventWithGrantedRefIds = {
id: '1731595374948-0',
event: 'update',
data: {
version: '4',
type: 'update',
scope: 'external',
message: 'adds `Filigran` in `Shared with`',
origin: {
socket: 'query',
ip: '::1',
user_id: '88ec0c6a-13ce-5e39-b486-354fe4a7084f',
group_ids: ['9c746e48-28fd-432a-abd7-d7593eb310c4'],
organization_ids: [],
user_metadata: {},
referer: 'http://localhost:3000/dashboard/analyses/reports/58fbfcfa-01ce-4440-8edf-7ea38e7a6ae9'
},
data: {
id: 'report--d27398f3-8086-50e7-9c71-088b9bd69605',
spec_version: '2.1',
type: 'report',
extensions: {
'extension-definition--ea279b3e-5c71-4632-ac08-831c66a786ba': {
extension_type: 'property-extension',
id: '58fbfcfa-01ce-4440-8edf-7ea38e7a6ae9',
type: 'Report',
created_at: '2024-02-20T15:34:17.203Z',
updated_at: '2024-11-14T14:42:37.551Z',
is_inferred: false,
granted_refs: ['identity--67fabb23-c547-5c4a-b253-9d9a8548c466', 'identity--8cb00c79-ab20-5ed4-b37d-337241b96a29'],
creator_ids: ['88ec0c6a-13ce-5e39-b486-354fe4a7084f'],
granted_refs_ids: ['c080a677-f640-4643-9d2a-75929ac07b1c', '0c897410-3579-4770-b26e-1fce2e441204'],
workflow_id: '78973513-cebc-49f9-a316-12487acd7903',
labels_ids: ['7b705594-e2bc-48f8-bdc3-8c55ce1adb0e']
}
},
created: '2024-02-20T15:34:11.000Z',
modified: '2024-11-14T14:42:37.551Z',
revoked: false,
confidence: 100,
lang: 'en',
labels: ['label-debug-rename2'],
name: 'test',
published: '2024-02-20T15:34:11.000Z',
},
context: {
patch: [
{ op: 'add', path: '/extensions/extension-definition--ea279b3e-5c71-4632-ac08-831c66a786ba/granted_refs_ids/1', value: '0c897410-3579-4770-b26e-1fce2e441204' },
{ op: 'add', path: '/extensions/extension-definition--ea279b3e-5c71-4632-ac08-831c66a786ba/granted_refs/1', value: 'identity--8cb00c79-ab20-5ed4-b37d-337241b96a29' }
],
reverse_patch: [
{ op: 'remove', path: '/extensions/extension-definition--ea279b3e-5c71-4632-ac08-831c66a786ba/granted_refs_ids/1' },
{ op: 'remove', path: '/extensions/extension-definition--ea279b3e-5c71-4632-ac08-831c66a786ba/granted_refs/1' }
]
}
}
};

const eventWithGrantedRefsOnly = {
id: '1731597042395-0',
event: 'update',
data: {
version: '4',
type: 'update',
scope: 'external',
message: 'adds `TestOrganization` in `Shared with`',
origin: {
socket: 'query',
ip: '::1',
user_id: '88ec0c6a-13ce-5e39-b486-354fe4a7084f',
group_ids: ['9c746e48-28fd-432a-abd7-d7593eb310c4'],
organization_ids: [],
user_metadata: {},
referer: 'http://localhost:3000/dashboard/analyses/reports/58fbfcfa-01ce-4440-8edf-7ea38e7a6ae9'
},
data: {
id: 'report--609acc0c-c821-52e0-a6b2-25be0050bbc0',
spec_version: '2.1',
type: 'report',
extensions: {
'extension-definition--ea279b3e-5c71-4632-ac08-831c66a786ba': {
extension_type: 'property-extension',
id: 'a691be02-fb06-4358-8cf6-a08d97788340',
type: 'Report',
created_at: '2024-06-10T12:55:17.446Z',
updated_at: '2024-07-22T09:21:43.375Z',
is_inferred: false,
granted_refs: ['identity--a16d7ba8-5bea-5fe5-9d92-931e20e36727'], // TestOrganization
creator_ids: ['a93d949b-b56d-4426-b7fe-b79ec3718b0e'],
workflow_id: 'b28a370a-317b-4c50-8f0d-483b17d11abb'
}
},
created: '2024-06-10T12:55:08.000Z',
modified: '2024-06-10T12:55:40.833Z',
revoked: false,
confidence: 100,
lang: 'en',
name: 'test',
published: '2024-06-10T12:55:08.000Z',
object_refs: ['attack-pattern--033921be-85df-5f05-8bc0-d3d9fc945db9']
},
context: {
patch: [
{ op: 'add', path: '/extensions/extension-definition--ea279b3e-5c71-4632-ac08-831c66a786ba/granted_refs', value: ['identity--a16d7ba8-5bea-5fe5-9d92-931e20e36727'] }
],
reverse_patch: [
{ op: 'remove', path: '/extensions/extension-definition--ea279b3e-5c71-4632-ac08-831c66a786ba/granted_refs' }
]
}
}
};

describe('History manager test resolveGrantedRefsIds', () => {
it('should return empty map if granted refs ids are present', async () => {
const organizationByIdsMap = await resolveGrantedRefsIds(testContext, [eventWithGrantedRefIds]);
expect(organizationByIdsMap.size).toEqual(0);
});

it('should return organization if granted refs are present and not granted refs ids', async () => {
const organizationByIdsMap = await resolveGrantedRefsIds(testContext, [eventWithGrantedRefsOnly]);
expect(organizationByIdsMap.size).toEqual(1);
expect(organizationByIdsMap.has('identity--a16d7ba8-5bea-5fe5-9d92-931e20e36727'));
});
});

0 comments on commit 69b1553

Please sign in to comment.