public async Task Handle_GivenResponseIsResultWithErrorAndIsNotSuccessful_ExpectInformationLogged() { var auditLogger = new Mock <ILogger>(); var authenticatedUserProvider = new Mock <ICurrentAuthenticatedUserProvider>(); authenticatedUserProvider.Setup(x => x.CurrentAuthenticatedUser).Returns( Maybe.From <ISystemUser>(new UnauthenticatedUser(TestVariables.UserId, MfaProvider.None))); var request = new UnlockAccountCommand(TestVariables.UserId); var auditBehavior = new AuditBehavior <UnlockAccountCommand, ResultWithError <ErrorData> >( auditLogger.Object, authenticatedUserProvider.Object); var requestHandlerDelegate = new Mock <RequestHandlerDelegate <ResultWithError <ErrorData> > >(); requestHandlerDelegate.Setup(x => x()) .ReturnsAsync(ResultWithError.Fail( new ErrorData(ErrorCodes.SavingChanges))); await auditBehavior.Handle(request, CancellationToken.None, requestHandlerDelegate.Object); auditLogger.Verify(x => x.Error(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <UnlockAccountCommand>(), It.Is <string>(x => x == TestVariables.UserId.ToString()))); }
public void Handle(UnlockAccountCommand command) { var account = _repository.GetById <Account>(command.AccountId); account.UnlockAccount(); _repository.Save(account); }
public void Validate_GivenAllPropertiesAreValid_ExpectValidationSuccess() { var cmd = new UnlockAccountCommand(TestVariables.UserId); var validator = new UnlockAccountCommandValidator(); var result = validator.Validate(cmd); Assert.True(result.IsValid); }
public ActionResult Unlock(Guid AccountId) { var command = new UnlockAccountCommand(AccountId); Configuration.Instance().Bus.Handle(command); return(RedirectToAction("Unlocked")); }
public void Handle(UnlockAccountCommand message) { var aggregate = this.Repository.GetById <AccountDomainModel>(message.Id); aggregate.UnlockAccount(); // here we are sending the aggregate to the NEventStore // to serialize the current state or current delta this.Repository.Save(aggregate); }
public void Validate_GivenDeviceIdIsEmpty_ExpectValidationFailure() { var cmd = new UnlockAccountCommand(Guid.Empty); var validator = new UnlockAccountCommandValidator(); var result = validator.Validate(cmd); Assert.False(result.IsValid); Assert.Contains( result.Errors, failure => failure.ErrorCode.Equals(ValidationCodes.FieldIsRequired) && failure.PropertyName == "UserId"); }
public async Task <ResultWithError <ErrorData> > Handle(UnlockAccountCommand request, CancellationToken cancellationToken) { var result = await this.Process(request, cancellationToken); var dbResult = await this._userRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); if (!dbResult) { return(ResultWithError.Fail(new ErrorData( ErrorCodes.SavingChanges, "Failed To Save Database"))); } return(result); }
private async Task <ResultWithError <ErrorData> > Process(UnlockAccountCommand request, CancellationToken cancellationToken) { var whenHappened = this._clock.GetCurrentInstant().ToDateTimeUtc(); var userMaybe = await this._userRepository.Find(request.UserId, cancellationToken); if (userMaybe.HasNoValue) { return(ResultWithError.Fail(new ErrorData(ErrorCodes.UserNotFound))); } var user = userMaybe.Value; user.UnlockAccount(whenHappened, whenHappened.AddHours(this._identitySettings.PasswordTokenLifetime)); this._userRepository.Update(user); return(ResultWithError.Ok <ErrorData>()); }
private async Task <ResultWithError <ErrorData> > Process(UnlockAccountCommand request, CancellationToken cancellationToken) { var whenHappened = this._clock.GetCurrentInstant().ToDateTimeUtc(); var userMaybe = await this._userRepository.Find(request.UserId, cancellationToken); if (userMaybe.HasNoValue) { return(ResultWithError.Fail(new ErrorData(ErrorCodes.UserNotFound))); } var user = userMaybe.Value; user.UnlockAccount(); var token = user.GenerateNewPasswordResetToken(whenHappened, TimeSpan.FromDays(this._securitySettings.PasswordTokenLifetime)); user.AddDomainEvent(new PasswordResetTokenGeneratedEvent(user.EmailAddress, user.Profile.FirstName, user.Profile.LastName, token)); this._userRepository.Update(user); return(ResultWithError.Ok <ErrorData>()); }
public async Task Handle_GivenUserDoesExist_ExpectFailedResult() { var userRepository = new Mock <IUserRepository>(); var unitOfWork = new Mock <IUnitOfWork>(); unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true); userRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object); userRepository.Setup(x => x.Find(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(() => Maybe <IUser> .Nothing); var clock = new Mock <IClock>(); var securitySettings = new Mock <IOptions <SecuritySettings> >(); securitySettings.Setup(x => x.Value).Returns(new SecuritySettings()); var handler = new UnlockAccountCommandHandler(userRepository.Object, clock.Object, securitySettings.Object); var cmd = new UnlockAccountCommand(TestVariables.UserId); var result = await handler.Handle(cmd, CancellationToken.None); Assert.True(result.IsFailure); }
public async Task Handle_GivenSavingSucceeds_ExpectSuccessfulResult() { var user = new Mock <IUser>(); user.Setup(x => x.Profile).Returns(new Profile(TestVariables.UserId, "first-name", "last-name")); var userRepository = new Mock <IUserRepository>(); var unitOfWork = new Mock <IUnitOfWork>(); unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true); userRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object); userRepository.Setup(x => x.Find(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(() => Maybe.From(user.Object)); var clock = new Mock <IClock>(); var securitySettings = new Mock <IOptions <SecuritySettings> >(); securitySettings.Setup(x => x.Value).Returns(new SecuritySettings()); var handler = new UnlockAccountCommandHandler(userRepository.Object, clock.Object, securitySettings.Object); var cmd = new UnlockAccountCommand(TestVariables.UserId); var result = await handler.Handle(cmd, CancellationToken.None); Assert.True(result.IsSuccess); }
public async Task Handle_GivenUserExists_ExpectAccountUnlockedAndPasswordResetTokenGeneratedAndDomainEventRaised() { var user = new Mock <IUser>(); user.Setup(x => x.Profile).Returns(new Profile(TestVariables.UserId, "first-name", "last-name")); var userRepository = new Mock <IUserRepository>(); var unitOfWork = new Mock <IUnitOfWork>(); unitOfWork.Setup(x => x.SaveEntitiesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(() => true); userRepository.Setup(x => x.UnitOfWork).Returns(unitOfWork.Object); userRepository.Setup(x => x.Find(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(() => Maybe.From(user.Object)); var clock = new Mock <IClock>(); var securitySettings = new Mock <IOptions <SecuritySettings> >(); securitySettings.Setup(x => x.Value).Returns(new SecuritySettings()); var handler = new UnlockAccountCommandHandler(userRepository.Object, clock.Object, securitySettings.Object); var cmd = new UnlockAccountCommand(TestVariables.UserId); await handler.Handle(cmd, CancellationToken.None); user.Verify(x => x.UnlockAccount(), Times.Once); user.Verify(x => x.GenerateNewPasswordResetToken(It.IsAny <DateTime>(), It.IsAny <TimeSpan>()), Times.Once); user.Verify(x => x.AddDomainEvent(It.IsAny <PasswordResetTokenGeneratedEvent>())); }
public void Constructor_GiveValidArguments_PropertiesAreSet() { var command = new UnlockAccountCommand(TestVariables.UserId); Assert.Equal(TestVariables.UserId, command.UserId); }