Skip to content

Commit

Permalink
refactor command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Jun 11, 2024
1 parent 7026326 commit 5e94e0a
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 251 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/commands/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData'
import config from '@/config';
import type { Job, JobId, JobResponse, WebhookResponse } from '@/Queue';
import { Queue } from '@/Queue';
import { N8N_VERSION } from '@/constants';
import { N8N_VERSION, inTest } from '@/constants';
import { ExecutionRepository } from '@db/repositories/execution.repository';
import { WorkflowRepository } from '@db/repositories/workflow.repository';
import type { ICredentialsOverwrite } from '@/Interfaces';
Expand Down Expand Up @@ -494,7 +494,7 @@ export class Worker extends BaseCommand {
}

// Make sure that the process does not close
await new Promise(() => {});
if (!inTest) await new Promise(() => {});
}

async catch(error: Error) {
Expand Down
45 changes: 14 additions & 31 deletions packages/cli/test/integration/commands/credentials.cmd.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
import { Config } from '@oclif/core';
import { nanoid } from 'nanoid';

import { InternalHooks } from '@/InternalHooks';
import { ImportCredentialsCommand } from '@/commands/import/credentials';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';

import { setupTestCommand } from '@test-integration/utils/testCommand';
import { mockInstance } from '../../shared/mocking';
import * as testDb from '../shared/testDb';
import { getAllCredentials, getAllSharedCredentials } from '../shared/db/credentials';
import { createMember, createOwner } from '../shared/db/users';
import { getPersonalProject } from '../shared/db/projects';
import { nanoid } from 'nanoid';

const oclifConfig = new Config({ root: __dirname });

async function importCredential(argv: string[]) {
const importer = new ImportCredentialsCommand(argv, oclifConfig);
await importer.init();
await importer.run();
}

beforeAll(async () => {
mockInstance(InternalHooks);
mockInstance(LoadNodesAndCredentials);
await testDb.init();
await oclifConfig.load();
});
mockInstance(InternalHooks);
mockInstance(LoadNodesAndCredentials);
const command = setupTestCommand(ImportCredentialsCommand);

beforeEach(async () => {
await testDb.truncate(['Credentials', 'SharedCredentials', 'User']);
});

afterAll(async () => {
await testDb.terminate();
});

test('import:credentials should import a credential', async () => {
//
// ARRANGE
Expand All @@ -44,9 +29,7 @@ test('import:credentials should import a credential', async () => {
//
// ACT
//
await importCredential([
'--input=./test/integration/commands/importCredentials/credentials.json',
]);
await command.run(['--input=./test/integration/commands/importCredentials/credentials.json']);

//
// ASSERT
Expand Down Expand Up @@ -78,7 +61,7 @@ test('import:credentials should import a credential from separated files', async
// ACT
//
// import credential the first time, assigning it to the owner
await importCredential([
await command.run([
'--separate',
'--input=./test/integration/commands/importCredentials/separate',
]);
Expand Down Expand Up @@ -117,7 +100,7 @@ test('`import:credentials --userId ...` should fail if the credential exists alr
const member = await createMember();

// import credential the first time, assigning it to the owner
await importCredential([
await command.run([
'--input=./test/integration/commands/importCredentials/credentials.json',
`--userId=${owner.id}`,
]);
Expand Down Expand Up @@ -145,7 +128,7 @@ test('`import:credentials --userId ...` should fail if the credential exists alr
// Import again while updating the name we try to assign the
// credential to another user.
await expect(
importCredential([
command.run([
'--input=./test/integration/commands/importCredentials/credentials-updated.json',
`--userId=${member.id}`,
]),
Expand Down Expand Up @@ -188,7 +171,7 @@ test("only update credential, don't create or update owner if neither `--userId`
const memberProject = await getPersonalProject(member);

// import credential the first time, assigning it to a member
await importCredential([
await command.run([
'--input=./test/integration/commands/importCredentials/credentials.json',
`--userId=${member.id}`,
]);
Expand All @@ -213,7 +196,7 @@ test("only update credential, don't create or update owner if neither `--userId`
// ACT
//
// Import again only updating the name and omitting `--userId`
await importCredential([
await command.run([
'--input=./test/integration/commands/importCredentials/credentials-updated.json',
]);

Expand Down Expand Up @@ -253,7 +236,7 @@ test('`import:credential --projectId ...` should fail if the credential already
const memberProject = await getPersonalProject(member);

// import credential the first time, assigning it to the owner
await importCredential([
await command.run([
'--input=./test/integration/commands/importCredentials/credentials.json',
`--userId=${owner.id}`,
]);
Expand Down Expand Up @@ -281,7 +264,7 @@ test('`import:credential --projectId ...` should fail if the credential already
// Import again while updating the name we try to assign the
// credential to another user.
await expect(
importCredential([
command.run([
'--input=./test/integration/commands/importCredentials/credentials-updated.json',
`--projectId=${memberProject.id}`,
]),
Expand Down Expand Up @@ -317,7 +300,7 @@ test('`import:credential --projectId ...` should fail if the credential already

test('`import:credential --projectId ... --userId ...` fails explaining that only one of the options can be used at a time', async () => {
await expect(
importCredential([
command.run([
'--input=./test/integration/commands/importCredentials/credentials-updated.json',
`--projectId=${nanoid()}`,
`--userId=${nanoid()}`,
Expand Down
48 changes: 14 additions & 34 deletions packages/cli/test/integration/commands/import.cmd.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
import { Config } from '@oclif/core';
import { nanoid } from 'nanoid';

import { InternalHooks } from '@/InternalHooks';
import { ImportWorkflowsCommand } from '@/commands/import/workflow';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';

import { setupTestCommand } from '@test-integration/utils/testCommand';
import { mockInstance } from '../../shared/mocking';
import * as testDb from '../shared/testDb';
import { getAllSharedWorkflows, getAllWorkflows } from '../shared/db/workflows';
import { createMember, createOwner } from '../shared/db/users';
import { getPersonalProject } from '../shared/db/projects';
import { nanoid } from 'nanoid';

const oclifConfig = new Config({ root: __dirname });

async function importWorkflow(argv: string[]) {
const importer = new ImportWorkflowsCommand(argv, oclifConfig);
await importer.init();
await importer.run();
}

beforeAll(async () => {
mockInstance(InternalHooks);
mockInstance(LoadNodesAndCredentials);
await testDb.init();
await oclifConfig.load();
});
mockInstance(InternalHooks);
mockInstance(LoadNodesAndCredentials);
const command = setupTestCommand(ImportWorkflowsCommand);

beforeEach(async () => {
await testDb.truncate(['Workflow', 'SharedWorkflow', 'User']);
});

afterAll(async () => {
await testDb.terminate();
});

test('import:workflow should import active workflow and deactivate it', async () => {
//
// ARRANGE
Expand All @@ -44,10 +29,7 @@ test('import:workflow should import active workflow and deactivate it', async ()
//
// ACT
//
await importWorkflow([
'--separate',
'--input=./test/integration/commands/importWorkflows/separate',
]);
await command.run(['--separate', '--input=./test/integration/commands/importWorkflows/separate']);

//
// ASSERT
Expand Down Expand Up @@ -86,9 +68,7 @@ test('import:workflow should import active workflow from combined file and deact
//
// ACT
//
await importWorkflow([
'--input=./test/integration/commands/importWorkflows/combined/combined.json',
]);
await command.run(['--input=./test/integration/commands/importWorkflows/combined/combined.json']);

//
// ASSERT
Expand Down Expand Up @@ -126,7 +106,7 @@ test('`import:workflow --userId ...` should fail if the workflow exists already
const member = await createMember();

// Import workflow the first time, assigning it to a member.
await importWorkflow([
await command.run([
'--input=./test/integration/commands/importWorkflows/combined-with-update/original.json',
`--userId=${owner.id}`,
]);
Expand All @@ -153,7 +133,7 @@ test('`import:workflow --userId ...` should fail if the workflow exists already
// Import the same workflow again, with another name but the same ID, and try
// to assign it to the member.
await expect(
importWorkflow([
command.run([
'--input=./test/integration/commands/importWorkflows/combined-with-update/updated.json',
`--userId=${member.id}`,
]),
Expand Down Expand Up @@ -190,7 +170,7 @@ test("only update the workflow, don't create or update the owner if `--userId` i
const memberProject = await getPersonalProject(member);

// Import workflow the first time, assigning it to a member.
await importWorkflow([
await command.run([
'--input=./test/integration/commands/importWorkflows/combined-with-update/original.json',
`--userId=${member.id}`,
]);
Expand All @@ -215,7 +195,7 @@ test("only update the workflow, don't create or update the owner if `--userId` i
// ACT
//
// Import the same workflow again, with another name but the same ID.
await importWorkflow([
await command.run([
'--input=./test/integration/commands/importWorkflows/combined-with-update/updated.json',
]);

Expand Down Expand Up @@ -249,7 +229,7 @@ test('`import:workflow --projectId ...` should fail if the credential already ex
const memberProject = await getPersonalProject(member);

// Import workflow the first time, assigning it to a member.
await importWorkflow([
await command.run([
'--input=./test/integration/commands/importWorkflows/combined-with-update/original.json',
`--userId=${owner.id}`,
]);
Expand All @@ -276,7 +256,7 @@ test('`import:workflow --projectId ...` should fail if the credential already ex
// Import the same workflow again, with another name but the same ID, and try
// to assign it to the member.
await expect(
importWorkflow([
command.run([
'--input=./test/integration/commands/importWorkflows/combined-with-update/updated.json',
`--projectId=${memberProject.id}`,
]),
Expand Down Expand Up @@ -306,7 +286,7 @@ test('`import:workflow --projectId ...` should fail if the credential already ex

test('`import:workflow --projectId ... --userId ...` fails explaining that only one of the options can be used at a time', async () => {
await expect(
importWorkflow([
command.run([
'--input=./test/integration/commands/importWorkflows/combined-with-update/updated.json',
`--userId=${nanoid()}`,
`--projectId=${nanoid()}`,
Expand Down
Loading

0 comments on commit 5e94e0a

Please sign in to comment.