2023-02-25 15:12:03 +01:00
|
|
|
import { when } from 'jest-when';
|
|
|
|
import {
|
|
|
|
assetEntityStub,
|
|
|
|
newAssetRepositoryMock,
|
|
|
|
newStorageRepositoryMock,
|
|
|
|
newSystemConfigRepositoryMock,
|
2023-05-22 05:18:10 +02:00
|
|
|
newUserRepositoryMock,
|
2023-02-25 15:12:03 +01:00
|
|
|
systemConfigStub,
|
2023-05-22 05:18:10 +02:00
|
|
|
userEntityStub,
|
2023-02-25 15:12:03 +01:00
|
|
|
} from '../../test';
|
|
|
|
import { IAssetRepository } from '../asset';
|
|
|
|
import { StorageTemplateService } from '../storage-template';
|
|
|
|
import { IStorageRepository } from '../storage/storage.repository';
|
|
|
|
import { ISystemConfigRepository } from '../system-config';
|
2023-05-22 05:18:10 +02:00
|
|
|
import { IUserRepository } from '../user';
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
describe(StorageTemplateService.name, () => {
|
|
|
|
let sut: StorageTemplateService;
|
|
|
|
let assetMock: jest.Mocked<IAssetRepository>;
|
|
|
|
let configMock: jest.Mocked<ISystemConfigRepository>;
|
|
|
|
let storageMock: jest.Mocked<IStorageRepository>;
|
2023-05-22 05:18:10 +02:00
|
|
|
let userMock: jest.Mocked<IUserRepository>;
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
assetMock = newAssetRepositoryMock();
|
|
|
|
configMock = newSystemConfigRepositoryMock();
|
|
|
|
storageMock = newStorageRepositoryMock();
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock = newUserRepositoryMock();
|
|
|
|
|
|
|
|
sut = new StorageTemplateService(assetMock, configMock, systemConfigStub.defaults, storageMock, userMock);
|
2023-02-25 15:12:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('handle template migration', () => {
|
|
|
|
it('should handle no assets', async () => {
|
2023-05-22 20:05:06 +02:00
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock.getList.mockResolvedValue([]);
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle an asset with a duplicate destination', async () => {
|
2023-05-22 20:05:06 +02:00
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [assetEntityStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-02-25 15:12:03 +01:00
|
|
|
assetMock.save.mockResolvedValue(assetEntityStub.image);
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
when(storageMock.checkFileExists)
|
2023-03-25 15:50:57 +01:00
|
|
|
.calledWith('upload/library/user-id/2023/2023-02-23/asset-id.ext')
|
2023-02-25 15:12:03 +01:00
|
|
|
.mockResolvedValue(true);
|
|
|
|
|
|
|
|
when(storageMock.checkFileExists)
|
2023-03-25 15:50:57 +01:00
|
|
|
.calledWith('upload/library/user-id/2023/2023-02-23/asset-id+1.ext')
|
2023-02-25 15:12:03 +01:00
|
|
|
.mockResolvedValue(false);
|
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(storageMock.checkFileExists).toHaveBeenCalledTimes(2);
|
|
|
|
expect(assetMock.save).toHaveBeenCalledWith({
|
|
|
|
id: assetEntityStub.image.id,
|
2023-03-25 15:50:57 +01:00
|
|
|
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id+1.ext',
|
2023-02-25 15:12:03 +01:00
|
|
|
});
|
2023-05-22 05:18:10 +02:00
|
|
|
expect(userMock.getList).toHaveBeenCalled();
|
2023-02-25 15:12:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip when an asset already matches the template', async () => {
|
2023-05-22 20:05:06 +02:00
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
...assetEntityStub.image,
|
|
|
|
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id.ext',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(storageMock.moveFile).not.toHaveBeenCalled();
|
|
|
|
expect(storageMock.checkFileExists).not.toHaveBeenCalledTimes(2);
|
|
|
|
expect(assetMock.save).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip when an asset is probably a duplicate', async () => {
|
2023-05-22 20:05:06 +02:00
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
...assetEntityStub.image,
|
|
|
|
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id+1.ext',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(storageMock.moveFile).not.toHaveBeenCalled();
|
|
|
|
expect(storageMock.checkFileExists).not.toHaveBeenCalledTimes(2);
|
|
|
|
expect(assetMock.save).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should move an asset', async () => {
|
2023-05-22 20:05:06 +02:00
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [assetEntityStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-02-25 15:12:03 +01:00
|
|
|
assetMock.save.mockResolvedValue(assetEntityStub.image);
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(storageMock.moveFile).toHaveBeenCalledWith(
|
|
|
|
'/original/path.ext',
|
2023-03-25 15:50:57 +01:00
|
|
|
'upload/library/user-id/2023/2023-02-23/asset-id.ext',
|
2023-02-25 15:12:03 +01:00
|
|
|
);
|
|
|
|
expect(assetMock.save).toHaveBeenCalledWith({
|
|
|
|
id: assetEntityStub.image.id,
|
2023-03-25 15:50:57 +01:00
|
|
|
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id.ext',
|
2023-02-25 15:12:03 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-22 05:18:10 +02:00
|
|
|
it('should use the user storage label', async () => {
|
2023-05-22 20:05:06 +02:00
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [assetEntityStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-05-22 05:18:10 +02:00
|
|
|
assetMock.save.mockResolvedValue(assetEntityStub.image);
|
|
|
|
userMock.getList.mockResolvedValue([userEntityStub.storageLabel]);
|
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(storageMock.moveFile).toHaveBeenCalledWith(
|
|
|
|
'/original/path.ext',
|
|
|
|
'upload/library/label-1/2023/2023-02-23/asset-id.ext',
|
|
|
|
);
|
|
|
|
expect(assetMock.save).toHaveBeenCalledWith({
|
|
|
|
id: assetEntityStub.image.id,
|
|
|
|
originalPath: 'upload/library/label-1/2023/2023-02-23/asset-id.ext',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-25 15:12:03 +01:00
|
|
|
it('should not update the database if the move fails', async () => {
|
2023-05-22 20:05:06 +02:00
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [assetEntityStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-02-25 15:12:03 +01:00
|
|
|
storageMock.moveFile.mockRejectedValue(new Error('Read only system'));
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(storageMock.moveFile).toHaveBeenCalledWith(
|
|
|
|
'/original/path.ext',
|
2023-03-25 15:50:57 +01:00
|
|
|
'upload/library/user-id/2023/2023-02-23/asset-id.ext',
|
2023-02-25 15:12:03 +01:00
|
|
|
);
|
|
|
|
expect(assetMock.save).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should move the asset back if the database fails', async () => {
|
2023-05-22 20:05:06 +02:00
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [assetEntityStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-02-25 15:12:03 +01:00
|
|
|
assetMock.save.mockRejectedValue('Connection Error!');
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock.getList.mockResolvedValue([userEntityStub.user1]);
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(assetMock.save).toHaveBeenCalledWith({
|
|
|
|
id: assetEntityStub.image.id,
|
2023-03-25 15:50:57 +01:00
|
|
|
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id.ext',
|
2023-02-25 15:12:03 +01:00
|
|
|
});
|
|
|
|
expect(storageMock.moveFile.mock.calls).toEqual([
|
2023-03-25 15:50:57 +01:00
|
|
|
['/original/path.ext', 'upload/library/user-id/2023/2023-02-23/asset-id.ext'],
|
|
|
|
['upload/library/user-id/2023/2023-02-23/asset-id.ext', '/original/path.ext'],
|
2023-02-25 15:12:03 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle an error', async () => {
|
|
|
|
storageMock.removeEmptyDirs.mockRejectedValue(new Error('Read only filesystem'));
|
2023-05-22 05:18:10 +02:00
|
|
|
userMock.getList.mockResolvedValue([]);
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await sut.handleTemplateMigration();
|
|
|
|
});
|
|
|
|
});
|