Exemple #1
0
        public async Task HandleAsync(ChangePasswordCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByIdAsync(command.AccountId);

            if (!getAccountResult.Success)
            {
                throw new ResourceNotFoundException(getAccountResult.Errors);
            }

            var passwordIsSetVerificationResult = _accountVerificationService.VerifyPasswordIsSet(getAccountResult.Value.PasswordHash);

            if (!passwordIsSetVerificationResult.Success)
            {
                throw new ValidationException(passwordIsSetVerificationResult.Errors);
            }

            var passwordIsCorrectVerificationResult = _accountVerificationService.VerifyPassword(getAccountResult.Value.PasswordHash, command.OldPassword);

            if (!passwordIsCorrectVerificationResult.Success)
            {
                throw new ValidationException(passwordIsCorrectVerificationResult.Errors);
            }

            getAccountResult.Value.ChangePassword(_passwordService.HashPassword(command.NewPassword), Guid.NewGuid());
            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountRepository.UpdateAsync(getAccountResult.Value);
        }
Exemple #2
0
        public void VerifyPassword_Should_Return_VerificationResult_With_Success_True()
        {
            var account = Account.Builder()
                          .SetId(Guid.NewGuid())
                          .SetEmail("*****@*****.**")
                          .SetConfirmed(true)
                          .SetPasswordHash("PasswordHash")
                          .SetSecurityStamp(Guid.NewGuid())
                          .SetCreated(DateTimeOffset.UtcNow)
                          .SetRoles(new List <Guid> {
                Guid.NewGuid()
            })
                          .Build();
            var expectedResult = VerificationResult.Ok();

            _passwordServiceMock.Setup(x => x.VerifyHashedPassword(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(true);

            var result = _accountVerificationService.VerifyPassword(account.PasswordHash, "password");

            result.Should().BeEquivalentTo(expectedResult);
        }