From 0da906959fc6000afed2912f5eb2d2ee9bce3d99 Mon Sep 17 00:00:00 2001 From: Thiago Ramalho Date: Fri, 28 Jun 2024 17:44:03 -0300 Subject: [PATCH 1/3] chore: add validations --- .../cache-crud.controller.e2e-spec.ts | 41 +++++++++++++++++++ .../src/services/cache.service.ts | 3 ++ .../src/reference/reference-id.dto.ts | 2 + 3 files changed, 46 insertions(+) diff --git a/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts b/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts index 9f7883697..c52fac459 100644 --- a/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts +++ b/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts @@ -191,6 +191,47 @@ describe('CacheAssignmentController (e2e)', () => { }); }); + it.only('POST /cache/user null after create', async () => { + const payload: any = { + key: 'dashboard-1', + type: 'filter', + data: '{}', + expiresIn: '1d', + assignee: { id: user.id }, + }; + + await supertest(app.getHttpServer()) + .post('/cache/user') + .send(payload) + .expect(201) + .then((res) => { + expect(res.body.key).toBe(payload.key); + expect(res.body.assignee.id).toBe(user.id); + }); + + payload.data = '{ "name": "John Doe" }'; + payload.expiresIn = null; + payload.assignee = { id: null }; + + await supertest(app.getHttpServer()) + .post('/cache/user') + .send(payload) + .expect(400); + + payload.assignee = { id: ''}; + await supertest(app.getHttpServer()) + .post('/cache/user') + .send(payload) + .expect(500); + + payload.assignee = null; + await supertest(app.getHttpServer()) + .post('/cache/user') + .send(payload) + .expect(400); + + }); + it('POST /cache/user Update', async () => { const payload: CacheCreatableInterface = { key: 'dashboard-1', diff --git a/packages/nestjs-cache/src/services/cache.service.ts b/packages/nestjs-cache/src/services/cache.service.ts index a6d5d810f..b5f6b4c40 100644 --- a/packages/nestjs-cache/src/services/cache.service.ts +++ b/packages/nestjs-cache/src/services/cache.service.ts @@ -270,6 +270,9 @@ export class CacheService implements CacheServiceInterface { const { key, type, assignee } = cache; try { const repo = repoProxy.repository(queryOptions); + if (!key || !type || !assignee || !assignee.id) { + return null; + } const cache = await repo.findOne({ where: { key, diff --git a/packages/nestjs-common/src/reference/reference-id.dto.ts b/packages/nestjs-common/src/reference/reference-id.dto.ts index 3f1161cba..3d7f4d803 100644 --- a/packages/nestjs-common/src/reference/reference-id.dto.ts +++ b/packages/nestjs-common/src/reference/reference-id.dto.ts @@ -1,6 +1,7 @@ import { Exclude, Expose } from 'class-transformer'; import { ApiProperty } from '@nestjs/swagger'; import { ReferenceIdInterface } from '@concepta/ts-core'; +import { IsString } from 'class-validator'; @Exclude() export class ReferenceIdDto implements ReferenceIdInterface { @@ -9,5 +10,6 @@ export class ReferenceIdDto implements ReferenceIdInterface { type: 'string', description: 'Unique identifier', }) + @IsString() id = ''; } From c42e2b078ba62e51aee7fe12862b304a848a4ede Mon Sep 17 00:00:00 2001 From: Thiago Ramalho Date: Fri, 28 Jun 2024 17:45:41 -0300 Subject: [PATCH 2/3] chore: lint --- .../src/controllers/cache-crud.controller.e2e-spec.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts b/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts index c52fac459..0a44574f6 100644 --- a/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts +++ b/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts @@ -217,19 +217,18 @@ describe('CacheAssignmentController (e2e)', () => { .post('/cache/user') .send(payload) .expect(400); - - payload.assignee = { id: ''}; + + payload.assignee = { id: '' }; await supertest(app.getHttpServer()) .post('/cache/user') .send(payload) .expect(500); - + payload.assignee = null; await supertest(app.getHttpServer()) .post('/cache/user') .send(payload) .expect(400); - }); it('POST /cache/user Update', async () => { From c3dc207d3e4f5ac26dcbac48bbafc2578ad1927a Mon Sep 17 00:00:00 2001 From: Thiago Ramalho Date: Fri, 28 Jun 2024 18:11:15 -0300 Subject: [PATCH 3/3] chore: update tests --- .../src/controllers/cache-crud.controller.e2e-spec.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts b/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts index 0a44574f6..44bfa39d4 100644 --- a/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts +++ b/packages/nestjs-cache/src/controllers/cache-crud.controller.e2e-spec.ts @@ -191,8 +191,15 @@ describe('CacheAssignmentController (e2e)', () => { }); }); - it.only('POST /cache/user null after create', async () => { - const payload: any = { + it('POST /cache/user null after create', async () => { + interface ExtendedCacheCreatableInterface + extends Pick< + CacheCreatableInterface, + 'key' | 'expiresIn' | 'type' | 'data' + > { + assignee: { id: string | null } | null; + } + const payload: ExtendedCacheCreatableInterface = { key: 'dashboard-1', type: 'filter', data: '{}',