public void IsValid_WhenEmailAddressByUserId_IsFound_CaseInsensitively(string userName, string emailValue)
        {
            var          userId       = FakeData.Id();
            var          queries      = new Mock <IProcessQueries>(MockBehavior.Strict);
            EmailAddress emailAddress = new ProxiedEmailAddress(FakeData.Id())
            {
                UserId     = userId,
                IsVerified = true,
                Value      = emailValue,
            };
            var data = new[] { emailAddress }.AsQueryable();
            var dbSet     = new Mock <DbSet <EmailAddress> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <EmailAddress>(dbSet.Object, entities.Object);
            Expression <Func <EmailAddressesBy, bool> > expectedQuery = x => x.UserId == userId;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(entitySet.AsQueryable()));
            var command = new FakeMustNotBeUnverifiedEmailUserNameCommand
            {
                UserName = userName,
                UserId   = userId,
            };
            var validator = new FakeMustNotBeUnverifiedEmailUserNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            queries.Verify(x => x.Execute(It.IsAny <EmailVerificationBy>()), Times.Never);
            validator.ShouldNotHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
            queries.Verify(x => x.Execute(It.IsAny <EmailVerificationBy>()), Times.Never);
        }
        public void IsValid_WhenEmailVerificationByTicket_HasEmailMatchingUserName_CaseInsensitively(
            string userName, string ticketEmail)
        {
            string       ticket       = FakeData.String();
            var          queries      = new Mock <IProcessQueries>(MockBehavior.Strict);
            EmailAddress emailAddress = new ProxiedEmailAddress(FakeData.Id())
            {
                Value = ticketEmail,
            };
            EmailVerification verification = new ProxiedEmailVerification(FakeData.Id())
            {
                Ticket       = ticket,
                EmailAddress = emailAddress,
            };
            Expression <Func <EmailVerificationBy, bool> > expectedQuery = x => x.Ticket == ticket;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(verification));
            var command = new FakeMustNotBeUnverifiedEmailUserNameCommand
            {
                UserName = userName,
                Ticket   = ticket,
            };
            var validator = new FakeMustNotBeUnverifiedEmailUserNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            queries.Verify(x => x.Execute(It.IsAny <EmailAddressesBy>()), Times.Never);
            validator.ShouldNotHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
            queries.Verify(x => x.Execute(It.IsAny <EmailAddressesBy>()), Times.Never);
        }
        public void IsValid_WhenEmailVerificationByTicket_IsNotFound()
        {
            string userName = FakeData.Email();
            string ticket   = FakeData.String();
            var    queries  = new Mock <IProcessQueries>(MockBehavior.Strict);
            Expression <Func <EmailVerificationBy, bool> > expectedQuery = x => x.Ticket == ticket;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(null as EmailVerification));
            var command = new FakeMustNotBeUnverifiedEmailUserNameCommand
            {
                UserName = userName,
                Ticket   = ticket,
            };
            var validator = new FakeMustNotBeUnverifiedEmailUserNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            queries.Verify(x => x.Execute(It.IsAny <EmailAddressesBy>()), Times.Never);
            validator.ShouldNotHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
            queries.Verify(x => x.Execute(It.IsAny <EmailAddressesBy>()), Times.Never);
        }
        public void IsInvalid_WhenEmailVerificationByTicket_HasEmailNotMatchingUserName(
            string userName, string ticketEmail)
        {
            string       ticket       = FakeData.String();
            var          queries      = new Mock <IProcessQueries>(MockBehavior.Strict);
            EmailAddress emailAddress = new ProxiedEmailAddress(FakeData.Id())
            {
                Value = ticketEmail,
            };
            EmailVerification verification = new ProxiedEmailVerification(FakeData.Id())
            {
                Ticket       = ticket,
                EmailAddress = emailAddress,
            };
            Expression <Func <EmailVerificationBy, bool> > expectedQuery = x => x.Ticket == ticket;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(verification));
            var command = new FakeMustNotBeUnverifiedEmailUserNameCommand
            {
                UserName = userName,
                Ticket   = ticket,
            };
            var validator = new FakeMustNotBeUnverifiedEmailUserNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> ticketError = x => x.PropertyName == command.PropertyName(y => y.UserName);

            result.Errors.Count(ticketError).ShouldEqual(1);
            result.Errors.Single(ticketError).ErrorMessage.ShouldEqual(Resources
                                                                       .Validation_UserName_AllowedEmailAddress
                                                                       .Replace("{PropertyName}", User.Constraints.NameLabel.ToLower())
                                                                       .Replace("{PropertyValue}", userName)
                                                                       );
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            queries.Verify(x => x.Execute(It.IsAny <EmailAddressesBy>()), Times.Never);
            validator.ShouldHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
            queries.Verify(x => x.Execute(It.IsAny <EmailAddressesBy>()), Times.Never);
        }
        public void IsInvalid_WhenEmailAddressByUserId_IsFound_CaseInsensitively(string userName, string emailValue)
        {
            var          userId       = FakeData.Id();
            var          queries      = new Mock <IProcessQueries>(MockBehavior.Strict);
            EmailAddress emailAddress = new ProxiedEmailAddress(FakeData.Id())
            {
                UserId     = userId,
                IsVerified = true,
                Value      = emailValue,
            };
            var data = new[] { emailAddress }.AsQueryable();
            var dbSet     = new Mock <DbSet <EmailAddress> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <EmailAddress>(dbSet.Object, entities.Object);
            Expression <Func <EmailAddressesBy, bool> > expectedQuery = x => x.UserId == userId;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(entitySet.AsQueryable()));
            var command = new FakeMustNotBeUnverifiedEmailUserNameCommand
            {
                UserName = userName,
                UserId   = userId,
            };
            var validator = new FakeMustNotBeUnverifiedEmailUserNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> ticketError = x => x.PropertyName == command.PropertyName(y => y.UserName);

            result.Errors.Count(ticketError).ShouldEqual(1);
            result.Errors.Single(ticketError).ErrorMessage.ShouldEqual(Resources
                                                                       .Validation_UserName_AllowedEmailAddress
                                                                       .Replace("{PropertyName}", User.Constraints.NameLabel.ToLower())
                                                                       .Replace("{PropertyValue}", userName)
                                                                       );
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            queries.Verify(x => x.Execute(It.IsAny <EmailVerificationBy>()), Times.Never);
            validator.ShouldHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
            queries.Verify(x => x.Execute(It.IsAny <EmailVerificationBy>()), Times.Never);
        }
        public void IsValid_WhenUserName_IsEmpty_WithTicket(string userName)
        {
            string ticket  = FakeData.String();
            var    queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var    command = new FakeMustNotBeUnverifiedEmailUserNameCommand
            {
                UserName = userName,
                Ticket   = ticket,
            };
            var validator = new FakeMustNotBeUnverifiedEmailUserNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.IsAny <EmailVerificationBy>()), Times.Never);
            queries.Verify(x => x.Execute(It.IsAny <EmailAddressesBy>()), Times.Never);
            validator.ShouldNotHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.IsAny <EmailVerificationBy>()), Times.Never);
            queries.Verify(x => x.Execute(It.IsAny <EmailAddressesBy>()), Times.Never);
        }