2024-02-28 04:12:28 -08:00
|
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
import { User } from '@db/entities/User';
|
|
|
|
import { UserService } from '@/services/user.service';
|
2024-02-28 04:12:28 -08:00
|
|
|
import { UrlService } from '@/services/url.service';
|
2024-05-17 01:53:15 -07:00
|
|
|
import { mockInstance } from '../../shared/mocking';
|
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
2023-11-07 06:35:43 -08:00
|
|
|
|
|
|
|
describe('UserService', () => {
|
2024-02-28 04:12:28 -08:00
|
|
|
const urlService = new UrlService();
|
2024-05-17 01:53:15 -07:00
|
|
|
const userRepository = mockInstance(UserRepository);
|
|
|
|
const userService = new UserService(mock(), userRepository, mock(), urlService);
|
2023-12-07 00:19:16 -08:00
|
|
|
|
|
|
|
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
|
|
|
});
|
2024-05-17 01:53:15 -07:00
|
|
|
|
|
|
|
describe('update', () => {
|
|
|
|
// We need to use `save` so that that the subscriber in
|
|
|
|
// packages/cli/src/databases/entities/Project.ts receives the full user.
|
|
|
|
// With `update` it would only receive the updated fields, e.g. the `id`
|
|
|
|
// would be missing.
|
|
|
|
it('should use `save` instead of `update`', async () => {
|
|
|
|
const user = new User();
|
|
|
|
user.firstName = 'Not Nathan';
|
|
|
|
user.lastName = 'Nathaniel';
|
|
|
|
|
|
|
|
const userId = '1234';
|
|
|
|
const data = {
|
|
|
|
firstName: 'Nathan',
|
|
|
|
};
|
|
|
|
|
|
|
|
userRepository.findOneBy.mockResolvedValueOnce(user);
|
|
|
|
|
|
|
|
await userService.update(userId, data);
|
|
|
|
|
|
|
|
expect(userRepository.save).toHaveBeenCalledWith({ ...user, ...data }, { transaction: true });
|
|
|
|
expect(userRepository.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2023-11-07 06:35:43 -08:00
|
|
|
});
|