Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(groups): validate group DTOs so participant endpoints stop returning 400 (#210)
Yudhi Armyndharis5a49c6e433e7
diff --git a/src/modules/group/dto/group.dto.spec.ts b/src/modules/group/dto/group.dto.spec.tsnew file mode 100644index 000000000..67cf5be2b--- /dev/null+++ b/src/modules/group/dto/group.dto.spec.ts@@ -0,0 +1,43 @@+import 'reflect-metadata';+import { validate, ValidationError } from 'class-validator';+import { plainToInstance } from 'class-transformer';+import { ParticipantsDto, CreateGroupDto, GroupSubjectDto } from './group.dto';++// Mirror the global ValidationPipe options (src/main.ts): whitelist + forbidNonWhitelisted.+const PIPE_OPTS = { whitelist: true, forbidNonWhitelisted: true };++function errorsFor<T extends object>(cls: new () => T, payload: unknown): Promise<ValidationError[]> {+ return validate(plainToInstance(cls, payload as object), PIPE_OPTS);+}++describe('group DTO validation', () => {+ it('accepts a valid participants body (regression for #190)', async () => {+ const errors = await errorsFor(ParticipantsDto, { participants: ['[email protected]'] });+ expect(errors).toHaveLength(0);+ });++ it('rejects an empty participants array', async () => {+ const errors = await errorsFor(ParticipantsDto, { participants: [] });+ expect(errors.length).toBeGreaterThan(0);+ });++ it('rejects a non-array participants value', async () => {+ const errors = await errorsFor(ParticipantsDto, { participants: 'not-an-array' });+ expect(errors.length).toBeGreaterThan(0);+ });++ it('still rejects unknown properties (forbidNonWhitelisted intact)', async () => {+ const errors = await errorsFor(ParticipantsDto, { participants: ['[email protected]'], hacker: true });+ expect(errors.some(e => e.property === 'hacker')).toBe(true);+ });++ it('requires both name and participants on CreateGroupDto', async () => {+ const errors = await errorsFor(CreateGroupDto, {});+ expect(errors.map(e => e.property)).toEqual(expect.arrayContaining(['name', 'participants']));+ });++ it('requires a non-empty subject on GroupSubjectDto', async () => {+ const errors = await errorsFor(GroupSubjectDto, { subject: '' });+ expect(errors.length).toBeGreaterThan(0);+ });+});diff --git a/src/modules/group/dto/group.dto.ts b/src/modules/group/dto/group.dto.tsnew file mode 100644index 000000000..c15a677d1--- /dev/null+++ b/src/modules/group/dto/group.dto.ts@@ -0,0 +1,36 @@+import { ApiProperty } from '@nestjs/swagger';+import { IsArray, ArrayNotEmpty, IsString, IsNotEmpty } from 'class-validator';++export class CreateGroupDto {+ @ApiProperty({ description: 'Group subject/name' })+ @IsString()+ @IsNotEmpty()+ name: string;++ @ApiProperty({ description: 'Participant WhatsApp IDs (e.g. [email protected])', type: [String] })+ @IsArray()+ @ArrayNotEmpty()+ @IsString({ each: true })+ participants: string[];+}++export class ParticipantsDto {+ @ApiProperty({ description: 'Participant WhatsApp IDs (e.g. [email protected])', type: [String] })+ @IsArray()+ @ArrayNotEmpty()+ @IsString({ each: true })+ participants: string[];+}++export class GroupSubjectDto {+ @ApiProperty({ description: 'New group subject/name' })+ @IsString()+ @IsNotEmpty()+ subject: string;+}++export class GroupDescriptionDto {+ @ApiProperty({ description: 'New group description (may be empty to clear it)' })+ @IsString()+ description: string;+}diff --git a/src/modules/group/group.controller.ts b/src/modules/group/group.controller.tsindex 3ece1f11a..5ed99ce84 100644--- a/src/modules/group/group.controller.ts+++ b/src/modules/group/group.controller.ts@@ -13,24 +13,7 @@ import {} from '@nestjs/common';import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';import { SessionService } from '../session/session.service';--// DTOs-class CreateGroupDto {- name: string;- participants: string[];-}--class ParticipantsDto {- participants: string[];-}--class GroupSubjectDto {- subject: string;-}--class GroupDescriptionDto {- description: string;-}+import { CreateGroupDto, ParticipantsDto, GroupSubjectDto, GroupDescriptionDto } from './dto/group.dto';@ApiTags('groups')@Controller('sessions/:sessionId/groups')