2023-10-25 07:35:22 -07:00
|
|
|
import type { IRun, WorkflowExecuteMode } from 'n8n-workflow';
|
2023-07-18 02:28:24 -07:00
|
|
|
import {
|
|
|
|
QueryFailedError,
|
|
|
|
type DataSource,
|
|
|
|
type EntityManager,
|
|
|
|
type EntityMetadata,
|
|
|
|
} from 'typeorm';
|
|
|
|
import { mocked } from 'jest-mock';
|
2023-03-17 09:24:05 -07:00
|
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
|
2022-12-06 06:55:40 -08:00
|
|
|
import config from '@/config';
|
2023-07-18 02:28:24 -07:00
|
|
|
import type { User } from '@db/entities/User';
|
2023-05-02 01:37:19 -07:00
|
|
|
import type { WorkflowStatistics } from '@db/entities/WorkflowStatistics';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';
|
2023-07-18 02:28:24 -07:00
|
|
|
import { EventsService } from '@/services/events.service';
|
2023-08-22 06:58:05 -07:00
|
|
|
import { UserService } from '@/services/user.service';
|
2023-07-31 02:37:09 -07:00
|
|
|
import { OwnershipService } from '@/services/ownership.service';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../../shared/mocking';
|
2022-12-06 06:55:40 -08:00
|
|
|
|
2023-07-18 02:28:24 -07:00
|
|
|
describe('EventsService', () => {
|
2023-04-05 05:51:43 -07:00
|
|
|
const dbType = config.getEnv('database.type');
|
2023-07-18 02:28:24 -07:00
|
|
|
const fakeUser = mock<User>({ id: 'abcde-fghij' });
|
2023-07-31 02:37:09 -07:00
|
|
|
const ownershipService = mockInstance(OwnershipService);
|
2023-08-22 06:58:05 -07:00
|
|
|
const userService = mockInstance(UserService);
|
2023-07-18 02:28:24 -07:00
|
|
|
|
|
|
|
const entityManager = mock<EntityManager>();
|
|
|
|
const dataSource = mock<DataSource>({
|
|
|
|
manager: entityManager,
|
|
|
|
getMetadata: () =>
|
|
|
|
mock<EntityMetadata>({
|
|
|
|
tableName: 'workflow_statistics',
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
Object.assign(entityManager, { connection: dataSource });
|
2023-03-17 09:24:05 -07:00
|
|
|
|
2023-07-18 02:28:24 -07:00
|
|
|
config.set('diagnostics.enabled', true);
|
|
|
|
config.set('deployment.type', 'n8n-testing');
|
2023-07-31 02:37:09 -07:00
|
|
|
mocked(ownershipService.getWorkflowOwnerCached).mockResolvedValue(fakeUser);
|
2023-08-22 06:58:05 -07:00
|
|
|
const updateSettingsMock = jest.spyOn(userService, 'updateSettings').mockImplementation();
|
2023-03-17 09:24:05 -07:00
|
|
|
|
2023-07-31 02:37:09 -07:00
|
|
|
const eventsService = new EventsService(
|
2023-10-25 07:35:22 -07:00
|
|
|
mock(),
|
2023-07-31 02:37:09 -07:00
|
|
|
new WorkflowStatisticsRepository(dataSource),
|
|
|
|
ownershipService,
|
|
|
|
);
|
2022-12-06 06:55:40 -08:00
|
|
|
|
2023-07-18 02:28:24 -07:00
|
|
|
const onFirstProductionWorkflowSuccess = jest.fn();
|
|
|
|
const onFirstWorkflowDataLoad = jest.fn();
|
|
|
|
eventsService.on('telemetry.onFirstProductionWorkflowSuccess', onFirstProductionWorkflowSuccess);
|
|
|
|
eventsService.on('telemetry.onFirstWorkflowDataLoad', onFirstWorkflowDataLoad);
|
2022-12-06 06:55:40 -08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-07-18 02:28:24 -07:00
|
|
|
jest.clearAllMocks();
|
2022-12-06 06:55:40 -08:00
|
|
|
});
|
|
|
|
|
2023-04-05 05:51:43 -07:00
|
|
|
const mockDBCall = (count = 1) => {
|
|
|
|
if (dbType === 'sqlite') {
|
2023-07-18 02:28:24 -07:00
|
|
|
entityManager.findOne.mockResolvedValueOnce(mock<WorkflowStatistics>({ count }));
|
2023-04-05 05:51:43 -07:00
|
|
|
} else {
|
|
|
|
const result = dbType === 'postgresdb' ? [{ count }] : { affectedRows: count };
|
2023-07-18 02:28:24 -07:00
|
|
|
entityManager.query.mockImplementationOnce(async (query) =>
|
2023-04-05 05:51:43 -07:00
|
|
|
query.startsWith('INSERT INTO') ? result : null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2022-12-06 06:55:40 -08:00
|
|
|
|
|
|
|
describe('workflowExecutionCompleted', () => {
|
|
|
|
test('should create metrics for production successes', async () => {
|
|
|
|
// Call the function with a production success result, ensure metrics hook gets called
|
|
|
|
const workflow = {
|
|
|
|
id: '1',
|
|
|
|
name: '',
|
|
|
|
active: false,
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
nodes: [],
|
|
|
|
connections: {},
|
|
|
|
};
|
2023-03-17 09:24:05 -07:00
|
|
|
const runData: IRun = {
|
2022-12-06 06:55:40 -08:00
|
|
|
finished: true,
|
2023-03-17 09:24:05 -07:00
|
|
|
status: 'success',
|
2022-12-06 06:55:40 -08:00
|
|
|
data: { resultData: { runData: {} } },
|
|
|
|
mode: 'internal' as WorkflowExecuteMode,
|
|
|
|
startedAt: new Date(),
|
|
|
|
};
|
2023-04-05 05:51:43 -07:00
|
|
|
mockDBCall();
|
|
|
|
|
2023-07-18 02:28:24 -07:00
|
|
|
await eventsService.workflowExecutionCompleted(workflow, runData);
|
2023-08-22 06:58:05 -07:00
|
|
|
expect(updateSettingsMock).toHaveBeenCalledTimes(1);
|
2023-07-18 02:28:24 -07:00
|
|
|
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(1);
|
|
|
|
expect(onFirstProductionWorkflowSuccess).toHaveBeenNthCalledWith(1, {
|
2023-03-17 09:24:05 -07:00
|
|
|
user_id: fakeUser.id,
|
2023-01-02 08:42:32 -08:00
|
|
|
workflow_id: workflow.id,
|
2022-12-06 06:55:40 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should only create metrics for production successes', async () => {
|
|
|
|
// Call the function with a non production success result, ensure metrics hook is never called
|
|
|
|
const workflow = {
|
|
|
|
id: '1',
|
|
|
|
name: '',
|
|
|
|
active: false,
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
nodes: [],
|
|
|
|
connections: {},
|
|
|
|
};
|
2023-03-17 09:24:05 -07:00
|
|
|
const runData: IRun = {
|
2022-12-06 06:55:40 -08:00
|
|
|
finished: false,
|
2023-03-17 09:24:05 -07:00
|
|
|
status: 'failed',
|
2022-12-06 06:55:40 -08:00
|
|
|
data: { resultData: { runData: {} } },
|
|
|
|
mode: 'internal' as WorkflowExecuteMode,
|
|
|
|
startedAt: new Date(),
|
|
|
|
};
|
2023-07-18 02:28:24 -07:00
|
|
|
await eventsService.workflowExecutionCompleted(workflow, runData);
|
|
|
|
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(0);
|
2022-12-06 06:55:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should not send metrics for updated entries', async () => {
|
2023-01-30 08:34:26 -08:00
|
|
|
// Call the function with a fail insert, ensure update is called *and* metrics aren't sent
|
2022-12-06 06:55:40 -08:00
|
|
|
const workflow = {
|
2023-01-30 08:34:26 -08:00
|
|
|
id: '1',
|
2022-12-06 06:55:40 -08:00
|
|
|
name: '',
|
|
|
|
active: false,
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
nodes: [],
|
|
|
|
connections: {},
|
|
|
|
};
|
2023-03-17 09:24:05 -07:00
|
|
|
const runData: IRun = {
|
2022-12-06 06:55:40 -08:00
|
|
|
finished: true,
|
2023-03-17 09:24:05 -07:00
|
|
|
status: 'success',
|
2022-12-06 06:55:40 -08:00
|
|
|
data: { resultData: { runData: {} } },
|
|
|
|
mode: 'internal' as WorkflowExecuteMode,
|
|
|
|
startedAt: new Date(),
|
|
|
|
};
|
2023-04-05 05:51:43 -07:00
|
|
|
mockDBCall(2);
|
2023-07-18 02:28:24 -07:00
|
|
|
await eventsService.workflowExecutionCompleted(workflow, runData);
|
|
|
|
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(0);
|
2022-12-06 06:55:40 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('nodeFetchedData', () => {
|
|
|
|
test('should create metrics when the db is updated', async () => {
|
|
|
|
// Call the function with a production success result, ensure metrics hook gets called
|
|
|
|
const workflowId = '1';
|
|
|
|
const node = {
|
|
|
|
id: 'abcde',
|
|
|
|
name: 'test node',
|
|
|
|
typeVersion: 1,
|
|
|
|
type: '',
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
parameters: {},
|
|
|
|
};
|
2023-07-18 02:28:24 -07:00
|
|
|
await eventsService.nodeFetchedData(workflowId, node);
|
|
|
|
expect(onFirstWorkflowDataLoad).toBeCalledTimes(1);
|
|
|
|
expect(onFirstWorkflowDataLoad).toHaveBeenNthCalledWith(1, {
|
2023-03-17 09:24:05 -07:00
|
|
|
user_id: fakeUser.id,
|
2023-01-02 08:42:32 -08:00
|
|
|
workflow_id: workflowId,
|
2022-12-06 06:55:40 -08:00
|
|
|
node_type: node.type,
|
|
|
|
node_id: node.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create metrics with credentials when the db is updated', async () => {
|
|
|
|
// Call the function with a production success result, ensure metrics hook gets called
|
|
|
|
const workflowId = '1';
|
|
|
|
const node = {
|
|
|
|
id: 'abcde',
|
|
|
|
name: 'test node',
|
|
|
|
typeVersion: 1,
|
|
|
|
type: '',
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
parameters: {},
|
|
|
|
credentials: {
|
|
|
|
testCredentials: {
|
|
|
|
id: '1',
|
|
|
|
name: 'Test Credentials',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2023-07-18 02:28:24 -07:00
|
|
|
await eventsService.nodeFetchedData(workflowId, node);
|
|
|
|
expect(onFirstWorkflowDataLoad).toBeCalledTimes(1);
|
|
|
|
expect(onFirstWorkflowDataLoad).toHaveBeenNthCalledWith(1, {
|
2023-03-17 09:24:05 -07:00
|
|
|
user_id: fakeUser.id,
|
2023-01-02 08:42:32 -08:00
|
|
|
workflow_id: workflowId,
|
2022-12-06 06:55:40 -08:00
|
|
|
node_type: node.type,
|
|
|
|
node_id: node.id,
|
|
|
|
credential_type: 'testCredentials',
|
|
|
|
credential_id: node.credentials.testCredentials.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not send metrics for entries that already have the flag set', async () => {
|
|
|
|
// Fetch data for workflow 2 which is set up to not be altered in the mocks
|
2023-07-18 02:28:24 -07:00
|
|
|
entityManager.insert.mockRejectedValueOnce(new QueryFailedError('', undefined, ''));
|
2023-01-30 08:34:26 -08:00
|
|
|
const workflowId = '1';
|
2022-12-06 06:55:40 -08:00
|
|
|
const node = {
|
|
|
|
id: 'abcde',
|
|
|
|
name: 'test node',
|
|
|
|
typeVersion: 1,
|
|
|
|
type: '',
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
parameters: {},
|
|
|
|
};
|
2023-07-18 02:28:24 -07:00
|
|
|
await eventsService.nodeFetchedData(workflowId, node);
|
|
|
|
expect(onFirstWorkflowDataLoad).toBeCalledTimes(0);
|
2022-12-06 06:55:40 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|