Skip to content

Commit

Permalink
EPMRPP-97062 new delete user avatar endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
grabsefx committed Jan 15, 2025
1 parent ca6fd32 commit 0dbd4fc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public interface EditUserHandler {
*/
OperationCompletionRS deletePhoto(String username);

/**
* Delete user's photo.
*
* @param userId id of user
*/
void deletePhoto(Long userId);

/**
* Change password
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ public OperationCompletionRS deletePhoto(String login) {
return new OperationCompletionRS("Profile photo has been deleted successfully");
}

@Override
public void deletePhoto(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, userId));
expect(user.getUserType(), equalTo(INTERNAL)).verify(ACCESS_DENIED,
"Unable to change photo for external user");
userBinaryDataService.deleteUserPhoto(user);
}

@Override
public OperationCompletionRS changePassword(ReportPortalUser loggedInUser,
ChangePasswordRQ request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void getUserPhoto(@PathVariable String projectKey,
@Transactional
@PostMapping(value = "/photo", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
@Operation(summary = "Upload user's photo")
@Deprecated(forRemoval = true)
public OperationCompletionRS uploadPhoto(@RequestParam("file") MultipartFile file,
@AuthenticationPrincipal ReportPortalUser user) {
return editUserHandler.uploadPhoto(EntityUtils.normalizeId(user.getUsername()), file);
Expand All @@ -119,6 +120,7 @@ public OperationCompletionRS uploadPhoto(@RequestParam("file") MultipartFile fil
@Transactional
@DeleteMapping(value = "/photo")
@Operation(summary = "Delete user's photo")
@Deprecated(forRemoval = true)
public OperationCompletionRS deletePhoto(@AuthenticationPrincipal ReportPortalUser user) {
return editUserHandler.deletePhoto(EntityUtils.normalizeId(user.getUsername()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,12 @@ public ResponseEntity<Void> postUsersUserIdAvatar(Long userId,
editUserHandler.uploadPhoto(userId, file);
return new ResponseEntity<>(HttpStatus.CREATED);
}

@Override
@Transactional
@PreAuthorize(ALLOWED_TO_USER_ITSELF)
public ResponseEntity<Void> deleteUsersUserIdAvatar(Long userId) {
editUserHandler.deletePhoto(userId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ void userPhoto() throws Exception {
token(oAuthHelper.getDefaultToken())))
.andExpect(status().isOk());

mockMvc.perform(delete("/v1/data/photo").with(token(oAuthHelper.getDefaultToken())))
.andExpect(status().isOk());
mockMvc.perform(delete("/users/2/avatar").with(token(oAuthHelper.getDefaultToken())))
.andExpect(status().isNoContent());
}

@Test
@Sql("/db/user/user-viewer.sql")
public void testUserPhotoAccessDeniedForCustomer() throws Exception {
void testUserPhotoAccessDeniedForCustomer() throws Exception {
mockMvc.perform(get("/v1/data/default_personal/userphoto?login=default").with(
token(oAuthHelper.getCustomerToken())))
.andExpect(status().isForbidden());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ void userPhoto() throws Exception {
token(oAuthHelper.getDefaultToken())))
.andExpect(status().isOk());

mockMvc.perform(delete("/v1/data/photo").with(token(oAuthHelper.getDefaultToken())))
.andExpect(status().isOk());
mockMvc.perform(delete("/users/2/avatar").with(token(oAuthHelper.getDefaultToken())))
.andExpect(status().isNoContent());
}
}

0 comments on commit 0dbd4fc

Please sign in to comment.