From 6d5b495d617c1c86eebef792e3f45eee8a334ff0 Mon Sep 17 00:00:00 2001 From: Danny Martini Date: Mon, 22 Jan 2024 18:02:17 +0100 Subject: [PATCH] add test for `n8n update:workflow ...` cli command and fix activating all workflows --- packages/cli/src/commands/update/workflow.ts | 12 +- .../commands/update/workflow.test.ts | 126 ++++++++++++++++++ 2 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 packages/cli/test/integration/commands/update/workflow.test.ts diff --git a/packages/cli/src/commands/update/workflow.ts b/packages/cli/src/commands/update/workflow.ts index 08f94033686816..86f6482db4f49b 100644 --- a/packages/cli/src/commands/update/workflow.ts +++ b/packages/cli/src/commands/update/workflow.ts @@ -51,13 +51,19 @@ export class UpdateWorkflowCommand extends BaseCommand { } const newState = flags.active === 'true'; + const action = newState ? 'Activating' : 'Deactivating'; if (flags.id) { - this.logger.info(`Deactivating workflow with ID: ${flags.id}`); + this.logger.info(`${action} workflow with ID: ${flags.id}`); await Container.get(WorkflowRepository).updateActiveState(flags.id, newState); } else { - this.logger.info('Deactivating all workflows'); - await Container.get(WorkflowRepository).deactivateAll(); + this.logger.info(`${action} all workflows`); + if (newState) { + // TODO: Maybe there should be an `updateActiveStateForAll(state)`? + await Container.get(WorkflowRepository).activateAll(); + } else { + await Container.get(WorkflowRepository).deactivateAll(); + } } this.logger.info('Done'); diff --git a/packages/cli/test/integration/commands/update/workflow.test.ts b/packages/cli/test/integration/commands/update/workflow.test.ts new file mode 100644 index 00000000000000..bf9efecf61194e --- /dev/null +++ b/packages/cli/test/integration/commands/update/workflow.test.ts @@ -0,0 +1,126 @@ +import { Config } from '@oclif/config'; +import { InternalHooks } from '@/InternalHooks'; +import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials'; +import { UpdateWorkflowCommand } from '@/commands/update/workflow'; + +import * as testDb from '../../shared/testDb'; +import { createWorkflowWithTrigger, getAllWorkflows } from '../../shared/db/workflows'; +import { mockInstance } from '../../../shared/mocking'; + +beforeAll(async () => { + mockInstance(InternalHooks); + mockInstance(LoadNodesAndCredentials); + await testDb.init(); +}); + +beforeEach(async () => { + await testDb.truncate(['Workflow']); +}); + +afterAll(async () => { + await testDb.terminate(); +}); + +test('update:workflow can activate all workflows', async () => { + // + // ARRANGE + // + const workflows = await Promise.all([ + createWorkflowWithTrigger({}), + createWorkflowWithTrigger({}), + ]); + expect(workflows).toMatchObject([{ active: false }, { active: false }]); + + // + // ACT + // + const config = new Config({ root: __dirname }); + const updater = new UpdateWorkflowCommand(['--all', '--active=true'], config); + await updater.init(); + await updater.run(); + + // + // ASSERT + // + const after = await getAllWorkflows(); + expect(after).toMatchObject([{ active: true }, { active: true }]); +}); + +test('update:workflow can deactivate all workflows', async () => { + // + // ARRANGE + // + const workflows = await Promise.all([ + createWorkflowWithTrigger({ active: true }), + createWorkflowWithTrigger({ active: true }), + ]); + expect(workflows).toMatchObject([{ active: true }, { active: true }]); + + // + // ACT + // + const config = new Config({ root: __dirname }); + const updater = new UpdateWorkflowCommand(['--all', '--active=false'], config); + await updater.init(); + await updater.run(); + + // + // ASSERT + // + const after = await getAllWorkflows(); + expect(after).toMatchObject([{ active: false }, { active: false }]); +}); + +test('update:workflow can activate a specific workflow', async () => { + // + // ARRANGE + // + const workflows = ( + await Promise.all([ + createWorkflowWithTrigger({ active: false }), + createWorkflowWithTrigger({ active: false }), + ]) + ).sort((wf1, wf2) => wf1.id.localeCompare(wf2.id)); + expect(workflows).toMatchObject([{ active: false }, { active: false }]); + + // + // ACT + // + const config = new Config({ root: __dirname }); + const updater = new UpdateWorkflowCommand([`--id=${workflows[0].id}`, '--active=true'], config); + await updater.init(); + await updater.run(); + + // + // ASSERT + // + const after = (await getAllWorkflows()).sort((wf1, wf2) => wf1.id.localeCompare(wf2.id)); + expect(after).toMatchObject([{ active: true }, { active: false }]); +}); + +test('update:workflow can deactivate a specific workflow', async () => { + // + // ARRANGE + // + const workflows = ( + await Promise.all([ + createWorkflowWithTrigger({ active: true }), + createWorkflowWithTrigger({ active: true }), + ]) + ).sort((wf1, wf2) => wf1.id.localeCompare(wf2.id)); + expect(workflows).toMatchObject([{ active: true }, { active: true }]); + + // + // ACT + // + const config = new Config({ root: __dirname }); + const updater = new UpdateWorkflowCommand([`--id=${workflows[0].id}`, '--active=false'], config); + await updater.init(); + await updater.run(); + + // + // ASSERT + // + const after = (await getAllWorkflows()).sort((wf1, wf2) => wf1.id.localeCompare(wf2.id)); + expect(after).toMatchObject([{ active: false }, { active: true }]); +});