Skip to content

High performance self-hosted photo and video management solution.

4.9 KB
Raw Blame
import { Kysely } from 'kysely';
import { SharingDirection } from 'src/enum.js';
import { AccessRepository } from 'src/repositories/access.repository.js';
import { LoggingRepository } from 'src/repositories/logging.repository.js';
import { PartnerRepository } from 'src/repositories/partner.repository.js';
import { UserRepository } from 'src/repositories/user.repository.js';
import { DB } from 'src/schema/index.js';
import { PartnerService } from 'src/services/partner.service.js';
import { newMediumService } from 'test/medium.factory.js';
import { factory, newUuid } from 'test/small.factory.js';
import { getKyselyDB } from 'test/utils.js';

let defaultDatabase: Kysely<DB>;

// eslint-disable-next-line unicorn/no-unnecessary-parameters
const setup = (db?: Kysely<DB>) => {
  return newMediumService(PartnerService, {
    database: db || defaultDatabase,
    real: [AccessRepository, PartnerRepository, UserRepository],
    mock: [LoggingRepository],
  });
};

beforeAll(async () => {
  defaultDatabase = await getKyselyDB();
});

describe(PartnerService.name, () => {
  describe('create', () => {
    it('should share with a new partner', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();
      const { user: partner } = await ctx.newUser();

      await expect(sut.create(factory.auth({ user }), { sharedWithId: partner.id })).resolves.toEqual(
        expect.objectContaining({ id: partner.id }),
      );
    });

    it('should not share with a partner that is already shared with', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();
      const { user: partner } = await ctx.newUser();
      await ctx.newPartner({ sharedById: user.id, sharedWithId: partner.id });

      await expect(sut.create(factory.auth({ user }), { sharedWithId: partner.id })).rejects.toThrow(
        'Partner already exists',
      );
    });

    it('should not share with an unknown user', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();

      await expect(sut.create(factory.auth({ user }), { sharedWithId: newUuid() })).rejects.toThrow('Invalid user');
    });
  });

  describe('search', () => {
    it('should return partners shared by the user', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();
      const { user: partner } = await ctx.newUser();
      await ctx.newPartner({ sharedById: user.id, sharedWithId: partner.id });

      await expect(sut.search(factory.auth({ user }), { direction: SharingDirection.SharedBy })).resolves.toEqual([
        expect.objectContaining({ id: partner.id }),
      ]);
    });

    it('should return partners that share with the user', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();
      const { user: partner } = await ctx.newUser();
      await ctx.newPartner({ sharedById: partner.id, sharedWithId: user.id });

      await expect(sut.search(factory.auth({ user }), { direction: SharingDirection.SharedWith })).resolves.toEqual([
        expect.objectContaining({ id: partner.id }),
      ]);
    });
  });

  describe('update', () => {
    it('should update a partner', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();
      const { user: partner } = await ctx.newUser();
      await ctx.newPartner({ sharedById: partner.id, sharedWithId: user.id });

      await expect(sut.update(factory.auth({ user }), partner.id, { inTimeline: false })).resolves.toEqual(
        expect.objectContaining({ id: partner.id, inTimeline: false }),
      );
    });

    it('should not update a partner that did not share with the user', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();
      const { user: partner } = await ctx.newUser();
      const { user: other } = await ctx.newUser();
      await ctx.newPartner({ sharedById: partner.id, sharedWithId: other.id });

      await expect(sut.update(factory.auth({ user }), partner.id, { inTimeline: false })).rejects.toThrow(
        'Not found or no partner.update access',
      );
    });
  });

  describe('remove', () => {
    it('should remove a partner', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();
      const { user: partner } = await ctx.newUser();
      await ctx.newPartner({ sharedById: user.id, sharedWithId: partner.id });

      await expect(sut.remove(factory.auth({ user }), partner.id)).resolves.toBeUndefined();
      await expect(sut.search(factory.auth({ user }), { direction: SharingDirection.SharedBy })).resolves.toEqual([]);
    });

    it('should throw when the partner does not exist', async () => {
      const { sut, ctx } = setup();
      const { user } = await ctx.newUser();

      await expect(sut.remove(factory.auth({ user }), newUuid())).rejects.toThrow('Partner not found');
    });
  });
});