From 6f2305ae6a33d75a3304f4d048b30cf7ffda16f9 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 14:02:10 +0200 Subject: [PATCH] add health tests; move utils to libs --- package.json | 3 +- .../unit/rpc-validation-pipe.usecase.spec.ts | 0 .../utils/pipes/rpc.validation-pipe.ts | 3 +- .../create-ad.grpc.controller.ts | 2 +- .../find-ad-by-id.grpc.controller.ts | 2 +- .../health.http.controller.ts | 10 ++- .../tests/unit/health.http.controller.spec.ts | 90 +++++++++++++++++++ tsconfig.json | 1 - 8 files changed, 102 insertions(+), 9 deletions(-) rename src/{utils => libs}/tests/unit/rpc-validation-pipe.usecase.spec.ts (100%) rename src/{ => libs}/utils/pipes/rpc.validation-pipe.ts (74%) create mode 100644 src/modules/health/tests/unit/health.http.controller.spec.ts diff --git a/package.json b/package.json index 26bd1fb..bd1ef20 100644 --- a/package.json +++ b/package.json @@ -118,8 +118,7 @@ "moduleNameMapper": { "^@libs(.*)": "/libs/$1", "^@modules(.*)": "/modules/$1", - "^@src(.*)": "$1", - "^@utils(.*)": "utils/$1" + "^@src(.*)": "$1" }, "testEnvironment": "node" } diff --git a/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts b/src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts similarity index 100% rename from src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts rename to src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts diff --git a/src/utils/pipes/rpc.validation-pipe.ts b/src/libs/utils/pipes/rpc.validation-pipe.ts similarity index 74% rename from src/utils/pipes/rpc.validation-pipe.ts rename to src/libs/utils/pipes/rpc.validation-pipe.ts index f2b8c19..75b1852 100644 --- a/src/utils/pipes/rpc.validation-pipe.ts +++ b/src/libs/utils/pipes/rpc.validation-pipe.ts @@ -1,3 +1,4 @@ +import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; import { Injectable, ValidationPipe } from '@nestjs/common'; import { RpcException } from '@nestjs/microservices'; @@ -6,7 +7,7 @@ export class RpcValidationPipe extends ValidationPipe { createExceptionFactory() { return (validationErrors = []) => { return new RpcException({ - code: 3, + code: RpcExceptionCode.INVALID_ARGUMENT, message: this.flattenValidationErrors(validationErrors), }); }; diff --git a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts index 91158b7..6a14291 100644 --- a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts +++ b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts @@ -7,7 +7,7 @@ import { AggregateID } from '@libs/ddd'; import { AdAlreadyExistsException } from '../../core/ad.errors'; import { IdResponse } from '@libs/api/id.response.dto'; import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; -import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe'; +import { RpcValidationPipe } from '@libs/utils/pipes/rpc.validation-pipe'; @UsePipes( new RpcValidationPipe({ diff --git a/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts index f7d5be3..37f937a 100644 --- a/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts +++ b/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts @@ -1,7 +1,6 @@ import { Controller, UsePipes } from '@nestjs/common'; import { QueryBus } from '@nestjs/cqrs'; import { GrpcMethod, RpcException } from '@nestjs/microservices'; -import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe'; import { FindAdByIdRequestDto } from './dtos/find-ad-by-id.request.dto'; import { FindAdByIdQuery } from '@modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query'; import { AdResponseDto } from '../dtos/ad.response.dto'; @@ -9,6 +8,7 @@ import { AdEntity } from '@modules/ad/core/ad.entity'; import { AdMapper } from '@modules/ad/ad.mapper'; import { NotFoundException } from '@libs/exceptions'; import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; +import { RpcValidationPipe } from '@libs/utils/pipes/rpc.validation-pipe'; @UsePipes( new RpcValidationPipe({ diff --git a/src/modules/health/interface/http-controllers/health.http.controller.ts b/src/modules/health/interface/http-controllers/health.http.controller.ts index 0e43fee..fbc9e87 100644 --- a/src/modules/health/interface/http-controllers/health.http.controller.ts +++ b/src/modules/health/interface/http-controllers/health.http.controller.ts @@ -1,6 +1,10 @@ +import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase'; import { Controller, Get } from '@nestjs/common'; -import { HealthCheckService, HealthCheck } from '@nestjs/terminus'; -import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase'; +import { + HealthCheckService, + HealthCheck, + HealthCheckResult, +} from '@nestjs/terminus'; @Controller('health') export class HealthHttpController { @@ -11,7 +15,7 @@ export class HealthHttpController { @Get() @HealthCheck() - async check() { + async check(): Promise { try { return await this.healthCheckService.check([ async () => diff --git a/src/modules/health/tests/unit/health.http.controller.spec.ts b/src/modules/health/tests/unit/health.http.controller.spec.ts new file mode 100644 index 0000000..8982358 --- /dev/null +++ b/src/modules/health/tests/unit/health.http.controller.spec.ts @@ -0,0 +1,90 @@ +import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase'; +import { HealthHttpController } from '@modules/health/interface/http-controllers/health.http.controller'; +import { HealthCheckResult, HealthCheckService } from '@nestjs/terminus'; +import { Test, TestingModule } from '@nestjs/testing'; + +const mockHealthCheckService = { + check: jest + .fn() + .mockImplementationOnce(() => ({ + status: 'ok', + info: { + repositories: { + status: 'up', + }, + }, + error: {}, + details: { + repositories: { + status: 'up', + }, + }, + })) + .mockImplementationOnce(() => ({ + status: 'error', + info: {}, + error: { + repository: + "\nInvalid `prisma.$queryRaw()` invocation:\n\n\nCan't reach database server at `v3-db`:`5432`\n\nPlease make sure your database server is running at `v3-db`:`5432`.", + }, + details: { + repository: + "\nInvalid `prisma.$queryRaw()` invocation:\n\n\nCan't reach database server at `v3-db`:`5432`\n\nPlease make sure your database server is running at `v3-db`:`5432`.", + }, + })), +}; + +const mockRepositoriesHealthIndicatorUseCase = { + isHealthy: jest.fn(), +}; + +describe('Health Http Controller', () => { + let healthHttpController: HealthHttpController; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: HealthCheckService, + useValue: mockHealthCheckService, + }, + { + provide: RepositoriesHealthIndicatorUseCase, + useValue: mockRepositoriesHealthIndicatorUseCase, + }, + HealthHttpController, + ], + }).compile(); + + healthHttpController = + module.get(HealthHttpController); + }); + + afterEach(async () => { + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(healthHttpController).toBeDefined(); + }); + + it('should return an HealthCheckResult with Ok status ', async () => { + jest.spyOn(mockHealthCheckService, 'check'); + jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); + + const healthCheckResult: HealthCheckResult = + await healthHttpController.check(); + expect(healthCheckResult.status).toBe('ok'); + expect(mockHealthCheckService.check).toHaveBeenCalledTimes(1); + }); + + it('should return an HealthCheckResult with Error status ', async () => { + jest.spyOn(mockHealthCheckService, 'check'); + jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); + + const healthCheckResult: HealthCheckResult = + await healthHttpController.check(); + expect(healthCheckResult.status).toBe('error'); + expect(mockHealthCheckService.check).toHaveBeenCalledTimes(1); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index c42aa05..e082b4e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,7 +21,6 @@ "@libs/*": ["src/libs/*"], "@modules/*": ["src/modules/*"], "@ports/*": ["src/ports/*"], - "@utils/*": ["src/utils/*"], "@src/*": ["src/*"], } }