Skip to content

Commit

Permalink
fix: Fix user / group controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGT96 committed Nov 7, 2024
1 parent 405513c commit 2c9ea70
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
75 changes: 40 additions & 35 deletions src/test/java/org/isf/users/rest/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))))
Expand Down Expand Up @@ -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))))
Expand All @@ -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))))
Expand Down

0 comments on commit 2c9ea70

Please sign in to comment.