Skip to content

High performance self-hosted photo and video management solution.

3.0 KB
Raw Blame
import request from 'supertest';
import { PartnerController } from 'src/controllers/partner.controller.js';
import { LoggingRepository } from 'src/repositories/logging.repository.js';
import { PartnerService } from 'src/services/partner.service.js';
import { errorDto } from 'test/medium/responses.js';
import { ControllerContext, automock, controllerSetup, mockBaseService } from 'test/utils.js';

describe(PartnerController.name, () => {
  let ctx: ControllerContext;
  const service = mockBaseService(PartnerService);

  beforeAll(async () => {
    ctx = await controllerSetup(PartnerController, [
      { provide: PartnerService, useValue: service },
      { provide: LoggingRepository, useValue: automock(LoggingRepository, { strict: false }) },
    ]);
    return () => ctx.close();
  });

  beforeEach(() => {
    service.resetAllMocks();
    ctx.reset();
  });

  describe('GET /partners', () => {
    it(`should require a direction`, async () => {
      const { status, body } = await request(ctx.getHttpServer()).get(`/partners`).set('Authorization', `Bearer token`);
      expect(status).toBe(400);
      expect(body).toEqual(
        errorDto.validationError([
          { path: ['direction'], message: expect.stringContaining('Invalid option: expected one of') },
        ]),
      );
    });

    it(`should require direction to be an enum`, async () => {
      const { status, body } = await request(ctx.getHttpServer())
        .get(`/partners`)
        .query({ direction: 'invalid' })
        .set('Authorization', `Bearer token`);
      expect(status).toBe(400);
      expect(body).toEqual(
        errorDto.validationError([
          { path: ['direction'], message: expect.stringContaining('Invalid option: expected one of') },
        ]),
      );
    });
  });

  describe('POST /partners', () => {
    it(`should require sharedWithId to be a uuid`, async () => {
      const { status, body } = await request(ctx.getHttpServer())
        .post(`/partners`)
        .send({ sharedWithId: 'invalid' })
        .set('Authorization', `Bearer token`);
      expect(status).toBe(400);
      expect(body).toEqual(errorDto.validationError([{ path: ['sharedWithId'], message: 'Invalid UUID' }]));
    });
  });

  describe('PUT /partners/:id', () => {
    it(`should require id to be a uuid`, async () => {
      const { status, body } = await request(ctx.getHttpServer())
        .put(`/partners/invalid`)
        .send({ inTimeline: true })
        .set('Authorization', `Bearer token`);
      expect(status).toBe(400);
      expect(body).toEqual(errorDto.validationError([{ path: ['id'], message: 'Invalid UUID' }]));
    });
  });

  describe('DELETE /partners/:id', () => {
    it(`should require id to be a uuid`, async () => {
      const { status, body } = await request(ctx.getHttpServer())
        .delete(`/partners/invalid`)
        .set('Authorization', `Bearer token`);
      expect(status).toBe(400);
      expect(body).toEqual(errorDto.validationError([{ path: ['id'], message: 'Invalid UUID' }]));
    });
  });
});