diff --git a/packages/cli/test/integration/shared/db/users.ts b/packages/cli/test/integration/shared/db/users.ts index 80508191ad..4f4ed8af18 100644 --- a/packages/cli/test/integration/shared/db/users.ts +++ b/packages/cli/test/integration/shared/db/users.ts @@ -12,12 +12,10 @@ import { randomApiKey, randomEmail, randomName, randomValidPassword } from '../r // pre-computed bcrypt hash for the string 'password', using `await hash('password', 10)` const passwordHash = '$2a$10$njedH7S6V5898mj6p0Jr..IGY9Ms.qNwR7RbSzzX9yubJocKfvGGK'; -/** - * Store a user in the DB, defaulting to a `member`. - */ -export async function createUser(attributes: Partial = {}): Promise { +/** Store a new user object, defaulting to a `member` */ +export async function newUser(attributes: Partial = {}): Promise { const { email, password, firstName, lastName, role, ...rest } = attributes; - const user = Container.get(UserRepository).create({ + return Container.get(UserRepository).create({ email: email ?? randomEmail(), password: password ? await hash(password, 1) : passwordHash, firstName: firstName ?? randomName(), @@ -25,8 +23,12 @@ export async function createUser(attributes: Partial = {}): Promise role: role ?? 'global:member', ...rest, }); - user.computeIsOwner(); +} +/** Store a user object in the DB */ +export async function createUser(attributes: Partial = {}): Promise { + const user = await newUser(attributes); + user.computeIsOwner(); return await Container.get(UserRepository).save(user); } @@ -98,21 +100,11 @@ export async function createManyUsers( amount: number, attributes: Partial = {}, ): Promise { - let { email, password, firstName, lastName, role, ...rest } = attributes; - const users = await Promise.all( - [...Array(amount)].map(async () => - Container.get(UserRepository).create({ - email: email ?? randomEmail(), - password: password ? await hash(password, 1) : passwordHash, - firstName: firstName ?? randomName(), - lastName: lastName ?? randomName(), - role: role ?? 'global:member', - ...rest, - }), - ), + Array(amount) + .fill(0) + .map(async () => await newUser(attributes)), ); - return await Container.get(UserRepository).save(users); } diff --git a/packages/cli/test/shared/mocking.ts b/packages/cli/test/shared/mocking.ts index 7f941378b1..5a6183c0e8 100644 --- a/packages/cli/test/shared/mocking.ts +++ b/packages/cli/test/shared/mocking.ts @@ -1,6 +1,7 @@ import { Container } from 'typedi'; import { mock } from 'jest-mock-extended'; import type { DeepPartial } from 'ts-essentials'; +import { DataSource, EntityManager, type EntityMetadata } from '@n8n/typeorm'; import type { Class } from 'n8n-core'; export const mockInstance = ( @@ -11,3 +12,13 @@ export const mockInstance = ( Container.set(serviceClass, instance); return instance; }; + +export const mockEntityManager = (entityClass: Class) => { + const entityManager = mockInstance(EntityManager); + const dataSource = mockInstance(DataSource, { + manager: entityManager, + getMetadata: () => mock({ target: entityClass }), + }); + Object.assign(entityManager, { connection: dataSource }); + return entityManager; +}; diff --git a/packages/cli/test/unit/repositories/execution.repository.test.ts b/packages/cli/test/unit/repositories/execution.repository.test.ts index 57a223df25..1beae1a487 100644 --- a/packages/cli/test/unit/repositories/execution.repository.test.ts +++ b/packages/cli/test/unit/repositories/execution.repository.test.ts @@ -1,20 +1,14 @@ -import { mock } from 'jest-mock-extended'; import Container from 'typedi'; -import type { EntityMetadata } from '@n8n/typeorm'; -import { EntityManager, DataSource, Not, LessThanOrEqual } from '@n8n/typeorm'; +import { Not, LessThanOrEqual } from '@n8n/typeorm'; import config from '@/config'; import { ExecutionEntity } from '@db/entities/ExecutionEntity'; import { ExecutionRepository } from '@db/repositories/execution.repository'; -import { mockInstance } from '../../shared/mocking'; +import { mockEntityManager } from '../../shared/mocking'; describe('ExecutionRepository', () => { - const entityManager = mockInstance(EntityManager); - const dataSource = mockInstance(DataSource, { manager: entityManager }); - dataSource.getMetadata.mockReturnValue(mock({ target: ExecutionEntity })); - Object.assign(entityManager, { connection: dataSource }); - + const entityManager = mockEntityManager(ExecutionEntity); const executionRepository = Container.get(ExecutionRepository); const mockDate = new Date('2023-12-28 12:34:56.789Z'); diff --git a/packages/cli/test/unit/repositories/sharedCredentials.repository.test.ts b/packages/cli/test/unit/repositories/sharedCredentials.repository.test.ts index 4a808bfb35..1624cf5e65 100644 --- a/packages/cli/test/unit/repositories/sharedCredentials.repository.test.ts +++ b/packages/cli/test/unit/repositories/sharedCredentials.repository.test.ts @@ -1,21 +1,16 @@ import { Container } from 'typedi'; -import { DataSource, EntityManager, type EntityMetadata } from '@n8n/typeorm'; import { mock } from 'jest-mock-extended'; +import { hasScope } from '@n8n/permissions'; + import type { User } from '@db/entities/User'; import type { CredentialsEntity } from '@db/entities/CredentialsEntity'; import { SharedCredentials } from '@db/entities/SharedCredentials'; import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository'; -import { mockInstance } from '../../shared/mocking'; import { memberPermissions, ownerPermissions } from '@/permissions/roles'; -import { hasScope } from '@n8n/permissions'; +import { mockEntityManager } from '../../shared/mocking'; describe('SharedCredentialsRepository', () => { - const entityManager = mockInstance(EntityManager); - const dataSource = mockInstance(DataSource, { - manager: entityManager, - getMetadata: () => mock({ target: SharedCredentials }), - }); - Object.assign(entityManager, { connection: dataSource }); + const entityManager = mockEntityManager(SharedCredentials); const repository = Container.get(SharedCredentialsRepository); describe('findCredentialForUser', () => { diff --git a/packages/cli/test/unit/repositories/workflowStatistics.test.ts b/packages/cli/test/unit/repositories/workflowStatistics.test.ts index ea56b2d84c..86e0ee1c92 100644 --- a/packages/cli/test/unit/repositories/workflowStatistics.test.ts +++ b/packages/cli/test/unit/repositories/workflowStatistics.test.ts @@ -1,22 +1,23 @@ -import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository'; -import { DataSource, EntityManager, InsertResult, QueryFailedError } from '@n8n/typeorm'; -import { mockInstance } from '../../shared/mocking'; +import { Container } from 'typedi'; +import { type InsertResult, QueryFailedError } from '@n8n/typeorm'; import { mock, mockClear } from 'jest-mock-extended'; -import { StatisticsNames, WorkflowStatistics } from '@/databases/entities/WorkflowStatistics'; -const entityManager = mockInstance(EntityManager); -const dataSource = mockInstance(DataSource, { manager: entityManager }); -dataSource.getMetadata.mockReturnValue(mock()); -Object.assign(entityManager, { connection: dataSource }); -const workflowStatisticsRepository = new WorkflowStatisticsRepository(dataSource); +import { StatisticsNames, WorkflowStatistics } from '@db/entities/WorkflowStatistics'; +import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository'; + +import { mockEntityManager } from '../../shared/mocking'; describe('insertWorkflowStatistics', () => { + const entityManager = mockEntityManager(WorkflowStatistics); + const workflowStatisticsRepository = Container.get(WorkflowStatisticsRepository); + beforeEach(() => { mockClear(entityManager.insert); }); + it('Successfully inserts data when it is not yet present', async () => { entityManager.findOne.mockResolvedValueOnce(null); - entityManager.insert.mockResolvedValueOnce(mockInstance(InsertResult)); + entityManager.insert.mockResolvedValueOnce(mock()); const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics( StatisticsNames.dataLoaded, @@ -27,7 +28,7 @@ describe('insertWorkflowStatistics', () => { }); it('Does not insert when data is present', async () => { - entityManager.findOne.mockResolvedValueOnce(mockInstance(WorkflowStatistics)); + entityManager.findOne.mockResolvedValueOnce(mock()); const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics( StatisticsNames.dataLoaded, 'workflowId', @@ -40,7 +41,7 @@ describe('insertWorkflowStatistics', () => { it('throws an error when insertion fails', async () => { entityManager.findOne.mockResolvedValueOnce(null); entityManager.insert.mockImplementation(async () => { - throw new QueryFailedError('Query', [], 'driver error'); + throw new QueryFailedError('Query', [], new Error('driver error')); }); const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(