Exemple #1
0
            public void ExecutesCommand_WhenAction_IsValid()
            {
                const int    number                = 2;
                const string personUserName        = "******";
                const string newValue              = "*****@*****.**";
                const string principalIdentityName = "*****@*****.**";
                var          scenarioOptions       = new ScenarioOptions
                {
                    PrincipalIdentityName = principalIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    Number         = number,
                    PersonUserName = personUserName,
                    Value          = newValue,
                    ReturnUrl      = "https://www.site.com/"
                };
                var controller = CreateController(scenarioOptions);
                var builder    = ReuseMock.TestControllerBuilder();
                var principal  = principalIdentityName.AsPrincipal();

                builder.HttpContext.User = principal;
                builder.InitializeController(controller);
                scenarioOptions.MockCommandHandler.Setup(m => m.Handle(It.Is(CommandBasedOn(model))));

                controller.Put(model);

                scenarioOptions.MockCommandHandler.Verify(m =>
                                                          m.Handle(It.Is(CommandBasedOn(model))),
                                                          Times.Once());
            }
Exemple #2
0
            public void FlashesNoChangesMessage_WhenCommand_ChangedState()
            {
                const int    number                = 2;
                const string personUserName        = "******";
                const string newValue              = "*****@*****.**";
                const string principalIdentityName = "*****@*****.**";
                var          scenarioOptions       = new ScenarioOptions
                {
                    PrincipalIdentityName = principalIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    Number         = number,
                    PersonUserName = personUserName,
                    Value          = newValue,
                    ReturnUrl      = "https://www.site.com/"
                };
                var controller = CreateController(scenarioOptions);
                var builder    = ReuseMock.TestControllerBuilder();
                var principal  = principalIdentityName.AsPrincipal();

                builder.HttpContext.User = principal;
                builder.InitializeController(controller);
                scenarioOptions.MockCommandHandler.Setup(m => m.Handle(It.Is(CommandBasedOn(model))));

                controller.Put(model);

                controller.TempData.ShouldNotBeNull();
                var message = controller.TempData.FeedbackMessage();

                message.ShouldNotBeNull();
                message.ShouldEqual(UpdateEmailValueController.NoChangesMessage);
            }
Exemple #3
0
            public void ReturnsView_WhenModelState_IsInvalid()
            {
                const string userIdentityName = "*****@*****.**";
                const string personUserName   = "******";
                var          scenarioOptions  = new ScenarioOptions
                {
                    PrincipalIdentityName = userIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    PersonUserName = personUserName,
                };
                var controller = CreateController(scenarioOptions);

                controller.ModelState.AddModelError("error", "message");

                var result = controller.Put(model);

                result.ShouldNotBeNull();
                result.ShouldBeType <ViewResult>();
                var viewResult = (ViewResult)result;

                viewResult.Model.ShouldNotBeNull();
                viewResult.Model.ShouldBeType <UpdateEmailValueForm>();
                viewResult.Model.ShouldEqual(model);
            }
Exemple #4
0
            public void ReturnsRedirect_ToModelReturnUrl_AfterCommandIsExecuted()
            {
                const int    number                = 2;
                const string personUserName        = "******";
                const string newValue              = "*****@*****.**";
                const string principalIdentityName = "*****@*****.**";
                var          scenarioOptions       = new ScenarioOptions
                {
                    PrincipalIdentityName = principalIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    Number         = number,
                    PersonUserName = personUserName,
                    Value          = newValue,
                    ReturnUrl      = "https://www.site.com/"
                };
                var controller = CreateController(scenarioOptions);
                var builder    = ReuseMock.TestControllerBuilder();
                var principal  = principalIdentityName.AsPrincipal();

                builder.HttpContext.User = principal;
                builder.InitializeController(controller);
                scenarioOptions.MockCommandHandler.Setup(m => m.Handle(It.Is(CommandBasedOn(model))));

                var result = controller.Put(model);

                result.ShouldNotBeNull();
                result.ShouldBeType <RedirectResult>();
                var redirectResult = (RedirectResult)result;

                redirectResult.Url.ShouldEqual(model.ReturnUrl);
                redirectResult.Permanent.ShouldBeFalse();
            }
