public void Validate_GivenAllPropertiesAreValid_ExpectValidationSuccess()
        {
            var cmd       = new DisableAccountCommand(TestVariables.UserId);
            var validator = new DisableAccountCommandValidator();
            var result    = validator.Validate(cmd);

            Assert.True(result.IsValid);
        }
        public void Validate_GivenDeviceIdIsEmpty_ExpectValidationFailure()
        {
            var cmd       = new DisableAccountCommand(Guid.Empty);
            var validator = new DisableAccountCommandValidator();
            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(DisableAccountCommand 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);
        }
        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 handler =
                new DisableAccountCommandHandler(userRepository.Object, clock.Object);

            var cmd    = new DisableAccountCommand(TestVariables.UserId);
            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsFailure);
            Assert.Equal(ErrorCodes.UserNotFound, result.Error.Code);
        }
        public async Task Handle_GivenUserExists_ExpectAccountDisabledAndDomainEventRaised()
        {
            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 handler = new DisableAccountCommandHandler(userRepository.Object, clock.Object);

            var cmd = new DisableAccountCommand(TestVariables.UserId);
            await handler.Handle(cmd, CancellationToken.None);

            user.Verify(x => x.DisableAccount(It.IsAny <DateTime>()), Times.Once);
            user.Verify(x => x.AddDomainEvent(It.IsAny <UserDisabledEvent>()));
        }
        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 handler =
                new DisableAccountCommandHandler(userRepository.Object, clock.Object);

            var cmd    = new DisableAccountCommand(TestVariables.UserId);
            var result = await handler.Handle(cmd, CancellationToken.None);

            Assert.True(result.IsSuccess);
        }
        private async Task <ResultWithError <ErrorData> > Process(DisableAccountCommand 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;

            if (user.IsDisabled)
            {
                return(ResultWithError.Fail(new ErrorData(ErrorCodes.UserAlreadyDisabled)));
            }

            user.DisableAccount(whenHappened);

            user.AddDomainEvent(new UserDisabledEvent(user.EmailAddress, user.Profile.FirstName, user.Profile.LastName));

            this._userRepository.Update(user);
            return(ResultWithError.Ok <ErrorData>());
        }
예제 #8
0
        public void Constructor_GiveValidArguments_PropertiesAreSet()
        {
            var command = new DisableAccountCommand(TestVariables.UserId);

            Assert.Equal(command.UserId, TestVariables.UserId);
        }