Skip to content

High performance self-hosted photo and video management solution.

3.3 KB
Raw Blame
import { WorkflowTrigger } from '@immich/plugin-sdk';
import request from 'supertest';
import { WorkflowController } from 'src/controllers/workflow.controller.js';
import { LoggingRepository } from 'src/repositories/logging.repository.js';
import { WorkflowService } from 'src/services/workflow.service.js';
import { errorDto } from 'test/medium/responses.js';
import { ControllerContext, automock, controllerSetup, mockBaseService } from 'test/utils.js';

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

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

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

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

    it(`should require a valid enabled value`, async () => {
      const { status, body } = await request(ctx.getHttpServer())
        .post(`/workflows`)
        .send({ enabled: 'invalid' })
        .set('Authorization', `Bearer token`);
      expect(status).toBe(400);
      expect(body).toEqual(
        errorDto.validationError([{ path: ['enabled'], message: 'Invalid input: expected boolean, received string' }]),
      );
    });

    it(`should not require a name`, async () => {
      const { status } = await request(ctx.getHttpServer())
        .post(`/workflows`)
        .send({ trigger: WorkflowTrigger.AssetCreate })
        .set('Authorization', `Bearer token`);
      expect(status).toBe(201);
      expect(service.create).toHaveBeenCalled();
    });
  });

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

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

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