2023-07-13 01:14:48 -07:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
import cookieParser from 'cookie-parser';
|
|
|
|
import express from 'express';
|
|
|
|
import type superagent from 'superagent';
|
|
|
|
import request from 'supertest';
|
|
|
|
import { URL } from 'url';
|
|
|
|
|
|
|
|
import config from '@/config';
|
|
|
|
import { AUTH_COOKIE_NAME } from '@/constants';
|
|
|
|
import type { User } from '@db/entities/User';
|
|
|
|
import { issueJWT } from '@/auth/jwt';
|
|
|
|
import { registerController } from '@/decorators';
|
2023-08-11 00:18:33 -07:00
|
|
|
import { rawBodyReader, bodyParser, setupAuthMiddlewares } from '@/middlewares';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { PostHogClient } from '@/posthog';
|
|
|
|
import { License } from '@/License';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Logger } from '@/Logger';
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../../../shared/mocking';
|
2023-11-08 07:29:39 -08:00
|
|
|
import * as testDb from '../../shared/testDb';
|
|
|
|
import { AUTHLESS_ENDPOINTS, PUBLIC_API_REST_PATH_SEGMENT, REST_PATH_SEGMENT } from '../constants';
|
|
|
|
import type { SetupProps, TestServer } from '../types';
|
2023-11-16 09:39:43 -08:00
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
/**
|
|
|
|
* Plugin to prefix a path segment into a request URL pathname.
|
|
|
|
*
|
|
|
|
* Example: http://127.0.0.1:62100/me/password → http://127.0.0.1:62100/rest/me/password
|
|
|
|
*/
|
|
|
|
function prefix(pathSegment: string) {
|
|
|
|
return async function (request: superagent.SuperAgentRequest) {
|
|
|
|
const url = new URL(request.url);
|
|
|
|
|
|
|
|
// enforce consistency at call sites
|
|
|
|
if (url.pathname[0] !== '/') {
|
|
|
|
throw new Error('Pathname must start with a forward slash');
|
|
|
|
}
|
|
|
|
|
|
|
|
url.pathname = pathSegment + url.pathname;
|
|
|
|
request.url = url.toString();
|
|
|
|
return request;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createAgent(app: express.Application, options?: { auth: boolean; user: User }) {
|
|
|
|
const agent = request.agent(app);
|
|
|
|
void agent.use(prefix(REST_PATH_SEGMENT));
|
|
|
|
if (options?.auth && options?.user) {
|
2023-08-03 05:03:26 -07:00
|
|
|
const { token } = issueJWT(options.user);
|
|
|
|
agent.jar.setCookie(`${AUTH_COOKIE_NAME}=${token}`);
|
2023-07-13 01:14:48 -07:00
|
|
|
}
|
|
|
|
return agent;
|
|
|
|
}
|
|
|
|
|
|
|
|
function publicApiAgent(
|
|
|
|
app: express.Application,
|
|
|
|
{ user, version = 1 }: { user: User; version?: number },
|
|
|
|
) {
|
|
|
|
const agent = request.agent(app);
|
|
|
|
void agent.use(prefix(`${PUBLIC_API_REST_PATH_SEGMENT}/v${version}`));
|
|
|
|
if (user.apiKey) {
|
|
|
|
void agent.set({ 'X-N8N-API-KEY': user.apiKey });
|
|
|
|
}
|
|
|
|
return agent;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const setupTestServer = ({
|
|
|
|
endpointGroups,
|
|
|
|
applyAuth = true,
|
|
|
|
enabledFeatures,
|
|
|
|
}: SetupProps): TestServer => {
|
|
|
|
const app = express();
|
2023-08-11 00:18:33 -07:00
|
|
|
app.use(rawBodyReader);
|
2023-08-01 08:32:30 -07:00
|
|
|
app.use(cookieParser());
|
|
|
|
|
2023-10-25 07:35:22 -07:00
|
|
|
// Mock all telemetry and logging
|
|
|
|
const logger = mockInstance(Logger);
|
|
|
|
mockInstance(InternalHooks);
|
|
|
|
mockInstance(PostHogClient);
|
2023-09-20 06:21:42 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const testServer: TestServer = {
|
|
|
|
app,
|
|
|
|
httpServer: app.listen(0),
|
|
|
|
authAgentFor: (user: User) => createAgent(app, { auth: true, user }),
|
|
|
|
authlessAgent: createAgent(app),
|
|
|
|
publicApiAgentFor: (user) => publicApiAgent(app, { user }),
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await testDb.init();
|
|
|
|
|
|
|
|
config.set('userManagement.jwtSecret', 'My JWT secret');
|
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', true);
|
|
|
|
|
|
|
|
if (enabledFeatures) {
|
|
|
|
Container.get(License).isFeatureEnabled = (feature) => enabledFeatures.includes(feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
const enablePublicAPI = endpointGroups?.includes('publicApi');
|
|
|
|
if (applyAuth && !enablePublicAPI) {
|
|
|
|
setupAuthMiddlewares(app, AUTHLESS_ENDPOINTS, REST_PATH_SEGMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!endpointGroups) return;
|
|
|
|
|
2023-08-11 00:18:33 -07:00
|
|
|
app.use(bodyParser);
|
2023-08-01 08:32:30 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
if (enablePublicAPI) {
|
|
|
|
const { loadPublicApiVersions } = await import('@/PublicApi');
|
|
|
|
const { apiRouters } = await loadPublicApiVersions(PUBLIC_API_REST_PATH_SEGMENT);
|
|
|
|
app.use(...apiRouters);
|
|
|
|
}
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
if (endpointGroups.length) {
|
|
|
|
for (const group of endpointGroups) {
|
|
|
|
switch (group) {
|
|
|
|
case 'credentials':
|
|
|
|
const { credentialsController } = await import('@/credentials/credentials.controller');
|
|
|
|
app.use(`/${REST_PATH_SEGMENT}/credentials`, credentialsController);
|
|
|
|
break;
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
case 'workflows':
|
|
|
|
const { workflowsController } = await import('@/workflows/workflows.controller');
|
|
|
|
app.use(`/${REST_PATH_SEGMENT}/workflows`, workflowsController);
|
|
|
|
break;
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
case 'executions':
|
|
|
|
const { executionsController } = await import('@/executions/executions.controller');
|
|
|
|
app.use(`/${REST_PATH_SEGMENT}/executions`, executionsController);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'variables':
|
|
|
|
const { variablesController } = await import(
|
|
|
|
'@/environments/variables/variables.controller'
|
|
|
|
);
|
|
|
|
app.use(`/${REST_PATH_SEGMENT}/variables`, variablesController);
|
|
|
|
break;
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
case 'license':
|
|
|
|
const { licenseController } = await import('@/license/license.controller');
|
|
|
|
app.use(`/${REST_PATH_SEGMENT}/license`, licenseController);
|
|
|
|
break;
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-08-04 11:51:07 -07:00
|
|
|
case 'metrics':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { MetricsService } = await import('@/services/metrics.service');
|
2023-08-04 11:51:07 -07:00
|
|
|
await Container.get(MetricsService).configureMetrics(app);
|
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'eventBus':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { EventBusController } = await import('@/eventbus/eventBus.controller');
|
|
|
|
const { EventBusControllerEE } = await import('@/eventbus/eventBus.controller.ee');
|
2023-07-13 01:14:48 -07:00
|
|
|
registerController(app, config, new EventBusController());
|
2023-09-05 04:32:09 -07:00
|
|
|
registerController(app, config, new EventBusControllerEE());
|
2023-07-13 01:14:48 -07:00
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'auth':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { AuthController } = await import('@/controllers/auth.controller');
|
2023-10-25 07:35:22 -07:00
|
|
|
registerController(app, config, Container.get(AuthController));
|
2023-07-13 01:14:48 -07:00
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-08-23 19:59:16 -07:00
|
|
|
case 'mfa':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { MFAController } = await import('@/controllers/mfa.controller');
|
|
|
|
registerController(app, config, Container.get(MFAController));
|
|
|
|
break;
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'ldap':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { LdapManager } = await import('@/Ldap/LdapManager.ee');
|
|
|
|
const { handleLdapInit } = await import('@/Ldap/helpers');
|
|
|
|
const { LdapController } = await import('@/controllers/ldap.controller');
|
2023-07-13 01:14:48 -07:00
|
|
|
Container.get(License).isLdapEnabled = () => true;
|
|
|
|
await handleLdapInit();
|
|
|
|
const { service, sync } = LdapManager.getInstance();
|
2023-11-08 07:29:39 -08:00
|
|
|
registerController(
|
|
|
|
app,
|
|
|
|
config,
|
|
|
|
new LdapController(service, sync, Container.get(InternalHooks)),
|
|
|
|
);
|
2023-07-13 01:14:48 -07:00
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'saml':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { setSamlLoginEnabled } = await import('@/sso/saml/samlHelpers');
|
|
|
|
const { SamlController } = await import('@/sso/saml/routes/saml.controller.ee');
|
2023-07-13 01:14:48 -07:00
|
|
|
await setSamlLoginEnabled(true);
|
|
|
|
registerController(app, config, Container.get(SamlController));
|
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'sourceControl':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { SourceControlController } = await import(
|
|
|
|
'@/environments/sourceControl/sourceControl.controller.ee'
|
|
|
|
);
|
2023-07-13 01:14:48 -07:00
|
|
|
registerController(app, config, Container.get(SourceControlController));
|
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
case 'community-packages':
|
|
|
|
const { CommunityPackagesController } = await import(
|
|
|
|
'@/controllers/communityPackages.controller'
|
2023-07-13 01:14:48 -07:00
|
|
|
);
|
2023-10-09 07:09:23 -07:00
|
|
|
registerController(app, config, Container.get(CommunityPackagesController));
|
2023-11-08 07:29:39 -08:00
|
|
|
break;
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'me':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { MeController } = await import('@/controllers/me.controller');
|
2023-10-25 07:35:22 -07:00
|
|
|
registerController(app, config, Container.get(MeController));
|
2023-07-13 01:14:48 -07:00
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'passwordReset':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { PasswordResetController } = await import(
|
|
|
|
'@/controllers/passwordReset.controller'
|
|
|
|
);
|
2023-11-07 06:35:43 -08:00
|
|
|
registerController(app, config, Container.get(PasswordResetController));
|
2023-07-13 01:14:48 -07:00
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'owner':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { UserService } = await import('@/services/user.service');
|
2023-11-10 06:04:26 -08:00
|
|
|
const { SettingsRepository } = await import('@db/repositories/settings.repository');
|
2023-11-08 07:29:39 -08:00
|
|
|
const { OwnerController } = await import('@/controllers/owner.controller');
|
2023-07-13 01:14:48 -07:00
|
|
|
registerController(
|
|
|
|
app,
|
|
|
|
config,
|
2023-08-25 04:23:22 -07:00
|
|
|
new OwnerController(
|
2023-08-22 06:58:05 -07:00
|
|
|
config,
|
|
|
|
logger,
|
2023-11-08 07:29:39 -08:00
|
|
|
Container.get(InternalHooks),
|
2023-08-25 04:23:22 -07:00
|
|
|
Container.get(SettingsRepository),
|
2023-11-08 07:29:39 -08:00
|
|
|
Container.get(UserService),
|
2023-08-25 04:23:22 -07:00
|
|
|
),
|
2023-07-13 01:14:48 -07:00
|
|
|
);
|
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
case 'users':
|
2023-11-10 06:04:26 -08:00
|
|
|
const { SharedCredentialsRepository } = await import(
|
|
|
|
'@db/repositories/sharedCredentials.repository'
|
|
|
|
);
|
|
|
|
const { SharedWorkflowRepository } = await import(
|
|
|
|
'@db/repositories/sharedWorkflow.repository'
|
|
|
|
);
|
2023-11-08 07:29:39 -08:00
|
|
|
const { ActiveWorkflowRunner } = await import('@/ActiveWorkflowRunner');
|
|
|
|
const { UserService: US } = await import('@/services/user.service');
|
2023-11-16 09:39:43 -08:00
|
|
|
const { ExternalHooks: EH } = await import('@/ExternalHooks');
|
|
|
|
const { RoleService: RS } = await import('@/services/role.service');
|
2023-11-08 07:29:39 -08:00
|
|
|
const { UsersController } = await import('@/controllers/users.controller');
|
2023-07-13 01:14:48 -07:00
|
|
|
registerController(
|
|
|
|
app,
|
|
|
|
config,
|
2023-08-25 04:23:22 -07:00
|
|
|
new UsersController(
|
|
|
|
logger,
|
2023-11-16 09:39:43 -08:00
|
|
|
Container.get(EH),
|
2023-11-08 07:29:39 -08:00
|
|
|
Container.get(InternalHooks),
|
2023-08-25 04:23:22 -07:00
|
|
|
Container.get(SharedCredentialsRepository),
|
|
|
|
Container.get(SharedWorkflowRepository),
|
|
|
|
Container.get(ActiveWorkflowRunner),
|
2023-11-16 09:39:43 -08:00
|
|
|
Container.get(RS),
|
2023-11-08 07:29:39 -08:00
|
|
|
Container.get(US),
|
2023-08-25 04:23:22 -07:00
|
|
|
),
|
2023-07-13 01:14:48 -07:00
|
|
|
);
|
2023-07-20 02:34:45 -07:00
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
case 'invitations':
|
|
|
|
const { InvitationController } = await import('@/controllers/invitation.controller');
|
|
|
|
const { ExternalHooks: EHS } = await import('@/ExternalHooks');
|
|
|
|
const { UserService: USE } = await import('@/services/user.service');
|
|
|
|
|
|
|
|
registerController(
|
|
|
|
app,
|
|
|
|
config,
|
|
|
|
new InvitationController(
|
|
|
|
config,
|
|
|
|
logger,
|
|
|
|
Container.get(InternalHooks),
|
|
|
|
Container.get(EHS),
|
|
|
|
Container.get(USE),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2023-07-20 02:34:45 -07:00
|
|
|
case 'tags':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { TagsController } = await import('@/controllers/tags.controller');
|
2023-08-08 05:08:56 -07:00
|
|
|
registerController(app, config, Container.get(TagsController));
|
2023-07-20 02:34:45 -07:00
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-08-25 01:33:46 -07:00
|
|
|
case 'externalSecrets':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { ExternalSecretsController } = await import(
|
|
|
|
'@/ExternalSecrets/ExternalSecrets.controller.ee'
|
|
|
|
);
|
2023-08-25 01:33:46 -07:00
|
|
|
registerController(app, config, Container.get(ExternalSecretsController));
|
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-09-27 07:22:39 -07:00
|
|
|
case 'workflowHistory':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { WorkflowHistoryController } = await import(
|
|
|
|
'@/workflows/workflowHistory/workflowHistory.controller.ee'
|
|
|
|
);
|
2023-09-27 07:22:39 -07:00
|
|
|
registerController(app, config, Container.get(WorkflowHistoryController));
|
|
|
|
break;
|
2023-11-08 07:29:39 -08:00
|
|
|
|
2023-10-06 07:21:13 -07:00
|
|
|
case 'binaryData':
|
2023-11-08 07:29:39 -08:00
|
|
|
const { BinaryDataController } = await import('@/controllers/binaryData.controller');
|
2023-10-06 07:21:13 -07:00
|
|
|
registerController(app, config, Container.get(BinaryDataController));
|
|
|
|
break;
|
2023-07-13 01:14:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await testDb.terminate();
|
|
|
|
testServer.httpServer.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
return testServer;
|
|
|
|
};
|