mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-23 11:44:06 -08:00
ci: Delete some duplicate code in cli tests (no-changelog) (#9049)
This commit is contained in:
parent
b8ab049932
commit
ff77ef4b62
|
@ -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<User> = {}): Promise<User> {
|
||||
/** Store a new user object, defaulting to a `member` */
|
||||
export async function newUser(attributes: Partial<User> = {}): Promise<User> {
|
||||
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<User> = {}): Promise<User>
|
|||
role: role ?? 'global:member',
|
||||
...rest,
|
||||
});
|
||||
user.computeIsOwner();
|
||||
}
|
||||
|
||||
/** Store a user object in the DB */
|
||||
export async function createUser(attributes: Partial<User> = {}): Promise<User> {
|
||||
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<User> = {},
|
||||
): Promise<User[]> {
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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 = <T>(
|
||||
|
@ -11,3 +12,13 @@ export const mockInstance = <T>(
|
|||
Container.set(serviceClass, instance);
|
||||
return instance;
|
||||
};
|
||||
|
||||
export const mockEntityManager = (entityClass: Class) => {
|
||||
const entityManager = mockInstance(EntityManager);
|
||||
const dataSource = mockInstance(DataSource, {
|
||||
manager: entityManager,
|
||||
getMetadata: () => mock<EntityMetadata>({ target: entityClass }),
|
||||
});
|
||||
Object.assign(entityManager, { connection: dataSource });
|
||||
return entityManager;
|
||||
};
|
||||
|
|
|
@ -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<EntityMetadata>({ 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');
|
||||
|
||||
|
|
|
@ -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<EntityMetadata>({ target: SharedCredentials }),
|
||||
});
|
||||
Object.assign(entityManager, { connection: dataSource });
|
||||
const entityManager = mockEntityManager(SharedCredentials);
|
||||
const repository = Container.get(SharedCredentialsRepository);
|
||||
|
||||
describe('findCredentialForUser', () => {
|
||||
|
|
|
@ -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<InsertResult>());
|
||||
|
||||
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<WorkflowStatistics>());
|
||||
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(
|
||||
|
|
Loading…
Reference in a new issue