mirror of
https://gitlab.com/mobicoop/v3/service/auth.git
synced 2026-03-14 18:15:49 +00:00
user updated message handler
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import { UserUpdatedMessageHandler } from '@modules/authentication/interface/message-handlers/user-updated.message-handler';
|
||||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
|
||||
const userEmailUpdatedMessage =
|
||||
'{"userId":"2436d413-b7c7-429e-9792-b78edc17b3ca","email":"new-john.doe@email.com"}';
|
||||
|
||||
const userPhoneUpdatedMessage =
|
||||
'{"userId":"2436d413-b7c7-429e-9792-b78edc17b3ca","phone":"+33611224455"}';
|
||||
|
||||
const userBirthDateUpdatedMessage =
|
||||
'{"userId":"2436d413-b7c7-429e-9792-b78edc17b3ca","birthDate":"1976-10-23"}';
|
||||
|
||||
const userIdNotProvidedUpdatedMessage =
|
||||
'{"user":"2436d413-b7c7-429e-9792-b78edc17b300","email":"new-john.doe@email.com"}';
|
||||
|
||||
const mockCommandBus = {
|
||||
execute: jest
|
||||
.fn()
|
||||
.mockImplementationOnce(() => 'new-john.doe@email.com')
|
||||
.mockImplementationOnce(() => '+33611224455'),
|
||||
};
|
||||
|
||||
describe('User Updated Message Handler', () => {
|
||||
let userUpdatedMessageHandler: UserUpdatedMessageHandler;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
{
|
||||
provide: CommandBus,
|
||||
useValue: mockCommandBus,
|
||||
},
|
||||
UserUpdatedMessageHandler,
|
||||
],
|
||||
}).compile();
|
||||
|
||||
userUpdatedMessageHandler = module.get<UserUpdatedMessageHandler>(
|
||||
UserUpdatedMessageHandler,
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(userUpdatedMessageHandler).toBeDefined();
|
||||
});
|
||||
|
||||
it('should update an email username', async () => {
|
||||
jest.spyOn(mockCommandBus, 'execute');
|
||||
await userUpdatedMessageHandler.userUpdated(userEmailUpdatedMessage);
|
||||
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should update a phone username', async () => {
|
||||
jest.spyOn(mockCommandBus, 'execute');
|
||||
await userUpdatedMessageHandler.userUpdated(userPhoneUpdatedMessage);
|
||||
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should not update a username if message does not contain email nor phone', async () => {
|
||||
jest.spyOn(mockCommandBus, 'execute');
|
||||
await userUpdatedMessageHandler.userUpdated(userBirthDateUpdatedMessage);
|
||||
expect(mockCommandBus.execute).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('should not update a username if userId is unknown', async () => {
|
||||
jest.spyOn(mockCommandBus, 'execute');
|
||||
await userUpdatedMessageHandler.userUpdated(
|
||||
userIdNotProvidedUpdatedMessage,
|
||||
);
|
||||
expect(mockCommandBus.execute).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
// 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);
|
||||
// });
|
||||
});
|
||||
Reference in New Issue
Block a user