Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return correct status codes and messages when a user changes their password #540

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class PasswordChangeDTO {
String oldPassword = "";
@Getter
@Setter
@Length(min = UserServiceImpl.MIN_PASSWORD_LENGTH)
@NotEmpty
String newPassword = "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
Expand Down Expand Up @@ -89,8 +90,17 @@ public class CurrentUserRestController {
@PostMapping("/password")
public ResponseEntity<?> changeCurrentUserPassword(@AuthenticationPrincipal User user,
@RequestBody @Validated PasswordChangeDTO passwordChangeDTO) {
userService.changePassword(user.getId(),
passwordChangeDTO.getOldPassword(), passwordChangeDTO.getNewPassword());
try {
userService.changePassword(user.getId(),
passwordChangeDTO.getOldPassword(), passwordChangeDTO.getNewPassword());
} catch (Exception e) {
dsluijk marked this conversation as resolved.
Show resolved Hide resolved
if (e instanceof IllegalArgumentException) {
return createResponseEntity(HttpStatus.NOT_MODIFIED, e.getMessage());

} else if (e instanceof AccessDeniedException) {
return createResponseEntity(HttpStatus.FORBIDDEN, e.getMessage());
}
}

return createResponseEntity(HttpStatus.OK, "Password successfully changed");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public void testChangeShortPassword() {
contentType(ContentType.JSON).
post("/users/current/password").
then().
statusCode(HttpStatus.SC_BAD_REQUEST);
statusCode(HttpStatus.SC_NOT_MODIFIED);
//@formatter:on
}

Expand Down Expand Up @@ -796,6 +796,24 @@ public void testChangePasswordMissingOldPassword() {
//@formatter:on
}

@Test
public void testChangePasswordShortNewPassword() {
User user = createUser();
Map<String, String> passwordDTO = new HashMap<>();
passwordDTO.put("newPassword", "new");

//@formatter:off
given().
header(getXAuthTokenHeaderForUser(user)).
when().
body(passwordDTO).
contentType(ContentType.JSON).
post("/users/current/password").
then().
statusCode(HttpStatus.SC_BAD_REQUEST);
//@formatter:on
}

//TODO: Move to SchedulerTest
/* @Test
public void testExpiredUsersNoneExpired() {
Expand Down