Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(group): pin the media body rules of picture setters

Yudhi Armyndharisf3cc69d94cfd
diff --git a/src/modules/group/group.service.spec.ts b/src/modules/group/group.service.spec.ts
index 98ea71874..f9db33531 100644
--- a/src/modules/group/group.service.spec.ts
+++ b/src/modules/group/group.service.spec.ts
@@ -1,4 +1,4 @@
-import { BadRequestException, HttpException, NotFoundException } from '@nestjs/common';
+import { BadRequestException, HttpException, NotFoundException, PayloadTooLargeException } from '@nestjs/common';
import { GroupService } from './group.service';
import { EngineRegistry } from '../../engine/engine-registry.service';
import { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';
@@ -78,6 +78,59 @@ describe('GroupService', () => {
});
});
+ // The service is the only enforcer of these body rules: SetGroupPictureDto.mimetype is optional, and a
+ // plugin send or a hook rewrite reaches the service without the DTO's url check.
+ describe('setGroupPicture checks its media body', () => {
+ const GROUP = '[email protected]';
+ const origCap = process.env.MEDIA_DOWNLOAD_MAX_BYTES;
+ afterEach(() => {
+ if (origCap === undefined) delete process.env.MEDIA_DOWNLOAD_MAX_BYTES;
+ else process.env.MEDIA_DOWNLOAD_MAX_BYTES = origCap;
+ });
+
+ it.each([
+ ['neither url nor base64', {}, 'Either url or base64 must be provided'],
+ ['base64 without a mimetype', { base64: 'QUJD' }, 'mimetype is required when using base64 data'],
+ [
+ 'a url that is not an absolute http(s) URL',
+ { url: 'ftp://example.com/g.jpg' },
+ 'url must be an absolute http(s) URL',
+ ],
+ ])('refuses %s with 400 and never reaches the engine', (_label, dto, message) => {
+ const setGroupPicture = jest.fn();
+ const svc = makeService({ setGroupPicture });
+
+ expect(() => svc.setGroupPicture('s1', GROUP, dto)).toThrow(BadRequestException);
+ expect(() => svc.setGroupPicture('s1', GROUP, dto)).toThrow(message);
+ expect(setGroupPicture).not.toHaveBeenCalled();
+ });
+
+ it('refuses base64 over MEDIA_DOWNLOAD_MAX_BYTES with 413', () => {
+ process.env.MEDIA_DOWNLOAD_MAX_BYTES = '1024';
+ const setGroupPicture = jest.fn();
+ const dto = { base64: Buffer.alloc(1025).toString('base64'), mimetype: 'image/jpeg' };
+
+ expect(() => makeService({ setGroupPicture }).setGroupPicture('s1', GROUP, dto)).toThrow(
+ PayloadTooLargeException,
+ );
+ expect(setGroupPicture).not.toHaveBeenCalled();
+ });
+
+ it('types a url body image/jpeg and keeps the mimetype a base64 body names', async () => {
+ const setGroupPicture = jest.fn().mockResolvedValue(undefined);
+ const svc = makeService({ setGroupPicture });
+
+ await svc.setGroupPicture('s1', GROUP, { url: 'https://example.com/g.jpg' });
+ await svc.setGroupPicture('s1', GROUP, { base64: 'QUJD', mimetype: 'image/png' });
+
+ expect(setGroupPicture).toHaveBeenNthCalledWith(1, GROUP, {
+ mimetype: 'image/jpeg',
+ data: 'https://example.com/g.jpg',
+ });
+ expect(setGroupPicture).toHaveBeenNthCalledWith(2, GROUP, { mimetype: 'image/png', data: 'QUJD' });
+ });
+ });
+
it('throws 400 "Session is not started" when the engine is missing (guard preserved)', () => {
// The guard throws synchronously; the controller methods are `async`, so this still surfaces
// as a rejected promise → 400 at the HTTP layer.
diff --git a/src/modules/profile/profile.service.spec.ts b/src/modules/profile/profile.service.spec.ts
index 2cb55acd2..bfc28f3f1 100644
--- a/src/modules/profile/profile.service.spec.ts
+++ b/src/modules/profile/profile.service.spec.ts
@@ -56,6 +56,16 @@ describe('ProfileService', () => {
);
});
+ it('rejects a url that is not an absolute http(s) URL (400), also when it reaches the service without the DTO', () => {
+ const setProfilePicture = jest.fn();
+ const svc = makeService({ setProfilePicture });
+ for (const url of ['ftp://example.com/a.jpg', 'example.com/a.jpg']) {
+ expect(() => svc.setProfilePicture('s1', { url })).toThrow(BadRequestException);
+ expect(() => svc.setProfilePicture('s1', { url })).toThrow('url must be an absolute http(s) URL');
+ }
+ expect(setProfilePicture).not.toHaveBeenCalled();
+ });
+
it('maps a base64 body to a MediaInput (base64 wins over a stale url, #670)', async () => {
const setProfilePicture = jest.fn().mockResolvedValue(undefined);
await makeService({ setProfilePicture }).setProfilePicture('s1', {