public void WillShowTheViewWithErrorsIfTheModelStateIsInvalid()
            {
                var controller = new TestableUsersController();

                controller.ModelState.AddModelError(String.Empty, "aFakeError");

                var result = controller.Register(null) as ViewResult;

                Assert.NotNull(result);
                Assert.Empty(result.ViewName);
            }
            public void WillInvalidateModelStateAndShowTheViewWhenAnEntityExceptionIsThrow()
            {
                var controller = new TestableUsersController();

                controller.MockUserService
                .Setup(x => x.Create(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Throws(new EntityException("aMessage"));

                var result = controller.Register(
                    new RegisterRequest
                {
                    Username     = "******",
                    Password     = "******",
                    EmailAddress = "theEmailAddress",
                }) as ViewResult;

                Assert.NotNull(result);
                Assert.Empty(result.ViewName);
                Assert.False(controller.ModelState.IsValid);
                Assert.Equal("aMessage", controller.ModelState[String.Empty].Errors[0].ErrorMessage);
            }
            public void WillCreateTheUser()
            {
                var controller = new TestableUsersController();

                controller.MockUserService
                .Setup(x => x.Create(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(new User {
                    Username = "******", EmailAddress = "*****@*****.**"
                });

                controller.Register(
                    new RegisterRequest
                {
                    Username     = "******",
                    Password     = "******",
                    EmailAddress = "theEmailAddress",
                });

                controller.MockUserService
                .Verify(x => x.Create("theUsername", "thePassword", "theEmailAddress"));
            }
            public void WillSendNewUserEmailIfConfirmationRequired()
            {
                string      sentConfirmationUrl = null;
                MailAddress sentToAddress       = null;
                var         controller          = new TestableUsersController();

                controller.MockMessageService
                .Setup(m => m.SendNewAccountEmail(It.IsAny <MailAddress>(), It.IsAny <string>()))
                .Callback <MailAddress, string>((to, url) =>
                {
                    sentToAddress       = to;
                    sentConfirmationUrl = url;
                });
                controller.MockUserService
                .Setup(x => x.Create(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(new User
                {
                    Username               = "******",
                    EmailAddress           = "*****@*****.**",
                    EmailConfirmationToken = "confirmation"
                });
                controller.MockConfig
                .Setup(x => x.ConfirmEmailAddresses)
                .Returns(true);

                controller.Register(
                    new RegisterRequest
                {
                    Username     = "******",
                    Password     = "******",
                    EmailAddress = "*****@*****.**",
                });

                // We use a catch-all route for unit tests so we can see the parameters
                // are passed correctly.
                Assert.Equal("https://example.org/?Controller=Users&Action=Confirm&username=theUsername&token=confirmation", sentConfirmationUrl);
                Assert.Equal("*****@*****.**", sentToAddress.Address);
            }
            public void WillSendNewUserEmailIfConfirmationRequired()
            {
                string sentConfirmationUrl = null;
                MailAddress sentToAddress = null;
                var controller = new TestableUsersController();
                controller.MockMessageService
                          .Setup(m => m.SendNewAccountEmail(It.IsAny<MailAddress>(), It.IsAny<string>()))
                          .Callback<MailAddress, string>((to, url) =>
                          {
                              sentToAddress = to;
                              sentConfirmationUrl = url;
                          });
                controller.MockUserService
                          .Setup(x => x.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
                          .Returns(new User
                          {
                              Username = "******",
                              EmailAddress = "*****@*****.**",
                              EmailConfirmationToken = "confirmation"
                          });
                controller.MockConfig
                          .Setup(x => x.ConfirmEmailAddresses)
                          .Returns(true);
                
                controller.Register(
                    new RegisterRequest
                        {
                            Username = "******",
                            Password = "******",
                            EmailAddress = "*****@*****.**",
                        });

                // We use a catch-all route for unit tests so we can see the parameters 
                // are passed correctly.
                Assert.Equal("https://example.org/?Controller=Users&Action=Confirm&username=theUsername&token=confirmation", sentConfirmationUrl);
                Assert.Equal("*****@*****.**", sentToAddress.Address);
            }
            public void WillInvalidateModelStateAndShowTheViewWhenAnEntityExceptionIsThrow()
            {
                var controller = new TestableUsersController();
                controller.MockUserService
                          .Setup(x => x.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
                          .Throws(new EntityException("aMessage"));
                
                var result = controller.Register(
                    new RegisterRequest
                        {
                            Username = "******",
                            Password = "******",
                            EmailAddress = "theEmailAddress",
                        }) as ViewResult;

                Assert.NotNull(result);
                Assert.Empty(result.ViewName);
                Assert.False(controller.ModelState.IsValid);
                Assert.Equal("aMessage", controller.ModelState[String.Empty].Errors[0].ErrorMessage);
            }
            public void WillCreateTheUser()
            {
                var controller = new TestableUsersController();
                controller.MockUserService
                          .Setup(x => x.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
                          .Returns(new User { Username = "******", EmailAddress = "*****@*****.**" });
                
                controller.Register(
                    new RegisterRequest
                        {
                            Username = "******",
                            Password = "******",
                            EmailAddress = "theEmailAddress",
                        });

                controller.MockUserService
                          .Verify(x => x.Create("theUsername", "thePassword", "theEmailAddress"));
            }
            public void WillShowTheViewWithErrorsIfTheModelStateIsInvalid()
            {
                var controller = new TestableUsersController();
                controller.ModelState.AddModelError(String.Empty, "aFakeError");

                var result = controller.Register(null) as ViewResult;

                Assert.NotNull(result);
                Assert.Empty(result.ViewName);
            }