public void CanLogOnAfterRegisteringAndVerifyingAccount()
        {
            // register
            var model = new RegisterViewModel
                {
                    FirstName = "F",
                    LastName = "L",
                    Email = "*****@*****.**",
                    ConfirmEmail = "*****@*****.**",
                    Password = "******"
                };

            var controller1 = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };
            using (DomainEvent.Disable()) controller1.Register(model);

            // normally done by infrastructure (special action filter)
            Session.SaveChanges();

            // verify
            var registeredUser = Session.FindUserByEmail("*****@*****.**");
            Assert.NotNull(registeredUser);
            var key = registeredUser.ActivationKey;

            var controller2 = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };
            controller2.Verify(Guid.Parse(key));

            // logon
            var loggedOn = false;
            var service = Mock.Of<IAuthenticationService>();
            Mock.Get(service)
                .Setup(s => s.SetAuthCookie(It.Is<string>(e => e == "*****@*****.**"), It.IsAny<bool>()))
                .Callback(() => loggedOn = true);

            var controller3 = new AccountController(service)
                {
                    DocumentSession = Session,
                    Url = CreateUrlHelper()
                };
            controller3.LogOn(
                new LogOnViewModel
                    {
                        Email = "*****@*****.**",
                        Password = "******"
                    },
                string.Empty);

            Assert.True(loggedOn);
        }
        public void CannotRegisterSameEmailTwice()
        {
            // Arrange
            CreateActivatedUser("F", "L", "*****@*****.**", "some pwd");
            var controller = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };

            // Act
            var result = controller.Register(new RegisterViewModel { Email = "*****@*****.**" });

            // Assert
            result.AssertViewRendered().ForView(string.Empty);
            var view = result as ViewResult;
            Assert.NotNull(view);
            Assert.Equal(1, controller.ModelState.Count);
            Assert.True(controller.ModelState.ContainsKey("Email"));
        }
        public void RegisterDoesNotLogin()
        {
            using (DomainEvent.Disable())
            {
                var authService = Mock.Of<IAuthenticationService>();

                // assert through mock object
                Mock.Get(authService)
                    .Setup(s => s.SetAuthCookie(It.IsAny<string>(), It.IsAny<bool>()))
                    .Throws(new Exception("Register should not set authorization cookie"));
                var controller = new AccountController(authService) { DocumentSession = Session };
                controller.Register(new RegisterViewModel
                    {
                        FirstName = "f",
                        LastName = "l",
                        Email = "email"
                    });
            }
        }
        public void ShouldCreateInitializedUser()
        {
            var controller = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };
            using (DomainEvent.Disable())
                controller.Register(new RegisterViewModel
                {
                    FirstName = "first name",
                    LastName = "last name",
                    Email = "email",
                });

            // normally done by infrastructure (special action filter)
            Session.SaveChanges();

            var user = Session.FindUserByEmail("email");
            Assert.NotNull(user);
            Assert.Equal("first name", user.FirstName);
            Assert.Equal("last name", user.LastName);
            Assert.Equal("email", user.Email);
            Assert.False(user.IsActive);
        }
        public void ShouldInitializeNewUser()
        {
            NewUserCreatedEvent ev = null;
            using (DomainEvent.TestWith(e => ev = (NewUserCreatedEvent)e))
            {
                var controller = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };
                controller.Register(new RegisterViewModel
                {
                    FirstName = "first name",
                    LastName = "last name",
                    Email = "email",
                });
            }

            Assert.NotNull(ev);
        }
        public void SuccessfulRegisterRedirectsToSuccessPage()
        {
            using (DomainEvent.Disable())
            {
                var controller = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };
                var result = controller.Register(new RegisterViewModel
                {
                    FirstName = "f",
                    LastName = "l",
                    Email = "email"
                });

                result.AssertActionRedirect().ToAction("RegisterSuccess");
            }
        }