2023-11-07 06:35:43 -08:00
|
|
|
import Container from 'typedi';
|
|
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
import { Logger } from '@/Logger';
|
|
|
|
import config from '@/config';
|
|
|
|
import { User } from '@db/entities/User';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
2023-11-07 06:35:43 -08:00
|
|
|
import { UserService } from '@/services/user.service';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../../shared/mocking';
|
2023-11-16 09:39:43 -08:00
|
|
|
import { RoleService } from '@/services/role.service';
|
2023-11-07 06:35:43 -08:00
|
|
|
|
|
|
|
describe('UserService', () => {
|
|
|
|
config.set('userManagement.jwtSecret', 'random-secret');
|
|
|
|
|
|
|
|
mockInstance(Logger);
|
|
|
|
const repository = mockInstance(UserRepository);
|
2023-11-16 09:39:43 -08:00
|
|
|
mockInstance(RoleService);
|
2023-11-07 06:35:43 -08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|