public HttpResponseMessage ChangePassword(UserChangePasswordData changePasswordData) { if ((string.IsNullOrEmpty(changePasswordData.OldPassword)) || (string.IsNullOrEmpty(changePasswordData.NewPassword))) throw new HttpResponseException(HttpStatusCode.BadRequest); int authUserId = credentialsChecker.GetAuthUserId(HttpContext.Current); if (authUserId == -1) throw new HttpResponseException(HttpStatusCode.Unauthorized); User user = repositoryUsers.GetSingleUser(authUserId); if (user == null) throw new HttpResponseException(HttpStatusCode.NotFound); repositoryUsers.ChangePassword(ref user, changePasswordData.OldPassword, changePasswordData.NewPassword); return Request.CreateResponse(HttpStatusCode.OK); }
public void ChangePasswordShouldCallRepositoryTest() { UserRepositorySpy spy_repository = new UserRepositorySpy(); CredentialsCheckerSpy_AlwaysPass spy_checker = new CredentialsCheckerSpy_AlwaysPass(); UsersController target = new UsersController(spy_repository, spy_checker); UserChangePasswordData user = new UserChangePasswordData() { NewPassword = "******", OldPassword = "******" }; spy_repository.GetSingleUserResult = new User() { Id = 1, Login = "******" }; var config = new HttpConfiguration(); var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/1.0/users/changepassword"); var route = config.Routes.MapHttpRoute("TestApi", "api/1.0/{controller}/{action}"); var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "users" }, { "action", "changepasword" } }); target.ControllerContext = new HttpControllerContext(config, routeData, request); target.Request = request; target.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; HttpResponseMessage actual = target.ChangePassword(user); Assert.IsTrue(spy_repository.ChangePasswordNewPassword == "new_password" && spy_repository.ChangePasswordOldPassword == "old_password" && spy_repository.ChangePasswordUser.Id == 1); }