Esempio n. 1
0
        public ActionResult ChangePassword(PasswordViewModel viewModel)
        {
            try
            {
                if (!((bool?)this.Session["LoggedIn"] ?? false))
                    return RedirectToAction("Login", "Account");
                using (var proxy = factory.GetWorkChannel())
                {
                    UserDTO user = ((UserDTO)this.Session["User"]);
                    var newUser = proxy.UpdateUser(new UpdateRequest<UserDTO>
                    {
                        Login = user.Email,
                        Password = user.Password,
                        Data = new UserDTO
                        {
                            UserID = user.UserID,
                            Email = user.Email,
                            Name = user.Name,
                            Password = viewModel.NewPassword,
                            Phone = user.Phone,
                            Address = user.Address,
                            Rights = user.Rights
                        }
                    });

                    this.Session["User"] = newUser.Data;
                }
            }
            catch (Exception e)
            {
                return HandleFaults(e);
            }

            return RedirectToAction("Index");
        }
        public void HomeControllerChangePassword()
        {
            const bool ISAJAX = true;
            var info = new PasswordViewModel()
            {
                NewPassword = "******",
                NewPasswordAgain = "Password2",
                Password = "******"
            };

            var factory = new WorkChannelFactoryMock();
            HomeController ctrl = new HomeController(factory);
            FakeControllerSession.SetFakeControllerContext(ctrl, ISAJAX);

            //Redirect if not logged
            // TODO - maybe we need to remove this
            ctrl.Session["LoggedIn"] = false;
            var res1 = ctrl.ChangePassword(info) as RedirectToRouteResult;
            Assert.IsNotNull(res1);
            Assert.AreEqual(res1.RouteValues["action"], "Login");
            Assert.AreEqual(res1.RouteValues["controller"], "Account");

            //Pass if everything ok
            ctrl.Session["LoggedIn"] = true;
            var res2 = ctrl.ChangePassword(info) as RedirectToRouteResult;
            Assert.IsNotNull(res2);
            Assert.AreEqual(res2.RouteValues["action"], "Index");

            var upd = factory.LastWorkChannel.UsersUpdated;
            Assert.AreEqual(upd.Count, 1);
            Assert.AreEqual(upd[0].Password, ((UserDTO)ctrl.Session["User"]).Password);
            Assert.AreEqual(upd[0].Password, "Password2");

            //Fault when wrong user
            ((UserDTO)ctrl.Session["User"]).Email = "Unknown";
            var res3 = ctrl.ChangePassword(info) as ViewResult;
            Assert.IsNotNull(res3);
            Assert.AreEqual(res3.Model, "wrong user or password");
        }