package com.example.erp.module.usr; import com.example.erp.config.BeanConfig; import com.example.erp.config.JwtAuthenticationFilter; import com.example.erp.config.JwtProperties; import com.example.erp.config.SecurityConfig; import com.example.erp.common.util.JwtUtil; import com.example.erp.module.usr.controller.UserController; import com.example.erp.module.usr.service.UserService; import com.example.erp.common.vo.PageVO; import com.example.erp.module.usr.dto.UserListQueryDTO; import com.example.erp.module.usr.dto.UserUpdateReqDTO; import com.example.erp.module.usr.vo.UserCreateRespVO; import com.example.erp.module.usr.vo.UserListItemVO; import com.example.erp.module.usr.vo.UserUpdateRespVO; import com.example.erp.module.usr.vo.StaffVO; import com.example.erp.module.usr.vo.PermissionGroupVO; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.RequestPostProcessor; import java.util.List; import java.util.Map; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @ActiveProfiles("test") @WebMvcTest(controllers = UserController.class) @Import({SecurityConfig.class, JwtAuthenticationFilter.class, BeanConfig.class, JwtUtil.class, JwtProperties.class}) class UserControllerTest { @Autowired private MockMvc mockMvc; @Autowired private ObjectMapper objectMapper; @Autowired private JwtUtil jwtUtil; @MockBean private UserService userService; RequestPostProcessor superAdmin() { String token = jwtUtil.generateAccessToken("u1", "admin", "超级管理员", "b1"); return request -> { request.addHeader("Authorization", "Bearer " + token); return request; }; } @Test void createUser_noAuth_returns401() throws Exception { mockMvc.perform(post("/api/usr/users") .contentType(MediaType.APPLICATION_JSON) .content("{}")) .andExpect(status().isUnauthorized()); } @Test void createUser_validRequest_returns200() throws Exception { UserCreateRespVO resp = new UserCreateRespVO(); resp.setUserId("new-user-id"); resp.setUserCode("UC001"); resp.setUsername("testuser"); when(userService.createUser(any(), any())).thenReturn(resp); Map body = Map.of( "userCode", "UC001", "username", "testuser", "userType", "普通用户", "language", "中文"); mockMvc.perform(post("/api/usr/users") .with(superAdmin()) .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(body))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data.userId").value("new-user-id")); } @Test void createUser_missingUserCode_returns40001() throws Exception { Map body = Map.of( "username", "testuser", "userType", "普通用户", "language", "中文"); mockMvc.perform(post("/api/usr/users") .with(superAdmin()) .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(body))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(40001)); } @Test void getStaffs_returns200() throws Exception { StaffVO s = new StaffVO(); s.setSId("s1"); s.setSStaffName("张三"); when(userService.getStaffs(anyString())).thenReturn(List.of(s)); mockMvc.perform(get("/api/usr/users/staffs").with(superAdmin())) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data[0].sId").value("s1")); } @Test void getUsers_withToken_returns200() throws Exception { PageVO page = new PageVO<>(); page.setTotal(0); page.setPage(1); page.setPageSize(20); page.setList(List.of()); when(userService.getUserList(any(UserListQueryDTO.class), anyString())).thenReturn(page); mockMvc.perform(get("/api/usr/users").with(superAdmin())) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data.total").value(0)) .andExpect(jsonPath("$.data.list").isArray()); } @Test void getUsers_noAuth_returns401() throws Exception { mockMvc.perform(get("/api/usr/users")) .andExpect(status().isUnauthorized()); } @Test void updateUser_withToken_returns200() throws Exception { UserUpdateRespVO resp = new UserUpdateRespVO(); resp.setUserId("u-target"); resp.setUsername("alice"); resp.setUpdatedAt(java.time.LocalDateTime.now()); when(userService.updateUser(anyString(), any(UserUpdateReqDTO.class), any())).thenReturn(resp); UserUpdateReqDTO body = new UserUpdateReqDTO(); body.setUserType("普通用户"); body.setLanguage("中文"); body.setPermGroupIds(List.of()); mockMvc.perform(put("/api/usr/users/u-target").with(superAdmin()) .contentType(org.springframework.http.MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(body))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data.userId").value("u-target")) .andExpect(jsonPath("$.data.username").value("alice")); } @Test void updateUser_noAuth_returns401() throws Exception { mockMvc.perform(put("/api/usr/users/u-target") .contentType(org.springframework.http.MediaType.APPLICATION_JSON) .content("{}")) .andExpect(status().isUnauthorized()); } @Test void getPermissionGroups_returns200() throws Exception { PermissionGroupVO g = new PermissionGroupVO(); g.setSId("g1"); g.setSGroupCode("usr:create"); g.setSGroupName("新增用户"); when(userService.getPermissionGroups(anyString())).thenReturn(List.of(g)); mockMvc.perform(get("/api/usr/users/permission-groups").with(superAdmin())) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value(200)) .andExpect(jsonPath("$.data[0].sGroupCode").value("usr:create")); } }