refactor: better types for getList and getDeletedAfter (#16926)

This commit is contained in:
Jason Rasmussen
2025-03-17 15:32:12 -04:00
committed by GitHub
parent 93907a89d8
commit 6a40aa83b7
13 changed files with 342 additions and 194 deletions
+44 -51
View File
@@ -6,6 +6,7 @@ import { ImmichFileResponse } from 'src/utils/file';
import { authStub } from 'test/fixtures/auth.stub';
import { systemConfigStub } from 'test/fixtures/system-config.stub';
import { userStub } from 'test/fixtures/user.stub';
import { factory } from 'test/small.factory';
import { newTestService, ServiceMocks } from 'test/utils';
const makeDeletedAt = (daysAgo: number) => {
@@ -20,7 +21,6 @@ describe(UserService.name, () => {
beforeEach(() => {
({ sut, mocks } = newTestService(UserService));
mocks.user.get.mockImplementation((userId) =>
Promise.resolve([userStub.admin, userStub.user1].find((user) => user.id === userId) ?? undefined),
);
@@ -28,36 +28,40 @@ describe(UserService.name, () => {
describe('getAll', () => {
it('admin should get all users', async () => {
mocks.user.getList.mockResolvedValue([userStub.admin]);
await expect(sut.search(authStub.admin)).resolves.toEqual([
expect.objectContaining({
id: authStub.admin.user.id,
email: authStub.admin.user.email,
}),
]);
const user = factory.userAdmin();
const auth = factory.auth(user);
mocks.user.getList.mockResolvedValue([user]);
await expect(sut.search(auth)).resolves.toEqual([expect.objectContaining({ id: user.id, email: user.email })]);
expect(mocks.user.getList).toHaveBeenCalledWith({ withDeleted: false });
});
it('non-admin should get all users when publicUsers enabled', async () => {
mocks.user.getList.mockResolvedValue([userStub.user1]);
await expect(sut.search(authStub.user1)).resolves.toEqual([
expect.objectContaining({
id: authStub.user1.user.id,
email: authStub.user1.user.email,
}),
]);
expect(mocks.user.getList).toHaveBeenCalledWith({ withDeleted: false });
});
it('non-admin user should only receive itself when publicUsers is disabled', async () => {
mocks.user.getList.mockResolvedValue([userStub.user1]);
mocks.systemMetadata.get.mockResolvedValue(systemConfigStub.publicUsersDisabled);
await expect(sut.search(authStub.user1)).resolves.toEqual([
expect.objectContaining({
id: authStub.user1.user.id,
email: authStub.user1.user.email,
}),
]);
expect(mocks.user.getList).not.toHaveBeenCalledWith({ withDeleted: false });
});
});
@@ -65,13 +69,17 @@ describe(UserService.name, () => {
describe('get', () => {
it('should get a user by id', async () => {
mocks.user.get.mockResolvedValue(userStub.admin);
await sut.get(authStub.admin.user.id);
expect(mocks.user.get).toHaveBeenCalledWith(authStub.admin.user.id, { withDeleted: false });
});
it('should throw an error if a user is not found', async () => {
mocks.user.get.mockResolvedValue(void 0);
await expect(sut.get(authStub.admin.user.id)).rejects.toBeInstanceOf(BadRequestException);
expect(mocks.user.get).toHaveBeenCalledWith(authStub.admin.user.id, { withDeleted: false });
});
});
@@ -79,6 +87,7 @@ describe(UserService.name, () => {
describe('getMe', () => {
it("should get the auth user's info", async () => {
const user = authStub.admin.user;
await expect(sut.getMe(authStub.admin)).resolves.toMatchObject({
id: user.id,
email: user.email,
@@ -89,6 +98,7 @@ describe(UserService.name, () => {
describe('createProfileImage', () => {
it('should throw an error if the user does not exist', async () => {
const file = { path: '/profile/path' } as Express.Multer.File;
mocks.user.get.mockResolvedValue(void 0);
mocks.user.update.mockResolvedValue({ ...userStub.admin, profileImagePath: file.path });
@@ -105,20 +115,24 @@ describe(UserService.name, () => {
it('should delete the previous profile image', async () => {
const file = { path: '/profile/path' } as Express.Multer.File;
mocks.user.get.mockResolvedValue(userStub.profilePath);
const files = [userStub.profilePath.profileImagePath];
mocks.user.get.mockResolvedValue(userStub.profilePath);
mocks.user.update.mockResolvedValue({ ...userStub.admin, profileImagePath: file.path });
await sut.createProfileImage(authStub.admin, file);
expect(mocks.job.queue.mock.calls).toEqual([[{ name: JobName.DELETE_FILES, data: { files } }]]);
});
it('should not delete the profile image if it has not been set', async () => {
const file = { path: '/profile/path' } as Express.Multer.File;
mocks.user.get.mockResolvedValue(userStub.admin);
mocks.user.update.mockResolvedValue({ ...userStub.admin, profileImagePath: file.path });
await sut.createProfileImage(authStub.admin, file);
expect(mocks.job.queue).not.toHaveBeenCalled();
expect(mocks.job.queueAll).not.toHaveBeenCalled();
});
@@ -129,6 +143,7 @@ describe(UserService.name, () => {
mocks.user.get.mockResolvedValue(userStub.admin);
await expect(sut.deleteProfileImage(authStub.admin)).rejects.toBeInstanceOf(BadRequestException);
expect(mocks.job.queue).not.toHaveBeenCalled();
expect(mocks.job.queueAll).not.toHaveBeenCalled();
});
@@ -138,6 +153,7 @@ describe(UserService.name, () => {
const files = [userStub.profilePath.profileImagePath];
await sut.deleteProfileImage(authStub.admin);
expect(mocks.job.queue.mock.calls).toEqual([[{ name: JobName.DELETE_FILES, data: { files } }]]);
});
});
@@ -176,53 +192,22 @@ describe(UserService.name, () => {
describe('handleQueueUserDelete', () => {
it('should skip users not ready for deletion', async () => {
mocks.user.getDeletedUsers.mockResolvedValue([
{},
{ deletedAt: undefined },
{ deletedAt: null },
{ deletedAt: makeDeletedAt(5) },
] as UserEntity[]);
mocks.user.getDeletedAfter.mockResolvedValue([]);
await sut.handleUserDeleteCheck();
expect(mocks.user.getDeletedUsers).toHaveBeenCalled();
expect(mocks.job.queue).not.toHaveBeenCalled();
expect(mocks.job.queueAll).toHaveBeenCalledWith([]);
});
it('should skip users not ready for deletion - deleteDelay30', async () => {
mocks.systemMetadata.get.mockResolvedValue(systemConfigStub.deleteDelay30);
mocks.user.getDeletedUsers.mockResolvedValue([
{},
{ deletedAt: undefined },
{ deletedAt: null },
{ deletedAt: makeDeletedAt(15) },
] as UserEntity[]);
await sut.handleUserDeleteCheck();
expect(mocks.user.getDeletedUsers).toHaveBeenCalled();
expect(mocks.user.getDeletedAfter).toHaveBeenCalled();
expect(mocks.job.queue).not.toHaveBeenCalled();
expect(mocks.job.queueAll).toHaveBeenCalledWith([]);
});
it('should queue user ready for deletion', async () => {
const user = { id: 'deleted-user', deletedAt: makeDeletedAt(10) };
mocks.user.getDeletedUsers.mockResolvedValue([user] as UserEntity[]);
const user = factory.user();
mocks.user.getDeletedAfter.mockResolvedValue([{ id: user.id }]);
await sut.handleUserDeleteCheck();
expect(mocks.user.getDeletedUsers).toHaveBeenCalled();
expect(mocks.job.queueAll).toHaveBeenCalledWith([{ name: JobName.USER_DELETION, data: { id: user.id } }]);
});
it('should queue user ready for deletion - deleteDelay30', async () => {
const user = { id: 'deleted-user', deletedAt: makeDeletedAt(31) };
mocks.user.getDeletedUsers.mockResolvedValue([user] as UserEntity[]);
await sut.handleUserDeleteCheck();
expect(mocks.user.getDeletedUsers).toHaveBeenCalled();
expect(mocks.user.getDeletedAfter).toHaveBeenCalled();
expect(mocks.job.queueAll).toHaveBeenCalledWith([{ name: JobName.USER_DELETION, data: { id: user.id } }]);
});
});
@@ -230,6 +215,7 @@ describe(UserService.name, () => {
describe('handleUserDelete', () => {
it('should skip users not ready for deletion', async () => {
const user = { id: 'user-1', deletedAt: makeDeletedAt(5) } as UserEntity;
mocks.user.get.mockResolvedValue(user);
await sut.handleUserDelete({ id: user.id });
@@ -240,12 +226,12 @@ describe(UserService.name, () => {
it('should delete the user and associated assets', async () => {
const user = { id: 'deleted-user', deletedAt: makeDeletedAt(10) } as UserEntity;
const options = { force: true, recursive: true };
mocks.user.get.mockResolvedValue(user);
await sut.handleUserDelete({ id: user.id });
const options = { force: true, recursive: true };
expect(mocks.storage.unlinkDir).toHaveBeenCalledWith('upload/library/deleted-user', options);
expect(mocks.storage.unlinkDir).toHaveBeenCalledWith('upload/upload/deleted-user', options);
expect(mocks.storage.unlinkDir).toHaveBeenCalledWith('upload/profile/deleted-user', options);
@@ -257,6 +243,7 @@ describe(UserService.name, () => {
it('should delete the library path for a storage label', async () => {
const user = { id: 'deleted-user', deletedAt: makeDeletedAt(10), storageLabel: 'admin' } as UserEntity;
mocks.user.get.mockResolvedValue(user);
await sut.handleUserDelete({ id: user.id });
@@ -269,9 +256,10 @@ describe(UserService.name, () => {
describe('setLicense', () => {
it('should save client license if valid', async () => {
const license = { licenseKey: 'IMCL-license-key', activationKey: 'activation-key' };
mocks.user.upsertMetadata.mockResolvedValue();
const license = { licenseKey: 'IMCL-license-key', activationKey: 'activation-key' };
await sut.setLicense(authStub.user1, license);
expect(mocks.user.upsertMetadata).toHaveBeenCalledWith(authStub.user1.user.id, {
@@ -281,9 +269,10 @@ describe(UserService.name, () => {
});
it('should save server license as client if valid', async () => {
const license = { licenseKey: 'IMSV-license-key', activationKey: 'activation-key' };
mocks.user.upsertMetadata.mockResolvedValue();
const license = { licenseKey: 'IMSV-license-key', activationKey: 'activation-key' };
await sut.setLicense(authStub.user1, license);
expect(mocks.user.upsertMetadata).toHaveBeenCalledWith(authStub.user1.user.id, {
@@ -293,11 +282,13 @@ describe(UserService.name, () => {
});
it('should not save license if invalid', async () => {
mocks.user.upsertMetadata.mockResolvedValue();
const license = { licenseKey: 'license-key', activationKey: 'activation-key' };
const call = sut.setLicense(authStub.admin, license);
mocks.user.upsertMetadata.mockResolvedValue();
await expect(call).rejects.toThrowError('Invalid license key');
expect(mocks.user.upsertMetadata).not.toHaveBeenCalled();
});
});
@@ -307,6 +298,7 @@ describe(UserService.name, () => {
mocks.user.upsertMetadata.mockResolvedValue();
await sut.deleteLicense(authStub.admin);
expect(mocks.user.upsertMetadata).not.toHaveBeenCalled();
});
});
@@ -314,6 +306,7 @@ describe(UserService.name, () => {
describe('handleUserSyncUsage', () => {
it('should sync usage', async () => {
await sut.handleUserSyncUsage();
expect(mocks.user.syncUsage).toHaveBeenCalledTimes(1);
});
});