mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 17:14:05 -08:00
7625421b81
* ⚡ Declutter test logs * 🐛 Fix random passwords length * 🐛 Fix password hashing in test user creation * 🐛 Hash leftover password * ⚡ Improve error message for `compare` * ⚡ Restore `randomInvalidPassword` contant * ⚡ Mock Telemetry module to prevent `--forceExit` * ⚡ Silence logger * ⚡ Simplify condition * ⚡ Unhash password in payload
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import express = require('express');
|
|
|
|
import * as request from 'supertest';
|
|
import {
|
|
REST_PATH_SEGMENT,
|
|
ROUTES_REQUIRING_AUTHORIZATION,
|
|
ROUTES_REQUIRING_AUTHENTICATION,
|
|
} from './shared/constants';
|
|
import * as utils from './shared/utils';
|
|
import * as testDb from './shared/testDb';
|
|
|
|
jest.mock('../../src/telemetry');
|
|
|
|
let app: express.Application;
|
|
let testDbName = '';
|
|
|
|
beforeAll(async () => {
|
|
app = utils.initTestServer({
|
|
applyAuth: true,
|
|
endpointGroups: ['me', 'auth', 'owner', 'users'],
|
|
});
|
|
const initResult = await testDb.init();
|
|
testDbName = initResult.testDbName;
|
|
utils.initTestLogger();
|
|
utils.initTestTelemetry();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await testDb.terminate(testDbName);
|
|
});
|
|
|
|
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 response = await request(app)[method](endpoint).use(utils.prefix(REST_PATH_SEGMENT));
|
|
|
|
expect(response.statusCode).toBe(401);
|
|
});
|
|
});
|
|
|
|
ROUTES_REQUIRING_AUTHORIZATION.forEach(async (route) => {
|
|
const [method, endpoint] = getMethodAndEndpoint(route);
|
|
|
|
test(`${route} should return 403 Forbidden for member`, async () => {
|
|
const member = await testDb.createUser();
|
|
const authMemberAgent = utils.createAgent(app, { auth: true, user: member });
|
|
const response = await authMemberAgent[method](endpoint);
|
|
if (response.statusCode === 500) {
|
|
console.log(response);
|
|
}
|
|
|
|
expect(response.statusCode).toBe(403);
|
|
});
|
|
});
|
|
|
|
function getMethodAndEndpoint(route: string) {
|
|
return route.split(' ').map((segment, index) => {
|
|
return index % 2 === 0 ? segment.toLowerCase() : segment;
|
|
});
|
|
}
|