update username

This commit is contained in:
sbriat
2023-07-07 17:50:49 +02:00
parent 28c6ca0f63
commit 805a7fe24d
14 changed files with 372 additions and 3 deletions

View File

@@ -0,0 +1,88 @@
import { IdResponse } from '@mobicoop/ddd-library';
import { RpcExceptionCode } from '@mobicoop/ddd-library';
import { UsernameAlreadyExistsException } from '@modules/authentication/core/domain/authentication.errors';
import { Type } from '@modules/authentication/core/domain/username.types';
import { UpdateUsernameRequestDto } from '@modules/authentication/interface/grpc-controllers/dtos/update-username.request.dto';
import { UpdateUsernameGrpcController } from '@modules/authentication/interface/grpc-controllers/update-username.grpc.controller';
import { CommandBus } from '@nestjs/cqrs';
import { RpcException } from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing';
const updateUsernameRequest: UpdateUsernameRequestDto = {
userId: '78153e03-4861-4f58-a705-88526efee53b',
name: 'new-john.doe@email.com',
type: Type.EMAIL,
};
const mockCommandBus = {
execute: jest
.fn()
.mockImplementationOnce(() => 'new-john.doe@email.com')
.mockImplementationOnce(() => {
throw new UsernameAlreadyExistsException();
})
.mockImplementationOnce(() => {
throw new Error();
}),
};
describe('Update Username Grpc Controller', () => {
let updateUsernameGrpcController: UpdateUsernameGrpcController;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: CommandBus,
useValue: mockCommandBus,
},
UpdateUsernameGrpcController,
],
}).compile();
updateUsernameGrpcController = module.get<UpdateUsernameGrpcController>(
UpdateUsernameGrpcController,
);
});
afterEach(async () => {
jest.clearAllMocks();
});
it('should be defined', () => {
expect(updateUsernameGrpcController).toBeDefined();
});
it('should update a username', async () => {
jest.spyOn(mockCommandBus, 'execute');
const result: IdResponse =
await updateUsernameGrpcController.updateUsername(updateUsernameRequest);
expect(result).toBeInstanceOf(IdResponse);
expect(result.id).toBe('new-john.doe@email.com');
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
it('should throw a dedicated RpcException if username already exists', async () => {
jest.spyOn(mockCommandBus, 'execute');
expect.assertions(3);
try {
await updateUsernameGrpcController.updateUsername(updateUsernameRequest);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.ALREADY_EXISTS);
}
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
it('should throw a generic RpcException', async () => {
jest.spyOn(mockCommandBus, 'execute');
expect.assertions(3);
try {
await updateUsernameGrpcController.updateUsername(updateUsernameRequest);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
}
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
});