mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 21:19:43 -08:00
c378f60a25
## Summary Provide details about your pull request and what it adds, fixes, or changes. Photos and videos are recommended. Continue breaking down `UserManagementHelper.ts` ... #### How to test the change: 1. ... ## Issues fixed Include links to Github issue or Community forum post or **Linear ticket**: > Important in order to close automatically and provide context to reviewers ... ## Review / Merge checklist - [ ] PR title and summary are descriptive. **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** ([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md)) - [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. - [ ] Tests included. > A bug is not considered fixed, unless a test is added to prevent it from happening again. A feature is not complete without tests. > > *(internal)* You can use Slack commands to trigger [e2e tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227) or [deploy test instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce) or [deploy early access version on Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
314 lines
9.4 KiB
TypeScript
314 lines
9.4 KiB
TypeScript
import { v4 as uuid } from 'uuid';
|
|
import { compare } from 'bcryptjs';
|
|
import { Container } from 'typedi';
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
import { License } from '@/License';
|
|
import config from '@/config';
|
|
import type { Role } from '@db/entities/Role';
|
|
import type { User } from '@db/entities/User';
|
|
import { setCurrentAuthenticationMethod } from '@/sso/ssoHelpers';
|
|
import { ExternalHooks } from '@/ExternalHooks';
|
|
import { JwtService } from '@/services/jwt.service';
|
|
import { UserService } from '@/services/user.service';
|
|
import { UserManagementMailer } from '@/UserManagement/email';
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
|
|
|
import { mockInstance } from '../shared/mocking';
|
|
import { getAuthToken, setupTestServer } from './shared/utils/';
|
|
import {
|
|
randomEmail,
|
|
randomInvalidPassword,
|
|
randomName,
|
|
randomString,
|
|
randomValidPassword,
|
|
} from './shared/random';
|
|
import * as testDb from './shared/testDb';
|
|
import { getGlobalMemberRole, getGlobalOwnerRole } from './shared/db/roles';
|
|
import { createUser } from './shared/db/users';
|
|
import { PasswordUtility } from '@/services/password.utility';
|
|
|
|
config.set('userManagement.jwtSecret', randomString(5, 10));
|
|
|
|
let globalOwnerRole: Role;
|
|
let globalMemberRole: Role;
|
|
let owner: User;
|
|
let member: User;
|
|
|
|
const externalHooks = mockInstance(ExternalHooks);
|
|
const mailer = mockInstance(UserManagementMailer, { isEmailSetUp: true });
|
|
const testServer = setupTestServer({ endpointGroups: ['passwordReset'] });
|
|
const jwtService = Container.get(JwtService);
|
|
let userService: UserService;
|
|
|
|
beforeAll(async () => {
|
|
globalOwnerRole = await getGlobalOwnerRole();
|
|
globalMemberRole = await getGlobalMemberRole();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await testDb.truncate(['User']);
|
|
owner = await createUser({ globalRole: globalOwnerRole });
|
|
member = await createUser({ globalRole: globalMemberRole });
|
|
externalHooks.run.mockReset();
|
|
jest.replaceProperty(mailer, 'isEmailSetUp', true);
|
|
userService = Container.get(UserService);
|
|
});
|
|
|
|
describe('POST /forgot-password', () => {
|
|
test('should send password reset email', async () => {
|
|
const member = await createUser({
|
|
email: 'test@test.com',
|
|
globalRole: globalMemberRole,
|
|
});
|
|
|
|
await Promise.all(
|
|
[{ email: owner.email }, { email: member.email.toUpperCase() }].map(async (payload) => {
|
|
const response = await testServer.authlessAgent.post('/forgot-password').send(payload);
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.body).toEqual({});
|
|
}),
|
|
);
|
|
});
|
|
|
|
test('should fail if emailing is not set up', async () => {
|
|
jest.replaceProperty(mailer, 'isEmailSetUp', false);
|
|
|
|
await testServer.authlessAgent
|
|
.post('/forgot-password')
|
|
.send({ email: owner.email })
|
|
.expect(500);
|
|
});
|
|
|
|
test('should fail if SAML is authentication method', async () => {
|
|
await setCurrentAuthenticationMethod('saml');
|
|
const member = await createUser({
|
|
email: 'test@test.com',
|
|
globalRole: globalMemberRole,
|
|
});
|
|
|
|
await testServer.authlessAgent
|
|
.post('/forgot-password')
|
|
.send({ email: member.email })
|
|
.expect(403);
|
|
|
|
await setCurrentAuthenticationMethod('email');
|
|
});
|
|
|
|
test('should succeed if SAML is authentication method and requestor is owner', async () => {
|
|
await setCurrentAuthenticationMethod('saml');
|
|
|
|
const response = await testServer.authlessAgent
|
|
.post('/forgot-password')
|
|
.send({ email: owner.email });
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.body).toEqual({});
|
|
|
|
await setCurrentAuthenticationMethod('email');
|
|
});
|
|
|
|
test('should fail with invalid inputs', async () => {
|
|
const invalidPayloads = [
|
|
randomEmail(),
|
|
[randomEmail()],
|
|
{},
|
|
[{ name: randomName() }],
|
|
[{ email: randomName() }],
|
|
];
|
|
|
|
for (const invalidPayload of invalidPayloads) {
|
|
const response = await testServer.authlessAgent.post('/forgot-password').send(invalidPayload);
|
|
expect(response.statusCode).toBe(400);
|
|
}
|
|
});
|
|
|
|
test('should fail if user is not found', async () => {
|
|
const response = await testServer.authlessAgent
|
|
.post('/forgot-password')
|
|
.send({ email: randomEmail() });
|
|
|
|
expect(response.statusCode).toBe(200); // expect 200 to remain vague
|
|
});
|
|
});
|
|
|
|
describe('GET /resolve-password-token', () => {
|
|
test('should succeed with valid inputs', async () => {
|
|
const resetPasswordToken = userService.generatePasswordResetToken(owner);
|
|
|
|
const response = await testServer.authlessAgent
|
|
.get('/resolve-password-token')
|
|
.query({ userId: owner.id, token: resetPasswordToken });
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
});
|
|
|
|
test('should fail with invalid inputs', async () => {
|
|
await testServer.authlessAgent
|
|
.get('/resolve-password-token')
|
|
.query({ token: uuid() })
|
|
.expect(404);
|
|
|
|
await testServer.authlessAgent
|
|
.get('/resolve-password-token')
|
|
.query({ userId: owner.id })
|
|
.expect(400);
|
|
});
|
|
|
|
test('should fail if user is not found', async () => {
|
|
const token = jwtService.sign({ sub: uuid() });
|
|
|
|
const response = await testServer.authlessAgent
|
|
.get('/resolve-password-token')
|
|
.query({ userId: owner.id, token });
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
});
|
|
|
|
test('should fail if token is expired', async () => {
|
|
const resetPasswordToken = userService.generatePasswordResetToken(owner, '-1h');
|
|
|
|
const response = await testServer.authlessAgent
|
|
.get('/resolve-password-token')
|
|
.query({ userId: owner.id, token: resetPasswordToken });
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
});
|
|
|
|
test('should fail after password has changed', async () => {
|
|
const updatedUser = mock<User>({ ...owner, password: 'another-password' });
|
|
const resetPasswordToken = userService.generatePasswordResetToken(updatedUser);
|
|
|
|
const response = await testServer.authlessAgent
|
|
.get('/resolve-password-token')
|
|
.query({ userId: owner.id, token: resetPasswordToken });
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe('POST /change-password', () => {
|
|
const passwordToStore = randomValidPassword();
|
|
|
|
test('should succeed with valid inputs', async () => {
|
|
const resetPasswordToken = userService.generatePasswordResetToken(owner);
|
|
const response = await testServer.authlessAgent.post('/change-password').send({
|
|
token: resetPasswordToken,
|
|
userId: owner.id,
|
|
password: passwordToStore,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
const authToken = getAuthToken(response);
|
|
expect(authToken).toBeDefined();
|
|
|
|
const { password: storedPassword } = await Container.get(UserRepository).findOneByOrFail({
|
|
id: owner.id,
|
|
});
|
|
|
|
const comparisonResult = await Container.get(PasswordUtility).compare(
|
|
passwordToStore,
|
|
storedPassword,
|
|
);
|
|
expect(comparisonResult).toBe(true);
|
|
expect(storedPassword).not.toBe(passwordToStore);
|
|
|
|
expect(externalHooks.run).toHaveBeenCalledWith('user.password.update', [
|
|
owner.email,
|
|
storedPassword,
|
|
]);
|
|
});
|
|
|
|
test('should fail with invalid inputs', async () => {
|
|
const resetPasswordToken = userService.generatePasswordResetToken(owner);
|
|
|
|
const invalidPayloads = [
|
|
{ token: uuid() },
|
|
{ id: owner.id },
|
|
{ password: randomValidPassword() },
|
|
{ token: uuid(), id: owner.id },
|
|
{ token: uuid(), password: randomValidPassword() },
|
|
{ id: owner.id, password: randomValidPassword() },
|
|
{
|
|
id: owner.id,
|
|
password: randomInvalidPassword(),
|
|
token: resetPasswordToken,
|
|
},
|
|
{
|
|
id: owner.id,
|
|
password: randomValidPassword(),
|
|
token: uuid(),
|
|
},
|
|
];
|
|
|
|
for (const invalidPayload of invalidPayloads) {
|
|
const response = await testServer.authlessAgent
|
|
.post('/change-password')
|
|
.query(invalidPayload);
|
|
expect(response.statusCode).toBe(400);
|
|
const { password: storedPassword } = await Container.get(UserRepository).findOneByOrFail({
|
|
id: owner.id,
|
|
});
|
|
expect(owner.password).toBe(storedPassword);
|
|
}
|
|
});
|
|
|
|
test('should fail when token has expired', async () => {
|
|
const resetPasswordToken = userService.generatePasswordResetToken(owner, '-1h');
|
|
|
|
const response = await testServer.authlessAgent.post('/change-password').send({
|
|
token: resetPasswordToken,
|
|
userId: owner.id,
|
|
password: passwordToStore,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
|
|
expect(externalHooks.run).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('owner should be able to reset its password when quota:users = 1', async () => {
|
|
jest.spyOn(Container.get(License), 'getUsersLimit').mockReturnValueOnce(1);
|
|
|
|
const resetPasswordToken = userService.generatePasswordResetToken(owner);
|
|
const response = await testServer.authlessAgent.post('/change-password').send({
|
|
token: resetPasswordToken,
|
|
userId: owner.id,
|
|
password: passwordToStore,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
const authToken = getAuthToken(response);
|
|
expect(authToken).toBeDefined();
|
|
|
|
const { password: storedPassword } = await Container.get(UserRepository).findOneByOrFail({
|
|
id: owner.id,
|
|
});
|
|
|
|
const comparisonResult = await compare(passwordToStore, storedPassword);
|
|
expect(comparisonResult).toBe(true);
|
|
expect(storedPassword).not.toBe(passwordToStore);
|
|
|
|
expect(externalHooks.run).toHaveBeenCalledWith('user.password.update', [
|
|
owner.email,
|
|
storedPassword,
|
|
]);
|
|
});
|
|
|
|
test('member should not be able to reset its password when quota:users = 1', async () => {
|
|
jest.spyOn(Container.get(License), 'getUsersLimit').mockReturnValueOnce(1);
|
|
|
|
const resetPasswordToken = userService.generatePasswordResetToken(member);
|
|
const response = await testServer.authlessAgent.post('/change-password').send({
|
|
token: resetPasswordToken,
|
|
userId: member.id,
|
|
password: passwordToStore,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(403);
|
|
});
|
|
});
|