2024-08-28 08:57:46 -07:00
|
|
|
import type { User } from '@/databases/entities/user';
|
2023-11-10 06:04:26 -08:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
import { createOwner, createUser } from './shared/db/users';
|
2024-08-28 04:59:27 -07:00
|
|
|
import { createWorkflowHistoryItem } from './shared/db/workflow-history';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { createWorkflow } from './shared/db/workflows';
|
|
|
|
import * as testDb from './shared/test-db';
|
2024-05-31 00:40:03 -07:00
|
|
|
import type { SuperAgentTest } from './shared/types';
|
2024-09-12 09:07:18 -07:00
|
|
|
import * as utils from './shared/utils/';
|
2023-09-27 07:22:39 -07:00
|
|
|
|
|
|
|
let owner: User;
|
|
|
|
let authOwnerAgent: SuperAgentTest;
|
|
|
|
let member: User;
|
|
|
|
let authMemberAgent: SuperAgentTest;
|
|
|
|
|
2023-11-30 00:23:09 -08:00
|
|
|
const testServer = utils.setupTestServer({
|
|
|
|
endpointGroups: ['workflowHistory'],
|
|
|
|
enabledFeatures: ['feat:workflowHistory'],
|
2023-09-27 07:22:39 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
owner = await createOwner();
|
2023-09-27 07:22:39 -07:00
|
|
|
authOwnerAgent = testServer.authAgentFor(owner);
|
2023-11-08 07:29:39 -08:00
|
|
|
member = await createUser();
|
2023-09-27 07:22:39 -07:00
|
|
|
authMemberAgent = testServer.authAgentFor(member);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2023-11-10 06:04:26 -08:00
|
|
|
await testDb.truncate(['Workflow', 'SharedWorkflow', 'WorkflowHistory']);
|
2023-09-27 07:22:39 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET /workflow-history/:workflowId', () => {
|
|
|
|
test('should not work when license is not available', async () => {
|
2023-11-30 00:23:09 -08:00
|
|
|
testServer.license.disable('feat:workflowHistory');
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authOwnerAgent.get('/workflow-history/workflow/badid');
|
|
|
|
expect(resp.status).toBe(403);
|
|
|
|
expect(resp.text).toBe('Workflow History license data not found');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not return anything on an invalid workflow ID', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
await createWorkflow(undefined, owner);
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authOwnerAgent.get('/workflow-history/workflow/badid');
|
|
|
|
expect(resp.status).toBe(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not return anything if not shared with user', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authMemberAgent.get('/workflow-history/workflow/' + workflow.id);
|
|
|
|
expect(resp.status).toBe(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return any empty list if no versions', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authOwnerAgent.get('/workflow-history/workflow/' + workflow.id);
|
|
|
|
expect(resp.status).toBe(200);
|
|
|
|
expect(resp.body).toEqual({ data: [] });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return versions for workflow', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
2023-09-27 07:22:39 -07:00
|
|
|
const versions = await Promise.all(
|
|
|
|
new Array(10)
|
|
|
|
.fill(undefined)
|
2024-01-17 07:08:50 -08:00
|
|
|
.map(
|
|
|
|
async (_, i) =>
|
|
|
|
await createWorkflowHistoryItem(workflow.id, { createdAt: new Date(Date.now() + i) }),
|
2023-09-27 07:22:39 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2024-03-26 06:22:57 -07:00
|
|
|
const last = versions.sort((a, b) => b.createdAt.valueOf() - a.createdAt.valueOf())[0] as any;
|
2023-09-27 07:22:39 -07:00
|
|
|
delete last.nodes;
|
|
|
|
delete last.connections;
|
|
|
|
|
|
|
|
last.createdAt = last.createdAt.toISOString();
|
|
|
|
last.updatedAt = last.updatedAt.toISOString();
|
|
|
|
|
|
|
|
const resp = await authOwnerAgent.get('/workflow-history/workflow/' + workflow.id);
|
|
|
|
expect(resp.status).toBe(200);
|
|
|
|
expect(resp.body.data).toHaveLength(10);
|
|
|
|
expect(resp.body.data[0]).toEqual(last);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return versions only for workflow id provided', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
|
|
|
const workflow2 = await createWorkflow(undefined, owner);
|
2023-09-27 07:22:39 -07:00
|
|
|
const versions = await Promise.all(
|
|
|
|
new Array(10)
|
|
|
|
.fill(undefined)
|
2024-01-17 07:08:50 -08:00
|
|
|
.map(
|
|
|
|
async (_, i) =>
|
|
|
|
await createWorkflowHistoryItem(workflow.id, { createdAt: new Date(Date.now() + i) }),
|
2023-09-27 07:22:39 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2024-05-31 05:06:13 -07:00
|
|
|
await Promise.all(
|
2024-01-17 07:08:50 -08:00
|
|
|
new Array(10).fill(undefined).map(async (_) => await createWorkflowHistoryItem(workflow2.id)),
|
2023-09-27 07:22:39 -07:00
|
|
|
);
|
|
|
|
|
2024-03-26 06:22:57 -07:00
|
|
|
const last = versions.sort((a, b) => b.createdAt.valueOf() - a.createdAt.valueOf())[0] as any;
|
2023-09-27 07:22:39 -07:00
|
|
|
delete last.nodes;
|
|
|
|
delete last.connections;
|
|
|
|
|
|
|
|
last.createdAt = last.createdAt.toISOString();
|
|
|
|
last.updatedAt = last.updatedAt.toISOString();
|
|
|
|
|
|
|
|
const resp = await authOwnerAgent.get('/workflow-history/workflow/' + workflow.id);
|
|
|
|
expect(resp.status).toBe(200);
|
|
|
|
expect(resp.body.data).toHaveLength(10);
|
|
|
|
expect(resp.body.data[0]).toEqual(last);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with take parameter', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
2023-09-27 07:22:39 -07:00
|
|
|
const versions = await Promise.all(
|
|
|
|
new Array(10)
|
|
|
|
.fill(undefined)
|
2024-01-17 07:08:50 -08:00
|
|
|
.map(
|
|
|
|
async (_, i) =>
|
|
|
|
await createWorkflowHistoryItem(workflow.id, { createdAt: new Date(Date.now() + i) }),
|
2023-09-27 07:22:39 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2024-03-26 06:22:57 -07:00
|
|
|
const last = versions.sort((a, b) => b.createdAt.valueOf() - a.createdAt.valueOf())[0] as any;
|
2023-09-27 07:22:39 -07:00
|
|
|
delete last.nodes;
|
|
|
|
delete last.connections;
|
|
|
|
|
|
|
|
last.createdAt = last.createdAt.toISOString();
|
|
|
|
last.updatedAt = last.updatedAt.toISOString();
|
|
|
|
|
|
|
|
const resp = await authOwnerAgent.get(`/workflow-history/workflow/${workflow.id}?take=5`);
|
|
|
|
expect(resp.status).toBe(200);
|
|
|
|
expect(resp.body.data).toHaveLength(5);
|
|
|
|
expect(resp.body.data[0]).toEqual(last);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with skip parameter', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
2023-09-27 07:22:39 -07:00
|
|
|
const versions = await Promise.all(
|
|
|
|
new Array(10)
|
|
|
|
.fill(undefined)
|
2024-01-17 07:08:50 -08:00
|
|
|
.map(
|
|
|
|
async (_, i) =>
|
|
|
|
await createWorkflowHistoryItem(workflow.id, { createdAt: new Date(Date.now() + i) }),
|
2023-09-27 07:22:39 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2024-03-26 06:22:57 -07:00
|
|
|
const last = versions.sort((a, b) => b.createdAt.valueOf() - a.createdAt.valueOf())[5] as any;
|
2023-09-27 07:22:39 -07:00
|
|
|
delete last.nodes;
|
|
|
|
delete last.connections;
|
|
|
|
|
|
|
|
last.createdAt = last.createdAt.toISOString();
|
|
|
|
last.updatedAt = last.updatedAt.toISOString();
|
|
|
|
|
|
|
|
const resp = await authOwnerAgent.get(
|
|
|
|
`/workflow-history/workflow/${workflow.id}?skip=5&take=20`,
|
|
|
|
);
|
|
|
|
expect(resp.status).toBe(200);
|
|
|
|
expect(resp.body.data).toHaveLength(5);
|
|
|
|
expect(resp.body.data[0]).toEqual(last);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET /workflow-history/workflow/:workflowId/version/:versionId', () => {
|
|
|
|
test('should not work when license is not available', async () => {
|
2023-11-30 00:23:09 -08:00
|
|
|
testServer.license.disable('feat:workflowHistory');
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authOwnerAgent.get('/workflow-history/workflow/badid/version/badid');
|
|
|
|
expect(resp.status).toBe(403);
|
|
|
|
expect(resp.text).toBe('Workflow History license data not found');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not return anything on an invalid workflow ID', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
|
|
|
const version = await createWorkflowHistoryItem(workflow.id);
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authOwnerAgent.get(
|
|
|
|
`/workflow-history/workflow/badid/version/${version.versionId}`,
|
|
|
|
);
|
|
|
|
expect(resp.status).toBe(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not return anything on an invalid version ID', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
|
|
|
await createWorkflowHistoryItem(workflow.id);
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authOwnerAgent.get(
|
|
|
|
`/workflow-history/workflow/${workflow.id}/version/badid`,
|
|
|
|
);
|
|
|
|
expect(resp.status).toBe(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return version', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
|
|
|
const version = await createWorkflowHistoryItem(workflow.id);
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authOwnerAgent.get(
|
|
|
|
`/workflow-history/workflow/${workflow.id}/version/${version.versionId}`,
|
|
|
|
);
|
|
|
|
expect(resp.status).toBe(200);
|
|
|
|
expect(resp.body.data).toEqual({
|
|
|
|
...version,
|
|
|
|
createdAt: version.createdAt.toISOString(),
|
|
|
|
updatedAt: version.updatedAt.toISOString(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not return anything if not shared with user', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
|
|
|
const version = await createWorkflowHistoryItem(workflow.id);
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authMemberAgent.get(
|
|
|
|
`/workflow-history/workflow/${workflow.id}/version/${version.versionId}`,
|
|
|
|
);
|
|
|
|
expect(resp.status).toBe(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not return anything if not shared with user and using workflow owned by unshared user', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const workflow = await createWorkflow(undefined, owner);
|
|
|
|
const workflowMember = await createWorkflow(undefined, member);
|
|
|
|
const version = await createWorkflowHistoryItem(workflow.id);
|
2023-09-27 07:22:39 -07:00
|
|
|
const resp = await authMemberAgent.get(
|
|
|
|
`/workflow-history/workflow/${workflowMember.id}/version/${version.versionId}`,
|
|
|
|
);
|
|
|
|
expect(resp.status).toBe(404);
|
|
|
|
});
|
|
|
|
});
|