mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 19:11:13 +00:00
refactor(core): Reduce boilterplate code in between tests 🧹, and fix the tests in node.js 20 (no-changelog) (#6654)
refactor(core): Reduce boilterplate code in between tests also cleaned up some imports, and fixed the tests in node.js 20
This commit is contained in:
committed by
GitHub
parent
3e07ffa73e
commit
b895ba438a
@@ -1,7 +1,6 @@
|
||||
import type express from 'express';
|
||||
import type { SuperAgentTest } from 'supertest';
|
||||
import type { Entry as LdapUser } from 'ldapts';
|
||||
import { Not } from 'typeorm';
|
||||
import { Container } from 'typedi';
|
||||
import { jsonParse } from 'n8n-workflow';
|
||||
import config from '@/config';
|
||||
import * as Db from '@/Db';
|
||||
@@ -14,19 +13,17 @@ import { encryptPassword, saveLdapSynchronization } from '@/Ldap/helpers';
|
||||
import type { LdapConfig } from '@/Ldap/types';
|
||||
import { sanitizeUser } from '@/UserManagement/UserManagementHelper';
|
||||
import { getCurrentAuthenticationMethod, setCurrentAuthenticationMethod } from '@/sso/ssoHelpers';
|
||||
import { License } from '@/License';
|
||||
|
||||
import { randomEmail, randomName, uniqueId } from './../shared/random';
|
||||
import * as testDb from './../shared/testDb';
|
||||
import type { AuthAgent } from '../shared/types';
|
||||
import * as utils from '../shared/utils';
|
||||
import * as utils from '../shared/utils/';
|
||||
|
||||
jest.mock('@/telemetry');
|
||||
jest.mock('@/UserManagement/email/NodeMailer');
|
||||
|
||||
let app: express.Application;
|
||||
let globalMemberRole: Role;
|
||||
let owner: User;
|
||||
let authAgent: AuthAgent;
|
||||
let authOwnerAgent: SuperAgentTest;
|
||||
|
||||
const defaultLdapConfig = {
|
||||
...LDAP_DEFAULT_CONFIGURATION,
|
||||
@@ -42,23 +39,24 @@ const defaultLdapConfig = {
|
||||
bindingAdminPassword: 'adminPassword',
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
Container.get(License).isLdapEnabled = () => true;
|
||||
app = await utils.initTestServer({ endpointGroups: ['auth', 'ldap'] });
|
||||
const testServer = utils.setupTestServer({
|
||||
endpointGroups: ['auth', 'ldap'],
|
||||
enabledFeatures: ['feat:ldap'],
|
||||
});
|
||||
|
||||
beforeAll(async () => {
|
||||
const [globalOwnerRole, fetchedGlobalMemberRole] = await testDb.getAllRoles();
|
||||
|
||||
globalMemberRole = fetchedGlobalMemberRole;
|
||||
|
||||
owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
||||
|
||||
authAgent = utils.createAuthAgent(app);
|
||||
authOwnerAgent = testServer.authAgentFor(owner);
|
||||
|
||||
defaultLdapConfig.bindingAdminPassword = await encryptPassword(
|
||||
defaultLdapConfig.bindingAdminPassword,
|
||||
);
|
||||
|
||||
await utils.initConfigFile();
|
||||
await utils.initEncryptionKey();
|
||||
|
||||
await setCurrentAuthenticationMethod('email');
|
||||
});
|
||||
@@ -81,10 +79,6 @@ beforeEach(async () => {
|
||||
config.set('userManagement.emails.mode', '');
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await testDb.terminate();
|
||||
});
|
||||
|
||||
const createLdapConfig = async (attributes: Partial<LdapConfig> = {}): Promise<LdapConfig> => {
|
||||
const { value: ldapConfig } = await Db.collections.Settings.save({
|
||||
key: LDAP_FEATURE_NAME,
|
||||
@@ -99,21 +93,12 @@ const createLdapConfig = async (attributes: Partial<LdapConfig> = {}): Promise<L
|
||||
|
||||
test('Member role should not be able to access ldap routes', async () => {
|
||||
const member = await testDb.createUser({ globalRole: globalMemberRole });
|
||||
|
||||
let response = await authAgent(member).get('/ldap/config');
|
||||
expect(response.statusCode).toBe(403);
|
||||
|
||||
response = await authAgent(member).put('/ldap/config');
|
||||
expect(response.statusCode).toBe(403);
|
||||
|
||||
response = await authAgent(member).post('/ldap/test-connection');
|
||||
expect(response.statusCode).toBe(403);
|
||||
|
||||
response = await authAgent(member).post('/ldap/sync');
|
||||
expect(response.statusCode).toBe(403);
|
||||
|
||||
response = await authAgent(member).get('/ldap/sync');
|
||||
expect(response.statusCode).toBe(403);
|
||||
const authAgent = testServer.authAgentFor(member);
|
||||
await authAgent.get('/ldap/config').expect(403);
|
||||
await authAgent.put('/ldap/config').expect(403);
|
||||
await authAgent.post('/ldap/test-connection').expect(403);
|
||||
await authAgent.post('/ldap/sync').expect(403);
|
||||
await authAgent.get('/ldap/sync').expect(403);
|
||||
});
|
||||
|
||||
describe('PUT /ldap/config', () => {
|
||||
@@ -142,7 +127,7 @@ describe('PUT /ldap/config', () => {
|
||||
];
|
||||
|
||||
for (const invalidPayload of invalidPayloads) {
|
||||
const response = await authAgent(owner).put('/ldap/config').send(invalidPayload);
|
||||
const response = await authOwnerAgent.put('/ldap/config').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.body).toHaveProperty('message');
|
||||
}
|
||||
@@ -155,7 +140,7 @@ describe('PUT /ldap/config', () => {
|
||||
loginLabel: '',
|
||||
};
|
||||
|
||||
const response = await authAgent(owner).put('/ldap/config').send(validPayload);
|
||||
const response = await authOwnerAgent.put('/ldap/config').send(validPayload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body.data.loginEnabled).toBe(true);
|
||||
@@ -171,9 +156,7 @@ describe('PUT /ldap/config', () => {
|
||||
const configuration = ldapConfig;
|
||||
|
||||
// disable the login, so the strategy is applied
|
||||
await authAgent(owner)
|
||||
.put('/ldap/config')
|
||||
.send({ ...configuration, loginEnabled: false });
|
||||
await authOwnerAgent.put('/ldap/config').send({ ...configuration, loginEnabled: false });
|
||||
|
||||
const emailUser = await Db.collections.User.findOneByOrFail({ id: member.id });
|
||||
const localLdapIdentities = await testDb.getLdapIdentities();
|
||||
@@ -193,11 +176,11 @@ test('GET /ldap/config route should retrieve current configuration', async () =>
|
||||
loginLabel: '',
|
||||
};
|
||||
|
||||
let response = await authAgent(owner).put('/ldap/config').send(validPayload);
|
||||
let response = await authOwnerAgent.put('/ldap/config').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(getCurrentAuthenticationMethod()).toBe('ldap');
|
||||
|
||||
response = await authAgent(owner).get('/ldap/config');
|
||||
response = await authOwnerAgent.get('/ldap/config');
|
||||
|
||||
expect(response.body.data).toMatchObject(validPayload);
|
||||
});
|
||||
@@ -206,8 +189,7 @@ describe('POST /ldap/test-connection', () => {
|
||||
test('route should success', async () => {
|
||||
jest.spyOn(LdapService.prototype, 'testConnection').mockResolvedValue();
|
||||
|
||||
const response = await authAgent(owner).post('/ldap/test-connection');
|
||||
expect(response.statusCode).toBe(200);
|
||||
await authOwnerAgent.post('/ldap/test-connection').expect(200);
|
||||
});
|
||||
|
||||
test('route should fail', async () => {
|
||||
@@ -215,7 +197,7 @@ describe('POST /ldap/test-connection', () => {
|
||||
|
||||
jest.spyOn(LdapService.prototype, 'testConnection').mockRejectedValue(new Error(errorMessage));
|
||||
|
||||
const response = await authAgent(owner).post('/ldap/test-connection');
|
||||
const response = await authOwnerAgent.post('/ldap/test-connection');
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.body).toHaveProperty('message');
|
||||
expect(response.body.message).toStrictEqual(errorMessage);
|
||||
@@ -237,9 +219,7 @@ describe('POST /ldap/sync', () => {
|
||||
const runTest = async (ldapUsers: LdapUser[]) => {
|
||||
jest.spyOn(LdapService.prototype, 'searchWithAdminBinding').mockResolvedValue(ldapUsers);
|
||||
|
||||
const response = await authAgent(owner).post('/ldap/sync').send({ type: 'dry' });
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
await authOwnerAgent.post('/ldap/sync').send({ type: 'dry' }).expect(200);
|
||||
|
||||
const synchronization = await Db.collections.AuthProviderSyncHistory.findOneByOrFail({});
|
||||
|
||||
@@ -332,9 +312,7 @@ describe('POST /ldap/sync', () => {
|
||||
const runTest = async (ldapUsers: LdapUser[]) => {
|
||||
jest.spyOn(LdapService.prototype, 'searchWithAdminBinding').mockResolvedValue(ldapUsers);
|
||||
|
||||
const response = await authAgent(owner).post('/ldap/sync').send({ type: 'live' });
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
await authOwnerAgent.post('/ldap/sync').send({ type: 'live' }).expect(200);
|
||||
|
||||
const synchronization = await Db.collections.AuthProviderSyncHistory.findOneByOrFail({});
|
||||
|
||||
@@ -460,9 +438,9 @@ describe('POST /ldap/sync', () => {
|
||||
|
||||
jest.spyOn(LdapService.prototype, 'searchWithAdminBinding').mockResolvedValue([]);
|
||||
|
||||
await authAgent(owner).post('/ldap/sync').send({ type: 'live' });
|
||||
await authOwnerAgent.post('/ldap/sync').send({ type: 'live' });
|
||||
|
||||
const response = await authAgent(member).get('/login');
|
||||
const response = await testServer.authAgentFor(member).get('/login');
|
||||
expect(response.body.code).toBe(401);
|
||||
});
|
||||
});
|
||||
@@ -483,10 +461,10 @@ test('GET /ldap/sync should return paginated synchronizations', async () => {
|
||||
});
|
||||
}
|
||||
|
||||
let response = await authAgent(owner).get('/ldap/sync?perPage=1&page=0');
|
||||
let response = await authOwnerAgent.get('/ldap/sync?perPage=1&page=0');
|
||||
expect(response.body.data.length).toBe(1);
|
||||
|
||||
response = await authAgent(owner).get('/ldap/sync?perPage=1&page=1');
|
||||
response = await authOwnerAgent.get('/ldap/sync?perPage=1&page=1');
|
||||
expect(response.body.data.length).toBe(1);
|
||||
});
|
||||
|
||||
@@ -495,13 +473,11 @@ describe('POST /login', () => {
|
||||
const ldapConfig = await createLdapConfig();
|
||||
LdapManager.updateConfig(ldapConfig);
|
||||
|
||||
const authlessAgent = utils.createAgent(app);
|
||||
|
||||
jest.spyOn(LdapService.prototype, 'searchWithAdminBinding').mockResolvedValue([ldapUser]);
|
||||
|
||||
jest.spyOn(LdapService.prototype, 'validUser').mockResolvedValue();
|
||||
|
||||
const response = await authlessAgent
|
||||
const response = await testServer.authlessAgent
|
||||
.post('/login')
|
||||
.send({ email: ldapUser.mail, password: 'password' });
|
||||
|
||||
@@ -581,7 +557,7 @@ describe('Instance owner should able to delete LDAP users', () => {
|
||||
|
||||
const member = await testDb.createLdapUser({ globalRole: globalMemberRole }, uniqueId());
|
||||
|
||||
await authAgent(owner).post(`/users/${member.id}`);
|
||||
await authOwnerAgent.post(`/users/${member.id}`);
|
||||
});
|
||||
|
||||
test('transfer workflows and credentials', async () => {
|
||||
@@ -591,7 +567,7 @@ describe('Instance owner should able to delete LDAP users', () => {
|
||||
const member = await testDb.createLdapUser({ globalRole: globalMemberRole }, uniqueId());
|
||||
|
||||
// delete the LDAP member and transfer its workflows/credentials to instance owner
|
||||
await authAgent(owner).post(`/users/${member.id}?transferId=${owner.id}`);
|
||||
await authOwnerAgent.post(`/users/${member.id}?transferId=${owner.id}`);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user