Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

17.6 KB
Raw Blame
import { StreamableFile } from '@nestjs/common';
import { RESPONSE_PASSTHROUGH_METADATA } from '@nestjs/common/constants';
import { Test } from '@nestjs/testing';
import request from 'supertest';
import type { Server } from 'http';
import { EventEmitter } from 'events';
import { MessageController } from './message.controller';
import { MessageService } from './message.service';
import { BulkMessageService } from './bulk-message.service';
import { ChatScopeService } from '../auth/chat-scope.service';
import { SendIdempotencyService } from './idempotency/send-idempotency.service';
import { CHAT_SCOPED_KEY } from '../auth/decorators/auth.decorators';
import type { ApiKey } from '../auth/entities/api-key.entity';
import type { SendBulkMessageDto } from './dto/bulk-message.dto';
import type { Response } from 'express';

describe('MessageController - message-time selection', () => {
  const getMessages = jest.fn().mockResolvedValue({ messages: [], total: 0, unknownTimestampTotal: 0 });
  const controller = new MessageController(
    { getMessages } as unknown as MessageService,
    {} as unknown as BulkMessageService,
    new ChatScopeService(),
  );
  beforeEach(() => getMessages.mockClear());

  it('passes new HTTP options into the existing bounded list', async () => {
    await controller.getMessages(
      's1',
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      'false',
      undefined,
      '1000000.5',
      '2000000',
      'incoming',
      'timestamp',
    );
    expect(getMessages).toHaveBeenCalledWith(
      's1',
      expect.objectContaining({
        since: 1000000.5,
        until: 2000000,
        direction: 'incoming',
        orderBy: 'timestamp',
        inlineMedia: false,
      }),
    );
  });
  it('passes the type and message reference filters into the list', async () => {
    await controller.getMessages(
      's1',
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      'image',
      'wamid.ABC',
    );
    expect(getMessages).toHaveBeenCalledWith('s1', expect.objectContaining({ type: 'image', messageId: 'wamid.ABC' }));
  });
  it('rejects invalid selection before accessing storage', async () => {
    await expect(
      controller.getMessages(
        's1',
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        '2000',
        '1000',
      ),
    ).rejects.toThrow('since');
    expect(getMessages).not.toHaveBeenCalled();
  });
  it('keeps chat-restricted reads fenced even for new filters', async () => {
    await expect(
      controller.getMessages(
        's1',
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        { allowedChats: ['[email protected]'] } as ApiKey,
        '1000',
        '2000',
        'incoming',
        'timestamp',
      ),
    ).rejects.toThrow();
    expect(getMessages).not.toHaveBeenCalled();
  });
});

/**
 * `getChatMedia` serves third-party bytes from the API origin. The route shape, the roles and the
 * status codes are already held by the OpenAPI snapshot and the route-fence gates; the two headers
 * that keep those bytes inert in a browser were held by nothing — deleting either line from the
 * controller passed every suite in the repo.
 */
describe('MessageController — stored media download', () => {
  const getChatMedia = jest.fn().mockResolvedValue({ buffer: Buffer.from('GIF89a'), mimetype: 'image/gif' });
  const controller = new MessageController(
    { getChatMedia } as unknown as MessageService,
    {} as unknown as BulkMessageService,
    new ChatScopeService(),
  );

  /**
   * Express merges the object form of `res.set` into the header bag, so accumulating is both closer
   * to the real thing than recording the last call and independent of how many calls the handler
   * splits its headers across.
   */
  const mediaResponseHeaders = async (): Promise<Record<string, string>> => {
    const headers: Record<string, string> = {};
    const res = { set: (fields: Record<string, string>) => Object.assign(headers, fields) } as unknown as Response;
    await controller.getChatMedia('session-1', '[email protected]', 'msg-1', res);
    return headers;
  };

  it('sends nosniff, so a wrong Content-Type cannot be re-interpreted as active content', async () => {
    expect((await mediaResponseHeaders())['X-Content-Type-Options']).toBe('nosniff');
  });

  it('sends the media as an attachment, so it is never rendered on the API origin', async () => {
    expect((await mediaResponseHeaders())['Content-Disposition']).toBe('attachment');
  });

  /**
   * The headers above are set on the response object directly, so they survive a handler that sends
   * no body at all — a `404` with a perfect `Content-Disposition` would satisfy both. What actually
   * carries the bytes is the returned `StreamableFile`, and Nest only sends that when the response
   * parameter is declared passthrough: without it Nest treats the handler as having taken the
   * response over and discards the return value entirely. Both halves are asserted here so the pair
   * above cannot end up describing a response that is never sent.
   *
   * Nest records the flag under its own metadata key rather than in the route arguments, and only
   * when it is truthy — so reading it back is what distinguishes `@Res({ passthrough: true })` from
   * a bare `@Res()`.
   */
  it('declares the response passthrough, so Nest sends the returned file rather than discarding it', () => {
    expect(Reflect.getMetadata(RESPONSE_PASSTHROUGH_METADATA, MessageController, 'getChatMedia')).toBe(true);
  });

  it('returns the stored bytes as the response body', async () => {
    const res = { set: () => undefined } as unknown as Response;

    const body = await controller.getChatMedia('session-1', '[email protected]', 'msg-1', res);

    expect(body).toBeInstanceOf(StreamableFile);
    expect(body.getStream().read()).toEqual(Buffer.from('GIF89a'));
  });
});