Exemple #5
0
        public virtual ActionResult Put(UpdateEmailValueForm model)
        {
            // make sure user owns this email address
            if (model == null || !User.Identity.Name.Equals(model.PersonUserName, StringComparison.OrdinalIgnoreCase))
            {
                return(HttpNotFound());
            }

            // make sure model state is valid
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // execute command, set feedback message, and redirect
            var command = Mapper.Map <UpdateMyEmailValueCommand>(model);

            command.Principal = User;
            _services.CommandHandler.Handle(command);
            SetFeedbackMessage(command.ChangedState
                ? string.Format(SuccessMessageFormat, model.Value)
                : NoChangesMessage
                               );
            return(Redirect(model.ReturnUrl));
        }
Exemple #6
0
            public void ReturnsTrue_WhenModelStateIsValid()
            {
                var model      = new UpdateEmailValueForm();
                var controller = CreateController();

                var result = controller.ValidateValue(model);

                result.ShouldNotBeNull();
                result.ShouldBeType <JsonResult>();
                result.JsonRequestBehavior.ShouldEqual(JsonRequestBehavior.DenyGet);
                result.Data.ShouldEqual(true);
            }
 public void IsInvalidWhen_Value_IsWhiteSpace()
 {
     var validator = new UpdateEmailValueValidator(null);
     var model = new UpdateEmailValueForm { Value = " \r" };
     var results = validator.Validate(model);
     results.IsValid.ShouldBeFalse();
     results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
     var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);
     error.ShouldNotBeNull();
     // ReSharper disable PossibleNullReferenceException
     error.ErrorMessage.ShouldEqual(
         UpdateEmailValueValidator.FailedBecausePreviousSpellingDoesNotMatchValueCaseInsensitively);
     // ReSharper restore PossibleNullReferenceException
 }
Exemple #8
0
            public void ReturnsErrorMessage_WhenModelStateIsInvalid_ForValueProperty()
            {
                const string errorMessage = "Here is your error message.";
                var          model        = new UpdateEmailValueForm();
                var          controller   = CreateController();

                controller.ModelState.AddModelError(UpdateEmailValueForm.ValuePropertyName, errorMessage);

                var result = controller.ValidateValue(model);

                result.ShouldNotBeNull();
                result.ShouldBeType <JsonResult>();
                result.JsonRequestBehavior.ShouldEqual(JsonRequestBehavior.DenyGet);
                result.Data.ShouldEqual(errorMessage);
            }
