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

@ -1,6 +1,7 @@
import { AuthenticationEntity } from '@modules/authentication/core/domain/authentication.entity'; import { AuthenticationEntity } from '@modules/authentication/core/domain/authentication.entity';
import { CreateAuthenticationProps } from '@modules/authentication/core/domain/authentication.types'; import { CreateAuthenticationProps } from '@modules/authentication/core/domain/authentication.types';
import { AuthenticationDeletedDomainEvent } from '@modules/authentication/core/domain/events/authentication-deleted.domain-event'; import { AuthenticationDeletedDomainEvent } from '@modules/authentication/core/domain/events/authentication-deleted.domain-event';
import { PasswordUpdatedDomainEvent } from '@modules/authentication/core/domain/events/password-updated.domain-event';
import { Type } from '@modules/authentication/core/domain/username.types'; import { Type } from '@modules/authentication/core/domain/username.types';
const createAuthenticationProps: CreateAuthenticationProps = { const createAuthenticationProps: CreateAuthenticationProps = {
@ -62,3 +63,16 @@ describe('Authentication entity delete', () => {
); );
}); });
}); });
describe('Authentication password update', () => {
it('should update the password of an authentication entity', async () => {
const authenticationEntity: AuthenticationEntity =
await AuthenticationEntity.create(createAuthenticationProps);
const oldPassword: string = authenticationEntity.getProps().password;
await authenticationEntity.updatePassword('@Br@ndN3wP@$$w0rd');
expect(authenticationEntity.domainEvents.length).toBe(2);
expect(authenticationEntity.domainEvents[1]).toBeInstanceOf(
PasswordUpdatedDomainEvent,
);
expect(authenticationEntity.getProps().password).not.toBe(oldPassword);
});
});

View File

@ -8,7 +8,7 @@ import { UpdatePasswordCommand } from '@modules/authentication/core/application/
const updatePasswordRequest: UpdatePasswordRequestDto = { const updatePasswordRequest: UpdatePasswordRequestDto = {
userId: '165192d4-398a-4469-a16b-98c02cc6f531', userId: '165192d4-398a-4469-a16b-98c02cc6f531',
password: '@Br@ndN3wPa$$w0rd', password: '@Br@ndN3wP@$$w0rd',
}; };
const mockAuthenticationRepository = { const mockAuthenticationRepository = {

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);
});
});