/**
 * `inlineMedia` is an OPT-OUT, unlike every other boolean on this controller, so the parse reads the
 * same string pair the other way round. Inverting it would quietly strip media from every default
 * read, which no other suite would notice: the service takes a boolean and cannot tell who set it.
 */
describe('MessageController - inlineMedia is opt-out', () => {
  const getMessages = jest.fn().mockResolvedValue({ messages: [], total: 0 });
  const controller = new MessageController(
    { getMessages } as unknown as MessageService,
    {} as unknown as BulkMessageService,
    new ChatScopeService(),
  );

  const inlineMediaFor = async (raw?: string): Promise<boolean> => {
    getMessages.mockClear();
    await controller.getMessages('session-1', undefined, undefined, undefined, undefined, undefined, raw);
    const [, options] = getMessages.mock.calls[0] as [string, { inlineMedia: boolean }];
    return options.inlineMedia;
  };

  it.each([undefined, 'true', '1', '', 'no', 'False'])('keeps media inline for %p', async raw => {
    expect(await inlineMediaFor(raw)).toBe(true);
  });

  it.each(['false', '0'])('omits media for %p', async raw => {
    expect(await inlineMediaFor(raw)).toBe(false);
  });

  const afterFor = async (raw?: string): Promise<string | undefined> => {
    getMessages.mockClear();
    await controller.getMessages('session-1', undefined, undefined, undefined, undefined, raw, undefined);
    const [, options] = getMessages.mock.calls[0] as [string, { after?: string }];
    return options.after;
  };

  /**
   * The service only skips the keyset branch on `undefined`. A blank reached the anchor lookup,
   * matched no row, and answered 400 for what is an ordinary unfiltered first page: a client
   * templating a cursor it has not got yet sends exactly that.
   */
  it.each([undefined, '', '   '])('treats a blank after as absent for %p', async raw => {
    expect(await afterFor(raw)).toBeUndefined();
  });

  it('passes a real cursor through, trimmed', async () => {
    expect(await afterFor('db-42')).toBe('db-42');
    expect(await afterFor('  db-42  ')).toBe('db-42');
  });
});

/**
 * A key restricted to selected chats reads stored history only for a chat it names: the guard fences
 * the ?chatId= it sends, and the handler refuses the same key when it names none, which the service
 * would otherwise read as every chat in the session.
 */
