Files
Immich/server/src/services/file-vault.service.spec.ts
T

45 lines
2.1 KiB
TypeScript

import {
ConflictException,
ForbiddenException,
NotFoundException,
UnsupportedMediaTypeException,
} from '@nestjs/common';
import { Readable } from 'node:stream';
import { AuthDto } from 'src/dtos/auth.dto.js';
import { FileVaultFailure } from 'src/repositories/file-vault.repository.js';
import { FileVaultService } from 'src/services/file-vault.service.js';
import { factory } from 'test/small.factory.js';
import { newTestService } from 'test/utils.js';
describe(FileVaultService.name, () => {
it('rejects a parsed JSON body instead of storing an empty file', async () => {
const { sut, mocks } = newTestService(FileVaultService);
await expect(
sut.upload(factory.auth(), { name: 'report.json' }, Readable.from('{}'), 'application/json'),
).rejects.toBeInstanceOf(UnsupportedMediaTypeException);
expect(mocks.fileVault.upload).not.toHaveBeenCalled();
});
it.each(['apiKey', 'sharedLink'] as const)('rejects %s before accepting uploaded bytes', async (kind) => {
const { sut, mocks } = newTestService(FileVaultService);
const auth = { ...factory.auth(), [kind]: {} } as AuthDto;
await expect(sut.upload(auth, { name: 'private.pdf' }, Readable.from('private'))).rejects.toBeInstanceOf(
ForbiddenException,
);
expect(mocks.fileVault.upload).not.toHaveBeenCalled();
});
it('does not expose another account through lookup failures', async () => {
const { sut, mocks } = newTestService(FileVaultService);
const auth = factory.auth();
mocks.fileVault.download.mockRejectedValue(new FileVaultFailure('missing', 'File not found'));
await expect(sut.download(auth, factory.uuid())).rejects.toBeInstanceOf(NotFoundException);
expect(mocks.fileVault.download).toHaveBeenCalledWith(auth.user.id, expect.any(String));
});
it('returns a conflict without overwriting a colliding name', async () => {
const { sut, mocks } = newTestService(FileVaultService);
mocks.fileVault.restore.mockRejectedValue(new FileVaultFailure('conflict', 'Name already exists'));
await expect(sut.restore(factory.auth(), factory.uuid())).rejects.toBeInstanceOf(ConflictException);
});
});