示例#1
0
        public async Task HandleAsync_Should_Throw_ValidationException_When_Password_Is_Already_Set()
        {
            var assignPasswordCommand = new AssignPasswordCommand(Guid.NewGuid(), "Password1234");
            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 getAccountResult = GetResult <Account> .Ok(account);

            var errors = new Collection <IError>
            {
                new Error(AccountErrorCodeEnumeration.PasswordAlreadySet, AccountErrorMessage.PasswordAlreadySet)
            };
            var passwordIsNotSetVerificationResult = VerificationResult.Fail(errors);

            _accountGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(getAccountResult);
            _accountVerificationServiceMock.Setup(x => x.VerifyPasswordIsNotSet(It.IsAny <string>()))
            .Returns(passwordIsNotSetVerificationResult);

            Func <Task> result = async() => await _commandHandler.HandleAsync(assignPasswordCommand);

            var exceptionResult = await result.Should().ThrowAsync <ValidationException>();

            exceptionResult.And.Errors.Should().BeEquivalentTo(errors);
        }
示例#2
0
        public async Task <IActionResult> AssignPasswordAsync([FromRoute] Guid id, [FromBody] AssignPasswordRequest request)
        {
            var authResult = await _authorizationService.AuthorizeAsync(User, id, ResourceOwnerPolicy.ResourceOwnerPolicyName);

            if (!authResult.Succeeded)
            {
                return(CreateErrorResult(HttpStatusCode.Forbidden));
            }

            var assignPasswordCommand = new AssignPasswordCommand(id, request.Password);
            await _communicationBus.SendCommandAsync(assignPasswordCommand);

            return(NoContent());
        }
示例#3
0
        public async Task HandleAsync_Should_Throw_ResourceNotFoundException_When_Account_Is_Not_Found()
        {
            var assignPasswordCommand = new AssignPasswordCommand(Guid.NewGuid(), "Password1234");
            var errors = new Collection <IError>
            {
                new Error(AccountErrorCodeEnumeration.NotFound, AccountErrorMessage.NotFound)
            };
            var getAccountResult = GetResult <Account> .Fail(errors);

            _accountGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(getAccountResult);

            Func <Task> result = async() => await _commandHandler.HandleAsync(assignPasswordCommand);

            var exceptionResult = await result.Should().ThrowExactlyAsync <ResourceNotFoundException>();

            exceptionResult.And.Errors.Should().BeEquivalentTo(errors);
        }
示例#4
0
        public async Task HandleAsync_Should_Set_Password()
        {
            var assignPasswordCommand = new AssignPasswordCommand(Guid.NewGuid(), "Password1234");
            var account = Account.Builder()
                          .SetId(Guid.NewGuid())
                          .SetEmail("*****@*****.**")
                          .SetConfirmed(true)
                          .SetPasswordHash(string.Empty)
                          .SetSecurityStamp(Guid.NewGuid())
                          .SetCreated(DateTimeOffset.UtcNow)
                          .SetRoles(new List <Guid> {
                Guid.NewGuid()
            })
                          .Build();
            var getAccountResult = GetResult <Account> .Ok(account);

            var          passwordIsNotSetVerificationResult = VerificationResult.Ok();
            const string passwordHash      = "PasswordHash";
            var          cancellationToken = new CancellationToken();

            _accountGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(getAccountResult)
            .Verifiable();
            _accountVerificationServiceMock.Setup(x => x.VerifyPasswordIsNotSet(It.IsAny <string>()))
            .Returns(passwordIsNotSetVerificationResult)
            .Verifiable();
            _passwordServiceMock.Setup(x => x.HashPassword(It.IsAny <string>())).Returns(passwordHash).Verifiable();
            _communicationBusMock.Setup(x => x.DispatchDomainEventsAsync(It.IsAny <Account>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask)
            .Verifiable();
            _accountRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny <Account>())).Returns(Task.CompletedTask).Verifiable();

            Func <Task> result = async() => await _commandHandler.HandleAsync(assignPasswordCommand, cancellationToken);

            await result.Should().NotThrowAsync <Exception>();

            account.PasswordHash.Should().Be(passwordHash);
            _communicationBusMock.Verify(
                x => x.DispatchDomainEventsAsync(It.Is <Account>(a => a == account),
                                                 It.Is <CancellationToken>(ct => ct == cancellationToken)), Times.Once);
        }
示例#5
0
        public async Task AssignPasswordAsync_Should_Return_NoContentResult()
        {
            var assignPasswordRequest = new AssignPasswordRequest
            {
                Password        = "******",
                ConfirmPassword = "******"
            };
            var authResult            = AuthorizationResult.Success();
            var assignPasswordCommand = new AssignPasswordCommand(Guid.NewGuid(), assignPasswordRequest.Password);

            _authorizationServiceMock
            .Setup(x => x.AuthorizeAsync(It.IsAny <ClaimsPrincipal>(), It.IsAny <object>(), It.IsAny <string>()))
            .ReturnsAsync(authResult);
            _mapperMock.Setup(x => x.Map <AssignPasswordRequest, AssignPasswordCommand>(It.IsAny <AssignPasswordRequest>()))
            .Returns(assignPasswordCommand);
            _communicationBusMock.Setup(x => x.SendCommandAsync(It.IsAny <AssignPasswordCommand>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var result = await _controller.AssignPasswordAsync(assignPasswordCommand.AccountId, assignPasswordRequest);

            var noContentResult = result.As <NoContentResult>();

            noContentResult.Should().NotBeNull();
        }