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 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();
            }