describe('MessageController - stored history for a chat-restricted key', () => {
  const getMessages = jest.fn().mockResolvedValue({ messages: [], total: 0 });
  const controller = new MessageController(
    { getMessages } as unknown as MessageService,
    {} as unknown as BulkMessageService,
    new ChatScopeService(),
  );
  const restricted = { allowedChats: ['[email protected]'] } as ApiKey;
  const list = (chatId: string | undefined, apiKey?: ApiKey) =>
    controller.getMessages('s1', chatId, undefined, undefined, undefined, undefined, undefined, apiKey);

  beforeEach(() => getMessages.mockClear());

  it('is fenced', () => {
    expect(
      Reflect.getMetadata(
        CHAT_SCOPED_KEY,
        Object.getOwnPropertyDescriptor(MessageController.prototype, 'getMessages')!.value as object,
      ),
    ).toBe('fenced');
  });

  it.each([undefined, ''])('refuses a restricted key with chatId %p before reading', async chatId => {
    await expect(list(chatId, restricted)).rejects.toThrow('chatId is required for a key restricted to selected chats');
    expect(getMessages).not.toHaveBeenCalled();
  });

  it('reads the named chat for a restricted key, passing chatId through as sent', async () => {
    await list('[email protected]', restricted);
    expect(getMessages).toHaveBeenCalledWith('s1', expect.objectContaining({ chatId: '[email protected]' }));
  });

  it('leaves an unrestricted key free to list every chat', async () => {
    await list(undefined, { allowedChats: null } as ApiKey);
    expect(getMessages).toHaveBeenCalledWith('s1', expect.objectContaining({ chatId: undefined }));
  });
});

/**
 * The stored list narrows by sender, window and page with options the controller parses itself: a
 * dropped `from` returns every sender's history to a caller that asked for one.
 */
describe('MessageController - stored list options', () => {
  const getMessages = jest.fn().mockResolvedValue({ messages: [], total: 0 });
  const controller = new MessageController(
    { getMessages } as unknown as MessageService,
    {} as unknown as BulkMessageService,
    new ChatScopeService(),
  );

  beforeEach(() => getMessages.mockClear());

  it('passes the sender filter, the page window and the chat to the service', async () => {
    await controller.getMessages('s1', '[email protected]', '[email protected]', '25', '50');

    expect(getMessages).toHaveBeenCalledWith(
      's1',
      expect.objectContaining({ chatId: '[email protected]', from: '[email protected]', limit: 25, offset: 50 }),
    );
  });

  it('leaves the sender filter, limit and offset unset when none is sent', async () => {
    await controller.getMessages('s1');

    expect(getMessages).toHaveBeenCalledWith(
      's1',
      expect.objectContaining({ chatId: undefined, from: undefined, limit: undefined, offset: undefined }),
    );
  });
});

/**
 * The live history route reads from WhatsApp on demand, and `includeMedia` can mean dozens of multi-MB
 * downloads, so what the query turns on and what stops the loop when the client goes away are both
 * behaviour a caller depends on, and neither shows in the service's own tests.
 */
describe('MessageController - live chat history', () => {
  const getChatHistory = jest.fn().mockResolvedValue([]);
  const controller = new MessageController(
    { getChatHistory } as unknown as MessageService,
    {} as unknown as BulkMessageService,
    new ChatScopeService(),
  );

  type ServiceArgs = [
    sessionId: string,
    chatId: string,
    limit: number | undefined,
    includeMedia: boolean,
    deep: boolean,
    signal: AbortSignal,
  ];
  const call = async (query: { limit?: string; includeMedia?: string; deep?: string }, res = new EventEmitter()) => {
    getChatHistory.mockClear();
    await controller.getChatHistory(
      's1',
      '[email protected]',
      query.limit,
      query.includeMedia,
      query.deep,
      res as unknown as Response,
    );
    return getChatHistory.mock.calls[0] as ServiceArgs;
  };

  it('passes the session and the chat through', async () => {
    const [sessionId, chatId] = await call({});
    expect({ sessionId, chatId }).toEqual({ sessionId: 's1', chatId: '[email protected]' });
  });

  // 'true' and '1' are the two spellings the API spec accepts; any other string leaves the flag off.
  it.each([
    ['true', true],
    ['1', true],
    ['false', false],
    ['0', false],
    ['yes', false],
    ['TRUE', false],
    ['', false],
    [undefined, false],
  ])('reads includeMedia=%p as %p', async (raw, expected) => {
    const [, , , includeMedia, deep] = await call({ includeMedia: raw });
    expect({ includeMedia, deep }).toEqual({ includeMedia: expected, deep: false });
  });

  it.each([
    ['true', true],
    ['1', true],
    ['false', false],
    ['0', false],
    ['yes', false],
    ['TRUE', false],
    ['', false],
    [undefined, false],
  ])('reads deep=%p as %p', async (raw, expected) => {
    const [, , , includeMedia, deep] = await call({ deep: raw });
    expect({ includeMedia, deep }).toEqual({ includeMedia: false, deep: expected });
  });

  it.each([
    ['25', 25],
    ['abc', undefined],
    ['', undefined],
    [undefined, undefined],
  ])('reads limit=%p as %p, leaving a non-number to the service default', async (raw, expected) => {
    const [, , limit] = await call({ limit: raw });
    expect(limit).toBe(expected);
  });

  it('aborts the signal handed to the service when the client disconnects', async () => {
    const res = new EventEmitter();
    const [, , , , , signal] = await call({ includeMedia: 'true' }, res);

    expect(signal).toBeInstanceOf(AbortSignal);
    expect(signal.aborted).toBe(false);
    res.emit('close');
    expect(signal.aborted).toBe(true);
  });

  it('hands over a live signal even when there is no response to listen on', async () => {
    getChatHistory.mockClear();
    await controller.getChatHistory('s1', '[email protected]');

    const signal = (getChatHistory.mock.calls[0] as ServiceArgs)[5];
    expect(signal).toBeInstanceOf(AbortSignal);
    expect(signal.aborted).toBe(false);
  });
});

