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-12-07 00:19:16 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2023-11-07 06:35:43 -08:00
|
|
|
|
|
|
|
describe('UserService', () => {
|
|
|
|
config.set('userManagement.jwtSecret', 'random-secret');
|
|
|
|
|
|
|
|
mockInstance(Logger);
|
2023-11-16 09:39:43 -08:00
|
|
|
mockInstance(RoleService);
|
2023-12-07 00:19:16 -08:00
|
|
|
|
|
|
|
const userRepository = mockInstance(UserRepository);
|
|
|
|
const userService = Container.get(UserService);
|
|
|
|
|
|
|
|
const commonMockUser = Object.assign(new User(), {
|
|
|
|
id: uuid(),
|
2023-11-07 06:35:43 -08:00
|
|
|
password: 'passwordHash',
|
|
|
|
});
|
|
|
|
|
2023-12-07 00:19:16 -08:00
|
|
|
describe('toPublic', () => {
|
|
|
|
it('should remove sensitive properties', async () => {
|
|
|
|
const mockUser = Object.assign(new User(), {
|
|
|
|
id: uuid(),
|
|
|
|
password: 'passwordHash',
|
|
|
|
mfaEnabled: false,
|
|
|
|
mfaSecret: 'test',
|
|
|
|
mfaRecoveryCodes: ['test'],
|
|
|
|
updatedAt: new Date(),
|
|
|
|
authIdentities: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
type MaybeSensitiveProperties = Partial<
|
|
|
|
Pick<User, 'password' | 'mfaSecret' | 'mfaRecoveryCodes' | 'updatedAt' | 'authIdentities'>
|
|
|
|
>;
|
|
|
|
|
|
|
|
// to prevent typechecking from blocking assertions
|
|
|
|
const publicUser: MaybeSensitiveProperties = await userService.toPublic(mockUser);
|
|
|
|
|
|
|
|
expect(publicUser.password).toBeUndefined();
|
|
|
|
expect(publicUser.mfaSecret).toBeUndefined();
|
|
|
|
expect(publicUser.mfaRecoveryCodes).toBeUndefined();
|
|
|
|
expect(publicUser.updatedAt).toBeUndefined();
|
|
|
|
expect(publicUser.authIdentities).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add scopes if requested', async () => {
|
|
|
|
const scoped = await userService.toPublic(commonMockUser, { withScopes: true });
|
2023-12-07 01:53:31 -08:00
|
|
|
const unscoped = await userService.toPublic(commonMockUser);
|
2023-12-07 00:19:16 -08:00
|
|
|
|
2023-12-07 01:53:31 -08:00
|
|
|
expect(scoped.globalScopes).toEqual([]);
|
|
|
|
expect(unscoped.globalScopes).toBeUndefined();
|
2023-12-07 00:19:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add invite URL if requested', async () => {
|
2023-12-07 01:53:31 -08:00
|
|
|
const firstUser = Object.assign(new User(), { id: uuid() });
|
|
|
|
const secondUser = Object.assign(new User(), { id: uuid(), isPending: true });
|
2023-12-07 00:19:16 -08:00
|
|
|
|
2023-12-07 01:53:31 -08:00
|
|
|
const withoutUrl = await userService.toPublic(secondUser);
|
|
|
|
const withUrl = await userService.toPublic(secondUser, {
|
|
|
|
withInviteUrl: true,
|
|
|
|
inviterId: firstUser.id,
|
|
|
|
});
|
2023-12-07 00:19:16 -08:00
|
|
|
|
|
|
|
expect(withoutUrl.inviteAcceptUrl).toBeUndefined();
|
2023-12-07 01:53:31 -08:00
|
|
|
|
|
|
|
const url = new URL(withUrl.inviteAcceptUrl ?? '');
|
|
|
|
|
|
|
|
expect(url.searchParams.get('inviterId')).toBe(firstUser.id);
|
|
|
|
expect(url.searchParams.get('inviteeId')).toBe(secondUser.id);
|
2023-12-07 00:19:16 -08:00
|
|
|
});
|
2023-11-07 06:35:43 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('generatePasswordResetToken', () => {
|
|
|
|
it('should generate valid password-reset tokens', () => {
|
2023-12-07 00:19:16 -08:00
|
|
|
const token = userService.generatePasswordResetToken(commonMockUser);
|
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
const decoded = jwt.decode(token) as jwt.JwtPayload;
|
2023-12-07 00:19:16 -08:00
|
|
|
|
|
|
|
if (!decoded.exp) fail('Token does not contain expiry');
|
|
|
|
if (!decoded.iat) fail('Token does not contain issued-at');
|
|
|
|
|
|
|
|
expect(decoded.sub).toEqual(commonMockUser.id);
|
|
|
|
expect(decoded.exp - decoded.iat).toEqual(1200); // Expires in 20 minutes
|
2023-11-07 06:35:43 -08:00
|
|
|
expect(decoded.passwordSha).toEqual(
|
|
|
|
'31513c5a9e3c5afe5c06d5675ace74e8bc3fadd9744ab5d89c311f2a62ccbd39',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resolvePasswordResetToken', () => {
|
|
|
|
it('should not return a user if the token in invalid', async () => {
|
2023-12-07 00:19:16 -08:00
|
|
|
const user = await userService.resolvePasswordResetToken('invalid-token');
|
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
expect(user).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not return a user if the token in expired', async () => {
|
2023-12-07 00:19:16 -08:00
|
|
|
const token = userService.generatePasswordResetToken(commonMockUser, '-1h');
|
|
|
|
|
|
|
|
const user = await userService.resolvePasswordResetToken(token);
|
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
expect(user).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not return a user if the user does not exist in the DB', async () => {
|
2023-12-07 00:19:16 -08:00
|
|
|
userRepository.findOne.mockResolvedValueOnce(null);
|
|
|
|
const token = userService.generatePasswordResetToken(commonMockUser);
|
|
|
|
|
|
|
|
const user = await userService.resolvePasswordResetToken(token);
|
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
expect(user).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not return a user if the password sha does not match', async () => {
|
2023-12-07 00:19:16 -08:00
|
|
|
const token = userService.generatePasswordResetToken(commonMockUser);
|
|
|
|
const updatedUser = Object.create(commonMockUser);
|
2023-11-07 06:35:43 -08:00
|
|
|
updatedUser.password = 'something-else';
|
2023-12-07 00:19:16 -08:00
|
|
|
userRepository.findOne.mockResolvedValueOnce(updatedUser);
|
|
|
|
|
|
|
|
const user = await userService.resolvePasswordResetToken(token);
|
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
expect(user).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not return the user if all checks pass', async () => {
|
2023-12-07 00:19:16 -08:00
|
|
|
const token = userService.generatePasswordResetToken(commonMockUser);
|
|
|
|
userRepository.findOne.mockResolvedValueOnce(commonMockUser);
|
|
|
|
|
|
|
|
const user = await userService.resolvePasswordResetToken(token);
|
|
|
|
|
|
|
|
expect(user).toEqual(commonMockUser);
|
2023-11-07 06:35:43 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|