mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 21:19:43 -08:00
8e0ae3cf8c
This PR: - Creates `InvitationController` - Moves `POST /users` to `POST /invitations` and move related test to `invitations.api.tests` - Moves `POST /users/:id` to `POST /invitations/:id/accept` and move related test to `invitations.api.tests` - Adjusts FE to use new endpoints - Moves all the invitation logic to the `UserService` --------- Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
76 lines
2.7 KiB
TypeScript
76 lines
2.7 KiB
TypeScript
import Container from 'typedi';
|
|
import jwt from 'jsonwebtoken';
|
|
import { Logger } from '@/Logger';
|
|
import config from '@/config';
|
|
import { User } from '@db/entities/User';
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
|
import { UserService } from '@/services/user.service';
|
|
import { mockInstance } from '../../shared/mocking';
|
|
import { RoleService } from '@/services/role.service';
|
|
|
|
describe('UserService', () => {
|
|
config.set('userManagement.jwtSecret', 'random-secret');
|
|
|
|
mockInstance(Logger);
|
|
const repository = mockInstance(UserRepository);
|
|
mockInstance(RoleService);
|
|
const service = Container.get(UserService);
|
|
const testUser = Object.assign(new User(), {
|
|
id: '1234',
|
|
password: 'passwordHash',
|
|
mfaEnabled: false,
|
|
});
|
|
|
|
beforeEach(() => {
|
|
jest.resetAllMocks();
|
|
});
|
|
|
|
describe('generatePasswordResetToken', () => {
|
|
it('should generate valid password-reset tokens', () => {
|
|
const token = service.generatePasswordResetToken(testUser);
|
|
const decoded = jwt.decode(token) as jwt.JwtPayload;
|
|
expect(decoded.sub).toEqual(testUser.id);
|
|
expect(decoded.exp! - decoded.iat!).toEqual(1200); // Expires in 20 minutes
|
|
expect(decoded.passwordSha).toEqual(
|
|
'31513c5a9e3c5afe5c06d5675ace74e8bc3fadd9744ab5d89c311f2a62ccbd39',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('resolvePasswordResetToken', () => {
|
|
it('should not return a user if the token in invalid', async () => {
|
|
const user = await service.resolvePasswordResetToken('invalid-token');
|
|
expect(user).toBeUndefined();
|
|
});
|
|
|
|
it('should not return a user if the token in expired', async () => {
|
|
const token = service.generatePasswordResetToken(testUser, '-1h');
|
|
const user = await service.resolvePasswordResetToken(token);
|
|
expect(user).toBeUndefined();
|
|
});
|
|
|
|
it('should not return a user if the user does not exist in the DB', async () => {
|
|
repository.findOne.mockResolvedValueOnce(null);
|
|
const token = service.generatePasswordResetToken(testUser);
|
|
const user = await service.resolvePasswordResetToken(token);
|
|
expect(user).toBeUndefined();
|
|
});
|
|
|
|
it('should not return a user if the password sha does not match', async () => {
|
|
const token = service.generatePasswordResetToken(testUser);
|
|
const updatedUser = Object.create(testUser);
|
|
updatedUser.password = 'something-else';
|
|
repository.findOne.mockResolvedValueOnce(updatedUser);
|
|
const user = await service.resolvePasswordResetToken(token);
|
|
expect(user).toBeUndefined();
|
|
});
|
|
|
|
it('should not return the user if all checks pass', async () => {
|
|
const token = service.generatePasswordResetToken(testUser);
|
|
repository.findOne.mockResolvedValueOnce(testUser);
|
|
const user = await service.resolvePasswordResetToken(token);
|
|
expect(user).toEqual(testUser);
|
|
});
|
|
});
|
|
});
|