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,50 +1,51 @@
import type { SuperAgentTest } from 'supertest';
import {
ROUTES_REQUIRING_AUTHENTICATION,
ROUTES_REQUIRING_AUTHORIZATION,
} from './shared/constants';
import * as testDb from './shared/testDb';
import * as utils from './shared/utils';
import config from '@/config';
import * as utils from './shared/utils/';
let authlessAgent: SuperAgentTest;
let authMemberAgent: SuperAgentTest;
describe('Auth Middleware', () => {
const testServer = utils.setupTestServer({ endpointGroups: ['me', 'auth', 'owner', 'users'] });
beforeAll(async () => {
const app = await utils.initTestServer({ endpointGroups: ['me', 'auth', 'owner', 'users'] });
const globalMemberRole = await testDb.getGlobalMemberRole();
const member = await testDb.createUser({ globalRole: globalMemberRole });
/** Routes requiring a valid `n8n-auth` cookie for a user, either owner or member. */
const ROUTES_REQUIRING_AUTHENTICATION: Readonly<Array<[string, string]>> = [
['PATCH', '/me'],
['PATCH', '/me/password'],
['POST', '/me/survey'],
['POST', '/owner/setup'],
['GET', '/non-existent'],
];
authlessAgent = utils.createAgent(app);
authMemberAgent = utils.createAuthAgent(app)(member);
/** Routes requiring a valid `n8n-auth` cookie for an owner. */
const ROUTES_REQUIRING_AUTHORIZATION: Readonly<Array<[string, string]>> = [
['POST', '/users'],
['DELETE', '/users/123'],
['POST', '/users/123/reinvite'],
['POST', '/owner/setup'],
];
config.set('userManagement.isInstanceOwnerSetUp', true);
});
describe('Routes requiring Authentication', () => {
ROUTES_REQUIRING_AUTHENTICATION.concat(ROUTES_REQUIRING_AUTHORIZATION).forEach(
([method, endpoint]) => {
test(`${method} ${endpoint} should return 401 Unauthorized if no cookie`, async () => {
const { statusCode } = await testServer.authlessAgent[method.toLowerCase()](endpoint);
expect(statusCode).toBe(401);
});
},
);
});
afterAll(async () => {
await testDb.terminate();
});
describe('Routes requiring Authorization', () => {
let authMemberAgent: SuperAgentTest;
beforeAll(async () => {
const globalMemberRole = await testDb.getGlobalMemberRole();
const member = await testDb.createUser({ globalRole: globalMemberRole });
authMemberAgent = testServer.authAgentFor(member);
});
ROUTES_REQUIRING_AUTHENTICATION.concat(ROUTES_REQUIRING_AUTHORIZATION).forEach((route) => {
const [method, endpoint] = getMethodAndEndpoint(route);
test(`${route} should return 401 Unauthorized if no cookie`, async () => {
const { statusCode } = await authlessAgent[method](endpoint);
expect(statusCode).toBe(401);
ROUTES_REQUIRING_AUTHORIZATION.forEach(async ([method, endpoint]) => {
test(`${method} ${endpoint} should return 403 Forbidden for member`, async () => {
const { statusCode } = await authMemberAgent[method.toLowerCase()](endpoint);
expect(statusCode).toBe(403);
});
});
});
});
ROUTES_REQUIRING_AUTHORIZATION.forEach(async (route) => {
const [method, endpoint] = getMethodAndEndpoint(route);
test(`${route} should return 403 Forbidden for member`, async () => {
const { statusCode } = await authMemberAgent[method](endpoint);
expect(statusCode).toBe(403);
});
});
function getMethodAndEndpoint(route: string) {
return route.split(' ').map((segment, index) => {
return index % 2 === 0 ? segment.toLowerCase() : segment;
});
}