delete username usecase

This commit is contained in:
sbriat
2023-07-07 11:11:36 +02:00
parent a95caefaf2
commit 28c6ca0f63
12 changed files with 274 additions and 1 deletions

View File

@@ -56,7 +56,7 @@ describe('Delete Authentication Grpc Controller', () => {
expect(deleteAuthenticationGrpcController).toBeDefined();
});
it('should create a new authentication', async () => {
it('should delete an authentication', async () => {
jest.spyOn(mockCommandBus, 'execute');
await deleteAuthenticationGrpcController.delete(
deleteAuthenticationRequest,

View File

@@ -0,0 +1,99 @@
import {
DatabaseErrorException,
NotFoundException,
} from '@mobicoop/ddd-library';
import { RpcExceptionCode } from '@mobicoop/ddd-library';
import { DeleteUsernameGrpcController } from '@modules/authentication/interface/grpc-controllers/delete-username.grpc.controller';
import { DeleteUsernameRequestDto } from '@modules/authentication/interface/grpc-controllers/dtos/delete-username.request.dto';
import { CommandBus } from '@nestjs/cqrs';
import { RpcException } from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing';
const deleteUsernameRequest: DeleteUsernameRequestDto = {
name: 'john.doe@email.com',
};
const mockCommandBus = {
execute: jest
.fn()
.mockImplementationOnce(() => ({}))
.mockImplementationOnce(() => {
throw new NotFoundException();
})
.mockImplementationOnce(() => {
throw new DatabaseErrorException();
})
.mockImplementationOnce(() => {
throw new Error();
}),
};
describe('Delete Username Grpc Controller', () => {
let deleteUsernameGrpcController: DeleteUsernameGrpcController;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: CommandBus,
useValue: mockCommandBus,
},
DeleteUsernameGrpcController,
],
}).compile();
deleteUsernameGrpcController = module.get<DeleteUsernameGrpcController>(
DeleteUsernameGrpcController,
);
});
afterEach(async () => {
jest.clearAllMocks();
});
it('should be defined', () => {
expect(deleteUsernameGrpcController).toBeDefined();
});
it('should delete a username', async () => {
jest.spyOn(mockCommandBus, 'execute');
await deleteUsernameGrpcController.delete(deleteUsernameRequest);
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
it('should throw a dedicated RpcException if username does not exist', async () => {
jest.spyOn(mockCommandBus, 'execute');
expect.assertions(3);
try {
await deleteUsernameGrpcController.delete(deleteUsernameRequest);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.NOT_FOUND);
}
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
it('should throw a dedicated RpcException if a database error occurs', async () => {
jest.spyOn(mockCommandBus, 'execute');
expect.assertions(3);
try {
await deleteUsernameGrpcController.delete(deleteUsernameRequest);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.INTERNAL);
}
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
it('should throw a generic RpcException', async () => {
jest.spyOn(mockCommandBus, 'execute');
expect.assertions(3);
try {
await deleteUsernameGrpcController.delete(deleteUsernameRequest);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
}
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
});