public void Validate_Successful_With_Valid_Information()
        {
            // Arrange
            var model = new UpdateUserInputModel
            {
                UserId = 1,
                Email = "*****@*****.**",
                Password = "******",
                ConfirmPassword = "******"
            };
            var validationDictionary = new ValidationDictionary();

            // Act

            var result = model.ValidateRequest(validationDictionary);

            // Assert
            Assert.IsTrue(result);
        }
        public void Validate_Fails_Mismatched_Passwords()
        {
            // Arrange
            var model = new UpdateUserInputModel
                {
                    UserId = 1,
                    Email = "*****@*****.**",
                    Password = "******",
                    ConfirmPassword = "******"
                };
            var validationDictionary = new ValidationDictionary();

            // Act

            var result = model.ValidateRequest(validationDictionary);

            // Assert
            Assert.IsFalse(result);
        }
        public BaseResponseModel Update(UpdateUserInputModel inputModel)
        {
            var vm = new BaseResponseModel();

            // Validate request
            var validationState = new ValidationDictionary();
            inputModel.ValidateRequest(validationState);

            // Get existing user
            var user = UserService.GetUserById(inputModel.UserId);
            if (user == null)
            {
                throw new HttpException(404, "User not found.");
            }

            // Do not allow editing of users other than yourself if you
            // don't have permissions
            if (!CurrentUser.HasPermission(Permission.EditUsers)
                && user.Id != CurrentUser.Id)
            {
                throw new HttpException(401, "You do not have permissions to complete this action.");
            }

            // Copy properties
            bool emailChanged = user.Email != inputModel.Email;
            user.Email = inputModel.Email;
            string newPass = String.IsNullOrWhiteSpace(inputModel.Password)
                ? null : inputModel.Password;

            // Additional properties for admin users
            if (CurrentUser.HasPermission(Permission.EditUsers))
            {
                if (inputModel.Role.HasValue) user.Role = inputModel.Role.Value;
            }

            if (UserService.ValidateUser(user, validationState))
            {
                UserService.UpdateUser(user, newPass);
                if (emailChanged)
                {
                    ReAuthorizeUser(inputModel.Email);
                }

                LogService.CreateLog(new Log
                {
                    Category = LogCategory.Application,
                    IpAddress = GetClientIp(ControllerContext.Request),
                    Level = LogLevel.Info,
                    Message = "User " + inputModel.Email + " (ID #" + user.Id + ") was updated.",
                    User = CurrentUser
                });

                vm.Success = true;
            }

            vm.Errors = validationState.Errors;
            return vm;
        }