public void SetUp() { authService = new Mock <IAuthService>(); cryptoService = new Mock <ICryptoService>(); request = new ConfirmAccountRequest(); confirmAccountCommand = new ConfirmAccountCommand(authService.Object, cryptoService.Object); }
public ActionResult ConfirmAccount(Guid confirmGuid) { var command = new ConfirmAccountCommand { ConfirmGuid = confirmGuid }; HandleCommand(command, Json("User confirmed")); return(View()); }
public async Task <IActionResult> ConfirmAccount(ConfirmAccountRequest request, CancellationToken cancellationToken) { var command = new ConfirmAccountCommand { Token = request.Token, UserId = request.UserId }; var result = await _mediator.Send(command, cancellationToken); return(this.GetResponseFromResult(result)); }
public void Handle(ConfirmAccountCommand command) { using (var dbContext = new UserAccountDataContext()) { User user = dbContext.Users.SingleOrDefault(u => u.ConfirmGuid == command.ConfirmGuid); if (user == null) { throw new ServerSideException("User not find, check your activate link and try agine, or contact us"); } user.ConfirmAccount(); } }
public async Task HandleAsync_Should_Confirm_Account() { var confirmAccountCommand = new ConfirmAccountCommand("*****@*****.**", "123456"); var token = Token.Builder() .SetIssued(DateTimeOffset.UtcNow) .SetExpires(DateTimeOffset.UtcNow.AddDays(1)) .SetType(TokenTypeEnumeration.AccountConfirmation) .SetValue(confirmAccountCommand.Code) .Build(); var account = Account.Builder() .SetId(Guid.NewGuid()) .SetEmail(confirmAccountCommand.Email) .SetConfirmed(false) .SetPasswordHash("PasswordHash") .SetSecurityStamp(Guid.NewGuid()) .SetCreated(DateTimeOffset.UtcNow) .SetRoles(new List <Guid> { Guid.NewGuid() }) .SetTokens(new List <Token> { token }) .Build(); var getAccountResult = GetResult <Account> .Ok(account); var emailIsNotConfirmedVerificationResult = VerificationResult.Ok(); var tokenIsCorrectVerificationResult = VerificationResult.Ok(); var cancellationToken = new CancellationToken(); _accountGetterServiceMock.Setup(x => x.GetByEmailAsync(It.IsAny <string>())) .ReturnsAsync(getAccountResult); _accountVerificationServiceMock.Setup(x => x.VerifyAccountIsNotConfirmed(It.IsAny <bool>())) .Returns(emailIsNotConfirmedVerificationResult); _accountVerificationServiceMock .Setup(x => x.VerifyConfirmationCode(It.IsAny <Token>(), It.IsAny <string>())) .Returns(tokenIsCorrectVerificationResult); _communicationBusMock.Setup(x => x.DispatchDomainEventsAsync(It.IsAny <Account>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); _accountRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny <Account>())).Returns(Task.CompletedTask); Func <Task> result = async() => await _commandHandler.HandleAsync(confirmAccountCommand, cancellationToken); await result.Should().NotThrowAsync <Exception>(); account.Confirmed.Should().BeTrue(); account.Tokens.Should().NotContain(token); _communicationBusMock.Verify( x => x.DispatchDomainEventsAsync(It.Is <Account>(a => a == account), It.Is <CancellationToken>(ct => ct == cancellationToken)), Times.Once); }
public async Task HandleAsync_Should_Throw_ValidationException_When_Account_Is_Not_Found() { var confirmAccountCommand = new ConfirmAccountCommand("*****@*****.**", "123456"); var errors = new Collection <IError> { new Error(AccountErrorCodeEnumeration.NotFound, AccountErrorMessage.NotFound) }; var getAccountResult = GetResult <Account> .Fail(errors); _accountGetterServiceMock.Setup(x => x.GetByEmailAsync(It.IsAny <string>())) .ReturnsAsync(getAccountResult); Func <Task> result = async() => await _commandHandler.HandleAsync(confirmAccountCommand); var exceptionResult = await result.Should().ThrowExactlyAsync <ValidationException>(); exceptionResult.And.Errors.Should().BeEquivalentTo(errors); }
public async Task HandleAsync_Should_Throw_ValidationException_When_Account_Is_Already_Confirmed() { var confirmAccountCommand = new ConfirmAccountCommand("*****@*****.**", "123456"); var token = Token.Builder() .SetIssued(DateTimeOffset.UtcNow) .SetExpires(DateTimeOffset.UtcNow.AddDays(1)) .SetType(TokenTypeEnumeration.AccountConfirmation) .SetValue(confirmAccountCommand.Code) .Build(); var account = Account.Builder() .SetId(Guid.NewGuid()) .SetEmail(confirmAccountCommand.Email) .SetConfirmed(true) .SetPasswordHash("PasswordHash") .SetSecurityStamp(Guid.NewGuid()) .SetCreated(DateTimeOffset.UtcNow) .SetRoles(new List <Guid> { Guid.NewGuid() }) .SetTokens(new List <Token> { token }) .Build(); var getAccountResult = GetResult <Account> .Ok(account); var errors = new Collection <IError> { new Error(AccountErrorCodeEnumeration.AlreadyConfirmed, AccountErrorMessage.AlreadyConfirmed) }; var emailIsNotConfirmedVerificationResult = VerificationResult.Fail(errors); _accountGetterServiceMock.Setup(x => x.GetByEmailAsync(It.IsAny <string>())) .ReturnsAsync(getAccountResult); _accountVerificationServiceMock.Setup(x => x.VerifyAccountIsNotConfirmed(It.IsAny <bool>())) .Returns(emailIsNotConfirmedVerificationResult); Func <Task> result = async() => await _commandHandler.HandleAsync(confirmAccountCommand); var exceptionResult = await result.Should().ThrowExactlyAsync <ValidationException>(); exceptionResult.And.Errors.Should().BeEquivalentTo(errors); }
public void SetsConfirmationToTrue(string tName, string tPassword, string tEmail, int tNumber) { var vDataStore = GetEmbeddedDatabase; AddAccountToDataStore(vDataStore, tName, tPassword, tEmail, tNumber); var vModel = new ConfirmAccountModel { Name = tName, ConfirmationNumber = tNumber }; var vCommand = new ConfirmAccountCommand(vDataStore, vModel); vCommand.Execute(); using (var vSession = vDataStore.OpenSession()) { var vAccount = vSession.Load<Account>("Accounts/" + tName); Assert.AreEqual(true, vAccount.IsConfirmed); } }
public async Task ConfirmAccountAsync_Should_Return_NoContentResult() { var confirmAccountRequest = new ConfirmAccountRequest { Email = "*****@*****.**", Code = "123456" }; var confirmAccountCommand = new ConfirmAccountCommand(confirmAccountRequest.Email, confirmAccountRequest.Code); _mapperMock.Setup(x => x.Map <ConfirmAccountRequest, ConfirmAccountCommand>(It.IsAny <ConfirmAccountRequest>())) .Returns(confirmAccountCommand); _communicationBusMock.Setup(x => x.SendCommandAsync(It.IsAny <ConfirmAccountCommand>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); var result = await _controller.ConfirmAccountAsync(confirmAccountRequest); var noContentResult = result.As <NoContentResult>(); noContentResult.Should().NotBeNull(); }
public async Task HandleAsync_Should_Throw_ValidationException_When_Token_Was_Not_Generated() { var confirmAccountCommand = new ConfirmAccountCommand("*****@*****.**", "123456"); var account = Account.Builder() .SetId(Guid.NewGuid()) .SetEmail(confirmAccountCommand.Email) .SetConfirmed(false) .SetPasswordHash("PasswordHash") .SetSecurityStamp(Guid.NewGuid()) .SetCreated(DateTimeOffset.UtcNow) .SetRoles(new List <Guid> { Guid.NewGuid() }) .Build(); var getAccountResult = GetResult <Account> .Ok(account); var emailIsNotConfirmedVerificationResult = VerificationResult.Ok(); var errors = new Collection <IError> { new Error(AccountErrorCodeEnumeration.ConfirmationCodeWasNotGenerated, AccountErrorMessage.ConfirmationCodeWasNotGenerated) }; var tokenIsCorrectVerificationResult = VerificationResult.Fail(errors); _accountGetterServiceMock.Setup(x => x.GetByEmailAsync(It.IsAny <string>())) .ReturnsAsync(getAccountResult); _accountVerificationServiceMock.Setup(x => x.VerifyAccountIsNotConfirmed(It.IsAny <bool>())) .Returns(emailIsNotConfirmedVerificationResult); _accountVerificationServiceMock .Setup(x => x.VerifyConfirmationCode(It.IsAny <Token>(), It.IsAny <string>())) .Returns(tokenIsCorrectVerificationResult); Func <Task> result = async() => await _commandHandler.HandleAsync(confirmAccountCommand); var exceptionResult = await result.Should().ThrowAsync <ValidationException>(); exceptionResult.And.Errors.Should().BeEquivalentTo(errors); }