validate authentication use case

This commit is contained in:
sbriat
2023-07-11 11:43:56 +02:00
parent 92ce0cd93a
commit 487ae9c38e
13 changed files with 357 additions and 3 deletions

View File

@@ -0,0 +1,75 @@
import { IdResponse } from '@mobicoop/ddd-library';
import { RpcExceptionCode } from '@mobicoop/ddd-library';
import { ValidateAuthenticationRequestDto } from '@modules/authentication/interface/grpc-controllers/dtos/validate-authentication.request.dto';
import { ValidateAuthenticationGrpcController } from '@modules/authentication/interface/grpc-controllers/validate-authentication.grpc.controller';
import { QueryBus } from '@nestjs/cqrs';
import { RpcException } from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing';
const validateAuthenticationRequest: ValidateAuthenticationRequestDto = {
password: 'John123',
name: 'john.doe@email.com',
};
const mockQueryBus = {
execute: jest
.fn()
.mockImplementationOnce(() => '78153e03-4861-4f58-a705-88526efee53b')
.mockImplementationOnce(() => {
throw new Error();
}),
};
describe('Validate Authentication Grpc Controller', () => {
let validateAuthenticationGrpcController: ValidateAuthenticationGrpcController;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: QueryBus,
useValue: mockQueryBus,
},
ValidateAuthenticationGrpcController,
],
}).compile();
validateAuthenticationGrpcController =
module.get<ValidateAuthenticationGrpcController>(
ValidateAuthenticationGrpcController,
);
});
afterEach(async () => {
jest.clearAllMocks();
});
it('should be defined', () => {
expect(validateAuthenticationGrpcController).toBeDefined();
});
it('should validate an authentication', async () => {
jest.spyOn(mockQueryBus, 'execute');
const result: IdResponse =
await validateAuthenticationGrpcController.validate(
validateAuthenticationRequest,
);
expect(result).toBeInstanceOf(IdResponse);
expect(result.id).toBe('78153e03-4861-4f58-a705-88526efee53b');
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
});
it('should throw a dedicated RpcException if authentication fails', async () => {
jest.spyOn(mockQueryBus, 'execute');
expect.assertions(3);
try {
await validateAuthenticationGrpcController.validate(
validateAuthenticationRequest,
);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.PERMISSION_DENIED);
}
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
});
});