Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

6.7 KB
Raw Blame
import { ContactController } from './contact.controller';
import { ContactService } from './contact.service';
import { ChatScopeService } from '../auth/chat-scope.service';
import type { ApiKey } from '../auth/entities/api-key.entity';

describe('ContactController', () => {
  const service = {
    listContacts: jest.fn(),
    getProfilePictures: jest.fn(),
    getContactById: jest.fn(),
    getNumberId: jest.fn(),
    getProfilePicture: jest.fn(),
    resolveContactPhone: jest.fn(),
    upsertContact: jest.fn(),
    deleteContact: jest.fn(),
    blockContact: jest.fn(),
    unblockContact: jest.fn(),
    getBlockedContacts: jest.fn(),
  };
  const controller = new ContactController(service as unknown as ContactService, new ChatScopeService());

  beforeEach(() => jest.clearAllMocks());

  const contacts = (n: number) => Array.from({ length: n }, (_, i) => ({ id: `${i}@c.us` }));

  it('findAll applies limit/offset query strings', async () => {
    service.listContacts.mockResolvedValue(contacts(50));
    const page = (await controller.findAll('s1', '5', '10')) as { id: string }[];
    expect(service.listContacts).toHaveBeenCalledWith('s1', 10);
    expect(page.map(c => c.id)).toEqual(['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']);
  });

  it('findAll caps an unpaged list at the default window', async () => {
    service.listContacts.mockResolvedValue(contacts(1500));
    await expect(controller.findAll('s1')).resolves.toHaveLength(1000);
  });

  it('findAll filters to a chat-restricted key before the window, matching either phone dialect', async () => {
    // The disallowed contact comes first, so a page of 1 taken before filtering would hold nothing allowed.
    service.listContacts.mockResolvedValue([
      { id: '[email protected]' },
      { id: '[email protected]' },
      { id: '[email protected]' },
      { id: '555000111@lid' },
    ]);
    const apiKey = { allowedChats: ['[email protected]'] } as ApiKey;

    const all = (await controller.findAll('s1', undefined, undefined, apiKey)) as { id: string }[];
    expect(all.map(c => c.id)).toEqual(['[email protected]', '[email protected]']);
    const first = (await controller.findAll('s1', '1', '0', apiKey)) as { id: string }[];
    expect(first.map(c => c.id)).toEqual(['[email protected]']);
  });

  it('getProfilePictures splits, trims and drops empty ids', async () => {
    service.getProfilePictures.mockResolvedValue({ '[email protected]': null, '[email protected]': 'https://pps/2.jpg' });
    const out = await controller.getProfilePictures('s1', ' [email protected] , ,[email protected] ');
    expect(service.getProfilePictures).toHaveBeenCalledWith('s1', ['[email protected]', '[email protected]']);
    expect(out).toEqual({ pictures: { '[email protected]': null, '[email protected]': 'https://pps/2.jpg' } });
  });

  it('getProfilePictures defaults a missing ids param to an empty list', async () => {
    service.getProfilePictures.mockResolvedValue({});
    await controller.getProfilePictures('s1');
    expect(service.getProfilePictures).toHaveBeenCalledWith('s1', []);
  });

  it('findOne delegates to the service', async () => {
    service.getContactById.mockResolvedValue({ id: 'c1' });
    await controller.findOne('s1', 'c1');
    expect(service.getContactById).toHaveBeenCalledWith('s1', 'c1');
  });

  it('checkNumber maps a null whatsappId to exists:false', async () => {
    service.getNumberId.mockResolvedValue(null);
    await expect(controller.checkNumber('s1', '628123')).resolves.toEqual({
      number: '628123',
      exists: false,
      whatsappId: null,
    });
  });

  it('checkNumber returns the canonical id when the number exists', async () => {
    service.getNumberId.mockResolvedValue('[email protected]');
    await expect(controller.checkNumber('s1', '628123')).resolves.toEqual({
      number: '628123',
      exists: true,
      whatsappId: '[email protected]',
    });
  });

  it('getProfilePicture wraps the url', async () => {
    service.getProfilePicture.mockResolvedValue('https://pps/1.jpg');
    await expect(controller.getProfilePicture('s1', 'c1')).resolves.toEqual({ url: 'https://pps/1.jpg' });
  });

  it('resolvePhone wraps contactId and phone', async () => {
    service.resolveContactPhone.mockResolvedValue('628123456789');
    await expect(controller.resolvePhone('s1', '123@lid')).resolves.toEqual({
      contactId: '123@lid',
      phone: '628123456789',
    });
  });

  it('getBlockedContacts returns the service list as a bare array', async () => {
    service.getBlockedContacts.mockResolvedValue(['[email protected]', '[email protected]']);
    await expect(controller.getBlockedContacts('s1')).resolves.toEqual(['[email protected]', '[email protected]']);
    expect(service.getBlockedContacts).toHaveBeenCalledWith('s1');
  });

  it('upsertContact passes first/last name from the DTO', async () => {
    service.upsertContact.mockResolvedValue(undefined);
    await expect(controller.upsertContact('s1', 'c1', { firstName: 'A', lastName: 'B' })).resolves.toEqual({
      success: true,
      message: 'Contact saved',
    });
    expect(service.upsertContact).toHaveBeenCalledWith('s1', 'c1', 'A', 'B');
  });

  it.each([
    ['deleteContact', { success: true, message: 'Contact deleted' }],
    ['blockContact', { success: true, message: 'Contact blocked' }],
    ['unblockContact', { success: true, message: 'Contact unblocked' }],
  ] as const)('%s delegates and returns its success body', async (method, body) => {
    service[method].mockResolvedValue(undefined);
    await expect(controller[method]('s1', 'c1')).resolves.toEqual(body);
    expect(service[method]).toHaveBeenCalledWith('s1', 'c1');
  });
});

// Every route resolves the session's engine first, which answers 400 "Session is not started" for a
// session with no running engine; clients generated from the OpenAPI contract need it declared.
describe('ContactController OpenAPI error responses', () => {
  const handler = (name: string) => Object.getOwnPropertyDescriptor(ContactController.prototype, name)?.value as object;
  const routes = Object.getOwnPropertyNames(ContactController.prototype).filter(
    name => name !== 'constructor' && Reflect.getMetadata('path', handler(name)) !== undefined,
  );

  it('covers every route', () => {
    expect(routes).toHaveLength(11);
  });

  it.each(routes)('%s declares 400', method => {
    const responses = Reflect.getMetadata('swagger/apiResponse', handler(method)) as Record<string, unknown>;
    expect(Object.keys(responses)).toContain('400');
  });

  // A started session that is not ready answers 409, so the 400 must not describe it as "not ready".
  it.each(routes)('%s describes its 400 as a session that is not started', method => {
    const responses = Reflect.getMetadata('swagger/apiResponse', handler(method)) as Record<
      string,
      { description?: string }
    >;
    expect(responses['400']?.description).toMatch(/^Session is not started/);
  });
});