From 2c9ea70b2324bca8074098e48f6862c4dc08a272 Mon Sep 17 00:00:00 2001 From: SteveGT96 Date: Thu, 7 Nov 2024 16:40:22 +0100 Subject: [PATCH] fix: Fix user / group controller tests --- .../rest/UserGroupControllerTest.java | 1 + .../isf/users/rest/UserControllerTest.java | 75 ++++++++++--------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/test/java/org/isf/usergroups/rest/UserGroupControllerTest.java b/src/test/java/org/isf/usergroups/rest/UserGroupControllerTest.java index c4337a65..26a89552 100644 --- a/src/test/java/org/isf/usergroups/rest/UserGroupControllerTest.java +++ b/src/test/java/org/isf/usergroups/rest/UserGroupControllerTest.java @@ -205,6 +205,7 @@ void shouldUpdateUserGroup() throws Exception { UserGroupDTO userGroupDTO = userGroupMapper.map2DTO(userGroup); userGroupDTO.setPermissions(permissionDTOS); + when(userManager.findUserGroupByCode(userGroup.getCode(), true)).thenReturn(userGroup); when(userManager.findUserGroupByCode(any())).thenReturn(userGroup); when(userManager.updateUserGroup(any(), any())).thenReturn(userGroup); diff --git a/src/test/java/org/isf/users/rest/UserControllerTest.java b/src/test/java/org/isf/users/rest/UserControllerTest.java index 6f9c5e90..2644fd21 100644 --- a/src/test/java/org/isf/users/rest/UserControllerTest.java +++ b/src/test/java/org/isf/users/rest/UserControllerTest.java @@ -21,7 +21,23 @@ */ package org.isf.users.rest; -import com.fasterxml.jackson.databind.ObjectMapper; +import static org.hamcrest.Matchers.containsString; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.Collections; +import java.util.List; import org.isf.OpenHospitalApiApplication; import org.isf.menu.manager.UserBrowsingManager; @@ -47,17 +63,7 @@ import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.Matchers.containsString; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.fasterxml.jackson.databind.ObjectMapper; @SpringBootTest(classes = OpenHospitalApiApplication.class) @AutoConfigureMockMvc @@ -107,7 +113,7 @@ void createUser() throws Exception { void deleteUser() throws Exception { User user = UserHelper.generateUser(); - when(userManager.getUserByName(any())).thenReturn(user); + when(userManager.getUserByName(user.getUserName(), true)).thenReturn(user); doNothing().when(userManager).deleteUser(any()); var result = mvc.perform( @@ -118,6 +124,24 @@ void deleteUser() throws Exception { LOGGER.debug("result: {}", result); } + @Test + @WithMockUser(username = "john") + @DisplayName("Should get user profile") + void shouldGetUserProfile() throws Exception { + User user = UserHelper.generateUsers(1).get(0); + user.setUserName("john"); + + when(permissionManager.retrievePermissionsByUsername(user.getUserName())).thenReturn(Collections.emptyList()); + when(userManager.getUserByName(user.getUserName())).thenReturn(user); + + var result = mvc.perform( + get("/users/me").contentType(MediaType.APPLICATION_JSON)) + .andDo(log()) + .andExpect(status().isOk()) + .andReturn(); + + LOGGER.debug("result: {}", result); + } @Nested @DisplayName("Get user") @@ -169,25 +193,6 @@ void shouldThrowNotFoundErrorWhenUserNotFound() throws Exception { } } - @Test - @WithMockUser(username = "john") - @DisplayName("Should get user profile") - void shouldGetUserProfile() throws Exception { - User user = UserHelper.generateUsers(1).get(0); - user.setUserName("john"); - - when(permissionManager.retrievePermissionsByUsername(user.getUserName())).thenReturn(Collections.emptyList()); - when(userManager.getUserByName(user.getUserName())).thenReturn(user); - - var result = mvc.perform( - get("/users/me").contentType(MediaType.APPLICATION_JSON)) - .andDo(log()) - .andExpect(status().isOk()) - .andReturn(); - - LOGGER.debug("result: {}", result); - } - @Nested @DisplayName("Get users") class GetUsers { @@ -266,7 +271,7 @@ void shouldUpdateTheUser() throws Exception { when(userManager.updateUser(any())).thenReturn(user); when(userManager.updatePassword(any())).thenReturn(user); - when(userManager.getUserByName(any())).thenReturn(user); + when(userManager.isUserNamePresent(user.getUserName())).thenReturn(true); var result = mvc.perform( put("/users/doctor").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(userMapper.map2DTO(user)))) @@ -331,7 +336,7 @@ void shouldUpdateOnlyPassword() throws Exception { when(userManager.updateUser(any())).thenReturn(user); when(userManager.updatePassword(any())).thenReturn(user); - when(userManager.getUserByName(any())).thenReturn(user); + when(userManager.isUserNamePresent(user.getUserName())).thenReturn(true); var result = mvc.perform( put("/users/doctor").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(userMapper.map2DTO(user)))) @@ -353,7 +358,7 @@ void shouldUpdateUserWithoutUpdatingPassword() throws Exception { when(userManager.updateUser(any())).thenReturn(user); when(userManager.updatePassword(any())).thenReturn(user); - when(userManager.getUserByName(any())).thenReturn(user); + when(userManager.isUserNamePresent(user.getUserName())).thenReturn(true); var result = mvc.perform( put("/users/doctor").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(userMapper.map2DTO(user))))