2023-03-30 15:38:55 -04:00
|
|
|
import { UserEntity } from '@app/infra/entities';
|
2023-09-11 17:56:38 +02:00
|
|
|
import {
|
|
|
|
|
BadRequestException,
|
|
|
|
|
ForbiddenException,
|
|
|
|
|
InternalServerErrorException,
|
|
|
|
|
NotFoundException,
|
|
|
|
|
} from '@nestjs/common';
|
2023-02-25 09:12:03 -05:00
|
|
|
import {
|
2023-10-31 11:01:32 -04:00
|
|
|
authStub,
|
2023-02-25 09:12:03 -05:00
|
|
|
newAlbumRepositoryMock,
|
|
|
|
|
newAssetRepositoryMock,
|
|
|
|
|
newCryptoRepositoryMock,
|
|
|
|
|
newJobRepositoryMock,
|
2023-09-20 13:16:33 +02:00
|
|
|
newLibraryRepositoryMock,
|
2023-02-25 09:12:03 -05:00
|
|
|
newStorageRepositoryMock,
|
|
|
|
|
newUserRepositoryMock,
|
2023-09-11 17:56:38 +02:00
|
|
|
userStub,
|
2023-06-08 11:01:07 -04:00
|
|
|
} from '@test';
|
2023-06-16 15:54:17 -04:00
|
|
|
import { when } from 'jest-when';
|
2023-12-12 09:58:25 -05:00
|
|
|
import { ImmichFileResponse } from '../domain.util';
|
2023-10-09 10:25:03 -04:00
|
|
|
import { JobName } from '../job';
|
|
|
|
|
import {
|
|
|
|
|
IAlbumRepository,
|
|
|
|
|
IAssetRepository,
|
|
|
|
|
ICryptoRepository,
|
|
|
|
|
IJobRepository,
|
|
|
|
|
ILibraryRepository,
|
|
|
|
|
IStorageRepository,
|
|
|
|
|
IUserRepository,
|
|
|
|
|
} from '../repositories';
|
2022-12-23 21:08:50 +01:00
|
|
|
import { UpdateUserDto } from './dto/update-user.dto';
|
2023-10-31 11:01:32 -04:00
|
|
|
import { mapUser } from './response-dto';
|
2023-01-18 09:40:15 -05:00
|
|
|
import { UserService } from './user.service';
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
const makeDeletedAt = (daysAgo: number) => {
|
|
|
|
|
const deletedAt = new Date();
|
|
|
|
|
deletedAt.setDate(deletedAt.getDate() - daysAgo);
|
|
|
|
|
return deletedAt;
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-18 09:40:15 -05:00
|
|
|
describe(UserService.name, () => {
|
2023-01-12 17:07:57 -05:00
|
|
|
let sut: UserService;
|
2023-05-23 16:40:04 -04:00
|
|
|
let userMock: jest.Mocked<IUserRepository>;
|
2023-01-27 20:50:07 +00:00
|
|
|
let cryptoRepositoryMock: jest.Mocked<ICryptoRepository>;
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
let albumMock: jest.Mocked<IAlbumRepository>;
|
|
|
|
|
let assetMock: jest.Mocked<IAssetRepository>;
|
|
|
|
|
let jobMock: jest.Mocked<IJobRepository>;
|
2023-09-20 13:16:33 +02:00
|
|
|
let libraryMock: jest.Mocked<ILibraryRepository>;
|
2023-02-25 09:12:03 -05:00
|
|
|
let storageMock: jest.Mocked<IStorageRepository>;
|
|
|
|
|
|
2023-01-18 09:40:15 -05:00
|
|
|
beforeEach(async () => {
|
2023-02-25 09:12:03 -05:00
|
|
|
albumMock = newAlbumRepositoryMock();
|
|
|
|
|
assetMock = newAssetRepositoryMock();
|
2023-10-11 04:14:44 +02:00
|
|
|
cryptoRepositoryMock = newCryptoRepositoryMock();
|
2023-02-25 09:12:03 -05:00
|
|
|
jobMock = newJobRepositoryMock();
|
2023-09-20 13:16:33 +02:00
|
|
|
libraryMock = newLibraryRepositoryMock();
|
2023-02-25 09:12:03 -05:00
|
|
|
storageMock = newStorageRepositoryMock();
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock = newUserRepositoryMock();
|
|
|
|
|
|
2023-10-23 17:52:21 +02:00
|
|
|
sut = new UserService(albumMock, assetMock, cryptoRepositoryMock, jobMock, libraryMock, storageMock, userMock);
|
2023-05-23 16:40:04 -04:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
when(userMock.get).calledWith(authStub.admin.user.id, {}).mockResolvedValue(userStub.admin);
|
|
|
|
|
when(userMock.get).calledWith(authStub.admin.user.id, { withDeleted: true }).mockResolvedValue(userStub.admin);
|
|
|
|
|
when(userMock.get).calledWith(authStub.user1.user.id, {}).mockResolvedValue(userStub.user1);
|
|
|
|
|
when(userMock.get).calledWith(authStub.user1.user.id, { withDeleted: true }).mockResolvedValue(userStub.user1);
|
2022-09-18 09:27:06 -05:00
|
|
|
});
|
|
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
describe('getAll', () => {
|
2023-01-12 17:07:57 -05:00
|
|
|
it('should get all users', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.getList.mockResolvedValue([userStub.admin]);
|
|
|
|
|
await expect(sut.getAll(authStub.admin, false)).resolves.toEqual([
|
|
|
|
|
expect.objectContaining({
|
2023-12-09 23:34:12 -05:00
|
|
|
id: authStub.admin.user.id,
|
|
|
|
|
email: authStub.admin.user.email,
|
2023-10-31 11:01:32 -04:00
|
|
|
}),
|
2023-01-12 17:07:57 -05:00
|
|
|
]);
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.getList).toHaveBeenCalledWith({ withDeleted: true });
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
describe('get', () => {
|
2023-01-12 17:07:57 -05:00
|
|
|
it('should get a user by id', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.get(authStub.admin.user.id);
|
|
|
|
|
expect(userMock.get).toHaveBeenCalledWith(authStub.admin.user.id, { withDeleted: false });
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if a user is not found', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(null);
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.get(authStub.admin.user.id)).rejects.toBeInstanceOf(NotFoundException);
|
|
|
|
|
expect(userMock.get).toHaveBeenCalledWith(authStub.admin.user.id, { withDeleted: false });
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
describe('getMe', () => {
|
2023-01-12 17:07:57 -05:00
|
|
|
it("should get the auth user's info", async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
|
|
|
|
await sut.getMe(authStub.admin);
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(authStub.admin.user.id, {});
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if a user is not found', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(null);
|
2023-10-31 11:01:32 -04:00
|
|
|
await expect(sut.getMe(authStub.admin)).rejects.toBeInstanceOf(BadRequestException);
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(authStub.admin.user.id, {});
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('update', () => {
|
2022-12-16 14:26:12 -06:00
|
|
|
it('should update user', async () => {
|
2022-12-23 21:08:50 +01:00
|
|
|
const update: UpdateUserDto = {
|
2023-10-31 11:01:32 -04:00
|
|
|
id: userStub.user1.id,
|
2022-12-23 21:08:50 +01:00
|
|
|
shouldChangePassword: true,
|
2023-09-11 17:56:38 +02:00
|
|
|
email: 'immich@test.com',
|
|
|
|
|
storageLabel: 'storage_label',
|
2022-12-23 21:08:50 +01:00
|
|
|
};
|
2023-09-11 17:56:38 +02:00
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
|
|
|
|
userMock.getByStorageLabel.mockResolvedValue(null);
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.update.mockResolvedValue(userStub.user1);
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.update({ user: { ...authStub.user1.user, isAdmin: true } }, update);
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-09-11 17:56:38 +02:00
|
|
|
expect(userMock.getByEmail).toHaveBeenCalledWith(update.email);
|
|
|
|
|
expect(userMock.getByStorageLabel).toHaveBeenCalledWith(update.storageLabel);
|
2022-09-18 09:27:06 -05:00
|
|
|
});
|
|
|
|
|
|
2023-05-21 23:18:10 -04:00
|
|
|
it('should not set an empty string for storage label', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.update.mockResolvedValue(userStub.user1);
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.update(authStub.admin, { id: userStub.user1.id, storageLabel: '' });
|
|
|
|
|
expect(userMock.update).toHaveBeenCalledWith(userStub.user1.id, {
|
|
|
|
|
id: userStub.user1.id,
|
|
|
|
|
storageLabel: null,
|
|
|
|
|
});
|
2023-05-21 23:18:10 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should omit a storage label set by non-admin users', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.update.mockResolvedValue(userStub.user1);
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.update({ user: userStub.user1 }, { id: userStub.user1.id, storageLabel: 'admin' });
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(userStub.user1.id, { id: userStub.user1.id });
|
2023-05-21 23:18:10 -04:00
|
|
|
});
|
|
|
|
|
|
2022-12-23 21:08:50 +01:00
|
|
|
it('user can only update its information', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
when(userMock.get)
|
2023-10-31 11:01:32 -04:00
|
|
|
.calledWith('not_immich_auth_user_id', {})
|
2022-12-23 21:08:50 +01:00
|
|
|
.mockResolvedValueOnce({
|
2023-10-31 11:01:32 -04:00
|
|
|
...userStub.user1,
|
2022-12-23 21:08:50 +01:00
|
|
|
id: 'not_immich_auth_user_id',
|
|
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
const result = sut.update(
|
|
|
|
|
{ user: userStub.user1 },
|
|
|
|
|
{
|
|
|
|
|
id: 'not_immich_auth_user_id',
|
|
|
|
|
password: 'I take over your account now',
|
|
|
|
|
},
|
|
|
|
|
);
|
2022-12-23 21:08:50 +01:00
|
|
|
await expect(result).rejects.toBeInstanceOf(ForbiddenException);
|
2022-09-18 09:27:06 -05:00
|
|
|
});
|
|
|
|
|
|
2022-12-27 11:36:31 -05:00
|
|
|
it('should let a user change their email', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
const dto = { id: userStub.user1.id, email: 'updated@test.com' };
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
|
|
|
|
userMock.update.mockResolvedValue(userStub.user1);
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.update({ user: userStub.user1 }, dto);
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(userStub.user1.id, {
|
2023-05-21 23:18:10 -04:00
|
|
|
id: 'user-id',
|
2022-12-27 21:29:58 -05:00
|
|
|
email: 'updated@test.com',
|
|
|
|
|
});
|
2022-12-27 11:36:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not let a user change their email to one already in use', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
const dto = { id: userStub.user1.id, email: 'updated@test.com' };
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
|
|
|
|
userMock.getByEmail.mockResolvedValue(userStub.admin);
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.update({ user: userStub.user1 }, dto)).rejects.toBeInstanceOf(BadRequestException);
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.update).not.toHaveBeenCalled();
|
2022-12-27 11:36:31 -05:00
|
|
|
});
|
|
|
|
|
|
2023-05-21 23:18:10 -04:00
|
|
|
it('should not let the admin change the storage label to one already in use', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
const dto = { id: userStub.user1.id, storageLabel: 'admin' };
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
|
|
|
|
userMock.getByStorageLabel.mockResolvedValue(userStub.admin);
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.update(authStub.admin, dto)).rejects.toBeInstanceOf(BadRequestException);
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.update).not.toHaveBeenCalled();
|
2023-05-21 23:18:10 -04:00
|
|
|
});
|
|
|
|
|
|
2022-09-18 09:27:06 -05:00
|
|
|
it('admin can update any user information', async () => {
|
2022-12-23 21:08:50 +01:00
|
|
|
const update: UpdateUserDto = {
|
2023-10-31 11:01:32 -04:00
|
|
|
id: userStub.user1.id,
|
2022-12-23 21:08:50 +01:00
|
|
|
shouldChangePassword: true,
|
|
|
|
|
};
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
when(userMock.update).calledWith(userStub.user1.id, update).mockResolvedValueOnce(userStub.user1);
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.update(authStub.admin, update);
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(userStub.user1.id, {
|
|
|
|
|
id: 'user-id',
|
|
|
|
|
shouldChangePassword: true,
|
|
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
});
|
|
|
|
|
|
2022-12-23 21:08:50 +01:00
|
|
|
it('update user information should throw error if user not found', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
when(userMock.get).calledWith(userStub.user1.id, {}).mockResolvedValueOnce(null);
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
const result = sut.update(authStub.admin, {
|
2023-10-31 11:01:32 -04:00
|
|
|
id: userStub.user1.id,
|
2022-09-18 09:27:06 -05:00
|
|
|
shouldChangePassword: true,
|
|
|
|
|
});
|
2022-11-26 15:09:06 -06:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
await expect(result).rejects.toBeInstanceOf(BadRequestException);
|
2022-12-23 21:08:50 +01:00
|
|
|
});
|
2022-11-26 15:09:06 -06:00
|
|
|
|
2022-12-26 10:35:52 -05:00
|
|
|
it('should let the admin update himself', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
const dto = { id: userStub.admin.id, shouldChangePassword: true, isAdmin: true };
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
when(userMock.update).calledWith(userStub.admin.id, dto).mockResolvedValueOnce(userStub.admin);
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.update(authStub.admin, dto);
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(userStub.admin.id, dto);
|
2022-12-26 10:35:52 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not let the another user become an admin', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
const dto = { id: userStub.user1.id, shouldChangePassword: true, isAdmin: true };
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
when(userMock.get).calledWith(userStub.user1.id, {}).mockResolvedValueOnce(userStub.user1);
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.update(authStub.admin, dto)).rejects.toBeInstanceOf(BadRequestException);
|
2022-12-26 10:35:52 -05:00
|
|
|
});
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
describe('restore', () => {
|
2023-09-11 17:56:38 +02:00
|
|
|
it('should throw error if user could not be found', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
when(userMock.get).calledWith(userStub.admin.id, { withDeleted: true }).mockResolvedValue(null);
|
|
|
|
|
await expect(sut.restore(authStub.admin, userStub.admin.id)).rejects.toThrowError(BadRequestException);
|
2023-09-11 17:56:38 +02:00
|
|
|
expect(userMock.restore).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should restore an user', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
|
|
|
|
userMock.restore.mockResolvedValue(userStub.user1);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
await expect(sut.restore(authStub.admin, userStub.user1.id)).resolves.toEqual(mapUser(userStub.user1));
|
|
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.user1.id, { withDeleted: true });
|
|
|
|
|
expect(userMock.restore).toHaveBeenCalledWith(userStub.user1);
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
describe('delete', () => {
|
2023-09-11 17:56:38 +02:00
|
|
|
it('should throw error if user could not be found', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(null);
|
|
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
await expect(sut.delete(authStub.admin, userStub.admin.id)).rejects.toThrowError(BadRequestException);
|
2023-09-11 17:56:38 +02:00
|
|
|
expect(userMock.delete).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-12 17:07:57 -05:00
|
|
|
it('cannot delete admin user', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
await expect(sut.delete(authStub.admin, userStub.admin.id)).rejects.toBeInstanceOf(ForbiddenException);
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should require the auth user be an admin', async () => {
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.delete(authStub.user1, authStub.admin.user.id)).rejects.toBeInstanceOf(ForbiddenException);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.delete).not.toHaveBeenCalled();
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
it('should delete user', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
|
|
|
|
userMock.delete.mockResolvedValue(userStub.user1);
|
2023-09-11 17:56:38 +02:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.delete(authStub.admin, userStub.user1.id)).resolves.toEqual(mapUser(userStub.user1));
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.user1.id, {});
|
|
|
|
|
expect(userMock.delete).toHaveBeenCalledWith(userStub.user1);
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
2023-09-11 17:56:38 +02:00
|
|
|
describe('create', () => {
|
2022-12-23 21:08:50 +01:00
|
|
|
it('should not create a user if there is no local admin account', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
when(userMock.getAdmin).calledWith().mockResolvedValueOnce(null);
|
2022-12-23 21:08:50 +01:00
|
|
|
|
|
|
|
|
await expect(
|
2023-08-03 14:17:38 -04:00
|
|
|
sut.create({
|
2022-12-23 21:08:50 +01:00
|
|
|
email: 'john_smith@email.com',
|
2023-11-11 20:03:32 -05:00
|
|
|
name: 'John Smith',
|
2022-12-23 21:08:50 +01:00
|
|
|
password: 'password',
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
2022-11-26 15:09:06 -06:00
|
|
|
});
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
it('should create user', async () => {
|
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.admin);
|
|
|
|
|
userMock.create.mockResolvedValue(userStub.user1);
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
sut.create({
|
|
|
|
|
email: userStub.user1.email,
|
2023-11-11 20:03:32 -05:00
|
|
|
name: userStub.user1.name,
|
2023-09-11 17:56:38 +02:00
|
|
|
password: 'password',
|
|
|
|
|
storageLabel: 'label',
|
|
|
|
|
}),
|
|
|
|
|
).resolves.toEqual(mapUser(userStub.user1));
|
|
|
|
|
|
|
|
|
|
expect(userMock.getAdmin).toBeCalled();
|
|
|
|
|
expect(userMock.create).toBeCalledWith({
|
|
|
|
|
email: userStub.user1.email,
|
2023-11-11 20:03:32 -05:00
|
|
|
name: userStub.user1.name,
|
2023-09-11 17:56:38 +02:00
|
|
|
storageLabel: 'label',
|
|
|
|
|
password: expect.anything(),
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
});
|
2023-01-12 17:07:57 -05:00
|
|
|
|
|
|
|
|
describe('createProfileImage', () => {
|
|
|
|
|
it('should throw an error if the user does not exist', async () => {
|
|
|
|
|
const file = { path: '/profile/path' } as Express.Multer.File;
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.update.mockResolvedValue({ ...userStub.admin, profileImagePath: file.path });
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.createProfileImage(authStub.admin, file)).rejects.toThrowError(BadRequestException);
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
|
it('should throw an error if the user profile could not be updated with the new image', async () => {
|
|
|
|
|
const file = { path: '/profile/path' } as Express.Multer.File;
|
2023-11-14 04:10:35 +01:00
|
|
|
userMock.get.mockResolvedValue(userStub.profilePath);
|
2023-09-11 17:56:38 +02:00
|
|
|
userMock.update.mockRejectedValue(new InternalServerErrorException('mocked error'));
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.createProfileImage(authStub.admin, file)).rejects.toThrowError(InternalServerErrorException);
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
2023-11-14 04:10:35 +01:00
|
|
|
|
|
|
|
|
it('should delete the previous profile image', async () => {
|
|
|
|
|
const file = { path: '/profile/path' } as Express.Multer.File;
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.profilePath);
|
|
|
|
|
const files = [userStub.profilePath.profileImagePath];
|
|
|
|
|
userMock.update.mockResolvedValue({ ...userStub.admin, profileImagePath: file.path });
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.createProfileImage(authStub.admin, file);
|
2023-11-14 04:10:35 +01:00
|
|
|
await expect(jobMock.queue.mock.calls).toEqual([[{ name: JobName.DELETE_FILES, data: { files } }]]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not delete the profile image if it has not been set', async () => {
|
|
|
|
|
const file = { path: '/profile/path' } as Express.Multer.File;
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
|
|
|
|
userMock.update.mockResolvedValue({ ...userStub.admin, profileImagePath: file.path });
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.createProfileImage(authStub.admin, file);
|
2023-11-14 04:10:35 +01:00
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('deleteProfileImage', () => {
|
|
|
|
|
it('should send an http error has no profile image', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.deleteProfileImage(authStub.admin)).rejects.toBeInstanceOf(BadRequestException);
|
2023-11-14 04:10:35 +01:00
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should delete the profile image if user has one', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.profilePath);
|
|
|
|
|
const files = [userStub.profilePath.profileImagePath];
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.deleteProfileImage(authStub.admin);
|
2023-11-14 04:10:35 +01:00
|
|
|
await expect(jobMock.queue.mock.calls).toEqual([[{ name: JobName.DELETE_FILES, data: { files } }]]);
|
|
|
|
|
});
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getUserProfileImage', () => {
|
|
|
|
|
it('should throw an error if the user does not exist', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(null);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
await expect(sut.getProfileImage(userStub.admin.id)).rejects.toBeInstanceOf(BadRequestException);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.admin.id, {});
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if the user does not have a picture', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
await expect(sut.getProfileImage(userStub.admin.id)).rejects.toBeInstanceOf(NotFoundException);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.admin.id, {});
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
2023-10-30 19:38:34 -04:00
|
|
|
|
|
|
|
|
it('should return the profile picture', async () => {
|
|
|
|
|
userMock.get.mockResolvedValue(userStub.profilePath);
|
|
|
|
|
|
2023-12-12 09:58:25 -05:00
|
|
|
await expect(sut.getProfileImage(userStub.profilePath.id)).resolves.toEqual(
|
|
|
|
|
new ImmichFileResponse({
|
|
|
|
|
path: '/path/to/profile.jpg',
|
|
|
|
|
contentType: 'image/jpeg',
|
|
|
|
|
cacheControl: false,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2023-10-30 19:38:34 -04:00
|
|
|
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(userStub.profilePath.id, {});
|
2023-10-30 19:38:34 -04:00
|
|
|
});
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
2023-01-16 13:09:04 -05:00
|
|
|
|
|
|
|
|
describe('resetAdminPassword', () => {
|
|
|
|
|
it('should only work when there is an admin account', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.getAdmin.mockResolvedValue(null);
|
2023-01-16 13:09:04 -05:00
|
|
|
const ask = jest.fn().mockResolvedValue('new-password');
|
|
|
|
|
|
|
|
|
|
await expect(sut.resetAdminPassword(ask)).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
|
|
|
|
|
expect(ask).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should default to a random password', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.admin);
|
2023-01-16 13:09:04 -05:00
|
|
|
const ask = jest.fn().mockResolvedValue(undefined);
|
|
|
|
|
|
|
|
|
|
const response = await sut.resetAdminPassword(ask);
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
const [id, update] = userMock.update.mock.calls[0];
|
2023-01-16 13:09:04 -05:00
|
|
|
|
|
|
|
|
expect(response.provided).toBe(false);
|
|
|
|
|
expect(ask).toHaveBeenCalled();
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(id).toEqual(userStub.admin.id);
|
2023-01-16 13:09:04 -05:00
|
|
|
expect(update.password).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should use the supplied password', async () => {
|
2023-10-31 11:01:32 -04:00
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.admin);
|
2023-01-16 13:09:04 -05:00
|
|
|
const ask = jest.fn().mockResolvedValue('new-password');
|
|
|
|
|
|
|
|
|
|
const response = await sut.resetAdminPassword(ask);
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
const [id, update] = userMock.update.mock.calls[0];
|
2023-01-16 13:09:04 -05:00
|
|
|
|
|
|
|
|
expect(response.provided).toBe(true);
|
|
|
|
|
expect(ask).toHaveBeenCalled();
|
2023-10-31 11:01:32 -04:00
|
|
|
expect(id).toEqual(userStub.admin.id);
|
2023-01-16 13:09:04 -05:00
|
|
|
expect(update.password).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-03-24 08:19:48 -04:00
|
|
|
describe('handleQueueUserDelete', () => {
|
2023-02-25 09:12:03 -05:00
|
|
|
it('should skip users not ready for deletion', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.getDeletedUsers.mockResolvedValue([
|
2023-02-25 09:12:03 -05:00
|
|
|
{},
|
|
|
|
|
{ deletedAt: undefined },
|
|
|
|
|
{ deletedAt: null },
|
|
|
|
|
{ deletedAt: makeDeletedAt(5) },
|
|
|
|
|
] as UserEntity[]);
|
|
|
|
|
|
2023-05-17 13:07:17 -04:00
|
|
|
await sut.handleUserDeleteCheck();
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.getDeletedUsers).toHaveBeenCalled();
|
2023-02-25 09:12:03 -05:00
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should queue user ready for deletion', async () => {
|
2023-05-26 15:43:24 -04:00
|
|
|
const user = { id: 'deleted-user', deletedAt: makeDeletedAt(10) };
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.getDeletedUsers.mockResolvedValue([user] as UserEntity[]);
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-05-17 13:07:17 -04:00
|
|
|
await sut.handleUserDeleteCheck();
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.getDeletedUsers).toHaveBeenCalled();
|
2023-05-26 15:43:24 -04:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.USER_DELETION, data: { id: user.id } });
|
2023-02-25 09:12:03 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('handleUserDelete', () => {
|
|
|
|
|
it('should skip users not ready for deletion', async () => {
|
2023-05-26 15:43:24 -04:00
|
|
|
const user = { id: 'user-1', deletedAt: makeDeletedAt(5) } as UserEntity;
|
|
|
|
|
userMock.get.mockResolvedValue(user);
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
await sut.handleUserDelete({ id: user.id });
|
2023-02-25 09:12:03 -05:00
|
|
|
|
|
|
|
|
expect(storageMock.unlinkDir).not.toHaveBeenCalled();
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.delete).not.toHaveBeenCalled();
|
2023-02-25 09:12:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should delete the user and associated assets', async () => {
|
|
|
|
|
const user = { id: 'deleted-user', deletedAt: makeDeletedAt(10) } as UserEntity;
|
2023-05-26 15:43:24 -04:00
|
|
|
userMock.get.mockResolvedValue(user);
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
await sut.handleUserDelete({ id: user.id });
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-03-25 10:50:57 -04:00
|
|
|
const options = { force: true, recursive: true };
|
|
|
|
|
|
|
|
|
|
expect(storageMock.unlinkDir).toHaveBeenCalledWith('upload/library/deleted-user', options);
|
|
|
|
|
expect(storageMock.unlinkDir).toHaveBeenCalledWith('upload/upload/deleted-user', options);
|
|
|
|
|
expect(storageMock.unlinkDir).toHaveBeenCalledWith('upload/profile/deleted-user', options);
|
|
|
|
|
expect(storageMock.unlinkDir).toHaveBeenCalledWith('upload/thumbs/deleted-user', options);
|
|
|
|
|
expect(storageMock.unlinkDir).toHaveBeenCalledWith('upload/encoded-video/deleted-user', options);
|
2023-02-25 09:12:03 -05:00
|
|
|
expect(albumMock.deleteAll).toHaveBeenCalledWith(user.id);
|
|
|
|
|
expect(assetMock.deleteAll).toHaveBeenCalledWith(user.id);
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.delete).toHaveBeenCalledWith(user, true);
|
2023-02-25 09:12:03 -05:00
|
|
|
});
|
|
|
|
|
|
2023-05-21 23:18:10 -04:00
|
|
|
it('should delete the library path for a storage label', async () => {
|
|
|
|
|
const user = { id: 'deleted-user', deletedAt: makeDeletedAt(10), storageLabel: 'admin' } as UserEntity;
|
2023-05-26 15:43:24 -04:00
|
|
|
userMock.get.mockResolvedValue(user);
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
await sut.handleUserDelete({ id: user.id });
|
2023-05-21 23:18:10 -04:00
|
|
|
|
|
|
|
|
const options = { force: true, recursive: true };
|
|
|
|
|
|
|
|
|
|
expect(storageMock.unlinkDir).toHaveBeenCalledWith('upload/library/admin', options);
|
|
|
|
|
});
|
2023-02-25 09:12:03 -05:00
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
});
|