mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 17:14:05 -08:00
1e2d6daaa3
* ⚡ 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` * 🔥 Remove unused imports * 🔥 Remove unused import * ⚡ Add util for configuring test SMTP * ⚡ Isolate user creation * 🔥 De-duplicate `createFullUser` * ⚡ Centralize hashing * 🔥 Remove superfluous arg * 🔥 Remove outdated comment * ⚡ Prioritize shared tables during trucation * 🧪 Add login tests * ⚡ Use token helper * ✏️ Improve naming * ⚡ Make `createMemberShell` consistent * 🔥 Remove unneeded helper * 🔥 De-duplicate `beforeEach` * ✏️ Improve naming * 🚚 Move `categorize` to utils * ✏️ Update comment * 🧪 Simplify test * 📘 Improve `User.password` type * ⚡ Silence logger * ⚡ Simplify condition * ⚡ Unhash password in payload * 🐛 Fix comparison against unhashed password * ⚡ Increase timeout for fake SMTP service * 🔥 Remove unneeded import * ⚡ Use `isNull()` * 🧪 Use `Promise.all()` in creds tests * 🧪 Use `Promise.all()` in me tests * 🧪 Use `Promise.all()` in owner tests * 🧪 Use `Promise.all()` in password tests * 🧪 Use `Promise.all()` in users tests * ⚡ Re-set cookie if UM disabled * 🔥 Remove repeated line * ⚡ Refactor out shared owner data * 🔥 Remove unneeded import * 🔥 Remove repeated lines * ⚡ Organize imports * ⚡ Reuse helper * 🚚 Rename tests to match routers * 🚚 Rename `createFullUser()` to `createUser()` * ⚡ Consolidate user shell creation * ⚡ Make hashing async * ⚡ Add email to user shell * ⚡ Optimize array building * 🛠 refactor user shell factory * 🐛 Fix MySQL tests * ⚡ Silence logger in other DBs Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
284 lines
7.8 KiB
TypeScript
284 lines
7.8 KiB
TypeScript
import express = require('express');
|
|
import validator from 'validator';
|
|
|
|
import config = require('../../config');
|
|
import * as utils from './shared/utils';
|
|
import { LOGGED_OUT_RESPONSE_BODY } from './shared/constants';
|
|
import { Db } from '../../src';
|
|
import type { Role } from '../../src/databases/entities/Role';
|
|
import { randomValidPassword } from './shared/random';
|
|
import * as testDb from './shared/testDb';
|
|
import { AUTH_COOKIE_NAME } from '../../src/constants';
|
|
|
|
jest.mock('../../src/telemetry');
|
|
|
|
let app: express.Application;
|
|
let testDbName = '';
|
|
let globalOwnerRole: Role;
|
|
let globalMemberRole: Role;
|
|
|
|
beforeAll(async () => {
|
|
app = utils.initTestServer({ endpointGroups: ['auth'], applyAuth: true });
|
|
const initResult = await testDb.init();
|
|
testDbName = initResult.testDbName;
|
|
|
|
globalOwnerRole = await testDb.getGlobalOwnerRole();
|
|
globalMemberRole = await testDb.getGlobalMemberRole();
|
|
utils.initTestLogger();
|
|
utils.initTestTelemetry();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await testDb.truncate(['User'], testDbName);
|
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', true);
|
|
|
|
await Db.collections.Settings!.update(
|
|
{ key: 'userManagement.isInstanceOwnerSetUp' },
|
|
{ value: JSON.stringify(true) },
|
|
);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await testDb.terminate(testDbName);
|
|
});
|
|
|
|
test('POST /login should log user in', async () => {
|
|
const ownerPassword = randomValidPassword();
|
|
const owner = await testDb.createUser({
|
|
password: ownerPassword,
|
|
globalRole: globalOwnerRole,
|
|
});
|
|
|
|
const authlessAgent = utils.createAgent(app);
|
|
|
|
const response = await authlessAgent.post('/login').send({
|
|
email: owner.email,
|
|
password: ownerPassword,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
const {
|
|
id,
|
|
email,
|
|
firstName,
|
|
lastName,
|
|
password,
|
|
personalizationAnswers,
|
|
globalRole,
|
|
resetPasswordToken,
|
|
} = response.body.data;
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
expect(email).toBe(owner.email);
|
|
expect(firstName).toBe(owner.firstName);
|
|
expect(lastName).toBe(owner.lastName);
|
|
expect(password).toBeUndefined();
|
|
expect(personalizationAnswers).toBeNull();
|
|
expect(password).toBeUndefined();
|
|
expect(resetPasswordToken).toBeUndefined();
|
|
expect(globalRole).toBeDefined();
|
|
expect(globalRole.name).toBe('owner');
|
|
expect(globalRole.scope).toBe('global');
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeDefined();
|
|
});
|
|
|
|
test('GET /login should return 401 Unauthorized if no cookie', async () => {
|
|
const authlessAgent = utils.createAgent(app);
|
|
|
|
const response = await authlessAgent.get('/login');
|
|
|
|
expect(response.statusCode).toBe(401);
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeUndefined();
|
|
});
|
|
|
|
test('GET /login should return cookie if UM is disabled', async () => {
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', false);
|
|
|
|
await Db.collections.Settings!.update(
|
|
{ key: 'userManagement.isInstanceOwnerSetUp' },
|
|
{ value: JSON.stringify(false) },
|
|
);
|
|
|
|
const authOwnerShellAgent = utils.createAgent(app, { auth: true, user: ownerShell });
|
|
|
|
const response = await authOwnerShellAgent.get('/login');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeDefined();
|
|
});
|
|
|
|
test('GET /login should return 401 Unauthorized if invalid cookie', async () => {
|
|
const invalidAuthAgent = utils.createAgent(app);
|
|
invalidAuthAgent.jar.setCookie(`${AUTH_COOKIE_NAME}=invalid`);
|
|
|
|
const response = await invalidAuthAgent.get('/login');
|
|
|
|
expect(response.statusCode).toBe(401);
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeUndefined();
|
|
});
|
|
|
|
test('GET /login should return logged-in owner shell', async () => {
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
const authMemberAgent = utils.createAgent(app, { auth: true, user: ownerShell });
|
|
|
|
const response = await authMemberAgent.get('/login');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
const {
|
|
id,
|
|
email,
|
|
firstName,
|
|
lastName,
|
|
password,
|
|
personalizationAnswers,
|
|
globalRole,
|
|
resetPasswordToken,
|
|
} = response.body.data;
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
expect(email).toBeDefined();
|
|
expect(firstName).toBeNull();
|
|
expect(lastName).toBeNull();
|
|
expect(password).toBeUndefined();
|
|
expect(personalizationAnswers).toBeNull();
|
|
expect(password).toBeUndefined();
|
|
expect(resetPasswordToken).toBeUndefined();
|
|
expect(globalRole).toBeDefined();
|
|
expect(globalRole.name).toBe('owner');
|
|
expect(globalRole.scope).toBe('global');
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeUndefined();
|
|
});
|
|
|
|
test('GET /login should return logged-in member shell', async () => {
|
|
const memberShell = await testDb.createUserShell(globalMemberRole);
|
|
const authMemberAgent = utils.createAgent(app, { auth: true, user: memberShell });
|
|
|
|
const response = await authMemberAgent.get('/login');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
const {
|
|
id,
|
|
email,
|
|
firstName,
|
|
lastName,
|
|
password,
|
|
personalizationAnswers,
|
|
globalRole,
|
|
resetPasswordToken,
|
|
} = response.body.data;
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
expect(email).toBeDefined();
|
|
expect(firstName).toBeNull();
|
|
expect(lastName).toBeNull();
|
|
expect(password).toBeUndefined();
|
|
expect(personalizationAnswers).toBeNull();
|
|
expect(password).toBeUndefined();
|
|
expect(resetPasswordToken).toBeUndefined();
|
|
expect(globalRole).toBeDefined();
|
|
expect(globalRole.name).toBe('member');
|
|
expect(globalRole.scope).toBe('global');
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeUndefined();
|
|
});
|
|
|
|
test('GET /login should return logged-in owner', async () => {
|
|
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
|
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner });
|
|
|
|
const response = await authOwnerAgent.get('/login');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
const {
|
|
id,
|
|
email,
|
|
firstName,
|
|
lastName,
|
|
password,
|
|
personalizationAnswers,
|
|
globalRole,
|
|
resetPasswordToken,
|
|
} = response.body.data;
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
expect(email).toBe(owner.email);
|
|
expect(firstName).toBe(owner.firstName);
|
|
expect(lastName).toBe(owner.lastName);
|
|
expect(password).toBeUndefined();
|
|
expect(personalizationAnswers).toBeNull();
|
|
expect(password).toBeUndefined();
|
|
expect(resetPasswordToken).toBeUndefined();
|
|
expect(globalRole).toBeDefined();
|
|
expect(globalRole.name).toBe('owner');
|
|
expect(globalRole.scope).toBe('global');
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeUndefined();
|
|
});
|
|
|
|
test('GET /login should return logged-in member', async () => {
|
|
const member = await testDb.createUser({ globalRole: globalMemberRole });
|
|
const authMemberAgent = utils.createAgent(app, { auth: true, user: member });
|
|
|
|
const response = await authMemberAgent.get('/login');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
const {
|
|
id,
|
|
email,
|
|
firstName,
|
|
lastName,
|
|
password,
|
|
personalizationAnswers,
|
|
globalRole,
|
|
resetPasswordToken,
|
|
} = response.body.data;
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
expect(email).toBe(member.email);
|
|
expect(firstName).toBe(member.firstName);
|
|
expect(lastName).toBe(member.lastName);
|
|
expect(password).toBeUndefined();
|
|
expect(personalizationAnswers).toBeNull();
|
|
expect(password).toBeUndefined();
|
|
expect(resetPasswordToken).toBeUndefined();
|
|
expect(globalRole).toBeDefined();
|
|
expect(globalRole.name).toBe('member');
|
|
expect(globalRole.scope).toBe('global');
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeUndefined();
|
|
});
|
|
|
|
test('POST /logout should log user out', async () => {
|
|
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
|
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner });
|
|
|
|
const response = await authOwnerAgent.post('/logout');
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.body).toEqual(LOGGED_OUT_RESPONSE_BODY);
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
expect(authToken).toBeUndefined();
|
|
});
|