Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(status): check each malformed recipient JID on its own
Yudhi Armyndharis960c8dd00f6a
diff --git a/src/modules/status/dto/send-text-status.dto.spec.ts b/src/modules/status/dto/send-text-status.dto.spec.tsindex dd0c18a99..17dc1832d 100644--- a/src/modules/status/dto/send-text-status.dto.spec.ts+++ b/src/modules/status/dto/send-text-status.dto.spec.ts@@ -56,10 +56,10 @@ describe('SendTextStatusDto recipients validation', () => {expect(errors).toHaveLength(0);});- it('rejects malformed JIDs', async () => {- const errors = await validate(- plainToInstance(SendTextStatusDto, { text: 'hi', recipients: ['not-a-jid', '[email protected]', '@c.us', 'abc@lid'] }),- );+ // One entry per validate call: any single bad entry fails the whole array, so a combined list+ // would still fail if one of the others (a group JID, say) were wrongly accepted.+ it.each(['not-a-jid', '[email protected]', '@c.us', 'abc@lid'])('rejects the malformed JID %s', async jid => {+ const errors = await validate(plainToInstance(SendTextStatusDto, { text: 'hi', recipients: [jid] }));expect(errors.some(e => e.property === 'recipients')).toBe(true);});diff --git a/src/modules/status/dto/send-voice-status.dto.spec.ts b/src/modules/status/dto/send-voice-status.dto.spec.tsnew file mode 100644index 000000000..1b12928bb--- /dev/null+++ b/src/modules/status/dto/send-voice-status.dto.spec.ts@@ -0,0 +1,38 @@+import 'reflect-metadata';+import { plainToInstance } from 'class-transformer';+import { validate } from 'class-validator';+import { SendVoiceStatusDto } from './send-media-status.dto';++describe('SendVoiceStatusDto validation', () => {+ const audio = { base64: 'QUJD' };+ const errorsFor = async (body: object) => validate(plainToInstance(SendVoiceStatusDto, body));++ it('accepts audio alone', async () => {+ expect(await errorsFor({ audio })).toHaveLength(0);+ });++ it.each([{}, { audio: {} }])('rejects a body without an audio source: %j', async body => {+ expect((await errorsFor(body)).some(e => e.property === 'audio')).toBe(true);+ });++ it('accepts a #RRGGBB background colour', async () => {+ expect(await errorsFor({ audio, backgroundColor: '#25D366' })).toHaveLength(0);+ });++ it.each(['green', '25D366', '#25D36', '#25D3666'])('rejects the background colour %s', async backgroundColor => {+ expect((await errorsFor({ audio, backgroundColor })).some(e => e.property === 'backgroundColor')).toBe(true);+ });++ it('accepts @c.us and @lid recipients', async () => {+ expect(await errorsFor({ audio, recipients: ['[email protected]', '6281@lid'] })).toHaveLength(0);+ });++ it.each(['not-a-jid', '[email protected]', '@c.us', 'abc@lid'])('rejects the malformed JID %s', async jid => {+ expect((await errorsFor({ audio, recipients: [jid] })).some(e => e.property === 'recipients')).toBe(true);+ });++ it('rejects more than 256 recipients', async () => {+ const recipients = Array.from({ length: 257 }, (_, i) => `${i}@c.us`);+ expect((await errorsFor({ audio, recipients })).some(e => e.property === 'recipients')).toBe(true);+ });+});