public void InactiveUserCannotLogon()
        {
            Session.Store(new User(firstName: "f", lastName: "l", email: "*****@*****.**", password: "******"));
            Session.SaveChanges();

            bool loggedOn = false;
            var service = Mock.Of<IAuthenticationService>();
            Mock.Get(service).Setup(s => s.SetAuthCookie(It.IsAny<string>(), It.IsAny<bool>())).Callback(
                () => loggedOn = true);
            var controller = new AccountController(service)
                {
                    DocumentSession = Session,
                    Url = CreateUrlHelper()
                };
            var result = controller.LogOn(
                new LogOnViewModel
                    {
                        Email = "*****@*****.**",
                        Password = "******"
                    },
                string.Empty);
            Assert.True(controller.ModelState.ContainsKey("Inactive"));
            result.AssertViewRendered().ForView(string.Empty);

            Assert.False(loggedOn);
        }
        public void LogonReturnsView()
        {
            // Arrange
            var controller = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };

            // Act
            var result = controller.LogOn();

            // Assert
            result.AssertViewRendered().ForView(string.Empty);
        }
 private ActionResult VerifyActivationKeyForUser(User user)
 {
     bool loggedSomebodyOn = false;
     var service = Mock.Of<IAuthenticationService>();
     Mock.Get(service)
         .Setup(s => s.SetAuthCookie(It.IsAny<string>(), It.IsAny<bool>()))
         .Callback(() => loggedSomebodyOn = true);
     var controller = new AccountController(service) { DocumentSession = Session };
     var result = controller.Verify(Guid.Parse(user.ActivationKey));
     Assert.False(loggedSomebodyOn);
     return result;
 }
        public void ShouldSignOut()
        {
            var service = Mock.Of<IAuthenticationService>();
            bool signedOut = false;
            Mock.Get(service)
                .Setup(s => s.SignOut())
                .Callback(() => signedOut = true);

            var controller = new AccountController(service) { DocumentSession = Session };
            var result = controller.LogOff();
            result.AssertActionRedirect().ToController("Home").ToAction("Index");
            Assert.True(signedOut);
        }
        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");
            }
        }
 public void ChangePasswordSuccessReturnsView()
 {
     var controller = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };
     var result = controller.ChangePasswordSuccess();
     result.AssertViewRendered().ForView(string.Empty);
 }
 public void UnknownIdFails()
 {
     var controller = new AccountController(Mock.Of<IAuthenticationService>()) { DocumentSession = Session };
     var result = controller.Verify(Guid.NewGuid());
     result.AssertActionRedirect().ToAction("Register");
 }
        public void UnknownUserCannotLogon()
        {
            var service = Mock.Of<IAuthenticationService>();
            bool cookieSet = false;
            Mock.Get(service)
                .Setup(x => x.SetAuthCookie(It.IsAny<string>(), It.IsAny<bool>()))
                .Callback(() => cookieSet = true);

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

            result.AssertViewRendered().ForView(string.Empty);
            Assert.False(cookieSet);
        }
        private AccountController SetupPasswordTest(Action cookieSetAction)
        {
            // create an active user
            CreateActivatedUser("F", "L", "*****@*****.**", "some pwd");

            var service = Mock.Of<IAuthenticationService>();
            Mock.Get(service).Setup(
                x => x.SetAuthCookie(It.Is<string>(s => s == "*****@*****.**"), It.Is<bool>(b => b == false))).Callback(
                    cookieSetAction);

            var controller = new AccountController(service)
                {
                    DocumentSession = Session,
                    Url = CreateUrlHelper()
                };
            return controller;
        }