UserControllerTest.java
4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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.module.usr.vo.UserCreateRespVO;
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<String, Object> 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<String, Object> 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 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"));
}
}