Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(message): pin the chat id cap of bulk items and audio sends

Yudhi Armyndharis55bb877f16f4
diff --git a/src/modules/message/dto/id-length.dto.spec.ts b/src/modules/message/dto/id-length.dto.spec.ts
index 10df02464..b91824394 100644
--- a/src/modules/message/dto/id-length.dto.spec.ts
+++ b/src/modules/message/dto/id-length.dto.spec.ts
@@ -1,6 +1,6 @@
import { DECORATORS } from '@nestjs/swagger';
import { plainToInstance } from 'class-transformer';
-import { validate } from 'class-validator';
+import { validate, type ValidationError } from 'class-validator';
import {
ClickButtonDto,
DeleteMessageDto,
@@ -17,7 +17,13 @@ import {
VotePollDto,
} from './message-actions.dto';
import { SendBulkMessageDto } from './bulk-message.dto';
-import { BATCH_ID_MAX_LENGTH, CHAT_ID_MAX_LENGTH, SendMediaMessageDto, SendTextMessageDto } from './send-message.dto';
+import {
+ BATCH_ID_MAX_LENGTH,
+ CHAT_ID_MAX_LENGTH,
+ SendAudioMessageDto,
+ SendMediaMessageDto,
+ SendTextMessageDto,
+} from './send-message.dto';
import { SendTemplateMessageDto } from './send-template.dto';
// A chat id and a batch id are stored under btree indexes, and PostgreSQL refuses an index entry past
@@ -26,6 +32,7 @@ import { SendTemplateMessageDto } from './send-template.dto';
const fields: Array<[string, new () => object, string, number]> = [
['SendTextMessageDto', SendTextMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],
['SendMediaMessageDto', SendMediaMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],
+ ['SendAudioMessageDto', SendAudioMessageDto, 'chatId', CHAT_ID_MAX_LENGTH],
['SendLocationDto', SendLocationDto, 'chatId', CHAT_ID_MAX_LENGTH],
['SendContactDto', SendContactDto, 'chatId', CHAT_ID_MAX_LENGTH],
['SendPollDto', SendPollDto, 'chatId', CHAT_ID_MAX_LENGTH],
@@ -58,6 +65,23 @@ describe('caller-chosen id length caps', () => {
);
});
+ // The bulk item class is not exported, so it is reached through the body that holds the list.
+ it('caps the chat id of every bulk item, where most ids arrive', async () => {
+ const flatten = (errors: ValidationError[], prefix = ''): Array<[string, ValidationError['constraints']]> =>
+ errors.flatMap(error => [
+ [prefix + error.property, error.constraints],
+ ...flatten(error.children ?? [], `${prefix}${error.property}.`),
+ ]);
+ const refused = async (chatId: string): Promise<boolean> => {
+ const body = plainToInstance(SendBulkMessageDto, { messages: [{ chatId }] }, { enableImplicitConversion: true });
+ const found = flatten(await validate(body)).find(([path]) => path === 'messages.0.chatId');
+ return found?.[1]?.maxLength !== undefined;
+ };
+
+ expect(await refused('a'.repeat(CHAT_ID_MAX_LENGTH))).toBe(false);
+ expect(await refused('a'.repeat(CHAT_ID_MAX_LENGTH + 1))).toBe(true);
+ });
+
it.each(fields)('%s.%s publishes the cap in the schema', (_name, dto, field, max) => {
const published = Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, dto.prototype as object, field) as
{ maxLength?: number } | undefined;