Ejemplo n.º 1
0
        public void IsInvalid_WhenEmailAddress_IsFound_ButIsPrimary()
        {
            var emailAddress = new ProxiedEmailAddress(FakeData.Id())
            {
                Value     = FakeData.Email(),
                IsPrimary = true,
            };
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustNotBePrimaryEmailAddressCommand {
                EmailAddressId = emailAddress.Id
            };
            Expression <Func <EmailAddressBy, bool> > expectedQuery = x => x.Id == emailAddress.Id;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(emailAddress as EmailAddress));
            var validator = new FakeMustNotBePrimaryEmailAddressValidator(queries.Object);

            var result = validator.Validate(command);

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

            result.Errors.Count(emailError).ShouldEqual(1);
            result.Errors.Single(emailError).ErrorMessage.ShouldEqual(Resources.Validation_EmailAddress_CannotBePrimary
                                                                      .Replace("{PropertyName}", EmailAddress.Constraints.Label.ToLower())
                                                                      );
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldHaveValidationErrorFor(x => x.EmailAddressId, command.EmailAddressId);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
        public void IsValid_WhenEmailAddress_IsFound_WithNonNullUser()
        {
            var emailAddress = FakeData.Email();
            var queries      = new Mock <IProcessQueries>(MockBehavior.Strict);
            var user         = new User();
            var entity       = new ProxiedEmailAddress(FakeData.Id())
            {
                IsVerified = true,
                Value      = emailAddress,
                User       = user,
            };
            var command = new FakeMustFindUserByVerifiedEmailCommand {
                EmailAddress = emailAddress
            };
            Expression <Func <EmailAddressBy, bool> > expectedQuery = x => x.Value == emailAddress;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(entity as EmailAddress));
            var validator = new FakeMustFindUserByVerifiedEmailValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.EmailAddress, command.EmailAddress);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Ejemplo n.º 3
0
        public void Handler_ReturnsNonNullEmailAddress_WhenFound_ById_AndIsVerifiedMatches(
            bool?queryIsVerified, bool entityIsVerified)
        {
            var emailAddressId = FakeData.Id();
            var emailAddress   = new ProxiedEmailAddress(emailAddressId)
            {
                IsVerified = entityIsVerified,
            };
            var data = new[] { emailAddress }.AsQueryable();
            var query = new EmailAddressBy(emailAddressId)
            {
                IsVerified = queryIsVerified,
            };
            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);

            entities.Setup(x => x.Query <EmailAddress>()).Returns(entitySet);
            var handler = new HandleEmailAddressByQuery(entities.Object);

            EmailAddress result = handler.Handle(query).Result;

            result.ShouldNotBeNull();
            result.ShouldEqual(data.Single());
            entities.Verify(x => x.Query <EmailAddress>(), Times.Once);
        }
Ejemplo n.º 4
0
        public void Handler_ReturnsNonNullEmailAddress_WhenFound_ById_AndIsVerifiedMatches(
            bool? queryIsVerified, bool entityIsVerified)
        {
            var emailAddressId = FakeData.Id();
            var emailAddress = new ProxiedEmailAddress(emailAddressId)
            {
                IsVerified = entityIsVerified,
            };
            var data = new[] { emailAddress }.AsQueryable();
            var query = new EmailAddressBy(emailAddressId)
            {
                IsVerified = queryIsVerified,
            };
            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);
            entities.Setup(x => x.Query<EmailAddress>()).Returns(entitySet);
            var handler = new HandleEmailAddressByQuery(entities.Object);

            EmailAddress result = handler.Handle(query).Result;

            result.ShouldNotBeNull();
            result.ShouldEqual(data.Single());
            entities.Verify(x => x.Query<EmailAddress>(), Times.Once);
        }
        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 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_WhenEmailAddress_IsFoundById()
        {
            var emailAddressId = new Random().Next(1, int.MaxValue);
            var queries        = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command        = new FakeMustFindEmailAddressByIdCommand {
                EmailAddressId = emailAddressId
            };
            var entity = new ProxiedEmailAddress(emailAddressId);
            Expression <Func <EmailAddressBy, bool> > expectedQuery = x => x.Id == emailAddressId;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(entity as EmailAddress));
            var validator = new FakeMustFindEmailAddressByIdValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.EmailAddressId, command.EmailAddressId);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
        public void IsValid_WhenEmailAddress_IsFound_AndIsNotVerified()
        {
            EmailAddress emailAddress = new ProxiedEmailAddress(FakeData.Id())
            {
                Value = FakeData.Email(),
            };
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustNotBeVerifiedEmailAddressCommand {
                EmailAddress = emailAddress.Value
            };
            Expression <Func <EmailAddressBy, bool> > expectedQuery = x => x.Value == emailAddress.Value;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(emailAddress));
            var validator = new FakeMustNotBeVerifiedEmailAddressValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.EmailAddress, command.EmailAddress);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Ejemplo n.º 11
0
        public void Handler_ReturnsNullEmailAddress_WhenFound_ById_ButIsVerifiedDoesNotMatch(bool isVerified)
        {
            var emailAddressId = new Random().Next(1, int.MaxValue);
            var emailAddress   = new ProxiedEmailAddress(emailAddressId)
            {
                IsVerified = !isVerified,
            };
            var data = new[] { emailAddress }.AsQueryable();
            var query = new EmailAddressBy(emailAddressId)
            {
                IsVerified = isVerified
            };
            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);

            entities.Setup(x => x.Query <EmailAddress>()).Returns(entitySet);
            var handler = new HandleEmailAddressByQuery(entities.Object);

            EmailAddress result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query <EmailAddress>(), Times.Once);
        }