Skip to content

Commit

Permalink
chore: Align with core and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGT96 committed Nov 5, 2024
1 parent 8315a32 commit fda3dd4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public UserGroupDTO updateUserGroup(@PathVariable("group_code") String code, @Va
.toList();
}

boolean isUpdated = userManager.updateUserGroup(group, permissions);
if (isUpdated) {
UserGroup updatedUserGroup = userManager.updateUserGroup(group, permissions);
if (updatedUserGroup != null) {
return getUserGroup(group.getCode());
} else {
throw new OHAPIException(new OHExceptionMessage("User group not updated."));
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/isf/users/rest/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,16 @@ public UserDTO updateUser(
throw new OHAPIException(new OHExceptionMessage("The specified user does not exist."), HttpStatus.NOT_FOUND);
}
User user = userMapper.map2Model(userDTO);
boolean isUpdated;
User updatedUser;
if (!user.getPasswd().isEmpty()) {
isUpdated = userManager.updatePassword(user);
updatedUser = userManager.updatePassword(user);
} else {
isUpdated = userManager.updateUser(user);
updatedUser = userManager.updateUser(user);
}
if (isUpdated) {
if (updatedUser != null) {
LOGGER.info("User {} has been updated successfully.", userName);
user = userManager.getUserByName(userName);
user.setPasswd(null);
return userMapper.map2DTO(user);
updatedUser.setPasswd(null);
return userMapper.map2DTO(updatedUser);
} else {
throw new OHAPIException(new OHExceptionMessage("User not updated."));
}
Expand Down Expand Up @@ -226,13 +225,13 @@ public UserProfileDTO updateProfile(
}
User user = userMapper.map2Model(userDTO);
user.setUserGroupName(entity.getUserGroupName());
boolean isUpdated;
User updatedUser;
if (!user.getPasswd().isEmpty()) {
isUpdated = userManager.updatePassword(user);
updatedUser = userManager.updatePassword(user);
} else {
isUpdated = userManager.updateUser(user);
updatedUser = userManager.updateUser(user);
}
if (isUpdated) {
if (updatedUser != null) {
LOGGER.info("User {} has been successfully updated.", currentUser);
return retrieveProfile(currentUser);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void deleteUserGroup() throws Exception {
UserGroupDTO userGroupDTO = userGroupMapper.map2DTO(userGroup);

when(userManager.getUserGroup()).thenReturn(List.of(userGroup));
doNothing().when(userManager).deleteGroup(any());
when(userManager.deleteGroup(any())).thenReturn(userGroup);

var result = mvc.perform(
delete("/usergroups/{group_code}", userGroup.getCode()).contentType(MediaType.APPLICATION_JSON))
Expand All @@ -206,7 +206,7 @@ void shouldUpdateUserGroup() throws Exception {
userGroupDTO.setPermissions(permissionDTOS);

when(userManager.findUserGroupByCode(any())).thenReturn(userGroup);
when(userManager.updateUserGroup(any(), any())).thenReturn(true);
when(userManager.updateUserGroup(any(), any())).thenReturn(userGroup);

var result = mvc.perform(
put("/usergroups/{group_code}", userGroupDTO.getCode())
Expand Down
54 changes: 28 additions & 26 deletions src/test/java/org/isf/users/rest/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.isf.users.rest;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.isf.OpenHospitalApiApplication;
import org.isf.menu.manager.UserBrowsingManager;
import org.isf.menu.model.User;
Expand Down Expand Up @@ -80,7 +81,7 @@ public class UserControllerTest {
private PermissionManager permissionManager;

@Test
@WithMockUser(username = "admin", authorities = {"users.create"})
@WithMockUser(username = "admin", authorities = { "users.create" })
@DisplayName("Create user")
void createUser() throws Exception {
User user = UserHelper.generateUser();
Expand All @@ -101,13 +102,13 @@ void createUser() throws Exception {
}

@Test
@WithMockUser(username = "admin", authorities = {"users.delete"})
@WithMockUser(username = "admin", authorities = { "users.delete" })
@DisplayName("Delete user")
void deleteUser() throws Exception {
User user = UserHelper.generateUser();

when(userManager.getUserByName(any())).thenReturn(user);
doNothing().when(userManager).deleteUser(any());
when(userManager.deleteUser(any())).thenReturn(user);

var result = mvc.perform(
delete("/users/{username}", user.getUserName()).contentType(MediaType.APPLICATION_JSON))
Expand All @@ -121,8 +122,9 @@ void deleteUser() throws Exception {
@Nested
@DisplayName("Get user")
class GetUser {

@Test
@WithMockUser(username = "admin", authorities = {"users.read"})
@WithMockUser(username = "admin", authorities = { "users.read" })
@DisplayName("Should get user by name")
void shouldGetUserByName() throws Exception {
User user = UserHelper.generateUsers(1).get(0);
Expand Down Expand Up @@ -152,7 +154,7 @@ void shouldThrowForbiddenErrorWhenInsufficientPermissions() throws Exception {
}

@Test
@WithMockUser(username = "user", authorities = {"users.read"})
@WithMockUser(username = "user", authorities = { "users.read" })
@DisplayName("Should throw not found error when user not found")
void shouldThrowNotFoundErrorWhenUserNotFound() throws Exception {
when(userManager.getUserByName(any())).thenReturn(null);
Expand Down Expand Up @@ -191,7 +193,7 @@ void shouldGetUserProfile() throws Exception {
class GetUsers {

@Test
@WithMockUser(username = "admin", authorities = {"users.read"})
@WithMockUser(username = "admin", authorities = { "users.read" })
@DisplayName("Get all users")
void getAllUsers() throws Exception {
List<User> users = UserHelper.generateUsers(3);
Expand All @@ -210,7 +212,7 @@ void getAllUsers() throws Exception {
}

@Test
@WithMockUser(username = "admin", authorities = {"users.read"})
@WithMockUser(username = "admin", authorities = { "users.read" })
@DisplayName("Get users by group code")
void getAllUsersByGroupCode() throws Exception {
List<User> users = UserHelper.generateUsers(3);
Expand All @@ -230,7 +232,7 @@ void getAllUsersByGroupCode() throws Exception {
}

@Test
@WithMockUser(username = "admin", authorities = {"users.read"})
@WithMockUser(username = "admin", authorities = { "users.read" })
@DisplayName("Get user by name")
void getUserByName() throws Exception {
User user = UserHelper.generateUser();
Expand Down Expand Up @@ -258,12 +260,12 @@ class UpdateUserTests {

@Test
@DisplayName("Should update the user")
@WithMockUser(username = "admin", authorities = {"users.read", "users.update"})
@WithMockUser(username = "admin", authorities = { "users.read", "users.update" })
void shouldUpdateTheUser() throws Exception {
var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user");

when(userManager.updateUser(any())).thenReturn(true);
when(userManager.updatePassword(any())).thenReturn(true);
when(userManager.updateUser(any())).thenReturn(user);
when(userManager.updatePassword(any())).thenReturn(user);
when(userManager.getUserByName(any())).thenReturn(user);

var result = mvc.perform(
Expand All @@ -277,7 +279,7 @@ void shouldUpdateTheUser() throws Exception {

@Test
@DisplayName("Should fail to update when username in the patch doesn't match")
@WithMockUser(username = "admin", authorities = {"users.read", "users.update"})
@WithMockUser(username = "admin", authorities = { "users.read", "users.update" })
void shouldFailToUpdateWhenUsernameInThePathDoesNtMatch() throws Exception {
var user = new User("laboratorist", new UserGroup("doctor", "Doctor group"), "", "Simple user");
var result = mvc.perform(
Expand All @@ -291,7 +293,7 @@ void shouldFailToUpdateWhenUsernameInThePathDoesNtMatch() throws Exception {

@Test
@DisplayName("Should fail to update user when insufficient permissions")
@WithMockUser(username = "admin", authorities = {"roles.read"})
@WithMockUser(username = "admin", authorities = { "roles.read" })
void shouldFailToUpdateUserWhenInsufficientPermissions() throws Exception {
var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user");

Expand All @@ -306,7 +308,7 @@ void shouldFailToUpdateUserWhenInsufficientPermissions() throws Exception {

@Test
@DisplayName("Should fail when user to update doesn't exist")
@WithMockUser(username = "admin", authorities = {"users.read", "users.update"})
@WithMockUser(username = "admin", authorities = { "users.read", "users.update" })
void shouldFailWhenUserToUpdateIsNotFound() throws Exception {
var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user");

Expand All @@ -323,12 +325,12 @@ void shouldFailWhenUserToUpdateIsNotFound() throws Exception {

@Test
@DisplayName("Should update only password")
@WithMockUser(username = "admin", authorities = {"users.read", "users.update"})
@WithMockUser(username = "admin", authorities = { "users.read", "users.update" })
void shouldUpdateOnlyPassword() throws Exception {
var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "?..passwordAA", "Simple user");

when(userManager.updateUser(any())).thenReturn(true);
when(userManager.updatePassword(any())).thenReturn(true);
when(userManager.updateUser(any())).thenReturn(user);
when(userManager.updatePassword(any())).thenReturn(user);
when(userManager.getUserByName(any())).thenReturn(user);

var result = mvc.perform(
Expand All @@ -345,12 +347,12 @@ void shouldUpdateOnlyPassword() throws Exception {

@Test
@DisplayName("Should update user without updating password")
@WithMockUser(username = "admin", authorities = {"users.read", "users.update"})
@WithMockUser(username = "admin", authorities = { "users.read", "users.update" })
void shouldUpdateUserWithoutUpdatingPassword() throws Exception {
var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user");

when(userManager.updateUser(any())).thenReturn(true);
when(userManager.updatePassword(any())).thenReturn(true);
when(userManager.updateUser(any())).thenReturn(user);
when(userManager.updatePassword(any())).thenReturn(user);
when(userManager.getUserByName(any())).thenReturn(user);

var result = mvc.perform(
Expand Down Expand Up @@ -384,8 +386,8 @@ public void setup() throws OHServiceException {
void shouldUpdateUserProfile() throws Exception {
var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user");

when(userManager.updateUser(any())).thenReturn(true);
when(userManager.updatePassword(any())).thenReturn(true);
when(userManager.updateUser(any())).thenReturn(user);
when(userManager.updatePassword(any())).thenReturn(user);
when(userManager.getUserByName(any())).thenReturn(user);

var result = mvc.perform(
Expand Down Expand Up @@ -433,8 +435,8 @@ void shouldFailToUpdateUserWhenNotAuthenticated() throws Exception {
void shouldUpdateOnlyPassword() throws Exception {
var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "?..passwordAA", "Simple user");

when(userManager.updateUser(any())).thenReturn(true);
when(userManager.updatePassword(any())).thenReturn(true);
when(userManager.updateUser(any())).thenReturn(user);
when(userManager.updatePassword(any())).thenReturn(user);
when(userManager.getUserByName(any())).thenReturn(user);

var result = mvc.perform(
Expand All @@ -455,8 +457,8 @@ void shouldUpdateOnlyPassword() throws Exception {
void shouldUpdateUserWithoutUpdatingPassword() throws Exception {
var user = new User("doctor", new UserGroup("doctor", "Doctor group"), "", "Simple user");

when(userManager.updateUser(any())).thenReturn(true);
when(userManager.updatePassword(any())).thenReturn(true);
when(userManager.updateUser(any())).thenReturn(user);
when(userManager.updatePassword(any())).thenReturn(user);
when(userManager.getUserByName(any())).thenReturn(user);

var result = mvc.perform(
Expand Down

0 comments on commit fda3dd4

Please sign in to comment.