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:
कारतोफ्फेलस्क्रिप्ट™
2023-07-13 10:14:48 +02:00
committed by GitHub
parent 3e07ffa73e
commit b895ba438a
78 changed files with 1197 additions and 1597 deletions

View File

@@ -1,23 +1,21 @@
import { Container } from 'typedi';
import type { SuperAgentTest } from 'supertest';
import type { AuthenticationMethod } from 'n8n-workflow';
import type { User } from '@db/entities/User';
import { setSamlLoginEnabled } from '@/sso/saml/samlHelpers';
import { getCurrentAuthenticationMethod, setCurrentAuthenticationMethod } from '@/sso/ssoHelpers';
import { SamlUrls } from '@/sso/saml/constants';
import { License } from '@/License';
import { randomEmail, randomName, randomValidPassword } from '../shared/random';
import * as testDb from '../shared/testDb';
import * as utils from '../shared/utils';
import { sampleConfig } from './sampleMetadata';
import { InternalHooks } from '@/InternalHooks';
import { SamlService } from '@/sso/saml/saml.service.ee';
import config from '@/config';
import type { SamlUserAttributes } from '@/sso/saml/types/samlUserAttributes';
import type { AuthenticationMethod } from 'n8n-workflow';
import { randomEmail, randomName, randomValidPassword } from '../shared/random';
import * as testDb from '../shared/testDb';
import * as utils from '../shared/utils/';
import { sampleConfig } from './sampleMetadata';
let someUser: User;
let owner: User;
let noAuthMemberAgent: SuperAgentTest;
let authMemberAgent: SuperAgentTest;
let authOwnerAgent: SuperAgentTest;
@@ -25,20 +23,16 @@ async function enableSaml(enable: boolean) {
await setSamlLoginEnabled(enable);
}
beforeAll(async () => {
Container.get(License).isSamlEnabled = () => true;
const app = await utils.initTestServer({ endpointGroups: ['me', 'saml'] });
owner = await testDb.createOwner();
someUser = await testDb.createUser();
authOwnerAgent = utils.createAuthAgent(app)(owner);
authMemberAgent = utils.createAgent(app, { auth: true, user: someUser });
noAuthMemberAgent = utils.createAgent(app, { auth: false, user: someUser });
config.set('userManagement.isInstanceOwnerSetUp', true);
const testServer = utils.setupTestServer({
endpointGroups: ['me', 'saml'],
enabledFeatures: ['feat:saml'],
});
afterAll(async () => {
await testDb.terminate();
beforeAll(async () => {
owner = await testDb.createOwner();
someUser = await testDb.createUser();
authOwnerAgent = testServer.authAgentFor(owner);
authMemberAgent = testServer.authAgentFor(someUser);
});
describe('Instance owner', () => {
@@ -184,7 +178,7 @@ describe('Check endpoint permissions', () => {
});
test(`should be able to access GET ${SamlUrls.initSSO}`, async () => {
const response = await authOwnerAgent.get(`/sso/saml${SamlUrls.initSSO}`).expect(200);
await authOwnerAgent.get(`/sso/saml${SamlUrls.initSSO}`).expect(200);
});
test(`should be able to access GET ${SamlUrls.configTest}`, async () => {
@@ -223,7 +217,7 @@ describe('Check endpoint permissions', () => {
});
test(`should be able to access GET ${SamlUrls.initSSO}`, async () => {
const response = await authMemberAgent.get(`/sso/saml${SamlUrls.initSSO}`).expect(200);
await authMemberAgent.get(`/sso/saml${SamlUrls.initSSO}`).expect(200);
});
test(`should NOT be able to access GET ${SamlUrls.configTest}`, async () => {
@@ -232,41 +226,43 @@ describe('Check endpoint permissions', () => {
});
describe('Non-Authenticated User', () => {
test(`should be able to access ${SamlUrls.metadata}`, async () => {
await noAuthMemberAgent.get(`/sso/saml${SamlUrls.metadata}`).expect(200);
await testServer.authlessAgent.get(`/sso/saml${SamlUrls.metadata}`).expect(200);
});
test(`should NOT be able to access GET ${SamlUrls.config}`, async () => {
await noAuthMemberAgent.get(`/sso/saml${SamlUrls.config}`).expect(401);
await testServer.authlessAgent.get(`/sso/saml${SamlUrls.config}`).expect(401);
});
test(`should NOT be able to access POST ${SamlUrls.config}`, async () => {
await noAuthMemberAgent.post(`/sso/saml${SamlUrls.config}`).expect(401);
await testServer.authlessAgent.post(`/sso/saml${SamlUrls.config}`).expect(401);
});
test(`should NOT be able to access POST ${SamlUrls.configToggleEnabled}`, async () => {
await noAuthMemberAgent.post(`/sso/saml${SamlUrls.configToggleEnabled}`).expect(401);
await testServer.authlessAgent.post(`/sso/saml${SamlUrls.configToggleEnabled}`).expect(401);
});
test(`should be able to access GET ${SamlUrls.acs}`, async () => {
// Note that 401 here is coming from the missing SAML object,
// not from not being able to access the endpoint, so this is expected!
const response = await noAuthMemberAgent.get(`/sso/saml${SamlUrls.acs}`).expect(401);
const response = await testServer.authlessAgent.get(`/sso/saml${SamlUrls.acs}`).expect(401);
expect(response.text).toContain('SAML Authentication failed');
});
test(`should be able to access POST ${SamlUrls.acs}`, async () => {
// Note that 401 here is coming from the missing SAML object,
// not from not being able to access the endpoint, so this is expected!
const response = await noAuthMemberAgent.post(`/sso/saml${SamlUrls.acs}`).expect(401);
const response = await testServer.authlessAgent.post(`/sso/saml${SamlUrls.acs}`).expect(401);
expect(response.text).toContain('SAML Authentication failed');
});
test(`should be able to access GET ${SamlUrls.initSSO}`, async () => {
const response = await noAuthMemberAgent.get(`/sso/saml${SamlUrls.initSSO}`).expect(200);
const response = await testServer.authlessAgent
.get(`/sso/saml${SamlUrls.initSSO}`)
.expect(200);
});
test(`should NOT be able to access GET ${SamlUrls.configTest}`, async () => {
await noAuthMemberAgent.get(`/sso/saml${SamlUrls.configTest}`).expect(401);
await testServer.authlessAgent.get(`/sso/saml${SamlUrls.configTest}`).expect(401);
});
});
});