update password

This commit is contained in:
sbriat
2023-07-11 09:04:40 +02:00
parent de81325750
commit 92ce0cd93a
3 changed files with 85 additions and 1 deletions

View File

@@ -0,0 +1,70 @@
import { IdResponse } from '@mobicoop/ddd-library';
import { RpcExceptionCode } from '@mobicoop/ddd-library';
import { UpdatePasswordRequestDto } from '@modules/authentication/interface/grpc-controllers/dtos/update-password.request.dto';
import { UpdatePasswordGrpcController } from '@modules/authentication/interface/grpc-controllers/update-password.grpc.controller';
import { CommandBus } from '@nestjs/cqrs';
import { RpcException } from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing';
const updatePasswordRequest: UpdatePasswordRequestDto = {
userId: '78153e03-4861-4f58-a705-88526efee53b',
password: '@Br@ndN3wP@$$w0rd',
};
const mockCommandBus = {
execute: jest
.fn()
.mockImplementationOnce(() => '330bd6de-1eb8-450b-8674-0e3c9209f048')
.mockImplementationOnce(() => {
throw new Error();
}),
};
describe('Update Password Grpc Controller', () => {
let updatePasswordGrpcController: UpdatePasswordGrpcController;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: CommandBus,
useValue: mockCommandBus,
},
UpdatePasswordGrpcController,
],
}).compile();
updatePasswordGrpcController = module.get<UpdatePasswordGrpcController>(
UpdatePasswordGrpcController,
);
});
afterEach(async () => {
jest.clearAllMocks();
});
it('should be defined', () => {
expect(updatePasswordGrpcController).toBeDefined();
});
it('should update the password', async () => {
jest.spyOn(mockCommandBus, 'execute');
const result: IdResponse =
await updatePasswordGrpcController.updatePassword(updatePasswordRequest);
expect(result).toBeInstanceOf(IdResponse);
expect(result.id).toBe('330bd6de-1eb8-450b-8674-0e3c9209f048');
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
it('should throw a generic RpcException', async () => {
jest.spyOn(mockCommandBus, 'execute');
expect.assertions(3);
try {
await updatePasswordGrpcController.updatePassword(updatePasswordRequest);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
}
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
});
});