diff --git a/src/modules/authentication/tests/unit/core/authentication.entity.spec.ts b/src/modules/authentication/tests/unit/core/authentication.entity.spec.ts index 9e34a2f..1d5f7dd 100644 --- a/src/modules/authentication/tests/unit/core/authentication.entity.spec.ts +++ b/src/modules/authentication/tests/unit/core/authentication.entity.spec.ts @@ -1,6 +1,7 @@ import { AuthenticationEntity } from '@modules/authentication/core/domain/authentication.entity'; import { CreateAuthenticationProps } from '@modules/authentication/core/domain/authentication.types'; 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'; 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); + }); +}); diff --git a/src/modules/authentication/tests/unit/core/update-password.service.spec.ts b/src/modules/authentication/tests/unit/core/update-password.service.spec.ts index 57ede71..3270f0f 100644 --- a/src/modules/authentication/tests/unit/core/update-password.service.spec.ts +++ b/src/modules/authentication/tests/unit/core/update-password.service.spec.ts @@ -8,7 +8,7 @@ import { UpdatePasswordCommand } from '@modules/authentication/core/application/ const updatePasswordRequest: UpdatePasswordRequestDto = { userId: '165192d4-398a-4469-a16b-98c02cc6f531', - password: '@Br@ndN3wPa$$w0rd', + password: '@Br@ndN3wP@$$w0rd', }; const mockAuthenticationRepository = { diff --git a/src/modules/authentication/tests/unit/interface/update-password.grpc.controller.spec.ts b/src/modules/authentication/tests/unit/interface/update-password.grpc.controller.spec.ts new file mode 100644 index 0000000..3618d37 --- /dev/null +++ b/src/modules/authentication/tests/unit/interface/update-password.grpc.controller.spec.ts @@ -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, + ); + }); + + 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); + }); +});