2023-10-06 07:01:14 +00:00
|
|
|
import { AssetEntity, AssetType } from '@app/infra/entities';
|
2023-07-14 21:31:42 -04:00
|
|
|
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
|
2023-06-30 12:24:28 -04:00
|
|
|
import {
|
2023-09-04 15:45:59 -04:00
|
|
|
IAccessRepositoryMock,
|
2023-07-31 21:28:07 -04:00
|
|
|
assetStub,
|
2023-06-30 12:24:28 -04:00
|
|
|
authStub,
|
2023-10-06 07:01:14 +00:00
|
|
|
faceStub,
|
2023-06-30 12:24:28 -04:00
|
|
|
newAccessRepositoryMock,
|
|
|
|
|
newAssetRepositoryMock,
|
2023-10-06 15:48:11 -05:00
|
|
|
newCommunicationRepositoryMock,
|
2023-07-14 21:31:42 -04:00
|
|
|
newCryptoRepositoryMock,
|
2023-08-18 10:31:48 -04:00
|
|
|
newJobRepositoryMock,
|
2023-06-30 12:24:28 -04:00
|
|
|
newStorageRepositoryMock,
|
2023-10-06 07:01:14 +00:00
|
|
|
newSystemConfigRepositoryMock,
|
2023-06-30 12:24:28 -04:00
|
|
|
} from '@test';
|
2023-06-15 14:05:30 -04:00
|
|
|
import { when } from 'jest-when';
|
2023-06-30 12:24:28 -04:00
|
|
|
import { Readable } from 'stream';
|
2023-10-09 10:25:03 -04:00
|
|
|
import { JobName } from '../job';
|
|
|
|
|
import {
|
|
|
|
|
AssetStats,
|
2023-10-22 02:38:07 +00:00
|
|
|
CommunicationEvent,
|
2023-10-09 10:25:03 -04:00
|
|
|
IAssetRepository,
|
|
|
|
|
ICommunicationRepository,
|
|
|
|
|
ICryptoRepository,
|
|
|
|
|
IJobRepository,
|
|
|
|
|
IStorageRepository,
|
|
|
|
|
ISystemConfigRepository,
|
|
|
|
|
JobItem,
|
|
|
|
|
TimeBucketSize,
|
|
|
|
|
} from '../repositories';
|
2023-07-14 21:31:42 -04:00
|
|
|
import { AssetService, UploadFieldName } from './asset.service';
|
2023-08-18 10:31:48 -04:00
|
|
|
import { AssetJobName, AssetStatsResponseDto, DownloadResponseDto } from './dto';
|
2023-06-30 12:24:28 -04:00
|
|
|
import { mapAsset } from './response-dto';
|
|
|
|
|
|
|
|
|
|
const downloadResponse: DownloadResponseDto = {
|
|
|
|
|
totalSize: 105_000,
|
|
|
|
|
archives: [
|
|
|
|
|
{
|
|
|
|
|
assetIds: ['asset-id', 'asset-id'],
|
|
|
|
|
size: 105_000,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-07-14 09:30:17 -04:00
|
|
|
const stats: AssetStats = {
|
|
|
|
|
[AssetType.IMAGE]: 10,
|
|
|
|
|
[AssetType.VIDEO]: 23,
|
|
|
|
|
[AssetType.AUDIO]: 0,
|
|
|
|
|
[AssetType.OTHER]: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const statResponse: AssetStatsResponseDto = {
|
|
|
|
|
images: 10,
|
|
|
|
|
videos: 23,
|
|
|
|
|
total: 33,
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-14 21:31:42 -04:00
|
|
|
const uploadFile = {
|
|
|
|
|
nullAuth: {
|
|
|
|
|
authUser: null,
|
|
|
|
|
fieldName: UploadFieldName.ASSET_DATA,
|
|
|
|
|
file: {
|
|
|
|
|
checksum: Buffer.from('checksum', 'utf8'),
|
|
|
|
|
originalPath: 'upload/admin/image.jpeg',
|
|
|
|
|
originalName: 'image.jpeg',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
filename: (fieldName: UploadFieldName, filename: string) => {
|
|
|
|
|
return {
|
|
|
|
|
authUser: authStub.admin,
|
|
|
|
|
fieldName,
|
|
|
|
|
file: {
|
|
|
|
|
mimeType: 'image/jpeg',
|
|
|
|
|
checksum: Buffer.from('checksum', 'utf8'),
|
|
|
|
|
originalPath: `upload/admin/${filename}`,
|
|
|
|
|
originalName: filename,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-19 15:27:25 +01:00
|
|
|
const validImages = [
|
|
|
|
|
'.3fr',
|
|
|
|
|
'.ari',
|
|
|
|
|
'.arw',
|
|
|
|
|
'.avif',
|
|
|
|
|
'.cap',
|
|
|
|
|
'.cin',
|
|
|
|
|
'.cr2',
|
|
|
|
|
'.cr3',
|
|
|
|
|
'.crw',
|
|
|
|
|
'.dcr',
|
|
|
|
|
'.dng',
|
|
|
|
|
'.erf',
|
|
|
|
|
'.fff',
|
|
|
|
|
'.gif',
|
|
|
|
|
'.heic',
|
|
|
|
|
'.heif',
|
|
|
|
|
'.iiq',
|
|
|
|
|
'.jpeg',
|
|
|
|
|
'.jpg',
|
|
|
|
|
'.jxl',
|
|
|
|
|
'.k25',
|
|
|
|
|
'.kdc',
|
|
|
|
|
'.mrw',
|
|
|
|
|
'.nef',
|
|
|
|
|
'.orf',
|
|
|
|
|
'.ori',
|
|
|
|
|
'.pef',
|
|
|
|
|
'.png',
|
2023-09-24 17:03:14 +02:00
|
|
|
'.psd',
|
2023-07-19 15:27:25 +01:00
|
|
|
'.raf',
|
|
|
|
|
'.raw',
|
|
|
|
|
'.rwl',
|
|
|
|
|
'.sr2',
|
|
|
|
|
'.srf',
|
|
|
|
|
'.srw',
|
|
|
|
|
'.tiff',
|
|
|
|
|
'.webp',
|
|
|
|
|
'.x3f',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const validVideos = ['.3gp', '.avi', '.flv', '.m2ts', '.mkv', '.mov', '.mp4', '.mpg', '.mts', '.webm', '.wmv'];
|
|
|
|
|
|
2023-07-14 21:31:42 -04:00
|
|
|
const uploadTests = [
|
|
|
|
|
{
|
2023-07-19 15:27:25 +01:00
|
|
|
label: 'asset images',
|
|
|
|
|
fieldName: UploadFieldName.ASSET_DATA,
|
|
|
|
|
valid: validImages,
|
|
|
|
|
invalid: ['.html', '.xml'],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'asset videos',
|
2023-07-14 21:31:42 -04:00
|
|
|
fieldName: UploadFieldName.ASSET_DATA,
|
2023-07-19 15:27:25 +01:00
|
|
|
valid: validVideos,
|
|
|
|
|
invalid: ['.html', '.xml'],
|
2023-07-14 21:31:42 -04:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'live photo',
|
|
|
|
|
fieldName: UploadFieldName.LIVE_PHOTO_DATA,
|
2023-07-19 15:27:25 +01:00
|
|
|
valid: validVideos,
|
|
|
|
|
invalid: ['.html', '.jpeg', '.jpg', '.xml'],
|
2023-07-14 21:31:42 -04:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'sidecar',
|
|
|
|
|
fieldName: UploadFieldName.SIDECAR_DATA,
|
2023-07-19 15:27:25 +01:00
|
|
|
valid: ['.xmp'],
|
|
|
|
|
invalid: ['.html', '.jpeg', '.jpg', '.mov', '.mp4', '.xml'],
|
2023-07-14 21:31:42 -04:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'profile',
|
|
|
|
|
fieldName: UploadFieldName.PROFILE_DATA,
|
2023-07-19 15:27:25 +01:00
|
|
|
valid: ['.avif', '.dng', '.heic', '.heif', '.jpeg', '.jpg', '.png', '.webp'],
|
|
|
|
|
invalid: ['.arf', '.cr2', '.html', '.mov', '.mp4', '.xml'],
|
2023-07-14 21:31:42 -04:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
describe(AssetService.name, () => {
|
|
|
|
|
let sut: AssetService;
|
2023-06-30 12:24:28 -04:00
|
|
|
let accessMock: IAccessRepositoryMock;
|
2023-03-02 21:47:08 -05:00
|
|
|
let assetMock: jest.Mocked<IAssetRepository>;
|
2023-07-14 21:31:42 -04:00
|
|
|
let cryptoMock: jest.Mocked<ICryptoRepository>;
|
2023-08-18 10:31:48 -04:00
|
|
|
let jobMock: jest.Mocked<IJobRepository>;
|
2023-06-30 12:24:28 -04:00
|
|
|
let storageMock: jest.Mocked<IStorageRepository>;
|
2023-10-06 15:48:11 -05:00
|
|
|
let communicationMock: jest.Mocked<ICommunicationRepository>;
|
2023-10-06 07:01:14 +00:00
|
|
|
let configMock: jest.Mocked<ISystemConfigRepository>;
|
2023-02-25 09:12:03 -05:00
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-06-30 12:24:28 -04:00
|
|
|
accessMock = newAccessRepositoryMock();
|
2023-03-02 21:47:08 -05:00
|
|
|
assetMock = newAssetRepositoryMock();
|
2023-10-06 15:48:11 -05:00
|
|
|
communicationMock = newCommunicationRepositoryMock();
|
2023-07-14 21:31:42 -04:00
|
|
|
cryptoMock = newCryptoRepositoryMock();
|
2023-08-18 10:31:48 -04:00
|
|
|
jobMock = newJobRepositoryMock();
|
2023-06-30 12:24:28 -04:00
|
|
|
storageMock = newStorageRepositoryMock();
|
2023-10-06 07:01:14 +00:00
|
|
|
configMock = newSystemConfigRepositoryMock();
|
2023-10-23 17:52:21 +02:00
|
|
|
sut = new AssetService(accessMock, assetMock, cryptoMock, jobMock, configMock, storageMock, communicationMock);
|
2023-10-06 07:01:14 +00:00
|
|
|
|
|
|
|
|
when(assetMock.getById)
|
|
|
|
|
.calledWith(assetStub.livePhotoStillAsset.id)
|
|
|
|
|
.mockResolvedValue(assetStub.livePhotoStillAsset as AssetEntity);
|
|
|
|
|
when(assetMock.getById)
|
|
|
|
|
.calledWith(assetStub.livePhotoMotionAsset.id)
|
|
|
|
|
.mockResolvedValue(assetStub.livePhotoMotionAsset as AssetEntity);
|
2023-07-14 21:31:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('canUpload', () => {
|
|
|
|
|
it('should require an authenticated user', () => {
|
|
|
|
|
expect(() => sut.canUploadFile(uploadFile.nullAuth)).toThrowError(UnauthorizedException);
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-19 15:27:25 +01:00
|
|
|
for (const { fieldName, valid, invalid } of uploadTests) {
|
|
|
|
|
describe(fieldName, () => {
|
|
|
|
|
for (const filetype of valid) {
|
2023-07-14 21:31:42 -04:00
|
|
|
it(`should accept ${filetype}`, () => {
|
|
|
|
|
expect(sut.canUploadFile(uploadFile.filename(fieldName, `asset${filetype}`))).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const filetype of invalid) {
|
|
|
|
|
it(`should reject ${filetype}`, () => {
|
|
|
|
|
expect(() => sut.canUploadFile(uploadFile.filename(fieldName, `asset${filetype}`))).toThrowError(
|
|
|
|
|
BadRequestException,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-07-19 15:27:25 +01:00
|
|
|
|
|
|
|
|
it('should be sorted (valid)', () => {
|
|
|
|
|
// TODO: use toSorted in NodeJS 20.
|
|
|
|
|
expect(valid).toEqual([...valid].sort());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be sorted (invalid)', () => {
|
|
|
|
|
// TODO: use toSorted in NodeJS 20.
|
|
|
|
|
expect(invalid).toEqual([...invalid].sort());
|
|
|
|
|
});
|
2023-07-14 21:31:42 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getUploadFilename', () => {
|
|
|
|
|
it('should require authentication', () => {
|
|
|
|
|
expect(() => sut.getUploadFilename(uploadFile.nullAuth)).toThrowError(UnauthorizedException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be the original extension for asset upload', () => {
|
|
|
|
|
expect(sut.getUploadFilename(uploadFile.filename(UploadFieldName.ASSET_DATA, 'image.jpg'))).toEqual(
|
|
|
|
|
'random-uuid.jpg',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be the mov extension for live photo upload', () => {
|
|
|
|
|
expect(sut.getUploadFilename(uploadFile.filename(UploadFieldName.LIVE_PHOTO_DATA, 'image.mp4'))).toEqual(
|
|
|
|
|
'random-uuid.mov',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be the xmp extension for sidecar upload', () => {
|
|
|
|
|
expect(sut.getUploadFilename(uploadFile.filename(UploadFieldName.SIDECAR_DATA, 'image.html'))).toEqual(
|
|
|
|
|
'random-uuid.xmp',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should be the original extension for profile upload', () => {
|
|
|
|
|
expect(sut.getUploadFilename(uploadFile.filename(UploadFieldName.PROFILE_DATA, 'image.jpg'))).toEqual(
|
|
|
|
|
'random-uuid.jpg',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getUploadFolder', () => {
|
|
|
|
|
it('should require authentication', () => {
|
|
|
|
|
expect(() => sut.getUploadFolder(uploadFile.nullAuth)).toThrowError(UnauthorizedException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return profile for profile uploads', () => {
|
|
|
|
|
expect(sut.getUploadFolder(uploadFile.filename(UploadFieldName.PROFILE_DATA, 'image.jpg'))).toEqual(
|
|
|
|
|
'upload/profile/admin_id',
|
|
|
|
|
);
|
|
|
|
|
expect(storageMock.mkdirSync).toHaveBeenCalledWith('upload/profile/admin_id');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return upload for everything else', () => {
|
|
|
|
|
expect(sut.getUploadFolder(uploadFile.filename(UploadFieldName.ASSET_DATA, 'image.jpg'))).toEqual(
|
|
|
|
|
'upload/upload/admin_id',
|
|
|
|
|
);
|
|
|
|
|
expect(storageMock.mkdirSync).toHaveBeenCalledWith('upload/upload/admin_id');
|
|
|
|
|
});
|
2023-03-02 21:47:08 -05:00
|
|
|
});
|
2023-05-21 08:26:06 +02:00
|
|
|
|
2023-06-30 12:24:28 -04:00
|
|
|
describe('getMapMarkers', () => {
|
2023-05-21 08:26:06 +02:00
|
|
|
it('should get geo information of assets', async () => {
|
|
|
|
|
assetMock.getMapMarkers.mockResolvedValue(
|
2023-07-31 21:28:07 -04:00
|
|
|
[assetStub.withLocation].map((asset) => ({
|
2023-05-21 08:26:06 +02:00
|
|
|
id: asset.id,
|
|
|
|
|
|
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
|
|
|
|
lat: asset.exifInfo!.latitude!,
|
|
|
|
|
|
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
|
|
|
|
lon: asset.exifInfo!.longitude!,
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const markers = await sut.getMapMarkers(authStub.user1, {});
|
|
|
|
|
|
|
|
|
|
expect(markers).toHaveLength(1);
|
|
|
|
|
expect(markers[0]).toEqual({
|
2023-07-31 21:28:07 -04:00
|
|
|
id: assetStub.withLocation.id,
|
2023-05-21 08:26:06 +02:00
|
|
|
lat: 100,
|
|
|
|
|
lon: 100,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-06-15 14:05:30 -04:00
|
|
|
|
|
|
|
|
describe('getMemoryLane', () => {
|
2023-10-04 18:11:11 -04:00
|
|
|
beforeAll(() => {
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
jest.setSystemTime(new Date('2024-01-15'));
|
2023-06-15 14:05:30 -04:00
|
|
|
});
|
|
|
|
|
|
2023-10-04 18:11:11 -04:00
|
|
|
afterAll(() => {
|
|
|
|
|
jest.useRealTimers();
|
2023-06-15 14:05:30 -04:00
|
|
|
});
|
2023-06-30 12:24:28 -04:00
|
|
|
|
|
|
|
|
it('should set the title correctly', async () => {
|
2023-10-04 18:11:11 -04:00
|
|
|
assetMock.getByDayOfYear.mockResolvedValue([assetStub.image, assetStub.imageFrom2015]);
|
|
|
|
|
|
|
|
|
|
await expect(sut.getMemoryLane(authStub.admin, { day: 15, month: 1 })).resolves.toEqual([
|
2023-07-31 21:28:07 -04:00
|
|
|
{ title: '1 year since...', assets: [mapAsset(assetStub.image)] },
|
2023-10-04 18:11:11 -04:00
|
|
|
{ title: '9 years since...', assets: [mapAsset(assetStub.imageFrom2015)] },
|
2023-06-30 12:24:28 -04:00
|
|
|
]);
|
|
|
|
|
|
2023-10-04 18:11:11 -04:00
|
|
|
expect(assetMock.getByDayOfYear.mock.calls).toEqual([[authStub.admin.id, { day: 15, month: 1 }]]);
|
2023-06-30 12:24:28 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-11 17:56:38 +02:00
|
|
|
describe('getTimeBuckets', () => {
|
|
|
|
|
it("should return buckets if userId and albumId aren't set", async () => {
|
|
|
|
|
assetMock.getTimeBuckets.mockResolvedValue([{ timeBucket: 'bucket', count: 1 }]);
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
sut.getTimeBuckets(authStub.admin, {
|
|
|
|
|
size: TimeBucketSize.DAY,
|
|
|
|
|
}),
|
|
|
|
|
).resolves.toEqual(expect.arrayContaining([{ timeBucket: 'bucket', count: 1 }]));
|
|
|
|
|
expect(assetMock.getTimeBuckets).toBeCalledWith({ size: TimeBucketSize.DAY, userId: authStub.admin.id });
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getByTimeBucket', () => {
|
|
|
|
|
it('should return the assets for a album time bucket if user has album.read', async () => {
|
|
|
|
|
accessMock.album.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
assetMock.getByTimeBucket.mockResolvedValue([assetStub.image]);
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
sut.getByTimeBucket(authStub.admin, { size: TimeBucketSize.DAY, timeBucket: 'bucket', albumId: 'album-id' }),
|
|
|
|
|
).resolves.toEqual(expect.arrayContaining([expect.objectContaining({ id: 'asset-id' })]));
|
|
|
|
|
|
|
|
|
|
expect(accessMock.album.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'album-id');
|
|
|
|
|
expect(assetMock.getByTimeBucket).toBeCalledWith('bucket', {
|
|
|
|
|
size: TimeBucketSize.DAY,
|
|
|
|
|
timeBucket: 'bucket',
|
|
|
|
|
albumId: 'album-id',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return the assets for a archive time bucket if user has archive.read', async () => {
|
|
|
|
|
assetMock.getByTimeBucket.mockResolvedValue([assetStub.image]);
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
sut.getByTimeBucket(authStub.admin, {
|
|
|
|
|
size: TimeBucketSize.DAY,
|
|
|
|
|
timeBucket: 'bucket',
|
|
|
|
|
isArchived: true,
|
|
|
|
|
userId: authStub.admin.id,
|
|
|
|
|
}),
|
|
|
|
|
).resolves.toEqual(expect.arrayContaining([expect.objectContaining({ id: 'asset-id' })]));
|
|
|
|
|
expect(assetMock.getByTimeBucket).toBeCalledWith('bucket', {
|
|
|
|
|
size: TimeBucketSize.DAY,
|
|
|
|
|
timeBucket: 'bucket',
|
|
|
|
|
isArchived: true,
|
|
|
|
|
userId: authStub.admin.id,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return the assets for a library time bucket if user has library.read', async () => {
|
|
|
|
|
assetMock.getByTimeBucket.mockResolvedValue([assetStub.image]);
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
sut.getByTimeBucket(authStub.admin, {
|
|
|
|
|
size: TimeBucketSize.DAY,
|
|
|
|
|
timeBucket: 'bucket',
|
|
|
|
|
userId: authStub.admin.id,
|
|
|
|
|
}),
|
|
|
|
|
).resolves.toEqual(expect.arrayContaining([expect.objectContaining({ id: 'asset-id' })]));
|
|
|
|
|
expect(assetMock.getByTimeBucket).toBeCalledWith('bucket', {
|
|
|
|
|
size: TimeBucketSize.DAY,
|
|
|
|
|
timeBucket: 'bucket',
|
|
|
|
|
userId: authStub.admin.id,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-30 12:24:28 -04:00
|
|
|
describe('downloadFile', () => {
|
|
|
|
|
it('should require the asset.download permission', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
|
accessMock.asset.hasAlbumAccess.mockResolvedValue(false);
|
|
|
|
|
accessMock.asset.hasPartnerAccess.mockResolvedValue(false);
|
|
|
|
|
|
|
|
|
|
await expect(sut.downloadFile(authStub.admin, 'asset-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
|
|
|
|
|
expect(accessMock.asset.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'asset-1');
|
|
|
|
|
expect(accessMock.asset.hasAlbumAccess).toHaveBeenCalledWith(authStub.admin.id, 'asset-1');
|
|
|
|
|
expect(accessMock.asset.hasPartnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'asset-1');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if the asset is not found', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
assetMock.getByIds.mockResolvedValue([]);
|
|
|
|
|
|
|
|
|
|
await expect(sut.downloadFile(authStub.admin, 'asset-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
|
|
|
|
|
expect(assetMock.getByIds).toHaveBeenCalledWith(['asset-1']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should download a file', async () => {
|
|
|
|
|
const stream = new Readable();
|
|
|
|
|
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-31 21:28:07 -04:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image]);
|
2023-06-30 12:24:28 -04:00
|
|
|
storageMock.createReadStream.mockResolvedValue({ stream });
|
|
|
|
|
|
|
|
|
|
await expect(sut.downloadFile(authStub.admin, 'asset-1')).resolves.toEqual({ stream });
|
|
|
|
|
|
2023-07-31 21:28:07 -04:00
|
|
|
expect(storageMock.createReadStream).toHaveBeenCalledWith(assetStub.image.originalPath, 'image/jpeg');
|
2023-06-30 12:24:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should download an archive', async () => {
|
|
|
|
|
const archiveMock = {
|
|
|
|
|
addFile: jest.fn(),
|
|
|
|
|
finalize: jest.fn(),
|
|
|
|
|
stream: new Readable(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-31 21:28:07 -04:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.noResizePath, assetStub.noWebpPath]);
|
2023-06-30 12:24:28 -04:00
|
|
|
storageMock.createZipStream.mockReturnValue(archiveMock);
|
|
|
|
|
|
|
|
|
|
await expect(sut.downloadArchive(authStub.admin, { assetIds: ['asset-1', 'asset-2'] })).resolves.toEqual({
|
|
|
|
|
stream: archiveMock.stream,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(archiveMock.addFile).toHaveBeenCalledTimes(2);
|
|
|
|
|
expect(archiveMock.addFile).toHaveBeenNthCalledWith(1, 'upload/library/IMG_123.jpg', 'IMG_123.jpg');
|
|
|
|
|
expect(archiveMock.addFile).toHaveBeenNthCalledWith(2, 'upload/library/IMG_456.jpg', 'IMG_456.jpg');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle duplicate file names', async () => {
|
|
|
|
|
const archiveMock = {
|
|
|
|
|
addFile: jest.fn(),
|
|
|
|
|
finalize: jest.fn(),
|
|
|
|
|
stream: new Readable(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-31 21:28:07 -04:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.noResizePath, assetStub.noResizePath]);
|
2023-06-30 12:24:28 -04:00
|
|
|
storageMock.createZipStream.mockReturnValue(archiveMock);
|
|
|
|
|
|
|
|
|
|
await expect(sut.downloadArchive(authStub.admin, { assetIds: ['asset-1', 'asset-2'] })).resolves.toEqual({
|
|
|
|
|
stream: archiveMock.stream,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(archiveMock.addFile).toHaveBeenCalledTimes(2);
|
|
|
|
|
expect(archiveMock.addFile).toHaveBeenNthCalledWith(1, 'upload/library/IMG_123.jpg', 'IMG_123.jpg');
|
|
|
|
|
expect(archiveMock.addFile).toHaveBeenNthCalledWith(2, 'upload/library/IMG_123.jpg', 'IMG_123+1.jpg');
|
|
|
|
|
});
|
2023-06-15 14:05:30 -04:00
|
|
|
});
|
|
|
|
|
|
2023-06-30 12:24:28 -04:00
|
|
|
describe('getDownloadInfo', () => {
|
|
|
|
|
it('should throw an error for an invalid dto', async () => {
|
|
|
|
|
await expect(sut.getDownloadInfo(authStub.admin, {})).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a list of archives (assetIds)', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-31 21:28:07 -04:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image, assetStub.video]);
|
2023-06-30 12:24:28 -04:00
|
|
|
|
|
|
|
|
const assetIds = ['asset-1', 'asset-2'];
|
|
|
|
|
await expect(sut.getDownloadInfo(authStub.admin, { assetIds })).resolves.toEqual(downloadResponse);
|
|
|
|
|
|
|
|
|
|
expect(assetMock.getByIds).toHaveBeenCalledWith(['asset-1', 'asset-2']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a list of archives (albumId)', async () => {
|
|
|
|
|
accessMock.album.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
assetMock.getByAlbumId.mockResolvedValue({
|
2023-07-31 21:28:07 -04:00
|
|
|
items: [assetStub.image, assetStub.video],
|
2023-06-30 12:24:28 -04:00
|
|
|
hasNextPage: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(sut.getDownloadInfo(authStub.admin, { albumId: 'album-1' })).resolves.toEqual(downloadResponse);
|
|
|
|
|
|
|
|
|
|
expect(accessMock.album.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'album-1');
|
|
|
|
|
expect(assetMock.getByAlbumId).toHaveBeenCalledWith({ take: 2500, skip: 0 }, 'album-1');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a list of archives (userId)', async () => {
|
2023-09-20 13:16:33 +02:00
|
|
|
accessMock.library.hasOwnerAccess.mockResolvedValue(true);
|
2023-06-30 12:24:28 -04:00
|
|
|
assetMock.getByUserId.mockResolvedValue({
|
2023-07-31 21:28:07 -04:00
|
|
|
items: [assetStub.image, assetStub.video],
|
2023-06-30 12:24:28 -04:00
|
|
|
hasNextPage: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(sut.getDownloadInfo(authStub.admin, { userId: authStub.admin.id })).resolves.toEqual(
|
|
|
|
|
downloadResponse,
|
|
|
|
|
);
|
|
|
|
|
|
2023-10-06 07:01:14 +00:00
|
|
|
expect(assetMock.getByUserId).toHaveBeenCalledWith({ take: 2500, skip: 0 }, authStub.admin.id, {
|
|
|
|
|
isVisible: true,
|
|
|
|
|
});
|
2023-06-30 12:24:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should split archives by size', async () => {
|
2023-09-20 13:16:33 +02:00
|
|
|
accessMock.library.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
|
2023-06-30 12:24:28 -04:00
|
|
|
assetMock.getByUserId.mockResolvedValue({
|
|
|
|
|
items: [
|
2023-07-31 21:28:07 -04:00
|
|
|
{ ...assetStub.image, id: 'asset-1' },
|
|
|
|
|
{ ...assetStub.video, id: 'asset-2' },
|
|
|
|
|
{ ...assetStub.withLocation, id: 'asset-3' },
|
|
|
|
|
{ ...assetStub.noWebpPath, id: 'asset-4' },
|
2023-06-30 12:24:28 -04:00
|
|
|
],
|
|
|
|
|
hasNextPage: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
sut.getDownloadInfo(authStub.admin, {
|
|
|
|
|
userId: authStub.admin.id,
|
|
|
|
|
archiveSize: 30_000,
|
|
|
|
|
}),
|
|
|
|
|
).resolves.toEqual({
|
|
|
|
|
totalSize: 251_456,
|
|
|
|
|
archives: [
|
|
|
|
|
{ assetIds: ['asset-1', 'asset-2'], size: 105_000 },
|
|
|
|
|
{ assetIds: ['asset-3', 'asset-4'], size: 146_456 },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should include the video portion of a live photo', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
when(assetMock.getByIds)
|
2023-07-31 21:28:07 -04:00
|
|
|
.calledWith([assetStub.livePhotoStillAsset.id])
|
|
|
|
|
.mockResolvedValue([assetStub.livePhotoStillAsset]);
|
2023-06-30 12:24:28 -04:00
|
|
|
when(assetMock.getByIds)
|
2023-07-31 21:28:07 -04:00
|
|
|
.calledWith([assetStub.livePhotoMotionAsset.id])
|
|
|
|
|
.mockResolvedValue([assetStub.livePhotoMotionAsset]);
|
2023-06-30 12:24:28 -04:00
|
|
|
|
2023-07-31 21:28:07 -04:00
|
|
|
const assetIds = [assetStub.livePhotoStillAsset.id];
|
2023-06-30 12:24:28 -04:00
|
|
|
await expect(sut.getDownloadInfo(authStub.admin, { assetIds })).resolves.toEqual({
|
|
|
|
|
totalSize: 125_000,
|
|
|
|
|
archives: [
|
|
|
|
|
{
|
2023-07-31 21:28:07 -04:00
|
|
|
assetIds: [assetStub.livePhotoStillAsset.id, assetStub.livePhotoMotionAsset.id],
|
2023-06-30 12:24:28 -04:00
|
|
|
size: 125_000,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-06-15 14:05:30 -04:00
|
|
|
});
|
2023-07-14 09:30:17 -04:00
|
|
|
|
|
|
|
|
describe('getStatistics', () => {
|
|
|
|
|
it('should get the statistics for a user, excluding archived assets', async () => {
|
|
|
|
|
assetMock.getStatistics.mockResolvedValue(stats);
|
|
|
|
|
await expect(sut.getStatistics(authStub.admin, { isArchived: false })).resolves.toEqual(statResponse);
|
|
|
|
|
expect(assetMock.getStatistics).toHaveBeenCalledWith(authStub.admin.id, { isArchived: false });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should get the statistics for a user for archived assets', async () => {
|
|
|
|
|
assetMock.getStatistics.mockResolvedValue(stats);
|
|
|
|
|
await expect(sut.getStatistics(authStub.admin, { isArchived: true })).resolves.toEqual(statResponse);
|
|
|
|
|
expect(assetMock.getStatistics).toHaveBeenCalledWith(authStub.admin.id, { isArchived: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should get the statistics for a user for favorite assets', async () => {
|
|
|
|
|
assetMock.getStatistics.mockResolvedValue(stats);
|
|
|
|
|
await expect(sut.getStatistics(authStub.admin, { isFavorite: true })).resolves.toEqual(statResponse);
|
|
|
|
|
expect(assetMock.getStatistics).toHaveBeenCalledWith(authStub.admin.id, { isFavorite: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should get the statistics for a user for all assets', async () => {
|
|
|
|
|
assetMock.getStatistics.mockResolvedValue(stats);
|
|
|
|
|
await expect(sut.getStatistics(authStub.admin, {})).resolves.toEqual(statResponse);
|
|
|
|
|
expect(assetMock.getStatistics).toHaveBeenCalledWith(authStub.admin.id, {});
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-08-16 16:04:55 -04:00
|
|
|
|
2023-09-04 22:25:31 -04:00
|
|
|
describe('update', () => {
|
|
|
|
|
it('should require asset write access for the id', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
|
await expect(sut.update(authStub.admin, 'asset-1', { isArchived: false })).rejects.toBeInstanceOf(
|
|
|
|
|
BadRequestException,
|
|
|
|
|
);
|
|
|
|
|
expect(assetMock.save).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update the asset', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
assetMock.save.mockResolvedValue(assetStub.image);
|
|
|
|
|
await sut.update(authStub.admin, 'asset-1', { isFavorite: true });
|
|
|
|
|
expect(assetMock.save).toHaveBeenCalledWith({ id: 'asset-1', isFavorite: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update the exif description', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
assetMock.save.mockResolvedValue(assetStub.image);
|
|
|
|
|
await sut.update(authStub.admin, 'asset-1', { description: 'Test description' });
|
|
|
|
|
expect(assetMock.upsertExif).toHaveBeenCalledWith({ assetId: 'asset-1', description: 'Test description' });
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-16 16:04:55 -04:00
|
|
|
describe('updateAll', () => {
|
|
|
|
|
it('should require asset write access for all ids', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
|
await expect(
|
|
|
|
|
sut.updateAll(authStub.admin, {
|
|
|
|
|
ids: ['asset-1'],
|
|
|
|
|
isArchived: false,
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update all assets', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.updateAll(authStub.admin, { ids: ['asset-1', 'asset-2'], isArchived: true });
|
|
|
|
|
expect(assetMock.updateAll).toHaveBeenCalledWith(['asset-1', 'asset-2'], { isArchived: true });
|
|
|
|
|
});
|
2023-10-22 02:38:07 +00:00
|
|
|
|
|
|
|
|
/// Stack related
|
|
|
|
|
|
|
|
|
|
it('should require asset update access for parent', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
when(accessMock.asset.hasOwnerAccess).calledWith(authStub.user1.id, 'parent').mockResolvedValue(false);
|
|
|
|
|
await expect(
|
|
|
|
|
sut.updateAll(authStub.user1, {
|
|
|
|
|
ids: ['asset-1'],
|
|
|
|
|
stackParentId: 'parent',
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update parent asset when children are added', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.updateAll(authStub.user1, {
|
|
|
|
|
ids: [],
|
|
|
|
|
stackParentId: 'parent',
|
|
|
|
|
}),
|
|
|
|
|
expect(assetMock.updateAll).toHaveBeenCalledWith(['parent'], { stackParentId: null });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update parent asset when children are removed', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
assetMock.getByIds.mockResolvedValue([{ id: 'child-1', stackParentId: 'parent' } as AssetEntity]);
|
|
|
|
|
|
|
|
|
|
await sut.updateAll(authStub.user1, {
|
|
|
|
|
ids: ['child-1'],
|
|
|
|
|
removeParent: true,
|
|
|
|
|
}),
|
|
|
|
|
expect(assetMock.updateAll).toHaveBeenCalledWith(expect.arrayContaining(['parent']), { stackParentId: null });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('update parentId for new children', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.updateAll(authStub.user1, {
|
|
|
|
|
stackParentId: 'parent',
|
|
|
|
|
ids: ['child-1', 'child-2'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(assetMock.updateAll).toBeCalledWith(['child-1', 'child-2'], { stackParentId: 'parent' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('nullify parentId for remove children', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.updateAll(authStub.user1, {
|
|
|
|
|
removeParent: true,
|
|
|
|
|
ids: ['child-1', 'child-2'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(assetMock.updateAll).toBeCalledWith(['child-1', 'child-2'], { stackParentId: null });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('merge stacks if new child has children', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
assetMock.getByIds.mockResolvedValue([
|
|
|
|
|
{ id: 'child-1', stack: [{ id: 'child-2' } as AssetEntity] } as AssetEntity,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await sut.updateAll(authStub.user1, {
|
|
|
|
|
ids: ['child-1'],
|
|
|
|
|
stackParentId: 'parent',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(assetMock.updateAll).toBeCalledWith(['child-1', 'child-2'], { stackParentId: 'parent' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should send ws asset update event', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.updateAll(authStub.user1, {
|
|
|
|
|
ids: ['asset-1'],
|
|
|
|
|
stackParentId: 'parent',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(communicationMock.send).toHaveBeenCalledWith(CommunicationEvent.ASSET_UPDATE, authStub.user1.id, [
|
|
|
|
|
'asset-1',
|
|
|
|
|
]);
|
|
|
|
|
});
|
2023-08-16 16:04:55 -04:00
|
|
|
});
|
2023-08-18 10:31:48 -04:00
|
|
|
|
2023-10-06 07:01:14 +00:00
|
|
|
describe('deleteAll', () => {
|
2023-10-22 02:38:07 +00:00
|
|
|
it('should require asset delete access for all ids', async () => {
|
2023-10-06 07:01:14 +00:00
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
|
await expect(
|
|
|
|
|
sut.deleteAll(authStub.user1, {
|
|
|
|
|
ids: ['asset-1'],
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should force delete a batch of assets', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
|
|
|
|
|
await sut.deleteAll(authStub.user1, { ids: ['asset1', 'asset2'], force: true });
|
|
|
|
|
|
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
|
[{ name: JobName.ASSET_DELETION, data: { id: 'asset1' } }],
|
|
|
|
|
[{ name: JobName.ASSET_DELETION, data: { id: 'asset2' } }],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should soft delete a batch of assets', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
|
|
|
|
|
await sut.deleteAll(authStub.user1, { ids: ['asset1', 'asset2'], force: false });
|
|
|
|
|
|
|
|
|
|
expect(assetMock.softDeleteAll).toHaveBeenCalledWith(['asset1', 'asset2']);
|
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.SEARCH_REMOVE_ASSET,
|
|
|
|
|
data: { ids: ['asset1', 'asset2'] },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('restoreAll', () => {
|
2023-10-22 02:38:07 +00:00
|
|
|
it('should require asset restore access for all ids', async () => {
|
2023-10-06 07:01:14 +00:00
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
|
await expect(
|
|
|
|
|
sut.deleteAll(authStub.user1, {
|
|
|
|
|
ids: ['asset-1'],
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should restore a batch of assets', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
|
|
|
|
|
await sut.restoreAll(authStub.user1, { ids: ['asset1', 'asset2'] });
|
|
|
|
|
|
|
|
|
|
expect(assetMock.restoreAll).toHaveBeenCalledWith(['asset1', 'asset2']);
|
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.SEARCH_INDEX_ASSET,
|
|
|
|
|
data: { ids: ['asset1', 'asset2'] },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('handleAssetDeletion', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
when(jobMock.queue)
|
|
|
|
|
.calledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
name: JobName.ASSET_DELETION,
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.mockImplementation(async (item: JobItem) => {
|
|
|
|
|
const jobData = (item as { data?: any })?.data || {};
|
|
|
|
|
await sut.handleAssetDeletion(jobData);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should remove faces', async () => {
|
|
|
|
|
const assetWithFace = { ...(assetStub.image as AssetEntity), faces: [faceStub.face1, faceStub.mergeFace1] };
|
|
|
|
|
|
|
|
|
|
when(assetMock.getById).calledWith(assetWithFace.id).mockResolvedValue(assetWithFace);
|
|
|
|
|
|
|
|
|
|
await sut.handleAssetDeletion({ id: assetWithFace.id });
|
|
|
|
|
|
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.SEARCH_REMOVE_FACE,
|
|
|
|
|
data: { assetId: faceStub.face1.assetId, personId: faceStub.face1.personId },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.SEARCH_REMOVE_FACE,
|
|
|
|
|
data: { assetId: faceStub.mergeFace1.assetId, personId: faceStub.mergeFace1.personId },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[{ name: JobName.SEARCH_REMOVE_ASSET, data: { ids: [assetWithFace.id] } }],
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.DELETE_FILES,
|
|
|
|
|
data: {
|
|
|
|
|
files: [
|
|
|
|
|
assetWithFace.webpPath,
|
|
|
|
|
assetWithFace.resizePath,
|
|
|
|
|
assetWithFace.encodedVideoPath,
|
|
|
|
|
assetWithFace.sidecarPath,
|
|
|
|
|
assetWithFace.originalPath,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
expect(assetMock.remove).toHaveBeenCalledWith(assetWithFace);
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-22 02:38:07 +00:00
|
|
|
it('should update stack parent if asset has stack children', async () => {
|
|
|
|
|
when(assetMock.getById)
|
|
|
|
|
.calledWith(assetStub.primaryImage.id)
|
|
|
|
|
.mockResolvedValue(assetStub.primaryImage as AssetEntity);
|
|
|
|
|
|
|
|
|
|
await sut.handleAssetDeletion({ id: assetStub.primaryImage.id });
|
|
|
|
|
|
|
|
|
|
expect(assetMock.updateAll).toHaveBeenCalledWith(['stack-child-asset-2'], {
|
|
|
|
|
stackParentId: 'stack-child-asset-1',
|
|
|
|
|
});
|
|
|
|
|
expect(assetMock.updateAll).toHaveBeenCalledWith(['stack-child-asset-1'], {
|
|
|
|
|
stackParentId: null,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-06 07:01:14 +00:00
|
|
|
it('should not schedule delete-files job for readonly assets', async () => {
|
|
|
|
|
when(assetMock.getById)
|
|
|
|
|
.calledWith(assetStub.readOnly.id)
|
|
|
|
|
.mockResolvedValue(assetStub.readOnly as AssetEntity);
|
|
|
|
|
|
|
|
|
|
await sut.handleAssetDeletion({ id: assetStub.readOnly.id });
|
|
|
|
|
|
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
|
[{ name: JobName.SEARCH_REMOVE_ASSET, data: { ids: [assetStub.readOnly.id] } }],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
expect(assetMock.remove).toHaveBeenCalledWith(assetStub.readOnly);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not process assets from external library without fromExternal flag', async () => {
|
|
|
|
|
when(assetMock.getById)
|
|
|
|
|
.calledWith(assetStub.external.id)
|
|
|
|
|
.mockResolvedValue(assetStub.external as AssetEntity);
|
|
|
|
|
|
|
|
|
|
await sut.handleAssetDeletion({ id: assetStub.external.id });
|
|
|
|
|
|
|
|
|
|
expect(jobMock.queue).not.toBeCalled();
|
|
|
|
|
expect(assetMock.remove).not.toBeCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should process assets from external library with fromExternal flag', async () => {
|
|
|
|
|
when(assetMock.getById)
|
|
|
|
|
.calledWith(assetStub.external.id)
|
|
|
|
|
.mockResolvedValue(assetStub.external as AssetEntity);
|
|
|
|
|
|
|
|
|
|
await sut.handleAssetDeletion({ id: assetStub.external.id, fromExternal: true });
|
|
|
|
|
|
|
|
|
|
expect(assetMock.remove).toHaveBeenCalledWith(assetStub.external);
|
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
|
[{ name: JobName.SEARCH_REMOVE_ASSET, data: { ids: [assetStub.external.id] } }],
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.DELETE_FILES,
|
|
|
|
|
data: {
|
|
|
|
|
files: [
|
|
|
|
|
assetStub.external.webpPath,
|
|
|
|
|
assetStub.external.resizePath,
|
|
|
|
|
assetStub.external.encodedVideoPath,
|
|
|
|
|
assetStub.external.sidecarPath,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should delete a live photo', async () => {
|
|
|
|
|
await sut.handleAssetDeletion({ id: assetStub.livePhotoStillAsset.id });
|
|
|
|
|
|
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
|
[{ name: JobName.SEARCH_REMOVE_ASSET, data: { ids: [assetStub.livePhotoStillAsset.id] } }],
|
|
|
|
|
[{ name: JobName.ASSET_DELETION, data: { id: assetStub.livePhotoMotionAsset.id } }],
|
|
|
|
|
[{ name: JobName.SEARCH_REMOVE_ASSET, data: { ids: [assetStub.livePhotoMotionAsset.id] } }],
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.DELETE_FILES,
|
|
|
|
|
data: {
|
|
|
|
|
files: [undefined, undefined, undefined, undefined, 'fake_path/asset_1.mp4'],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
name: JobName.DELETE_FILES,
|
|
|
|
|
data: {
|
|
|
|
|
files: [undefined, undefined, undefined, undefined, 'fake_path/asset_1.jpeg'],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-18 10:31:48 -04:00
|
|
|
describe('run', () => {
|
|
|
|
|
it('should run the refresh metadata job', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.run(authStub.admin, { assetIds: ['asset-1'], name: AssetJobName.REFRESH_METADATA }),
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.METADATA_EXTRACTION, data: { id: 'asset-1' } });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should run the refresh thumbnails job', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.run(authStub.admin, { assetIds: ['asset-1'], name: AssetJobName.REGENERATE_THUMBNAIL }),
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.GENERATE_JPEG_THUMBNAIL, data: { id: 'asset-1' } });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should run the transcode video', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.run(authStub.admin, { assetIds: ['asset-1'], name: AssetJobName.TRANSCODE_VIDEO }),
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.VIDEO_CONVERSION, data: { id: 'asset-1' } });
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-10-22 02:38:07 +00:00
|
|
|
|
|
|
|
|
describe('updateStackParent', () => {
|
|
|
|
|
it('should require asset update access for new parent', async () => {
|
|
|
|
|
when(accessMock.asset.hasOwnerAccess).calledWith(authStub.user1.id, 'old').mockResolvedValue(true);
|
|
|
|
|
when(accessMock.asset.hasOwnerAccess).calledWith(authStub.user1.id, 'new').mockResolvedValue(false);
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
|
await expect(
|
|
|
|
|
sut.updateStackParent(authStub.user1, {
|
|
|
|
|
oldParentId: 'old',
|
|
|
|
|
newParentId: 'new',
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should require asset read access for old parent', async () => {
|
|
|
|
|
when(accessMock.asset.hasOwnerAccess).calledWith(authStub.user1.id, 'old').mockResolvedValue(false);
|
|
|
|
|
when(accessMock.asset.hasOwnerAccess).calledWith(authStub.user1.id, 'new').mockResolvedValue(true);
|
|
|
|
|
await expect(
|
|
|
|
|
sut.updateStackParent(authStub.user1, {
|
|
|
|
|
oldParentId: 'old',
|
|
|
|
|
newParentId: 'new',
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('make old parent the child of new parent', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
when(assetMock.getById)
|
|
|
|
|
.calledWith(assetStub.image.id)
|
|
|
|
|
.mockResolvedValue(assetStub.image as AssetEntity);
|
|
|
|
|
|
|
|
|
|
await sut.updateStackParent(authStub.user1, {
|
|
|
|
|
oldParentId: assetStub.image.id,
|
|
|
|
|
newParentId: 'new',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(assetMock.updateAll).toBeCalledWith([assetStub.image.id], { stackParentId: 'new' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('remove stackParentId of new parent', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
await sut.updateStackParent(authStub.user1, {
|
|
|
|
|
oldParentId: assetStub.primaryImage.id,
|
|
|
|
|
newParentId: 'new',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(assetMock.updateAll).toBeCalledWith(['new'], { stackParentId: null });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('update stackParentId of old parents children to new parent', async () => {
|
|
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
when(assetMock.getById)
|
|
|
|
|
.calledWith(assetStub.primaryImage.id)
|
|
|
|
|
.mockResolvedValue(assetStub.primaryImage as AssetEntity);
|
|
|
|
|
|
|
|
|
|
await sut.updateStackParent(authStub.user1, {
|
|
|
|
|
oldParentId: assetStub.primaryImage.id,
|
|
|
|
|
newParentId: 'new',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(assetMock.updateAll).toBeCalledWith(
|
|
|
|
|
[assetStub.primaryImage.id, 'stack-child-asset-1', 'stack-child-asset-2'],
|
|
|
|
|
{ stackParentId: 'new' },
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-02-25 09:12:03 -05:00
|
|
|
});
|