public void ExecutesQuery_ToGetEmailAddress_ByUserNameAndNumber()
            {
                var princial = "".AsPrincipal();
                var command = new UpdateMyEmailValueCommand { Principal = princial, };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<EmailAddress>()).Returns(new EmailAddress[] { }.AsQueryable);
                var handler = new UpdateMyEmailValueHandler(entities.Object);

                handler.Handle(command);

                //queryProcessor.Verify(m => m.Execute(It.Is(emailAddressQueryFromCommand)), Times.Once());
                entities.Verify(m => m.Get<EmailAddress>(), Times.Once());
            }
            public void DoesNotUpdate_WhenEmailAddressIsNull()
            {
                var princial = "".AsPrincipal();
                var command = new UpdateMyEmailValueCommand { Principal = princial, };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<EmailAddress>()).Returns(new EmailAddress[] { }.AsQueryable);
                entities.Setup(m => m.Update(It.IsAny<EmailAddress>()));
                var handler = new UpdateMyEmailValueHandler(entities.Object);

                handler.Handle(command);

                entities.Verify(m => m.Update(It.IsAny<EmailAddress>()), Times.Never());
                command.ChangedState.ShouldEqual(false);
            }
 public void IsInvalidWhen_IdentityName_IsWhiteSpace()
 {
     const string principalIdentityName = "\t";
     var principal = principalIdentityName.AsPrincipal();
     var command = new UpdateMyEmailValueCommand { Principal = principal };
     var validator = new UpdateMyEmailValueValidator(null);
     var results = validator.Validate(command);
     results.IsValid.ShouldBeFalse();
     results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
     var error = results.Errors.SingleOrDefault(e => e.PropertyName == "Principal");
     error.ShouldNotBeNull();
     // ReSharper disable PossibleNullReferenceException
     error.ErrorMessage.ShouldEqual(ValidatePrincipal.FailedBecauseIdentityNameWasEmpty);
     // ReSharper restore PossibleNullReferenceException
 }
            public void DoesNotUpdate_WhenNewValue_IsSameAsOldSpelling()
            {
                var princial = "".AsPrincipal();
                const string value = "*****@*****.**";
                var command = new UpdateMyEmailValueCommand { NewValue = value, Principal = princial, };
                var emailAddress = new EmailAddress
                {
                    Value = value,
                    Person = new Person { User = new User { Name = princial.Identity.Name } },
                };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable);
                entities.Setup(m => m.Update(It.IsAny<EmailAddress>()));
                var handler = new UpdateMyEmailValueHandler(entities.Object);

                handler.Handle(command);

                entities.Verify(m => m.Update(It.IsAny<EmailAddress>()), Times.Never());
                command.ChangedState.ShouldEqual(false);
            }
            public void ChangesEmailSpelling_WhenNewValue_IsDifferentFromOldSpelling()
            {
                const string newValue = "*****@*****.**";
                const string oldValue = "*****@*****.**";
                var princial = "".AsPrincipal();
                var emailAddress = new EmailAddress
                {
                    Value = oldValue,
                    Person = new Person { User = new User { Name = princial.Identity.Name, } }
                };
                EmailAddress updatedEntity = null;
                var command = new UpdateMyEmailValueCommand { NewValue = newValue, Principal = princial, };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable);
                entities.Setup(m => m.Update(It.Is<EmailAddress>(a => a.Value == newValue)))
                    .Callback((Entity entity) => updatedEntity = (EmailAddress)entity);
                var handler = new UpdateMyEmailValueHandler(entities.Object);

                handler.Handle(command);

                entities.Verify(m => m.Update(It.Is<EmailAddress>(a => a.Value == newValue)), Times.Once());
                command.ChangedState.ShouldEqual(true);
                updatedEntity.Value.ShouldEqual(newValue);
            }
            public void IsInvalidWhen_IdentityName_DoesNotMatchUser()
            {
                const string principalIdentityName = "user@domain.";
                var principal = principalIdentityName.AsPrincipal();
                var command = new UpdateMyEmailValueCommand { Principal = principal };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<User>()).Returns(new User[] { }.AsQueryable);
                entities.Setup(m => m.Query<EmailAddress>()).Returns(new EmailAddress[] { }.AsQueryable);
                var validator = new UpdateMyEmailValueValidator(entities.Object);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "Principal");
                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                    ValidatePrincipal.FailedBecauseIdentityNameMatchedNoUser,
                        principalIdentityName));
                // ReSharper restore PossibleNullReferenceException
            }
            public void IsValidWhen_MatchesPreviousSpelling_CaseInsensitively()
            {
                var command = new UpdateMyEmailValueCommand { NewValue = "*****@*****.**", };
                var emailAddress = new EmailAddress { Value = "*****@*****.**" };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable);
                var validator = new UpdateMyEmailValueValidator(entities.Object);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "NewValue");
                error.ShouldBeNull();
            }
            public void IsValidWhen_EmailAddressWasNull()
            {
                var command = new UpdateMyEmailValueCommand { NewValue = "*****@*****.**", };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<EmailAddress>()).Returns(new EmailAddress[] { }.AsQueryable);
                var validator = new UpdateMyEmailValueValidator(entities.Object);

                var results = validator.Validate(command);

                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "NewValue");
                error.ShouldBeNull();
            }
            public void IsInvalidWhen_DoesNotMatchPreviousSpelling_CaseInsensitively()
            {
                const string principalIdentityName = "*****@*****.**";
                var principal = principalIdentityName.AsPrincipal();
                var command = new UpdateMyEmailValueCommand
                {
                    Principal = principal,
                    NewValue = "*****@*****.**",
                };
                var emailAddress = new EmailAddress
                {
                    Value = "*****@*****.**",
                    Person = new Person
                    {
                        User = new User
                        {
                            Name = principal.Identity.Name,
                        },
                    },
                };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<User>()).Returns(new User[] { }.AsQueryable);
                entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable);
                var validator = new UpdateMyEmailValueValidator(entities.Object);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "NewValue");
                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                    ValidateEmailAddress.FailedBecauseNewValueDidNotMatchCurrentValueCaseInvsensitively,
                        command.NewValue));
                // ReSharper restore PossibleNullReferenceException
            }
            public void IsInvalidWhen_FailsEmailAddressRegex()
            {
                var command = new UpdateMyEmailValueCommand { NewValue = "user@domain.", };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<EmailAddress>()).Returns(new EmailAddress[] { }.AsQueryable);
                var validator = new UpdateMyEmailValueValidator(entities.Object);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "NewValue");
                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                    ValidateEmailAddress.FailedBecauseValueWasNotValidEmailAddress,
                        command.NewValue));
                // ReSharper restore PossibleNullReferenceException
            }
            public void IsInvalidWhen_IsWhiteSpace()
            {
                var command = new UpdateMyEmailValueCommand { NewValue = "\t" };
                var validator = new UpdateMyEmailValueValidator(null);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "NewValue");
                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(ValidateEmailAddress.FailedBecauseValueWasEmpty);
                // ReSharper restore PossibleNullReferenceException
            }
            public void IsValidWhen_PrincipalWasNull()
            {
                const string principalIdentityName = "*****@*****.**";
                var command = new UpdateMyEmailValueCommand
                {
                    Number = 1,
                    NewValue = principalIdentityName.ToUpper(),
                };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<User>()).Returns(new User[] { }.AsQueryable);
                entities.Setup(m => m.Query<EmailAddress>()).Returns(new EmailAddress[] { }.AsQueryable);
                var validator = new UpdateMyEmailValueValidator(entities.Object);

                var results = validator.Validate(command);

                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "Number");
                error.ShouldBeNull();
            }
            public void IsValidWhen_IdentityName_IsNotEmpty_AndMatchesUser()
            {
                const string principalIdentityName = "*****@*****.**";
                var principal = principalIdentityName.AsPrincipal();
                var command = new UpdateMyEmailValueCommand { Principal = principal };
                var user = new User { Name = principal.Identity.Name };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<User>()).Returns(new[] { user }.AsQueryable);
                entities.Setup(m => m.Query<EmailAddress>()).Returns(new EmailAddress[] { }.AsQueryable);
                var validator = new UpdateMyEmailValueValidator(entities.Object);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == "Principal");
                error.ShouldBeNull();
            }