feat(TheHive Node): Overhaul (#6457)

This commit is contained in:
Michael Kret
2023-09-04 18:15:52 +03:00
committed by GitHub
parent f286bd33c1
commit 73e782e2cf
85 changed files with 8291 additions and 4 deletions

View File

@@ -0,0 +1,164 @@
import type { IExecuteFunctions } from 'n8n-workflow';
import * as transport from '../transport/requestApi';
import { theHiveApiQuery } from '../transport/queryHelper';
import nock from 'nock';
jest.mock('../transport/requestApi', () => {
const originalModule = jest.requireActual('../transport/requestApi');
return {
...originalModule,
theHiveApiRequest: jest.fn(async function () {
return {};
}),
};
});
const fakeExecuteFunction = {} as unknown as IExecuteFunctions;
describe('Test TheHiveProject, theHiveApiQuery', () => {
beforeAll(() => {
nock.disableNetConnect();
});
afterAll(() => {
nock.restore();
jest.unmock('../transport/requestApi');
});
it('should make list query request', async () => {
const scope = {
query: 'listOrganisationPage',
};
const filtersValues = [
{
field: 'title',
operator: '_like',
value: 'Test',
},
];
const sortFields = [
{
field: 'title',
direction: 'asc',
},
];
const limit = undefined;
const returnCount = false;
await theHiveApiQuery.call(
fakeExecuteFunction,
scope,
filtersValues,
sortFields,
limit,
returnCount,
);
expect(transport.theHiveApiRequest).toHaveBeenCalledTimes(1);
expect(transport.theHiveApiRequest).toHaveBeenCalledWith('POST', '/v1/query', {
query: [
{ _name: 'listOrganisationPage' },
{ _and: [{ _like: { _field: 'title', _value: 'Test' } }], _name: 'filter' },
{ _fields: [{ title: 'asc' }], _name: 'sort' },
{ _name: 'page', extraData: undefined, from: 0, to: 500 },
],
});
});
it('should make get query request', async () => {
const scope = {
query: 'getTask',
id: '~368644136',
restrictTo: 'logs',
};
const filtersValues = [
{
field: 'message',
operator: '_like',
value: 'Test',
},
{
field: 'date',
operator: '_gt',
value: 1687263671915,
},
];
const sortFields = [
{
field: 'message',
direction: 'desc',
},
];
const limit = undefined;
const returnCount = false;
const extraData = ['taskId', 'case'];
await theHiveApiQuery.call(
fakeExecuteFunction,
scope,
filtersValues,
sortFields,
limit,
returnCount,
extraData,
);
expect(transport.theHiveApiRequest).toHaveBeenCalledTimes(2);
expect(transport.theHiveApiRequest).toHaveBeenCalledWith('POST', '/v1/query', {
query: [
{ _name: 'getTask', idOrName: '~368644136' },
{ _name: 'logs' },
{
_and: [
{ _like: { _field: 'message', _value: 'Test' } },
{ _gt: { _field: 'date', _value: 1687263671915 } },
],
_name: 'filter',
},
{ _fields: [{ message: 'desc' }], _name: 'sort' },
{ _name: 'page', extraData: ['taskId', 'case'], from: 0, to: 500 },
],
});
});
it('should make return count query request', async () => {
const scope = {
query: 'listOrganisationPage',
};
const returnCount = true;
await theHiveApiQuery.call(
fakeExecuteFunction,
scope,
undefined,
undefined,
undefined,
returnCount,
);
expect(transport.theHiveApiRequest).toHaveBeenCalledTimes(3);
expect(transport.theHiveApiRequest).toHaveBeenCalledWith('POST', '/v1/query', {
query: [{ _name: 'listOrganisationPage' }, { _name: 'count' }],
});
});
it('should set limit to query request', async () => {
const scope = {
query: 'listOrganisationPage',
};
const limit = 15;
await theHiveApiQuery.call(fakeExecuteFunction, scope, undefined, undefined, limit);
expect(transport.theHiveApiRequest).toHaveBeenCalledTimes(4);
expect(transport.theHiveApiRequest).toHaveBeenCalledWith('POST', '/v1/query', {
query: [
{ _name: 'listOrganisationPage' },
{ _name: 'page', extraData: undefined, from: 0, to: 15 },
],
});
});
});

View File

@@ -0,0 +1,179 @@
import { splitAndTrim, fixFieldType, prepareInputItem, constructFilter } from '../helpers/utils';
describe('Test TheHiveProject, splitAndTrim', () => {
it('should split and trim string, removing empty entries', () => {
const data = 'a, b,, c, d, e, f,,';
const result = splitAndTrim(data);
expect(result).toEqual(['a', 'b', 'c', 'd', 'e', 'f']);
});
it('should return unchanged array', () => {
const data = ['a', 'b', 'c', 'd', 'e', 'f'];
const result = splitAndTrim(data);
expect(result).toEqual(data);
});
});
describe('Test TheHiveProject, fixFieldType', () => {
it('should split and trim tags', () => {
const data = {
tags: 'a, b,, c, d, e, f,,',
addTags: 'a, b,, c, d, e, f,,',
removeTags: 'a, b,, c, d, e, f,,',
notChanged: 'a, b,, c, d, e, f,,',
};
const result = fixFieldType(data);
expect(result).toEqual({
tags: ['a', 'b', 'c', 'd', 'e', 'f'],
addTags: ['a', 'b', 'c', 'd', 'e', 'f'],
removeTags: ['a', 'b', 'c', 'd', 'e', 'f'],
notChanged: 'a, b,, c, d, e, f,,',
});
});
it('should convert date strings to milis', () => {
const data = {
date: '2020-01-01T00:00:00.000Z',
lastSyncDate: '2020-01-01T00:00:00.000Z',
startDate: '2020-01-01T00:00:00.000Z',
endDate: '2020-01-01T00:00:00.000Z',
dueDate: '2020-01-01T00:00:00.000Z',
includeInTimeline: '2020-01-01T00:00:00.000Z',
sightedAt: '2020-01-01T00:00:00.000Z',
notChanged: '2020-01-01T00:00:00.000Z',
};
const result = fixFieldType(data);
expect(result).toEqual({
date: 1577836800000,
lastSyncDate: 1577836800000,
startDate: 1577836800000,
endDate: 1577836800000,
dueDate: 1577836800000,
includeInTimeline: 1577836800000,
sightedAt: 1577836800000,
notChanged: '2020-01-01T00:00:00.000Z',
});
});
});
describe('Test TheHiveProject, prepareInputItem', () => {
it('should return object with fields present in schema', () => {
const data = {
a: 1,
b: 2,
c: 3,
d: 4,
f: 5,
g: 6,
};
const schema = [
{
id: 'a',
required: true,
},
{
id: 'b',
required: true,
},
{
id: 'c',
},
{
id: 'd',
required: true,
},
{
id: 'e',
},
];
const result = prepareInputItem(data, schema, 0);
expect(result).toEqual({
a: 1,
b: 2,
c: 3,
d: 4,
});
});
});
describe('Test TheHiveProject, constructFilter', () => {
it('should add default operator _eq', () => {
const data = {
field: 'myField',
value: 'myValue',
};
const result = constructFilter(data);
expect(result).toEqual({
_eq: {
_field: 'myField',
_value: 'myValue',
},
});
});
it('should return filter _gte', () => {
const data = {
field: 'myField',
value: 'myValue',
operator: '_gte',
};
const result = constructFilter(data);
expect(result).toEqual({
_gte: {
_field: 'myField',
_value: 'myValue',
},
});
});
it('should return filter _in', () => {
const data = {
field: 'myField',
values: 'a, b,, c, d',
operator: '_in',
};
const result = constructFilter(data);
expect(result).toEqual({
_in: {
_field: 'myField',
_values: ['a', 'b', 'c', 'd'],
},
});
});
it('should return filter _between', () => {
const data = {
field: 'myField',
from: 'a',
to: 'b',
operator: '_between',
};
const result = constructFilter(data);
expect(result).toEqual({
_between: {
_field: 'myField',
_from: 'a',
_to: 'b',
},
});
});
});