mirror of
https://gitlab.com/mobicoop/v3/service/auth.git
synced 2026-03-14 18:15:49 +00:00
validate authentication use case
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user