mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
refactor(core): Switch over all user-management routes to use decorators (#5115)
This commit is contained in:
committed by
GitHub
parent
08a90d7e09
commit
845f0f9d20
@@ -22,7 +22,6 @@ import {
|
||||
toCronExpression,
|
||||
TriggerTime,
|
||||
} from 'n8n-workflow';
|
||||
import type { N8nApp } from '@/UserManagement/Interfaces';
|
||||
import superagent from 'superagent';
|
||||
import request from 'supertest';
|
||||
import { URL } from 'url';
|
||||
@@ -34,11 +33,6 @@ import { ExternalHooks } from '@/ExternalHooks';
|
||||
import { InternalHooksManager } from '@/InternalHooksManager';
|
||||
import { NodeTypes } from '@/NodeTypes';
|
||||
import * as ActiveWorkflowRunner from '@/ActiveWorkflowRunner';
|
||||
import { meNamespace as meEndpoints } from '@/UserManagement/routes/me';
|
||||
import { usersNamespace as usersEndpoints } from '@/UserManagement/routes/users';
|
||||
import { authenticationMethods as authEndpoints } from '@/UserManagement/routes/auth';
|
||||
import { ownerNamespace as ownerEndpoints } from '@/UserManagement/routes/owner';
|
||||
import { passwordResetNamespace as passwordResetEndpoints } from '@/UserManagement/routes/passwordReset';
|
||||
import { nodesController } from '@/api/nodes.api';
|
||||
import { workflowsController } from '@/workflows/workflows.controller';
|
||||
import { AUTH_COOKIE_NAME, NODE_PACKAGE_PREFIX } from '@/constants';
|
||||
@@ -47,8 +41,8 @@ import { InstalledPackages } from '@db/entities/InstalledPackages';
|
||||
import type { User } from '@db/entities/User';
|
||||
import { getLogger } from '@/Logger';
|
||||
import { loadPublicApiVersions } from '@/PublicApi/';
|
||||
import { issueJWT } from '@/UserManagement/auth/jwt';
|
||||
import { addRoutes as authMiddleware } from '@/UserManagement/routes';
|
||||
import { issueJWT } from '@/auth/jwt';
|
||||
import * as UserManagementMailer from '@/UserManagement/email/UserManagementMailer';
|
||||
import {
|
||||
AUTHLESS_ENDPOINTS,
|
||||
COMMUNITY_NODE_VERSION,
|
||||
@@ -66,9 +60,19 @@ import type {
|
||||
} from './types';
|
||||
import { licenseController } from '@/license/license.controller';
|
||||
import { eventBusRouter } from '@/eventbus/eventBusRoutes';
|
||||
import { registerController } from '@/decorators';
|
||||
import {
|
||||
AuthController,
|
||||
MeController,
|
||||
OwnerController,
|
||||
PasswordResetController,
|
||||
UsersController,
|
||||
} from '@/controllers';
|
||||
import { setupAuthMiddlewares } from '@/middlewares';
|
||||
import * as testDb from '../shared/testDb';
|
||||
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { handleLdapInit } from '../../../src/Ldap/helpers';
|
||||
import { handleLdapInit } from '@/Ldap/helpers';
|
||||
import { ldapController } from '@/Ldap/routes/ldap.controller.ee';
|
||||
|
||||
const loadNodesAndCredentials: INodesAndCredentials = {
|
||||
@@ -84,14 +88,15 @@ CredentialTypes(loadNodesAndCredentials);
|
||||
* Initialize a test server.
|
||||
*/
|
||||
export async function initTestServer({
|
||||
applyAuth,
|
||||
applyAuth = true,
|
||||
endpointGroups,
|
||||
enablePublicAPI = false,
|
||||
}: {
|
||||
applyAuth: boolean;
|
||||
applyAuth?: boolean;
|
||||
endpointGroups?: EndpointGroup[];
|
||||
enablePublicAPI?: boolean;
|
||||
}) {
|
||||
await testDb.init();
|
||||
const testServer = {
|
||||
app: express(),
|
||||
restEndpoint: REST_PATH_SEGMENT,
|
||||
@@ -99,6 +104,12 @@ export async function initTestServer({
|
||||
externalHooks: {},
|
||||
};
|
||||
|
||||
const logger = getLogger();
|
||||
LoggerProxy.init(logger);
|
||||
|
||||
// Pre-requisite: Mock the telemetry module before calling.
|
||||
await InternalHooksManager.init('test-instance-id', mockNodeTypes);
|
||||
|
||||
testServer.app.use(bodyParser.json());
|
||||
testServer.app.use(bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
@@ -106,7 +117,12 @@ export async function initTestServer({
|
||||
config.set('userManagement.isInstanceOwnerSetUp', false);
|
||||
|
||||
if (applyAuth) {
|
||||
authMiddleware.apply(testServer, [AUTHLESS_ENDPOINTS, REST_PATH_SEGMENT]);
|
||||
setupAuthMiddlewares(
|
||||
testServer.app,
|
||||
AUTHLESS_ENDPOINTS,
|
||||
REST_PATH_SEGMENT,
|
||||
Db.collections.User,
|
||||
);
|
||||
}
|
||||
|
||||
if (!endpointGroups) return testServer.app;
|
||||
@@ -147,36 +163,75 @@ export async function initTestServer({
|
||||
}
|
||||
|
||||
if (functionEndpoints.length) {
|
||||
const map: Record<string, (this: N8nApp) => void> = {
|
||||
me: meEndpoints,
|
||||
users: usersEndpoints,
|
||||
auth: authEndpoints,
|
||||
owner: ownerEndpoints,
|
||||
passwordReset: passwordResetEndpoints,
|
||||
};
|
||||
const externalHooks = ExternalHooks();
|
||||
const internalHooks = InternalHooksManager.getInstance();
|
||||
const mailer = UserManagementMailer.getInstance();
|
||||
const repositories = Db.collections;
|
||||
|
||||
for (const group of functionEndpoints) {
|
||||
map[group].apply(testServer);
|
||||
switch (group) {
|
||||
case 'auth':
|
||||
registerController(
|
||||
testServer.app,
|
||||
config,
|
||||
new AuthController({ config, logger, internalHooks, repositories }),
|
||||
);
|
||||
break;
|
||||
case 'me':
|
||||
registerController(
|
||||
testServer.app,
|
||||
config,
|
||||
new MeController({ logger, externalHooks, internalHooks, repositories }),
|
||||
);
|
||||
break;
|
||||
case 'passwordReset':
|
||||
registerController(
|
||||
testServer.app,
|
||||
config,
|
||||
new PasswordResetController({
|
||||
config,
|
||||
logger,
|
||||
externalHooks,
|
||||
internalHooks,
|
||||
repositories,
|
||||
}),
|
||||
);
|
||||
break;
|
||||
case 'owner':
|
||||
registerController(
|
||||
testServer.app,
|
||||
config,
|
||||
new OwnerController({ config, logger, internalHooks, repositories }),
|
||||
);
|
||||
break;
|
||||
case 'users':
|
||||
registerController(
|
||||
testServer.app,
|
||||
config,
|
||||
new UsersController({
|
||||
config,
|
||||
mailer,
|
||||
externalHooks,
|
||||
internalHooks,
|
||||
repositories,
|
||||
activeWorkflowRunner: ActiveWorkflowRunner.getInstance(),
|
||||
logger,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return testServer.app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-requisite: Mock the telemetry module before calling.
|
||||
*/
|
||||
export function initTestTelemetry() {
|
||||
void InternalHooksManager.init('test-instance-id', mockNodeTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify endpoint groups into `routerEndpoints` (newest, using `express.Router`),
|
||||
* and `functionEndpoints` (legacy, namespaced inside a function).
|
||||
*/
|
||||
const classifyEndpointGroups = (endpointGroups: string[]) => {
|
||||
const routerEndpoints: string[] = [];
|
||||
const functionEndpoints: string[] = [];
|
||||
const classifyEndpointGroups = (endpointGroups: EndpointGroup[]) => {
|
||||
const routerEndpoints: EndpointGroup[] = [];
|
||||
const functionEndpoints: EndpointGroup[] = [];
|
||||
|
||||
const ROUTER_GROUP = [
|
||||
'credentials',
|
||||
@@ -559,13 +614,6 @@ export async function initNodeTypes() {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a logger for test runs.
|
||||
*/
|
||||
export function initTestLogger() {
|
||||
LoggerProxy.init(getLogger());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a BinaryManager for test runs.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user