Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 97a2be9

Browse files
committed
Fixed bug, where user could not be updated.
1 parent 0ecaab8 commit 97a2be9

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/main/java/de/filefighter/rest/domain/user/business/UserBusinessService.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void updateUser(long userId, UserRegisterForm userToUpdate, User authenti
163163

164164
Update newUpdate = new Update();
165165

166-
boolean changesWereMade = updateUserName(newUpdate, userToUpdate.getUsername());
166+
boolean changesWereMade = updateUserName(newUpdate, userEntityToUpdate, userToUpdate.getUsername());
167167
boolean passwordWasUpdated = updatePassword(newUpdate, userToUpdate.getPassword(), userToUpdate.getConfirmationPassword());
168168
changesWereMade = passwordWasUpdated || changesWereMade;
169169
boolean userGroupsWereUpdated = updateGroups(newUpdate, userToUpdate.getGroupIds(), authenticatedUserIsAdmin);
@@ -221,13 +221,19 @@ private boolean updatePassword(Update newUpdate, String password, String confirm
221221
return false;
222222
}
223223

224-
private boolean updateUserName(Update update, String username) {
224+
private boolean updateUserName(Update update, UserEntity userEntityToUpdate, String username) {
225225
if (null != username) {
226226
if (!stringIsValid(username))
227227
throw new UserNotUpdatedException("Wanted to change username, but username was not valid.");
228228

229-
if (null != getUserWithUsername(username))
230-
throw new UserNotUpdatedException("Username already taken.");
229+
UserEntity userWithNewUsername = getUserWithUsername(username);
230+
if (null != userWithNewUsername) {
231+
if (userWithNewUsername.getUserId() == userEntityToUpdate.getUserId()) {
232+
return false;
233+
} else {
234+
throw new UserNotUpdatedException("Username already taken.");
235+
}
236+
}
231237

232238
update.set("username", username);
233239
update.set("lowercaseUsername", username.toLowerCase());

src/test/java/de/filefighter/rest/domain/user/business/UserBusinessServiceUnitTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ void updateUserNameWorks() {
329329
when(userRepositoryMock.findByUserId(userId)).thenReturn(userEntityMock);
330330

331331
assertDoesNotThrow(() -> userBusinessService.updateUser(userId, userRegisterForm, authenticatedUser));
332+
333+
// updating the user with the same username works.
334+
UserRegisterForm anotherOne = UserRegisterForm.builder().username(userEntityMock.getUsername()).build();
335+
when(userRepositoryMock.findByLowercaseUsername(userEntityMock.getLowercaseUsername())).thenReturn(userEntityMock);
336+
337+
UserNotUpdatedException exception = assertThrows(UserNotUpdatedException.class,
338+
() -> userBusinessService.updateUser(userId, anotherOne, authenticatedUser));
339+
assertEquals(UserNotUpdatedException.getErrorMessagePrefix() + " No changes were made.", exception.getMessage());
332340
}
333341

334342
@Test

0 commit comments

Comments
 (0)