public static void CopyPropFromAccountViewModel(this ApplicationUser user, AccountViewModel userView,IList<IdentityRole> roles) 
 {
     user.UserName = userView.UserName;
     user.Email = userView.Email;
     user.PhoneNumber = userView.PhoneNumber;
     user.Roles.Clear();
     foreach (var role in userView.UserInRole)
     {
         user.Roles.Add(role);
     }
 }
        public ActionResult UpdateUser(AccountViewModel userAccount,[DataSourceRequest] DataSourceRequest request)
        {
            try
            {

                var persistentUser = db.Users.GetById(userAccount.Id);
                persistentUser.CopyPropFromAccountViewModel(userAccount, roles);
                db.Users.Update(persistentUser);
                db.SaveChanges();
                return Json(new[] { persistentUser.ToAccountViewModel(this.db) }.ToDataSourceResult(request, ModelState));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(ex.GetType().ToString(), ex.Message);
                return Json(new[] { userAccount }.ToDataSourceResult(request, ModelState));
            }
        }
        public ActionResult DeleteUser([DataSourceRequest]DataSourceRequest request, AccountViewModel user)
        {

                if (user != null && ModelState.IsValid)
                {
                        if (user.Id == User.Identity.GetUserId())
                        {
                            ModelState.AddModelError("", "You cannot delete current user.");
                        }
                        else
                        { 
                            db.Users.Delete(user.Id);
                            db.SaveChanges();
                        }
                }

                var users = db.Users.All().Select(AccountViewModel.ToAccountViewModel).ToList();

                return Json(users.ToDataSourceResult(request, ModelState));
        }
 public void DeleteUserTest()
 {
     // Arrange - create the controller
     Mock.Get(this.usersRepository).Setup(x => x.All()).Returns(new List<ApplicationUser>() { users[1], users[2] }.AsQueryable());
     UserAdministrationController target = new UserAdministrationController(UowData);
     target.ControllerContext = this.controllerContext;
     var user = new AccountViewModel() { Id = "1" };
     // Act - add a product to the cart
     var result = (target.DeleteUser(new DataSourceRequest(), user) as JsonResult).Data;
     // Assert
     Assert.AreEqual(2, (result as Kendo.Mvc.UI.DataSourceResult).Total);
 }
        public void UpdateUserWithInvalidEmailMustSendErrorMessageTest()
        {
            // Arrange - create the controller
            UserAdministrationController target = new UserAdministrationController(UowData);
            var user = new AccountViewModel() { Id = "1" };
            string newEmail = "test";
            RoleViewModel removeRole = new RoleViewModel() { Id = roles[0].Id, Name = roles[0].Name };
            string newPhone = "1234567";
            string newUserName = "******";

            user.Email = newEmail;
            user.PhoneNumber = newPhone;
            user.Roles = new List<RoleViewModel>() { new RoleViewModel(){ Id = "1", Name="Administrator" },
                                                     new RoleViewModel(){ Id = "2", Name="User" } };
            user.UserName = newUserName;

            // Act - add a product to the cart
            var result = (target.UpdateUser(user, new DataSourceRequest()) as JsonResult).Data;
            var errors = (result as Kendo.Mvc.UI.DataSourceResult).Errors;
            var count = (errors as Dictionary<string, Dictionary<string,object>>).Count;
            // Assert
            Assert.AreEqual(1,count);
        }
        public void UpdateUserTest()
        {
            // Arrange - create the controller
            UserAdministrationController target = new UserAdministrationController(UowData);
            var user = new AccountViewModel() { Id = "1" };
            string newEmail = "*****@*****.**";
            RoleViewModel removeRole = new RoleViewModel() { Id = roles[0].Id, Name = roles[0].Name };
            string newPhone = "1234567";
            string newUserName = "******";

            user.Email = newEmail;
            user.PhoneNumber = newPhone;
            user.Roles = new List<RoleViewModel>() { new RoleViewModel(){ Id = "1", Name="Administrator" },
                                                     new RoleViewModel(){ Id = "2", Name="User" } };
            user.UserName = newUserName;

            // Act - add a product to the cart
            var result = (target.UpdateUser(user, new DataSourceRequest()) as JsonResult).Data;
            user = (result as Kendo.Mvc.UI.DataSourceResult).Data.AsQueryable().Cast<AccountViewModel>().FirstOrDefault();
            // Assert
            Assert.AreEqual(newEmail, user.Email);
            Assert.AreEqual(newPhone, user.PhoneNumber);
            Assert.AreEqual(2, user.Roles.Count);
            Assert.AreEqual(newUserName, user.UserName);

        }