Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(label): pin null handling and the DTO bounds of label edits
Yudhi Armyndharis5c2f59781583
diff --git a/src/modules/label/dto/upsert-label.dto.spec.ts b/src/modules/label/dto/upsert-label.dto.spec.tsnew file mode 100644index 000000000..adc962e3b--- /dev/null+++ b/src/modules/label/dto/upsert-label.dto.spec.ts@@ -0,0 +1,48 @@+import { plainToInstance } from 'class-transformer';+import { validate } from 'class-validator';+import { UpsertLabelDto } from './upsert-label.dto';+import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';++const published = (name: string) =>+ Reflect.getMetadata('swagger/apiModelProperties', UpsertLabelDto.prototype, name) as {+ minLength?: number;+ maxLength?: number;+ minimum?: number;+ maximum?: number;+ };++// The options the global ValidationPipe runs with, so a body is judged the way a request is.+const { transformOptions, ...pipeOptions } = GLOBAL_VALIDATION_OPTIONS;++/** The constraints that failed, keyed by property; empty when the body is valid. */+const failures = async (body: object): Promise<Record<string, string[]>> => {+ const errors = await validate(plainToInstance(UpsertLabelDto, body, transformOptions), pipeOptions);+ return Object.fromEntries(errors.map(e => [e.property, Object.keys(e.constraints ?? {})]));+};++// Each case judges a value at the bound and one past it with the real validator and reads the published+// value beside it, so the contract and the validator cannot drift apart unnoticed.+describe('UpsertLabelDto', () => {+ it('keeps the name between 1 and 100 characters, as the contract publishes', async () => {+ expect(published('name')).toMatchObject({ minLength: 1, maxLength: 100 });+ expect(await failures({ name: 'x' })).toEqual({});+ expect(await failures({ name: 'x'.repeat(100) })).toEqual({});+ expect(await failures({ name: 'x'.repeat(101) })).toEqual({ name: ['maxLength'] });+ expect(await failures({ name: '' })).toEqual({ name: ['minLength'] });+ });++ it('keeps the colour inside the 0-19 palette index, as the contract publishes, and refuses a blank one', async () => {+ expect(published('color')).toMatchObject({ minimum: 0, maximum: 19 });+ expect(await failures({ color: 0 })).toEqual({});+ expect(await failures({ color: 19 })).toEqual({});+ expect(await failures({ color: 20 })).toEqual({ color: ['max'] });+ expect(await failures({ color: -1 })).toEqual({ color: ['min'] });+ // Number('') is 0, a real colour, so a blank field must be refused instead of silently recolouring.+ expect(await failures({ color: '' })).toHaveProperty('color');+ });++ it('lets both fields be omitted or null; whether that is enough to write is a service rule', async () => {+ expect(await failures({})).toEqual({});+ expect(await failures({ name: null, color: null })).toEqual({});+ });+});diff --git a/src/modules/label/label.service.spec.ts b/src/modules/label/label.service.spec.tsindex 959b68e19..069b952f3 100644--- a/src/modules/label/label.service.spec.ts+++ b/src/modules/label/label.service.spec.ts@@ -110,11 +110,43 @@ describe('LabelService label editing', () => {// Omitted fields must stay omitted rather than becoming undefined-valued keys. The service sends// only the fields the caller set, and the adapter forwards them unchanged; the write replaces the- // whole label.+ // whole label. Compared with toStrictEqual: toHaveBeenCalledWith treats `name: undefined` as absent,+ // which is the very shape this test exists to refuse.it('passes through only the fields the caller set', async () => {const upsertLabel = jest.fn().mockResolvedValue(undefined);- await makeService({ upsertLabel }).upsertLabel('s1', 'l1', { color: 3 });- expect(upsertLabel).toHaveBeenCalledWith({ id: 'l1', color: 3 });+ const svc = makeService({ upsertLabel });+ await svc.upsertLabel('s1', 'l1', { color: 3 });+ await svc.upsertLabel('s1', 'l2', { name: 'VIP' });+ expect((upsertLabel.mock.calls as unknown[][]).map(call => call[0])).toStrictEqual([+ { id: 'l1', color: 3 },+ { id: 'l2', name: 'VIP' },+ ]);+ });++ // @IsOptional skips validation for null and class-transformer keeps it, so the DTO lets an explicit null+ // through although the type says `string | undefined`. The service reads it as "not set".+ type Body = { name?: string; color?: number };++ it('refuses a body whose only fields are null, as it refuses an empty one', () => {+ const upsertLabel = jest.fn();+ const svc = makeService({ upsertLabel });+ for (const body of [{ name: null }, { color: null }, { name: null, color: null }]) {+ expect(() => svc.upsertLabel('s1', 'l1', body as unknown as Body)).toThrow(+ 'At least one of name or color must be provided',+ );+ }+ expect(upsertLabel).not.toHaveBeenCalled();+ });++ it('drops a null field instead of forwarding it to the engine', async () => {+ const upsertLabel = jest.fn().mockResolvedValue(undefined);+ const svc = makeService({ upsertLabel });+ await svc.upsertLabel('s1', 'l1', { name: null, color: 3 } as unknown as Body);+ await svc.upsertLabel('s1', 'l2', { name: 'VIP', color: null } as unknown as Body);+ expect((upsertLabel.mock.calls as unknown[][]).map(call => call[0])).toStrictEqual([+ { id: 'l1', color: 3 },+ { id: 'l2', name: 'VIP' },+ ]);});it('delegates deleteLabel to the engine', async () => {