Exemple #9
0
            public void Returns404_WhenPersonUserName_IsNotEqualTo_UserIdentityName()
            {
                const string userIdentityName = "*****@*****.**";
                const string personUserName   = "******";
                var          scenarioOptions  = new ScenarioOptions
                {
                    PrincipalIdentityName = userIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    PersonUserName = personUserName,
                };
                var controller = CreateController(scenarioOptions);

                var result = controller.Put(model);

                result.ShouldNotBeNull();
                result.ShouldBeType <HttpNotFoundResult>();
            }
        public virtual ActionResult Put(UpdateEmailValueForm model)
        {
            // make sure user owns this email address
            if (model == null || !User.Identity.Name.Equals(model.PersonUserName, StringComparison.OrdinalIgnoreCase))
                return HttpNotFound();

            // make sure model state is valid
            if (!ModelState.IsValid) return View(model);

            // execute command, set feedback message, and redirect
            var command = Mapper.Map<UpdateMyEmailValueCommand>(model);
            command.Principal = User;
            _services.CommandHandler.Handle(command);
            SetFeedbackMessage(command.ChangedState
                ? string.Format(SuccessMessageFormat, model.Value)
                : NoChangesMessage
            );
            return Redirect(model.ReturnUrl);
        }
            public void ReturnsView_WhenModelState_IsInvalid()
            {
                const string userIdentityName = "*****@*****.**";
                const string personUserName = "******";
                var scenarioOptions = new ScenarioOptions
                {
                    PrincipalIdentityName = userIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    PersonUserName = personUserName,
                };
                var controller = CreateController(scenarioOptions);
                controller.ModelState.AddModelError("error", "message");

                var result = controller.Put(model);

                result.ShouldNotBeNull();
                result.ShouldBeType<ViewResult>();
                var viewResult = (ViewResult)result;
                viewResult.Model.ShouldNotBeNull();
                viewResult.Model.ShouldBeType<UpdateEmailValueForm>();
                viewResult.Model.ShouldEqual(model);
            }
 public void IsValidWhen_Value_MatchesPreviousSpelling_CaseInsensitively()
 {
     var model = new UpdateEmailValueForm
     {
         Value = "*****@*****.**",
         PersonUserName = string.Empty
     };
     var emailAddress = new EmailAddress
     {
         Value = "*****@*****.**",
         Person = new Person { User = new User { Name = model.PersonUserName } },
     };
     var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
     entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable);
     var validator = new UpdateEmailValueValidator(entities.Object);
     var results = validator.Validate(model);
     var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);
     error.ShouldBeNull();
 }
 public void IsValidWhen_Number_MatchesEmail_ForPersonUserName()
 {
     const string personUserName = "******";
     const string emailValue = "*****@*****.**";
     var form = new UpdateEmailValueForm
     {
         PersonUserName = personUserName,
         Number = 13,
         Value = emailValue.ToUpper(),
     };
     var emailAddress = new EmailAddress
     {
         Value = emailValue.ToLower(),
         Number = form.Number,
         Person = new Person { User = new User { Name = form.PersonUserName, } }
     };
     var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
     entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable);
     var validator = new UpdateEmailValueValidator(entities.Object);
     var results = validator.Validate(form);
     var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);
     error.ShouldBeNull();
 }
            public void FlashesNoChangesMessage_WhenCommand_ChangedState()
            {
                const int number = 2;
                const string personUserName = "******";
                const string newValue = "*****@*****.**";
                const string principalIdentityName = "*****@*****.**";
                var scenarioOptions = new ScenarioOptions
                {
                    PrincipalIdentityName = principalIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    Number = number,
                    PersonUserName = personUserName,
                    Value = newValue,
                    ReturnUrl = "https://www.site.com/"
                };
                var controller = CreateController(scenarioOptions);
                var builder = ReuseMock.TestControllerBuilder();
                var principal = principalIdentityName.AsPrincipal();
                builder.HttpContext.User = principal;
                builder.InitializeController(controller);
                scenarioOptions.MockCommandHandler.Setup(m => m.Handle(It.Is(CommandBasedOn(model))));

                controller.Put(model);

                controller.TempData.ShouldNotBeNull();
                var message = controller.TempData.FeedbackMessage();
                message.ShouldNotBeNull();
                message.ShouldEqual(UpdateEmailValueController.NoChangesMessage);
            }
            public void Ignores_Principal()
            {
                const string userName = "******";
                var model = new UpdateEmailValueForm { PersonUserName = userName };

                var command = Mapper.Map<UpdateMyEmailValueCommand>(model);

                command.ShouldNotBeNull();
                command.Principal.ShouldBeNull();
            }
 public void Implements_IReturnUrl()
 {
     var model = new UpdateEmailValueForm();
     model.ShouldImplement<IReturnUrl>();
 }
 private static Expression<Func<UpdateMyEmailValueCommand, bool>> CommandBasedOn(UpdateEmailValueForm model)
 {
     Expression<Func<UpdateMyEmailValueCommand, bool>> commandDerivedFromModel = command =>
         command.Number == model.Number &&
         command.Principal.Identity.Name == model.PersonUserName &&
         command.NewValue == model.Value
     ;
     return commandDerivedFromModel;
 }
            public void ReturnsErrorMessage_WhenModelStateIsInvalid_ForValueProperty()
            {
                const string errorMessage = "Here is your error message.";
                var model = new UpdateEmailValueForm();
                var controller = CreateController();
                controller.ModelState.AddModelError(UpdateEmailValueForm.ValuePropertyName, errorMessage);

                var result = controller.ValidateValue(model);

                result.ShouldNotBeNull();
                result.ShouldBeType<JsonResult>();
                result.JsonRequestBehavior.ShouldEqual(JsonRequestBehavior.DenyGet);
                result.Data.ShouldEqual(errorMessage);
            }
            public void ReturnsTrue_WhenModelStateIsValid()
            {
                var model = new UpdateEmailValueForm();
                var controller = CreateController();

                var result = controller.ValidateValue(model);

                result.ShouldNotBeNull();
                result.ShouldBeType<JsonResult>();
                result.JsonRequestBehavior.ShouldEqual(JsonRequestBehavior.DenyGet);
                result.Data.ShouldEqual(true);
            }
            public void ReturnsRedirect_ToModelReturnUrl_AfterCommandIsExecuted()
            {
                const int number = 2;
                const string personUserName = "******";
                const string newValue = "*****@*****.**";
                const string principalIdentityName = "*****@*****.**";
                var scenarioOptions = new ScenarioOptions
                {
                    PrincipalIdentityName = principalIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    Number = number,
                    PersonUserName = personUserName,
                    Value = newValue,
                    ReturnUrl = "https://www.site.com/"
                };
                var controller = CreateController(scenarioOptions);
                var builder = ReuseMock.TestControllerBuilder();
                var principal = principalIdentityName.AsPrincipal();
                builder.HttpContext.User = principal;
                builder.InitializeController(controller);
                scenarioOptions.MockCommandHandler.Setup(m => m.Handle(It.Is(CommandBasedOn(model))));

                var result = controller.Put(model);

                result.ShouldNotBeNull();
                result.ShouldBeType<RedirectResult>();
                var redirectResult = (RedirectResult) result;
                redirectResult.Url.ShouldEqual(model.ReturnUrl);
                redirectResult.Permanent.ShouldBeFalse();
            }
            public void Returns404_WhenPersonUserName_IsNotEqualTo_UserIdentityName()
            {
                const string userIdentityName = "*****@*****.**";
                const string personUserName = "******";
                var scenarioOptions = new ScenarioOptions
                {
                    PrincipalIdentityName = userIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    PersonUserName = personUserName,
                };
                var controller = CreateController(scenarioOptions);

                var result = controller.Put(model);

                result.ShouldNotBeNull();
                result.ShouldBeType<HttpNotFoundResult>();
            }
            public void IgnoresChangedState()
            {
                var model = new UpdateEmailValueForm();

                var command = Mapper.Map<UpdateMyEmailValueCommand>(model);

                command.ShouldNotBeNull();
                command.ChangedState.ShouldBeFalse();
            }
            public void MapsValue_ToNewValue()
            {
                const string value = "*****@*****.**";
                var model = new UpdateEmailValueForm { Value = value };

                var command = Mapper.Map<UpdateMyEmailValueCommand>(model);

                command.ShouldNotBeNull();
                command.NewValue.ShouldNotBeNull();
                command.NewValue.ShouldEqual(model.Value);
            }
 public void IsInvalidWhen_Number_DoesNotMatchEmail_ForNonNullPersonUserName()
 {
     const string personUserName = "******";
     const string emailValue = "*****@*****.**";
     var form = new UpdateEmailValueForm
     {
         PersonUserName = personUserName,
         Number = 13,
         Value = emailValue.ToUpper(),
     };
     var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
     entities.Setup(m => m.Query<EmailAddress>()).Returns(new EmailAddress[] { }.AsQueryable);
     var validator = new UpdateEmailValueValidator(entities.Object);
     var results = validator.Validate(form);
     results.IsValid.ShouldBeFalse();
     results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
     var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);
     error.ShouldNotBeNull();
     // ReSharper disable PossibleNullReferenceException
     error.ErrorMessage.ShouldEqual(string.Format(
         ValidateEmailAddress.FailedBecauseNumberAndPrincipalMatchedNoEntity,
             form.Number, form.PersonUserName));
     // ReSharper restore PossibleNullReferenceException
 }
