2023-03-30 15:38:55 -04:00
|
|
|
import { UserEntity } from '@app/infra/entities';
|
2022-12-23 21:08:50 +01:00
|
|
|
import { BadRequestException, ForbiddenException, NotFoundException } from '@nestjs/common';
|
2023-02-25 09:12:03 -05:00
|
|
|
import {
|
|
|
|
|
newAlbumRepositoryMock,
|
|
|
|
|
newAssetRepositoryMock,
|
|
|
|
|
newCryptoRepositoryMock,
|
|
|
|
|
newJobRepositoryMock,
|
|
|
|
|
newStorageRepositoryMock,
|
|
|
|
|
newUserRepositoryMock,
|
2023-06-08 11:01:07 -04:00
|
|
|
} from '@test';
|
2023-06-16 15:54:17 -04:00
|
|
|
import { when } from 'jest-when';
|
2023-02-25 09:12:03 -05:00
|
|
|
import { IAlbumRepository } from '../album';
|
|
|
|
|
import { IAssetRepository } from '../asset';
|
2023-01-31 13:11:49 -05:00
|
|
|
import { AuthUserDto } from '../auth';
|
|
|
|
|
import { ICryptoRepository } from '../crypto';
|
2023-02-25 09:12:03 -05:00
|
|
|
import { IJobRepository, JobName } from '../job';
|
|
|
|
|
import { IStorageRepository } from '../storage';
|
2022-12-23 21:08:50 +01:00
|
|
|
import { UpdateUserDto } from './dto/update-user.dto';
|
2023-08-09 22:01:16 -04:00
|
|
|
import { UserResponseDto } from './response-dto';
|
2023-02-25 09:12:03 -05:00
|
|
|
import { IUserRepository } from './user.repository';
|
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-12 17:07:57 -05:00
|
|
|
const adminUserAuth: AuthUserDto = Object.freeze({
|
|
|
|
|
id: 'admin_id',
|
|
|
|
|
email: 'admin@test.com',
|
|
|
|
|
isAdmin: true,
|
|
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-01-12 17:07:57 -05:00
|
|
|
const immichUserAuth: AuthUserDto = Object.freeze({
|
2023-05-21 23:18:10 -04:00
|
|
|
id: 'user-id',
|
2023-01-12 17:07:57 -05:00
|
|
|
email: 'immich@test.com',
|
|
|
|
|
isAdmin: false,
|
|
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-01-12 17:07:57 -05:00
|
|
|
const adminUser: UserEntity = Object.freeze({
|
|
|
|
|
id: adminUserAuth.id,
|
|
|
|
|
email: 'admin@test.com',
|
|
|
|
|
password: 'admin_password',
|
|
|
|
|
firstName: 'admin_first_name',
|
|
|
|
|
lastName: 'admin_last_name',
|
|
|
|
|
isAdmin: true,
|
|
|
|
|
oauthId: '',
|
|
|
|
|
shouldChangePassword: false,
|
|
|
|
|
profileImagePath: '',
|
2023-05-30 15:15:56 +02:00
|
|
|
createdAt: new Date('2021-01-01'),
|
|
|
|
|
deletedAt: null,
|
|
|
|
|
updatedAt: new Date('2021-01-01'),
|
2023-01-12 17:07:57 -05:00
|
|
|
tags: [],
|
2023-02-26 20:57:34 +01:00
|
|
|
assets: [],
|
2023-05-21 23:18:10 -04:00
|
|
|
storageLabel: 'admin',
|
feat(server): support for read-only assets and importing existing items in the filesystem (#2715)
* Added read-only flag for assets, endpoint to trigger file import vs upload
* updated fixtures with new property
* if upload is 'read-only', ensure there is no existing asset at the designated originalPath
* added test for file import as well as detecting existing image at read-only destination location
* Added storage service test for a case where it should not move read-only assets
* upload doesn't need the read-only flag available, just importing
* default isReadOnly on import endpoint to true
* formatting fixes
* create-asset dto needs isReadOnly, so set it to false by default on create, updated api generation
* updated code to reflect changes in MR
* fixed read stream promise return type
* new index for originalPath, check for existing path on import, reglardless of user, to prevent duplicates
* refactor: import asset
* chore: open api
* chore: tests
* Added externalPath support for individual users, updated UI to allow this to be set by admin
* added missing var for externalPath in ui
* chore: open api
* fix: compilation issues
* fix: server test
* built api, fixed user-response dto to include externalPath
* reverted accidental commit
* bad commit of duplicate externalPath in user response dto
* fixed tests to include externalPath on expected result
* fix: unit tests
* centralized supported filetypes, perform file type checking of asset and sidecar during file import process
* centralized supported filetype check method to keep regex DRY
* fixed typo
* combined migrations into one
* update api
* Removed externalPath from shared-link code, added column to admin user page whether external paths / import is enabled or not
* update mimetype
* Fixed detect correct mimetype
* revert asset-upload config
* reverted domain.constant
* refactor
* fix mime-type issue
* fix format
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-06-21 22:33:20 -04:00
|
|
|
externalPath: null,
|
2023-08-09 22:01:16 -04:00
|
|
|
memoriesEnabled: true,
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-01-12 17:07:57 -05:00
|
|
|
const immichUser: UserEntity = Object.freeze({
|
|
|
|
|
id: immichUserAuth.id,
|
|
|
|
|
email: 'immich@test.com',
|
|
|
|
|
password: 'immich_password',
|
|
|
|
|
firstName: 'immich_first_name',
|
|
|
|
|
lastName: 'immich_last_name',
|
|
|
|
|
isAdmin: false,
|
|
|
|
|
oauthId: '',
|
|
|
|
|
shouldChangePassword: false,
|
|
|
|
|
profileImagePath: '',
|
2023-05-30 15:15:56 +02:00
|
|
|
createdAt: new Date('2021-01-01'),
|
|
|
|
|
deletedAt: null,
|
|
|
|
|
updatedAt: new Date('2021-01-01'),
|
2023-01-12 17:07:57 -05:00
|
|
|
tags: [],
|
2023-02-26 20:57:34 +01:00
|
|
|
assets: [],
|
2023-05-21 23:18:10 -04:00
|
|
|
storageLabel: null,
|
feat(server): support for read-only assets and importing existing items in the filesystem (#2715)
* Added read-only flag for assets, endpoint to trigger file import vs upload
* updated fixtures with new property
* if upload is 'read-only', ensure there is no existing asset at the designated originalPath
* added test for file import as well as detecting existing image at read-only destination location
* Added storage service test for a case where it should not move read-only assets
* upload doesn't need the read-only flag available, just importing
* default isReadOnly on import endpoint to true
* formatting fixes
* create-asset dto needs isReadOnly, so set it to false by default on create, updated api generation
* updated code to reflect changes in MR
* fixed read stream promise return type
* new index for originalPath, check for existing path on import, reglardless of user, to prevent duplicates
* refactor: import asset
* chore: open api
* chore: tests
* Added externalPath support for individual users, updated UI to allow this to be set by admin
* added missing var for externalPath in ui
* chore: open api
* fix: compilation issues
* fix: server test
* built api, fixed user-response dto to include externalPath
* reverted accidental commit
* bad commit of duplicate externalPath in user response dto
* fixed tests to include externalPath on expected result
* fix: unit tests
* centralized supported filetypes, perform file type checking of asset and sidecar during file import process
* centralized supported filetype check method to keep regex DRY
* fixed typo
* combined migrations into one
* update api
* Removed externalPath from shared-link code, added column to admin user page whether external paths / import is enabled or not
* update mimetype
* Fixed detect correct mimetype
* revert asset-upload config
* reverted domain.constant
* refactor
* fix mime-type issue
* fix format
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-06-21 22:33:20 -04:00
|
|
|
externalPath: null,
|
2023-08-09 22:01:16 -04:00
|
|
|
memoriesEnabled: true,
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-08-09 22:01:16 -04:00
|
|
|
const updatedImmichUser = Object.freeze<UserEntity>({
|
2023-01-12 17:07:57 -05:00
|
|
|
id: immichUserAuth.id,
|
|
|
|
|
email: 'immich@test.com',
|
|
|
|
|
password: 'immich_password',
|
|
|
|
|
firstName: 'updated_immich_first_name',
|
|
|
|
|
lastName: 'updated_immich_last_name',
|
|
|
|
|
isAdmin: false,
|
|
|
|
|
oauthId: '',
|
|
|
|
|
shouldChangePassword: true,
|
|
|
|
|
profileImagePath: '',
|
2023-05-30 15:15:56 +02:00
|
|
|
createdAt: new Date('2021-01-01'),
|
|
|
|
|
deletedAt: null,
|
|
|
|
|
updatedAt: new Date('2021-01-01'),
|
2023-01-12 17:07:57 -05:00
|
|
|
tags: [],
|
2023-02-26 20:57:34 +01:00
|
|
|
assets: [],
|
2023-05-21 23:18:10 -04:00
|
|
|
storageLabel: null,
|
feat(server): support for read-only assets and importing existing items in the filesystem (#2715)
* Added read-only flag for assets, endpoint to trigger file import vs upload
* updated fixtures with new property
* if upload is 'read-only', ensure there is no existing asset at the designated originalPath
* added test for file import as well as detecting existing image at read-only destination location
* Added storage service test for a case where it should not move read-only assets
* upload doesn't need the read-only flag available, just importing
* default isReadOnly on import endpoint to true
* formatting fixes
* create-asset dto needs isReadOnly, so set it to false by default on create, updated api generation
* updated code to reflect changes in MR
* fixed read stream promise return type
* new index for originalPath, check for existing path on import, reglardless of user, to prevent duplicates
* refactor: import asset
* chore: open api
* chore: tests
* Added externalPath support for individual users, updated UI to allow this to be set by admin
* added missing var for externalPath in ui
* chore: open api
* fix: compilation issues
* fix: server test
* built api, fixed user-response dto to include externalPath
* reverted accidental commit
* bad commit of duplicate externalPath in user response dto
* fixed tests to include externalPath on expected result
* fix: unit tests
* centralized supported filetypes, perform file type checking of asset and sidecar during file import process
* centralized supported filetype check method to keep regex DRY
* fixed typo
* combined migrations into one
* update api
* Removed externalPath from shared-link code, added column to admin user page whether external paths / import is enabled or not
* update mimetype
* Fixed detect correct mimetype
* revert asset-upload config
* reverted domain.constant
* refactor
* fix mime-type issue
* fix format
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-06-21 22:33:20 -04:00
|
|
|
externalPath: null,
|
2023-08-09 22:01:16 -04:00
|
|
|
memoriesEnabled: true,
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-08-09 22:01:16 -04:00
|
|
|
const adminUserResponse = Object.freeze<UserResponseDto>({
|
2023-01-12 17:07:57 -05:00
|
|
|
id: adminUserAuth.id,
|
|
|
|
|
email: 'admin@test.com',
|
|
|
|
|
firstName: 'admin_first_name',
|
|
|
|
|
lastName: 'admin_last_name',
|
|
|
|
|
isAdmin: true,
|
|
|
|
|
oauthId: '',
|
|
|
|
|
shouldChangePassword: false,
|
|
|
|
|
profileImagePath: '',
|
2023-05-30 15:15:56 +02:00
|
|
|
createdAt: new Date('2021-01-01'),
|
|
|
|
|
deletedAt: null,
|
|
|
|
|
updatedAt: new Date('2021-01-01'),
|
2023-05-21 23:18:10 -04:00
|
|
|
storageLabel: 'admin',
|
feat(server): support for read-only assets and importing existing items in the filesystem (#2715)
* Added read-only flag for assets, endpoint to trigger file import vs upload
* updated fixtures with new property
* if upload is 'read-only', ensure there is no existing asset at the designated originalPath
* added test for file import as well as detecting existing image at read-only destination location
* Added storage service test for a case where it should not move read-only assets
* upload doesn't need the read-only flag available, just importing
* default isReadOnly on import endpoint to true
* formatting fixes
* create-asset dto needs isReadOnly, so set it to false by default on create, updated api generation
* updated code to reflect changes in MR
* fixed read stream promise return type
* new index for originalPath, check for existing path on import, reglardless of user, to prevent duplicates
* refactor: import asset
* chore: open api
* chore: tests
* Added externalPath support for individual users, updated UI to allow this to be set by admin
* added missing var for externalPath in ui
* chore: open api
* fix: compilation issues
* fix: server test
* built api, fixed user-response dto to include externalPath
* reverted accidental commit
* bad commit of duplicate externalPath in user response dto
* fixed tests to include externalPath on expected result
* fix: unit tests
* centralized supported filetypes, perform file type checking of asset and sidecar during file import process
* centralized supported filetype check method to keep regex DRY
* fixed typo
* combined migrations into one
* update api
* Removed externalPath from shared-link code, added column to admin user page whether external paths / import is enabled or not
* update mimetype
* Fixed detect correct mimetype
* revert asset-upload config
* reverted domain.constant
* refactor
* fix mime-type issue
* fix format
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-06-21 22:33:20 -04:00
|
|
|
externalPath: null,
|
2023-08-09 22:01:16 -04:00
|
|
|
memoriesEnabled: true,
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
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>;
|
|
|
|
|
let storageMock: jest.Mocked<IStorageRepository>;
|
|
|
|
|
|
2023-01-18 09:40:15 -05:00
|
|
|
beforeEach(async () => {
|
2023-01-27 20:50:07 +00:00
|
|
|
cryptoRepositoryMock = newCryptoRepositoryMock();
|
2023-02-25 09:12:03 -05:00
|
|
|
albumMock = newAlbumRepositoryMock();
|
|
|
|
|
assetMock = newAssetRepositoryMock();
|
|
|
|
|
jobMock = newJobRepositoryMock();
|
|
|
|
|
storageMock = newStorageRepositoryMock();
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock = newUserRepositoryMock();
|
|
|
|
|
|
|
|
|
|
sut = new UserService(userMock, cryptoRepositoryMock, albumMock, assetMock, jobMock, storageMock);
|
|
|
|
|
|
|
|
|
|
when(userMock.get).calledWith(adminUser.id).mockResolvedValue(adminUser);
|
|
|
|
|
when(userMock.get).calledWith(adminUser.id, undefined).mockResolvedValue(adminUser);
|
|
|
|
|
when(userMock.get).calledWith(immichUser.id).mockResolvedValue(immichUser);
|
|
|
|
|
when(userMock.get).calledWith(immichUser.id, undefined).mockResolvedValue(immichUser);
|
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-05-23 16:40:04 -04:00
|
|
|
userMock.getList.mockResolvedValue([adminUser]);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
const response = await sut.getAll(adminUserAuth, false);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.getList).toHaveBeenCalledWith({ withDeleted: true });
|
2023-01-12 17:07:57 -05:00
|
|
|
expect(response).toEqual([
|
|
|
|
|
{
|
|
|
|
|
id: adminUserAuth.id,
|
|
|
|
|
email: 'admin@test.com',
|
|
|
|
|
firstName: 'admin_first_name',
|
|
|
|
|
lastName: 'admin_last_name',
|
|
|
|
|
isAdmin: true,
|
|
|
|
|
oauthId: '',
|
|
|
|
|
shouldChangePassword: false,
|
|
|
|
|
profileImagePath: '',
|
2023-05-30 15:15:56 +02:00
|
|
|
createdAt: new Date('2021-01-01'),
|
|
|
|
|
deletedAt: null,
|
|
|
|
|
updatedAt: new Date('2021-01-01'),
|
2023-05-21 23:18:10 -04:00
|
|
|
storageLabel: 'admin',
|
feat(server): support for read-only assets and importing existing items in the filesystem (#2715)
* Added read-only flag for assets, endpoint to trigger file import vs upload
* updated fixtures with new property
* if upload is 'read-only', ensure there is no existing asset at the designated originalPath
* added test for file import as well as detecting existing image at read-only destination location
* Added storage service test for a case where it should not move read-only assets
* upload doesn't need the read-only flag available, just importing
* default isReadOnly on import endpoint to true
* formatting fixes
* create-asset dto needs isReadOnly, so set it to false by default on create, updated api generation
* updated code to reflect changes in MR
* fixed read stream promise return type
* new index for originalPath, check for existing path on import, reglardless of user, to prevent duplicates
* refactor: import asset
* chore: open api
* chore: tests
* Added externalPath support for individual users, updated UI to allow this to be set by admin
* added missing var for externalPath in ui
* chore: open api
* fix: compilation issues
* fix: server test
* built api, fixed user-response dto to include externalPath
* reverted accidental commit
* bad commit of duplicate externalPath in user response dto
* fixed tests to include externalPath on expected result
* fix: unit tests
* centralized supported filetypes, perform file type checking of asset and sidecar during file import process
* centralized supported filetype check method to keep regex DRY
* fixed typo
* combined migrations into one
* update api
* Removed externalPath from shared-link code, added column to admin user page whether external paths / import is enabled or not
* update mimetype
* Fixed detect correct mimetype
* revert asset-upload config
* reverted domain.constant
* refactor
* fix mime-type issue
* fix format
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-06-21 22:33:20 -04:00
|
|
|
externalPath: null,
|
2023-08-09 22:01:16 -04:00
|
|
|
memoriesEnabled: 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-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(adminUser);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
const response = await sut.get(adminUser.id);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(adminUser.id, false);
|
2023-01-12 17:07:57 -05:00
|
|
|
expect(response).toEqual(adminUserResponse);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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-01-12 17:07:57 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.get(adminUser.id)).rejects.toBeInstanceOf(NotFoundException);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(adminUser.id, 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-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(adminUser);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
const response = await sut.getMe(adminUser);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(adminUser.id, undefined);
|
2023-01-12 17:07:57 -05:00
|
|
|
expect(response).toEqual(adminUserResponse);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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-01-12 17:07:57 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.getMe(adminUser)).rejects.toBeInstanceOf(BadRequestException);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(adminUser.id, undefined);
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
describe('getCount', () => {
|
2023-01-12 17:07:57 -05:00
|
|
|
it('should get the user count', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.getList.mockResolvedValue([adminUser]);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
const response = await sut.getCount({});
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.getList).toHaveBeenCalled();
|
2023-01-12 17:07:57 -05:00
|
|
|
expect(response).toEqual({ userCount: 1 });
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
|
id: immichUser.id,
|
|
|
|
|
shouldChangePassword: true,
|
|
|
|
|
};
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
when(userMock.update).calledWith(update.id, update).mockResolvedValueOnce(updatedImmichUser);
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
const updatedUser = await sut.update(immichUserAuth, update);
|
2022-12-23 21:08:50 +01:00
|
|
|
expect(updatedUser.shouldChangePassword).toEqual(true);
|
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-05-23 16:40:04 -04:00
|
|
|
userMock.update.mockResolvedValue(updatedImmichUser);
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await sut.update(adminUserAuth, { id: immichUser.id, storageLabel: '' });
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(immichUser.id, { id: immichUser.id, storageLabel: null });
|
2023-05-21 23:18:10 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should omit a storage label set by non-admin users', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.update.mockResolvedValue(updatedImmichUser);
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await sut.update(immichUserAuth, { id: immichUser.id, storageLabel: 'admin' });
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(immichUser.id, { id: immichUser.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)
|
2022-12-23 21:08:50 +01:00
|
|
|
.calledWith('not_immich_auth_user_id', undefined)
|
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
|
...immichUser,
|
|
|
|
|
id: 'not_immich_auth_user_id',
|
|
|
|
|
});
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
const result = sut.update(immichUserAuth, {
|
2022-09-18 09:27:06 -05:00
|
|
|
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 () => {
|
|
|
|
|
const dto = { id: immichUser.id, email: 'updated@test.com' };
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(immichUser);
|
|
|
|
|
userMock.update.mockResolvedValue(immichUser);
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await sut.update(immichUser, dto);
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(immichUser.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 () => {
|
|
|
|
|
const dto = { id: immichUser.id, email: 'updated@test.com' };
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(immichUser);
|
|
|
|
|
userMock.getByEmail.mockResolvedValue(adminUser);
|
2022-12-27 11:36:31 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.update(immichUser, 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 () => {
|
|
|
|
|
const dto = { id: immichUser.id, storageLabel: 'admin' };
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(immichUser);
|
|
|
|
|
userMock.getByStorageLabel.mockResolvedValue(adminUser);
|
2023-05-21 23:18:10 -04:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.update(adminUser, 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 = {
|
|
|
|
|
id: immichUser.id,
|
|
|
|
|
shouldChangePassword: true,
|
|
|
|
|
};
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
when(userMock.update).calledWith(immichUser.id, update).mockResolvedValueOnce(updatedImmichUser);
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
const result = await sut.update(adminUserAuth, update);
|
2022-09-18 09:27:06 -05:00
|
|
|
|
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
|
expect(result.id).toEqual(updatedImmichUser.id);
|
|
|
|
|
expect(result.shouldChangePassword).toEqual(updatedImmichUser.shouldChangePassword);
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-23 21:08:50 +01:00
|
|
|
it('update user information should throw error if user not found', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
when(userMock.get).calledWith(immichUser.id, undefined).mockResolvedValueOnce(null);
|
2022-09-18 09:27:06 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
const result = sut.update(adminUser, {
|
2022-12-23 21:08:50 +01:00
|
|
|
id: immichUser.id,
|
2022-09-18 09:27:06 -05:00
|
|
|
shouldChangePassword: true,
|
|
|
|
|
});
|
2022-11-26 15:09:06 -06:00
|
|
|
|
2022-12-23 21:08:50 +01:00
|
|
|
await expect(result).rejects.toBeInstanceOf(NotFoundException);
|
|
|
|
|
});
|
2022-11-26 15:09:06 -06:00
|
|
|
|
2022-12-26 10:35:52 -05:00
|
|
|
it('should let the admin update himself', async () => {
|
|
|
|
|
const dto = { id: adminUser.id, shouldChangePassword: true, isAdmin: true };
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
when(userMock.get).calledWith(adminUser.id).mockResolvedValueOnce(null);
|
|
|
|
|
when(userMock.update).calledWith(adminUser.id, dto).mockResolvedValueOnce(adminUser);
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await sut.update(adminUser, dto);
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(adminUser.id, dto);
|
2022-12-26 10:35:52 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not let the another user become an admin', async () => {
|
|
|
|
|
const dto = { id: immichUser.id, shouldChangePassword: true, isAdmin: true };
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
when(userMock.get).calledWith(immichUser.id).mockResolvedValueOnce(immichUser);
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.update(adminUser, 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-01-12 17:07:57 -05:00
|
|
|
it('should require an admin', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
when(userMock.get).calledWith(adminUser.id, true).mockResolvedValue(adminUser);
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.restore(immichUserAuth, adminUser.id)).rejects.toBeInstanceOf(ForbiddenException);
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(adminUser.id, true);
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should require the auth user be an admin', async () => {
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.delete(immichUserAuth, adminUserAuth.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-08-03 14:17:38 -04:00
|
|
|
describe('delete', () => {
|
2023-01-12 17:07:57 -05:00
|
|
|
it('cannot delete admin user', async () => {
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.delete(adminUserAuth, adminUserAuth.id)).rejects.toBeInstanceOf(ForbiddenException);
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should require the auth user be an admin', async () => {
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.delete(immichUserAuth, adminUserAuth.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
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('update', () => {
|
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',
|
|
|
|
|
firstName: 'John',
|
|
|
|
|
lastName: 'Smith',
|
|
|
|
|
password: 'password',
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
2022-11-26 15:09:06 -06:00
|
|
|
});
|
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-05-23 16:40:04 -04:00
|
|
|
userMock.update.mockResolvedValue({ ...adminUser, profileImagePath: file.path });
|
2023-01-12 17:07:57 -05:00
|
|
|
|
|
|
|
|
await sut.createProfileImage(adminUserAuth, file);
|
|
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(adminUserAuth.id, { profileImagePath: file.path });
|
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-08-03 14:17:38 -04:00
|
|
|
await expect(sut.getProfileImage(adminUserAuth.id)).rejects.toBeInstanceOf(NotFoundException);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(adminUserAuth.id, undefined);
|
2023-01-12 17:07:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if the user does not have a picture', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.get.mockResolvedValue(adminUser);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-08-03 14:17:38 -04:00
|
|
|
await expect(sut.getProfileImage(adminUserAuth.id)).rejects.toBeInstanceOf(NotFoundException);
|
2023-01-12 17:07:57 -05:00
|
|
|
|
2023-05-23 16:40:04 -04:00
|
|
|
expect(userMock.get).toHaveBeenCalledWith(adminUserAuth.id, undefined);
|
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-05-23 16:40:04 -04:00
|
|
|
userMock.getAdmin.mockResolvedValue(adminUser);
|
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();
|
|
|
|
|
expect(id).toEqual(adminUser.id);
|
|
|
|
|
expect(update.password).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should use the supplied password', async () => {
|
2023-05-23 16:40:04 -04:00
|
|
|
userMock.getAdmin.mockResolvedValue(adminUser);
|
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();
|
|
|
|
|
expect(id).toEqual(adminUser.id);
|
|
|
|
|
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
|
|
|
});
|