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