Exemple #25
0
 public virtual JsonResult ValidateValue(
     [CustomizeValidator(Properties = UpdateEmailValueForm.ValuePropertyName)] UpdateEmailValueForm model)
 {
     return(ValidateRemote(UpdateEmailValueForm.ValuePropertyName));
 }
            public void ExecutesCommand_WhenAction_IsValid()
            {
                const int number = 2;
                const string personUserName = "******";
                const string newValue = "*****@*****.**";
                const string principalIdentityName = "*****@*****.**";
                var scenarioOptions = new ScenarioOptions
                {
                    PrincipalIdentityName = principalIdentityName,
                };
                var model = new UpdateEmailValueForm
                {
                    Number = number,
                    PersonUserName = personUserName,
                    Value = newValue,
                    ReturnUrl = "https://www.site.com/"
                };
                var controller = CreateController(scenarioOptions);
                var builder = ReuseMock.TestControllerBuilder();
                var principal = principalIdentityName.AsPrincipal();
                builder.HttpContext.User = principal;
                builder.InitializeController(controller);
                scenarioOptions.MockCommandHandler.Setup(m => m.Handle(It.Is(CommandBasedOn(model))));

                controller.Put(model);

                scenarioOptions.MockCommandHandler.Verify(m =>
                    m.Handle(It.Is(CommandBasedOn(model))),
                        Times.Once());
            }