/**
 * A caller may pick its own batchId. 'history' shares the two-segment shape of ':chatId/history',
 * and an id with a reserved character must survive the statusUrl handed back on creation.
 */
describe('MessageController - caller-supplied batch ids', () => {
  const bulk = {
    getBatchStatus: jest.fn().mockResolvedValue({ batchId: 'history', status: 'processing' }),
    createBatch: jest.fn().mockResolvedValue({ batchId: 'run/1?x', status: 'pending', messages: [] }),
  };
  const messages = { getChatHistory: jest.fn().mockResolvedValue([]) };

  it("routes GET batch/history to the batch status, not the history of chat 'batch'", async () => {
    const moduleRef = await Test.createTestingModule({
      controllers: [MessageController],
      providers: [
        { provide: MessageService, useValue: messages },
        { provide: BulkMessageService, useValue: bulk },
        ChatScopeService,
        // The send routes carry SendIdempotencyInterceptor; this test sends no Idempotency-Key.
        { provide: SendIdempotencyService, useValue: {} },
      ],
    }).compile();
    const app = moduleRef.createNestApplication();
    await app.init();
    try {
      await request(app.getHttpServer() as Server)
        .get('/sessions/s1/messages/batch/history')
        .expect(200);
      expect(bulk.getBatchStatus).toHaveBeenCalledWith('s1', 'history');
      expect(messages.getChatHistory).not.toHaveBeenCalled();
    } finally {
      await app.close();
    }
  });

  it('percent-encodes the batch id in the returned statusUrl', async () => {
    const controller = new MessageController(
      messages as unknown as MessageService,
      bulk as unknown as BulkMessageService,
      new ChatScopeService(),
    );

    const res = await controller.sendBulk('s1', {} as SendBulkMessageDto);

    expect(res.statusUrl).toBe('/api/sessions/s1/messages/batch/run%2F1%3Fx');
  });
});

// MessageService.getEngine() answers 400 for a session with no live engine; clients generated from
// the OpenAPI contract need it declared on the read routes too.
describe('MessageController OpenAPI error responses', () => {
  it.each(['getChatHistory', 'getReactions'])('%s declares 400', method => {
    const responses = Reflect.getMetadata(
      'swagger/apiResponse',
      Object.getOwnPropertyDescriptor(MessageController.prototype, method)!.value as object,
    ) as Record<string, unknown>;
    expect(Object.keys(responses)).toContain('400');
  });

  // An unknown message answers 404 (MessageNotFoundError), which these routes declare on its own, so
  // their 400 is never "message not found": a client reading it that way would never handle the 404 the
  // engine actually raises. The 400 is the missing live engine, or a body that fails validation (an unknown
  // field, or a value of the wrong type), which is why it also says "invalid request", as send-text does.
  it.each(['react', 'deleteMessage'])('%s keeps "message not found" out of its 400', method => {
    const responses = Reflect.getMetadata(
      'swagger/apiResponse',
      Object.getOwnPropertyDescriptor(MessageController.prototype, method)!.value as object,
    ) as Record<string, { description: string }>;
    expect(responses['400'].description).toBe('Session not active or invalid request');
    expect(responses['400'].description).not.toMatch(/not found/i);
    expect(Object.keys(responses)).toContain('404');
  });
});