Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

3.0 KB
Raw Blame
import { PATH_METADATA } from '@nestjs/common/constants';
import { Reflector } from '@nestjs/core';
import { PluginsController } from './plugins.controller';
import { REQUIRED_ROLE_KEY, UNSCOPED_KEY } from '../auth/decorators/auth.decorators';
import { ApiKeyRole } from '../auth/entities/api-key.entity';

describe('PluginsController authorization', () => {
  const reflector = new Reflector();

  // Plugin reads expose installed versions, non-secret config, and health/error text — privileged
  // inventory on par with the ADMIN-gated write routes and the infra controllers' convention. A
  // VIEWER/OPERATOR key (or a session-scoped key) must not be able to enumerate it via the raw API.
  // Install, update and uninstall run third-party code on the host.
  const adminOnly = [
    'findAll',
    'install',
    'installFromUrl',
    'catalog',
    'findOne',
    'healthCheck',
    'enable',
    'disable',
    'updateConfig',
    'getConfigUi',
    'updateSessionConfig',
    'updateSessions',
    'update',
    'uninstall',
  ] as const;

  it('lists every route handler, so a new route cannot skip the role check', () => {
    const proto = PluginsController.prototype as unknown as Record<string, unknown>;
    const routes = Object.getOwnPropertyNames(proto).filter(
      name => name !== 'constructor' && Reflect.getMetadata(PATH_METADATA, proto[name] as object) !== undefined,
    );
    expect([...routes].sort()).toEqual([...adminOnly].sort());
  });

  it.each(adminOnly)('%s requires the ADMIN role', method => {
    // Metadata lookup key, never invoked.
    // eslint-disable-next-line @typescript-eslint/unbound-method
    const handler = PluginsController.prototype[method];
    const role = reflector.get<ApiKeyRole | undefined>(REQUIRED_ROLE_KEY, handler);
    expect(role).toBe(ApiKeyRole.ADMIN);
  });

  it('updateSessions requires an unrestricted key because updateSessions replaces the complete active set', () => {
    // eslint-disable-next-line @typescript-eslint/unbound-method
    const handler = PluginsController.prototype.updateSessions;
    expect(reflector.get<boolean>(UNSCOPED_KEY, handler)).toBe(true);
  });
});

describe('PluginsController.install() OpenAPI responses', () => {
  it('declares the 413 the upload size limit returns', () => {
    const responses = Reflect.getMetadata(
      'swagger/apiResponse',
      Object.getOwnPropertyDescriptor(PluginsController.prototype, 'install')!.value as object,
    ) as Record<string, unknown>;
    expect(Object.keys(responses)).toContain('413');
  });

  // The global ValidationPipe refuses an invalid body, or one carrying an undeclared field, with a 400.
  it.each(['updateConfig', 'updateSessionConfig', 'updateSessions'] as const)(
    'declares the validation 400 on %s',
    handler => {
      const responses = (Reflect.getMetadata(
        'swagger/apiResponse',
        Object.getOwnPropertyDescriptor(PluginsController.prototype, handler)!.value as object,
      ) ?? {}) as Record<string, { description?: string }>;
      expect(responses['400']?.description).toMatch(/validation/i);
    },
  );
});