Exemple #27
0
        private static Expression <Func <UpdateMyEmailValueCommand, bool> > CommandBasedOn(UpdateEmailValueForm model)
        {
            Expression <Func <UpdateMyEmailValueCommand, bool> > commandDerivedFromModel = command =>
                                                                                           command.Number == model.Number &&
                                                                                           command.Principal.Identity.Name == model.PersonUserName &&
                                                                                           command.NewValue == model.Value
            ;

            return(commandDerivedFromModel);
        }
 public void IsInvalidWhen_Value_DoesNotMatchPreviousSpelling_CaseInsensitively()
 {
     var model = new UpdateEmailValueForm
     {
         Value = "*****@*****.**",
         PersonUserName = string.Empty
     };
     var emailAddress = new EmailAddress
     {
         Value = "*****@*****.**",
         Person = new Person { User = new User { Name = model.PersonUserName }, }
     };
     var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
     entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable);
     var validator = new UpdateEmailValueValidator(entities.Object);
     var results = validator.Validate(model);
     results.IsValid.ShouldBeFalse();
     results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
     var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);
     error.ShouldNotBeNull();
     // ReSharper disable PossibleNullReferenceException
     error.ErrorMessage.ShouldEqual(
         UpdateEmailValueValidator.FailedBecausePreviousSpellingDoesNotMatchValueCaseInsensitively);
     // ReSharper restore PossibleNullReferenceException
 }
 public void HasPublicSetter()
 {
     var obj = new UpdateEmailValueForm
     {
         OldSpelling = "*****@*****.**"
     };
     obj.ShouldNotBeNull();
 }
            public void MapsNumber_ToNumber()
            {
                const int number = 2;
                var model = new UpdateEmailValueForm { Number = number };

                var command = Mapper.Map<UpdateMyEmailValueCommand>(model);

                command.ShouldNotBeNull();
                command.Number.ShouldEqual(model.Number);
            }