mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 20:24:05 -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)`
|
// pre-computed bcrypt hash for the string 'password', using `await hash('password', 10)`
|
||||||
const passwordHash = '$2a$10$njedH7S6V5898mj6p0Jr..IGY9Ms.qNwR7RbSzzX9yubJocKfvGGK';
|
const passwordHash = '$2a$10$njedH7S6V5898mj6p0Jr..IGY9Ms.qNwR7RbSzzX9yubJocKfvGGK';
|
||||||
|
|
||||||
/**
|
/** Store a new user object, defaulting to a `member` */
|
||||||
* Store a user in the DB, defaulting to a `member`.
|
export async function newUser(attributes: Partial<User> = {}): Promise<User> {
|
||||||
*/
|
|
||||||
export async function createUser(attributes: Partial<User> = {}): Promise<User> {
|
|
||||||
const { email, password, firstName, lastName, role, ...rest } = attributes;
|
const { email, password, firstName, lastName, role, ...rest } = attributes;
|
||||||
const user = Container.get(UserRepository).create({
|
return Container.get(UserRepository).create({
|
||||||
email: email ?? randomEmail(),
|
email: email ?? randomEmail(),
|
||||||
password: password ? await hash(password, 1) : passwordHash,
|
password: password ? await hash(password, 1) : passwordHash,
|
||||||
firstName: firstName ?? randomName(),
|
firstName: firstName ?? randomName(),
|
||||||
|
@ -25,8 +23,12 @@ export async function createUser(attributes: Partial<User> = {}): Promise<User>
|
||||||
role: role ?? 'global:member',
|
role: role ?? 'global:member',
|
||||||
...rest,
|
...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);
|
return await Container.get(UserRepository).save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,21 +100,11 @@ export async function createManyUsers(
|
||||||
amount: number,
|
amount: number,
|
||||||
attributes: Partial<User> = {},
|
attributes: Partial<User> = {},
|
||||||
): Promise<User[]> {
|
): Promise<User[]> {
|
||||||
let { email, password, firstName, lastName, role, ...rest } = attributes;
|
|
||||||
|
|
||||||
const users = await Promise.all(
|
const users = await Promise.all(
|
||||||
[...Array(amount)].map(async () =>
|
Array(amount)
|
||||||
Container.get(UserRepository).create({
|
.fill(0)
|
||||||
email: email ?? randomEmail(),
|
.map(async () => await newUser(attributes)),
|
||||||
password: password ? await hash(password, 1) : passwordHash,
|
|
||||||
firstName: firstName ?? randomName(),
|
|
||||||
lastName: lastName ?? randomName(),
|
|
||||||
role: role ?? 'global:member',
|
|
||||||
...rest,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return await Container.get(UserRepository).save(users);
|
return await Container.get(UserRepository).save(users);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { Container } from 'typedi';
|
import { Container } from 'typedi';
|
||||||
import { mock } from 'jest-mock-extended';
|
import { mock } from 'jest-mock-extended';
|
||||||
import type { DeepPartial } from 'ts-essentials';
|
import type { DeepPartial } from 'ts-essentials';
|
||||||
|
import { DataSource, EntityManager, type EntityMetadata } from '@n8n/typeorm';
|
||||||
import type { Class } from 'n8n-core';
|
import type { Class } from 'n8n-core';
|
||||||
|
|
||||||
export const mockInstance = <T>(
|
export const mockInstance = <T>(
|
||||||
|
@ -11,3 +12,13 @@ export const mockInstance = <T>(
|
||||||
Container.set(serviceClass, instance);
|
Container.set(serviceClass, instance);
|
||||||
return 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 Container from 'typedi';
|
||||||
import type { EntityMetadata } from '@n8n/typeorm';
|
import { Not, LessThanOrEqual } from '@n8n/typeorm';
|
||||||
import { EntityManager, DataSource, Not, LessThanOrEqual } from '@n8n/typeorm';
|
|
||||||
|
|
||||||
import config from '@/config';
|
import config from '@/config';
|
||||||
import { ExecutionEntity } from '@db/entities/ExecutionEntity';
|
import { ExecutionEntity } from '@db/entities/ExecutionEntity';
|
||||||
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
||||||
|
|
||||||
import { mockInstance } from '../../shared/mocking';
|
import { mockEntityManager } from '../../shared/mocking';
|
||||||
|
|
||||||
describe('ExecutionRepository', () => {
|
describe('ExecutionRepository', () => {
|
||||||
const entityManager = mockInstance(EntityManager);
|
const entityManager = mockEntityManager(ExecutionEntity);
|
||||||
const dataSource = mockInstance(DataSource, { manager: entityManager });
|
|
||||||
dataSource.getMetadata.mockReturnValue(mock<EntityMetadata>({ target: ExecutionEntity }));
|
|
||||||
Object.assign(entityManager, { connection: dataSource });
|
|
||||||
|
|
||||||
const executionRepository = Container.get(ExecutionRepository);
|
const executionRepository = Container.get(ExecutionRepository);
|
||||||
const mockDate = new Date('2023-12-28 12:34:56.789Z');
|
const mockDate = new Date('2023-12-28 12:34:56.789Z');
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,16 @@
|
||||||
import { Container } from 'typedi';
|
import { Container } from 'typedi';
|
||||||
import { DataSource, EntityManager, type EntityMetadata } from '@n8n/typeorm';
|
|
||||||
import { mock } from 'jest-mock-extended';
|
import { mock } from 'jest-mock-extended';
|
||||||
|
import { hasScope } from '@n8n/permissions';
|
||||||
|
|
||||||
import type { User } from '@db/entities/User';
|
import type { User } from '@db/entities/User';
|
||||||
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
||||||
import { SharedCredentials } from '@db/entities/SharedCredentials';
|
import { SharedCredentials } from '@db/entities/SharedCredentials';
|
||||||
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
|
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
|
||||||
import { mockInstance } from '../../shared/mocking';
|
|
||||||
import { memberPermissions, ownerPermissions } from '@/permissions/roles';
|
import { memberPermissions, ownerPermissions } from '@/permissions/roles';
|
||||||
import { hasScope } from '@n8n/permissions';
|
import { mockEntityManager } from '../../shared/mocking';
|
||||||
|
|
||||||
describe('SharedCredentialsRepository', () => {
|
describe('SharedCredentialsRepository', () => {
|
||||||
const entityManager = mockInstance(EntityManager);
|
const entityManager = mockEntityManager(SharedCredentials);
|
||||||
const dataSource = mockInstance(DataSource, {
|
|
||||||
manager: entityManager,
|
|
||||||
getMetadata: () => mock<EntityMetadata>({ target: SharedCredentials }),
|
|
||||||
});
|
|
||||||
Object.assign(entityManager, { connection: dataSource });
|
|
||||||
const repository = Container.get(SharedCredentialsRepository);
|
const repository = Container.get(SharedCredentialsRepository);
|
||||||
|
|
||||||
describe('findCredentialForUser', () => {
|
describe('findCredentialForUser', () => {
|
||||||
|
|
|
@ -1,22 +1,23 @@
|
||||||
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';
|
import { Container } from 'typedi';
|
||||||
import { DataSource, EntityManager, InsertResult, QueryFailedError } from '@n8n/typeorm';
|
import { type InsertResult, QueryFailedError } from '@n8n/typeorm';
|
||||||
import { mockInstance } from '../../shared/mocking';
|
|
||||||
import { mock, mockClear } from 'jest-mock-extended';
|
import { mock, mockClear } from 'jest-mock-extended';
|
||||||
import { StatisticsNames, WorkflowStatistics } from '@/databases/entities/WorkflowStatistics';
|
|
||||||
|
|
||||||
const entityManager = mockInstance(EntityManager);
|
import { StatisticsNames, WorkflowStatistics } from '@db/entities/WorkflowStatistics';
|
||||||
const dataSource = mockInstance(DataSource, { manager: entityManager });
|
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';
|
||||||
dataSource.getMetadata.mockReturnValue(mock());
|
|
||||||
Object.assign(entityManager, { connection: dataSource });
|
import { mockEntityManager } from '../../shared/mocking';
|
||||||
const workflowStatisticsRepository = new WorkflowStatisticsRepository(dataSource);
|
|
||||||
|
|
||||||
describe('insertWorkflowStatistics', () => {
|
describe('insertWorkflowStatistics', () => {
|
||||||
|
const entityManager = mockEntityManager(WorkflowStatistics);
|
||||||
|
const workflowStatisticsRepository = Container.get(WorkflowStatisticsRepository);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockClear(entityManager.insert);
|
mockClear(entityManager.insert);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Successfully inserts data when it is not yet present', async () => {
|
it('Successfully inserts data when it is not yet present', async () => {
|
||||||
entityManager.findOne.mockResolvedValueOnce(null);
|
entityManager.findOne.mockResolvedValueOnce(null);
|
||||||
entityManager.insert.mockResolvedValueOnce(mockInstance(InsertResult));
|
entityManager.insert.mockResolvedValueOnce(mock<InsertResult>());
|
||||||
|
|
||||||
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
|
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
|
||||||
StatisticsNames.dataLoaded,
|
StatisticsNames.dataLoaded,
|
||||||
|
@ -27,7 +28,7 @@ describe('insertWorkflowStatistics', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Does not insert when data is present', async () => {
|
it('Does not insert when data is present', async () => {
|
||||||
entityManager.findOne.mockResolvedValueOnce(mockInstance(WorkflowStatistics));
|
entityManager.findOne.mockResolvedValueOnce(mock<WorkflowStatistics>());
|
||||||
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
|
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
|
||||||
StatisticsNames.dataLoaded,
|
StatisticsNames.dataLoaded,
|
||||||
'workflowId',
|
'workflowId',
|
||||||
|
@ -40,7 +41,7 @@ describe('insertWorkflowStatistics', () => {
|
||||||
it('throws an error when insertion fails', async () => {
|
it('throws an error when insertion fails', async () => {
|
||||||
entityManager.findOne.mockResolvedValueOnce(null);
|
entityManager.findOne.mockResolvedValueOnce(null);
|
||||||
entityManager.insert.mockImplementation(async () => {
|
entityManager.insert.mockImplementation(async () => {
|
||||||
throw new QueryFailedError('Query', [], 'driver error');
|
throw new QueryFailedError('Query', [], new Error('driver error'));
|
||||||
});
|
});
|
||||||
|
|
||||||
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
|
const insertionResult = await workflowStatisticsRepository.insertWorkflowStatistics(
|
||||||
|
|
Loading…
Reference in a new issue