2023-11-14 04:10:35 +01:00
|
|
|
import { UserAvatarColor } from '@app/infra/entities';
|
2023-05-15 19:30:53 +02:00
|
|
|
import { BadRequestException } from '@nestjs/common';
|
2023-06-08 17:01:07 +02:00
|
|
|
import { authStub, newPartnerRepositoryMock, partnerStub } from '@test';
|
2023-11-11 22:06:19 +01:00
|
|
|
import { IAccessRepository, IPartnerRepository, PartnerDirection } from '../repositories';
|
|
|
|
import { PartnerResponseDto } from './partner.dto';
|
2023-05-15 19:30:53 +02:00
|
|
|
import { PartnerService } from './partner.service';
|
|
|
|
|
|
|
|
const responseDto = {
|
2023-11-11 22:06:19 +01:00
|
|
|
admin: <PartnerResponseDto>{
|
2023-05-15 19:30:53 +02:00
|
|
|
email: 'admin@test.com',
|
2023-11-12 02:03:32 +01:00
|
|
|
name: 'admin_name',
|
2023-05-15 19:30:53 +02:00
|
|
|
id: 'admin_id',
|
|
|
|
isAdmin: true,
|
|
|
|
oauthId: '',
|
|
|
|
profileImagePath: '',
|
|
|
|
shouldChangePassword: false,
|
2023-05-22 05:18:10 +02:00
|
|
|
storageLabel: 'admin',
|
2023-05-30 15:15:56 +02:00
|
|
|
createdAt: new Date('2021-01-01'),
|
|
|
|
deletedAt: null,
|
|
|
|
updatedAt: new Date('2021-01-01'),
|
2023-08-10 04:01:16 +02:00
|
|
|
memoriesEnabled: true,
|
2023-11-14 04:10:35 +01:00
|
|
|
avatarColor: UserAvatarColor.PRIMARY,
|
2024-01-13 01:43:36 +01:00
|
|
|
quotaSizeInBytes: null,
|
2023-11-11 22:06:19 +01:00
|
|
|
inTimeline: true,
|
2024-01-13 01:43:36 +01:00
|
|
|
quotaUsageInBytes: 0,
|
2023-05-15 19:30:53 +02:00
|
|
|
},
|
2023-11-11 22:06:19 +01:00
|
|
|
user1: <PartnerResponseDto>{
|
2023-05-15 19:30:53 +02:00
|
|
|
email: 'immich@test.com',
|
2023-11-12 02:03:32 +01:00
|
|
|
name: 'immich_name',
|
2023-05-22 05:18:10 +02:00
|
|
|
id: 'user-id',
|
2023-05-15 19:30:53 +02:00
|
|
|
isAdmin: false,
|
|
|
|
oauthId: '',
|
|
|
|
profileImagePath: '',
|
|
|
|
shouldChangePassword: false,
|
2023-05-22 05:18:10 +02:00
|
|
|
storageLabel: null,
|
2023-05-30 15:15:56 +02:00
|
|
|
createdAt: new Date('2021-01-01'),
|
|
|
|
deletedAt: null,
|
|
|
|
updatedAt: new Date('2021-01-01'),
|
2023-08-10 04:01:16 +02:00
|
|
|
memoriesEnabled: true,
|
2023-11-14 04:10:35 +01:00
|
|
|
avatarColor: UserAvatarColor.PRIMARY,
|
2023-11-11 22:06:19 +01:00
|
|
|
inTimeline: true,
|
2024-01-13 01:43:36 +01:00
|
|
|
quotaSizeInBytes: null,
|
|
|
|
quotaUsageInBytes: 0,
|
2023-05-15 19:30:53 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
describe(PartnerService.name, () => {
|
|
|
|
let sut: PartnerService;
|
|
|
|
let partnerMock: jest.Mocked<IPartnerRepository>;
|
2023-11-11 22:06:19 +01:00
|
|
|
let accessMock: jest.Mocked<IAccessRepository>;
|
2023-05-15 19:30:53 +02:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
beforeEach(() => {
|
2023-05-15 19:30:53 +02:00
|
|
|
partnerMock = newPartnerRepositoryMock();
|
2023-11-11 22:06:19 +01:00
|
|
|
sut = new PartnerService(partnerMock, accessMock);
|
2023-05-15 19:30:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getAll', () => {
|
|
|
|
it("should return a list of partners with whom I've shared my library", async () => {
|
|
|
|
partnerMock.getAll.mockResolvedValue([partnerStub.adminToUser1, partnerStub.user1ToAdmin1]);
|
|
|
|
await expect(sut.getAll(authStub.user1, PartnerDirection.SharedBy)).resolves.toEqual([responseDto.admin]);
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(partnerMock.getAll).toHaveBeenCalledWith(authStub.user1.user.id);
|
2023-05-15 19:30:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a list of partners who have shared their libraries with me', async () => {
|
|
|
|
partnerMock.getAll.mockResolvedValue([partnerStub.adminToUser1, partnerStub.user1ToAdmin1]);
|
|
|
|
await expect(sut.getAll(authStub.user1, PartnerDirection.SharedWith)).resolves.toEqual([responseDto.admin]);
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(partnerMock.getAll).toHaveBeenCalledWith(authStub.user1.user.id);
|
2023-05-15 19:30:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('create', () => {
|
|
|
|
it('should create a new partner', async () => {
|
|
|
|
partnerMock.get.mockResolvedValue(null);
|
|
|
|
partnerMock.create.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.create(authStub.admin, authStub.user1.user.id)).resolves.toEqual(responseDto.user1);
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
expect(partnerMock.create).toHaveBeenCalledWith({
|
2023-12-10 05:34:12 +01:00
|
|
|
sharedById: authStub.admin.user.id,
|
|
|
|
sharedWithId: authStub.user1.user.id,
|
2023-05-15 19:30:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when the partner already exists', async () => {
|
|
|
|
partnerMock.get.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.create(authStub.admin, authStub.user1.user.id)).rejects.toBeInstanceOf(BadRequestException);
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
expect(partnerMock.create).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('remove', () => {
|
|
|
|
it('should remove a partner', async () => {
|
|
|
|
partnerMock.get.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await sut.remove(authStub.admin, authStub.user1.user.id);
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
expect(partnerMock.remove).toHaveBeenCalledWith(partnerStub.adminToUser1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when the partner does not exist', async () => {
|
|
|
|
partnerMock.get.mockResolvedValue(null);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.remove(authStub.admin, authStub.user1.user.id)).rejects.toBeInstanceOf(BadRequestException);
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
expect(partnerMock.remove).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|