mirror of
https://gitlab.com/mobicoop/v3/service/auth.git
synced 2026-03-24 14:45:48 +00:00
authorization module
This commit is contained in:
72
src/modules/health/tests/unit/health.grpc.controller.spec.ts
Normal file
72
src/modules/health/tests/unit/health.grpc.controller.spec.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/application/usecases/repositories.health-indicator.usecase';
|
||||
import {
|
||||
HealthGrpcController,
|
||||
ServingStatus,
|
||||
} from '@modules/health/interface/grpc-controllers/health.grpc.controller';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
|
||||
const mockRepositoriesHealthIndicatorUseCase = {
|
||||
isHealthy: jest
|
||||
.fn()
|
||||
.mockImplementationOnce(() => ({
|
||||
repositories: {
|
||||
status: 'up',
|
||||
},
|
||||
}))
|
||||
.mockImplementationOnce(() => ({
|
||||
repositories: {
|
||||
status: 'down',
|
||||
},
|
||||
})),
|
||||
};
|
||||
|
||||
describe('Health Grpc Controller', () => {
|
||||
let healthGrpcController: HealthGrpcController;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
{
|
||||
provide: RepositoriesHealthIndicatorUseCase,
|
||||
useValue: mockRepositoriesHealthIndicatorUseCase,
|
||||
},
|
||||
HealthGrpcController,
|
||||
],
|
||||
}).compile();
|
||||
|
||||
healthGrpcController =
|
||||
module.get<HealthGrpcController>(HealthGrpcController);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(healthGrpcController).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return a Serving status ', async () => {
|
||||
jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy');
|
||||
const servingStatus: { status: ServingStatus } =
|
||||
await healthGrpcController.check();
|
||||
expect(servingStatus).toEqual({
|
||||
status: ServingStatus.SERVING,
|
||||
});
|
||||
expect(
|
||||
mockRepositoriesHealthIndicatorUseCase.isHealthy,
|
||||
).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should return a Not Serving status ', async () => {
|
||||
jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy');
|
||||
const servingStatus: { status: ServingStatus } =
|
||||
await healthGrpcController.check();
|
||||
expect(servingStatus).toEqual({
|
||||
status: ServingStatus.NOT_SERVING,
|
||||
});
|
||||
expect(
|
||||
mockRepositoriesHealthIndicatorUseCase.isHealthy,
|
||||
).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
90
src/modules/health/tests/unit/health.http.controller.spec.ts
Normal file
90
src/modules/health/tests/unit/health.http.controller.spec.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/application/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>(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);
|
||||
});
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { Messager } from '../../adapters/secondaries/messager';
|
||||
|
||||
const mockAmqpConnection = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
const mockConfigService = {
|
||||
get: jest.fn().mockResolvedValue({
|
||||
RMQ_EXCHANGE: 'mobicoop',
|
||||
}),
|
||||
};
|
||||
|
||||
describe('Messager', () => {
|
||||
let messager: Messager;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
Messager,
|
||||
{
|
||||
provide: AmqpConnection,
|
||||
useValue: mockAmqpConnection,
|
||||
},
|
||||
{
|
||||
provide: ConfigService,
|
||||
useValue: mockConfigService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
messager = module.get<Messager>(Messager);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(messager).toBeDefined();
|
||||
});
|
||||
|
||||
it('should publish a message', async () => {
|
||||
jest.spyOn(mockAmqpConnection, 'publish');
|
||||
messager.publish('test.create.info', 'my-test');
|
||||
expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -1,58 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PrismaHealthIndicatorUseCase } from '../../domain/usecases/prisma.health-indicator.usecase';
|
||||
import { AuthenticationRepository } from '../../../oldauthentication/adapters/secondaries/authentication.repository';
|
||||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
|
||||
import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus';
|
||||
|
||||
const mockAuthenticationRepository = {
|
||||
healthCheck: jest
|
||||
.fn()
|
||||
.mockImplementationOnce(() => {
|
||||
return Promise.resolve(true);
|
||||
})
|
||||
.mockImplementation(() => {
|
||||
throw new PrismaClientKnownRequestError('Service unavailable', {
|
||||
code: 'code',
|
||||
clientVersion: 'version',
|
||||
});
|
||||
}),
|
||||
};
|
||||
|
||||
describe('PrismaHealthIndicatorUseCase', () => {
|
||||
let prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
{
|
||||
provide: AuthenticationRepository,
|
||||
useValue: mockAuthenticationRepository,
|
||||
},
|
||||
PrismaHealthIndicatorUseCase,
|
||||
],
|
||||
}).compile();
|
||||
|
||||
prismaHealthIndicatorUseCase = module.get<PrismaHealthIndicatorUseCase>(
|
||||
PrismaHealthIndicatorUseCase,
|
||||
);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(prismaHealthIndicatorUseCase).toBeDefined();
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
it('should check health successfully', async () => {
|
||||
const healthIndicatorResult: HealthIndicatorResult =
|
||||
await prismaHealthIndicatorUseCase.isHealthy('prisma');
|
||||
|
||||
expect(healthIndicatorResult['prisma'].status).toBe('up');
|
||||
});
|
||||
|
||||
it('should throw an error if database is unavailable', async () => {
|
||||
await expect(
|
||||
prismaHealthIndicatorUseCase.isHealthy('prisma'),
|
||||
).rejects.toBeInstanceOf(HealthCheckError);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus';
|
||||
import { RepositoriesHealthIndicatorUseCase } from '../../core/application/usecases/repositories.health-indicator.usecase';
|
||||
import { MESSAGE_PUBLISHER } from '@src/app.di-tokens';
|
||||
import { DatabaseErrorException } from '@mobicoop/ddd-library';
|
||||
import {
|
||||
AUTHENTICATION_REPOSITORY,
|
||||
USERNAME_REPOSITORY,
|
||||
} from '@modules/health/health.di-tokens';
|
||||
|
||||
const mockAuthenticationRepository = {
|
||||
healthCheck: jest
|
||||
.fn()
|
||||
.mockImplementationOnce(() => {
|
||||
return Promise.resolve(true);
|
||||
})
|
||||
.mockImplementation(() => {
|
||||
throw new DatabaseErrorException('An error occured in the database');
|
||||
}),
|
||||
};
|
||||
|
||||
const mockUsernameRepository = {
|
||||
healthCheck: jest
|
||||
.fn()
|
||||
.mockImplementationOnce(() => {
|
||||
return Promise.resolve(true);
|
||||
})
|
||||
.mockImplementation(() => {
|
||||
throw new DatabaseErrorException('An error occured in the database');
|
||||
}),
|
||||
};
|
||||
|
||||
const mockMessagePublisher = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
describe('RepositoriesHealthIndicatorUseCase', () => {
|
||||
let repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
RepositoriesHealthIndicatorUseCase,
|
||||
{
|
||||
provide: AUTHENTICATION_REPOSITORY,
|
||||
useValue: mockAuthenticationRepository,
|
||||
},
|
||||
{
|
||||
provide: USERNAME_REPOSITORY,
|
||||
useValue: mockUsernameRepository,
|
||||
},
|
||||
{
|
||||
provide: MESSAGE_PUBLISHER,
|
||||
useValue: mockMessagePublisher,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
repositoriesHealthIndicatorUseCase =
|
||||
module.get<RepositoriesHealthIndicatorUseCase>(
|
||||
RepositoriesHealthIndicatorUseCase,
|
||||
);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(repositoriesHealthIndicatorUseCase).toBeDefined();
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
it('should check health successfully', async () => {
|
||||
const healthIndicatorResult: HealthIndicatorResult =
|
||||
await repositoriesHealthIndicatorUseCase.isHealthy('repositories');
|
||||
expect(healthIndicatorResult['repositories'].status).toBe('up');
|
||||
});
|
||||
|
||||
it('should throw an error if database is unavailable', async () => {
|
||||
jest.spyOn(mockMessagePublisher, 'publish');
|
||||
await expect(
|
||||
repositoriesHealthIndicatorUseCase.isHealthy('repositories'),
|
||||
).rejects.toBeInstanceOf(HealthCheckError);
|
||||
expect(mockMessagePublisher.publish).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user