Skip to content

Commit dc18731

Browse files
chore: audit service unit tests (immich-app#13183)
1 parent 4adedea commit dc18731

File tree

1 file changed

+156
-2
lines changed

1 file changed

+156
-2
lines changed

server/src/services/audit.service.spec.ts

+156-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { DatabaseAction, EntityType } from 'src/enum';
1+
import { BadRequestException } from '@nestjs/common';
2+
import { FileReportItemDto } from 'src/dtos/audit.dto';
3+
import { AssetFileType, AssetPathType, DatabaseAction, EntityType, PersonPathType, UserPathType } from 'src/enum';
4+
import { IAssetRepository } from 'src/interfaces/asset.interface';
25
import { IAuditRepository } from 'src/interfaces/audit.interface';
6+
import { ICryptoRepository } from 'src/interfaces/crypto.interface';
37
import { JobStatus } from 'src/interfaces/job.interface';
8+
import { IPersonRepository } from 'src/interfaces/person.interface';
9+
import { IUserRepository } from 'src/interfaces/user.interface';
410
import { AuditService } from 'src/services/audit.service';
511
import { auditStub } from 'test/fixtures/audit.stub';
612
import { authStub } from 'test/fixtures/auth.stub';
@@ -10,9 +16,13 @@ import { Mocked } from 'vitest';
1016
describe(AuditService.name, () => {
1117
let sut: AuditService;
1218
let auditMock: Mocked<IAuditRepository>;
19+
let assetMock: Mocked<IAssetRepository>;
20+
let cryptoMock: Mocked<ICryptoRepository>;
21+
let personMock: Mocked<IPersonRepository>;
22+
let userMock: Mocked<IUserRepository>;
1323

1424
beforeEach(() => {
15-
({ sut, auditMock } = newTestService(AuditService));
25+
({ sut, auditMock, assetMock, cryptoMock, personMock, userMock } = newTestService(AuditService));
1626
});
1727

1828
it('should work', () => {
@@ -59,4 +69,148 @@ describe(AuditService.name, () => {
5969
});
6070
});
6171
});
72+
73+
describe('getChecksums', () => {
74+
it('should fail if the file is not in the immich path', async () => {
75+
await expect(sut.getChecksums({ filenames: ['foo/bar'] })).rejects.toBeInstanceOf(BadRequestException);
76+
77+
expect(cryptoMock.hashFile).not.toHaveBeenCalled();
78+
});
79+
80+
it('should get checksum for valid file', async () => {
81+
await expect(sut.getChecksums({ filenames: ['./upload/my-file.jpg'] })).resolves.toEqual([
82+
{ filename: './upload/my-file.jpg', checksum: expect.any(String) },
83+
]);
84+
85+
expect(cryptoMock.hashFile).toHaveBeenCalledWith('./upload/my-file.jpg');
86+
});
87+
});
88+
89+
describe('fixItems', () => {
90+
it('should fail if the file is not in the immich path', async () => {
91+
await expect(
92+
sut.fixItems([
93+
{ entityId: 'my-id', pathType: AssetPathType.ORIGINAL, pathValue: 'foo/bar' } as FileReportItemDto,
94+
]),
95+
).rejects.toBeInstanceOf(BadRequestException);
96+
97+
expect(assetMock.update).not.toHaveBeenCalled();
98+
expect(assetMock.upsertFile).not.toHaveBeenCalled();
99+
expect(personMock.update).not.toHaveBeenCalled();
100+
expect(userMock.update).not.toHaveBeenCalled();
101+
});
102+
103+
it('should update encoded video path', async () => {
104+
await sut.fixItems([
105+
{
106+
entityId: 'my-id',
107+
pathType: AssetPathType.ENCODED_VIDEO,
108+
pathValue: './upload/my-video.mp4',
109+
} as FileReportItemDto,
110+
]);
111+
112+
expect(assetMock.update).toHaveBeenCalledWith({ id: 'my-id', encodedVideoPath: './upload/my-video.mp4' });
113+
expect(assetMock.upsertFile).not.toHaveBeenCalled();
114+
expect(personMock.update).not.toHaveBeenCalled();
115+
expect(userMock.update).not.toHaveBeenCalled();
116+
});
117+
118+
it('should update preview path', async () => {
119+
await sut.fixItems([
120+
{
121+
entityId: 'my-id',
122+
pathType: AssetPathType.PREVIEW,
123+
pathValue: './upload/my-preview.png',
124+
} as FileReportItemDto,
125+
]);
126+
127+
expect(assetMock.upsertFile).toHaveBeenCalledWith({
128+
assetId: 'my-id',
129+
type: AssetFileType.PREVIEW,
130+
path: './upload/my-preview.png',
131+
});
132+
expect(assetMock.update).not.toHaveBeenCalled();
133+
expect(personMock.update).not.toHaveBeenCalled();
134+
expect(userMock.update).not.toHaveBeenCalled();
135+
});
136+
137+
it('should update thumbnail path', async () => {
138+
await sut.fixItems([
139+
{
140+
entityId: 'my-id',
141+
pathType: AssetPathType.THUMBNAIL,
142+
pathValue: './upload/my-thumbnail.webp',
143+
} as FileReportItemDto,
144+
]);
145+
146+
expect(assetMock.upsertFile).toHaveBeenCalledWith({
147+
assetId: 'my-id',
148+
type: AssetFileType.THUMBNAIL,
149+
path: './upload/my-thumbnail.webp',
150+
});
151+
expect(assetMock.update).not.toHaveBeenCalled();
152+
expect(personMock.update).not.toHaveBeenCalled();
153+
expect(userMock.update).not.toHaveBeenCalled();
154+
});
155+
156+
it('should update original path', async () => {
157+
await sut.fixItems([
158+
{
159+
entityId: 'my-id',
160+
pathType: AssetPathType.ORIGINAL,
161+
pathValue: './upload/my-original.png',
162+
} as FileReportItemDto,
163+
]);
164+
165+
expect(assetMock.update).toHaveBeenCalledWith({ id: 'my-id', originalPath: './upload/my-original.png' });
166+
expect(assetMock.upsertFile).not.toHaveBeenCalled();
167+
expect(personMock.update).not.toHaveBeenCalled();
168+
expect(userMock.update).not.toHaveBeenCalled();
169+
});
170+
171+
it('should update sidecar path', async () => {
172+
await sut.fixItems([
173+
{
174+
entityId: 'my-id',
175+
pathType: AssetPathType.SIDECAR,
176+
pathValue: './upload/my-sidecar.xmp',
177+
} as FileReportItemDto,
178+
]);
179+
180+
expect(assetMock.update).toHaveBeenCalledWith({ id: 'my-id', sidecarPath: './upload/my-sidecar.xmp' });
181+
expect(assetMock.upsertFile).not.toHaveBeenCalled();
182+
expect(personMock.update).not.toHaveBeenCalled();
183+
expect(userMock.update).not.toHaveBeenCalled();
184+
});
185+
186+
it('should update face path', async () => {
187+
await sut.fixItems([
188+
{
189+
entityId: 'my-id',
190+
pathType: PersonPathType.FACE,
191+
pathValue: './upload/my-face.jpg',
192+
} as FileReportItemDto,
193+
]);
194+
195+
expect(personMock.update).toHaveBeenCalledWith({ id: 'my-id', thumbnailPath: './upload/my-face.jpg' });
196+
expect(assetMock.update).not.toHaveBeenCalled();
197+
expect(assetMock.upsertFile).not.toHaveBeenCalled();
198+
expect(userMock.update).not.toHaveBeenCalled();
199+
});
200+
201+
it('should update profile path', async () => {
202+
await sut.fixItems([
203+
{
204+
entityId: 'my-id',
205+
pathType: UserPathType.PROFILE,
206+
pathValue: './upload/my-profile-pic.jpg',
207+
} as FileReportItemDto,
208+
]);
209+
210+
expect(userMock.update).toHaveBeenCalledWith('my-id', { profileImagePath: './upload/my-profile-pic.jpg' });
211+
expect(assetMock.update).not.toHaveBeenCalled();
212+
expect(assetMock.upsertFile).not.toHaveBeenCalled();
213+
expect(personMock.update).not.toHaveBeenCalled();
214+
});
215+
});
62216
});

0 commit comments

Comments
 (0)