Commit c82fd010ddbed32e4bbb77a66c88914c34bc68fa
1 parent
59bc9c13
test(usr): createUser 错误路径集成测试 (40001-40005/40101/40301) REQ-USR-001
Showing
1 changed file
with
97 additions
and
4 deletions
backend/src/test/java/com/xly/test4/module/usr/controller/UserControllerIT.java
| @@ -8,6 +8,9 @@ import com.xly.test4.module.usr.entity.User; | @@ -8,6 +8,9 @@ import com.xly.test4.module.usr.entity.User; | ||
| 8 | import com.xly.test4.module.usr.mapper.UserMapper; | 8 | import com.xly.test4.module.usr.mapper.UserMapper; |
| 9 | import com.xly.test4.module.usr.mapper.UserPermissionMapper; | 9 | import com.xly.test4.module.usr.mapper.UserPermissionMapper; |
| 10 | import com.xly.test4.support.TestJwtFactory; | 10 | import com.xly.test4.support.TestJwtFactory; |
| 11 | +import org.junit.jupiter.api.MethodOrderer; | ||
| 12 | +import org.junit.jupiter.api.Order; | ||
| 13 | +import org.junit.jupiter.api.TestMethodOrder; | ||
| 11 | import org.junit.jupiter.api.Test; | 14 | import org.junit.jupiter.api.Test; |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | 15 | import org.springframework.beans.factory.annotation.Autowired; |
| 13 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | 16 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| @@ -25,6 +28,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. | @@ -25,6 +28,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. | ||
| 25 | 28 | ||
| 26 | @SpringBootTest | 29 | @SpringBootTest |
| 27 | @AutoConfigureMockMvc | 30 | @AutoConfigureMockMvc |
| 31 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
| 28 | class UserControllerIT { | 32 | class UserControllerIT { |
| 29 | 33 | ||
| 30 | @Autowired | 34 | @Autowired |
| @@ -49,17 +53,23 @@ class UserControllerIT { | @@ -49,17 +53,23 @@ class UserControllerIT { | ||
| 49 | return "Bearer " + TestJwtFactory.adminToken(tokenProvider, admin.getIIncrement()); | 53 | return "Bearer " + TestJwtFactory.adminToken(tokenProvider, admin.getIIncrement()); |
| 50 | } | 54 | } |
| 51 | 55 | ||
| 52 | - @Test | ||
| 53 | - void createUser_validRequestWithAdminToken_returns200WithUserIdAndUserCode() throws Exception { | 56 | + private UserCreateDTO validDTO(String code, String name) { |
| 54 | UserCreateDTO dto = new UserCreateDTO(); | 57 | UserCreateDTO dto = new UserCreateDTO(); |
| 55 | - dto.setUserCode("U-IT-001"); | ||
| 56 | - dto.setUserName("it-user-001"); | 58 | + dto.setUserCode(code); |
| 59 | + dto.setUserName(name); | ||
| 57 | dto.setEmployeeId(null); | 60 | dto.setEmployeeId(null); |
| 58 | dto.setUserType("NORMAL"); | 61 | dto.setUserType("NORMAL"); |
| 59 | dto.setLanguage("zh-CN"); | 62 | dto.setLanguage("zh-CN"); |
| 60 | dto.setCanEditDoc(false); | 63 | dto.setCanEditDoc(false); |
| 61 | dto.setPassword("Pass1234"); | 64 | dto.setPassword("Pass1234"); |
| 62 | dto.setPermissionIds(List.of()); | 65 | dto.setPermissionIds(List.of()); |
| 66 | + return dto; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @Test | ||
| 70 | + @Order(1) | ||
| 71 | + void createUser_validRequestWithAdminToken_returns200WithUserIdAndUserCode() throws Exception { | ||
| 72 | + UserCreateDTO dto = validDTO("U-IT-001", "it-user-001"); | ||
| 63 | 73 | ||
| 64 | mockMvc.perform(post("/api/usr/user") | 74 | mockMvc.perform(post("/api/usr/user") |
| 65 | .header(HttpHeaders.AUTHORIZATION, adminBearer()) | 75 | .header(HttpHeaders.AUTHORIZATION, adminBearer()) |
| @@ -80,4 +90,87 @@ class UserControllerIT { | @@ -80,4 +90,87 @@ class UserControllerIT { | ||
| 80 | assertThat(created.getSPasswordHash()).startsWith("$2").hasSize(60); | 90 | assertThat(created.getSPasswordHash()).startsWith("$2").hasSize(60); |
| 81 | assertThat(created.getSPasswordHash()).isNotEqualTo("Pass1234"); | 91 | assertThat(created.getSPasswordHash()).isNotEqualTo("Pass1234"); |
| 82 | } | 92 | } |
| 93 | + | ||
| 94 | + @Test | ||
| 95 | + @Order(2) | ||
| 96 | + void createUser_duplicateUserName_returns40002() throws Exception { | ||
| 97 | + UserCreateDTO dto = validDTO("U-IT-DUP-NAME", "it-user-001"); | ||
| 98 | + // userName=it-user-001 已被 Order(1) 写入 → 40002 | ||
| 99 | + mockMvc.perform(post("/api/usr/user") | ||
| 100 | + .header(HttpHeaders.AUTHORIZATION, adminBearer()) | ||
| 101 | + .contentType(MediaType.APPLICATION_JSON) | ||
| 102 | + .content(objectMapper.writeValueAsString(dto))) | ||
| 103 | + .andExpect(jsonPath("$.code").value(40002)); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + @Test | ||
| 107 | + @Order(3) | ||
| 108 | + void createUser_duplicateUserCode_returns40003() throws Exception { | ||
| 109 | + UserCreateDTO dto = validDTO("U-IT-001", "it-user-dup-code"); | ||
| 110 | + // userCode=U-IT-001 已被 Order(1) 写入 → 40003 | ||
| 111 | + mockMvc.perform(post("/api/usr/user") | ||
| 112 | + .header(HttpHeaders.AUTHORIZATION, adminBearer()) | ||
| 113 | + .contentType(MediaType.APPLICATION_JSON) | ||
| 114 | + .content(objectMapper.writeValueAsString(dto))) | ||
| 115 | + .andExpect(jsonPath("$.code").value(40003)); | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + @Test | ||
| 119 | + @Order(4) | ||
| 120 | + void createUser_invalidEmployeeId_returns40004() throws Exception { | ||
| 121 | + UserCreateDTO dto = validDTO("U-IT-EMP", "it-user-emp"); | ||
| 122 | + dto.setEmployeeId(99999); | ||
| 123 | + mockMvc.perform(post("/api/usr/user") | ||
| 124 | + .header(HttpHeaders.AUTHORIZATION, adminBearer()) | ||
| 125 | + .contentType(MediaType.APPLICATION_JSON) | ||
| 126 | + .content(objectMapper.writeValueAsString(dto))) | ||
| 127 | + .andExpect(jsonPath("$.code").value(40004)); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + @Test | ||
| 131 | + @Order(5) | ||
| 132 | + void createUser_invalidPermissionIds_returns40005() throws Exception { | ||
| 133 | + UserCreateDTO dto = validDTO("U-IT-PERM", "it-user-perm"); | ||
| 134 | + dto.setPermissionIds(List.of(99999)); | ||
| 135 | + mockMvc.perform(post("/api/usr/user") | ||
| 136 | + .header(HttpHeaders.AUTHORIZATION, adminBearer()) | ||
| 137 | + .contentType(MediaType.APPLICATION_JSON) | ||
| 138 | + .content(objectMapper.writeValueAsString(dto))) | ||
| 139 | + .andExpect(jsonPath("$.code").value(40005)); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + @Test | ||
| 143 | + @Order(6) | ||
| 144 | + void createUser_missingUserName_returns40001() throws Exception { | ||
| 145 | + UserCreateDTO dto = validDTO("U-IT-MISSING", null); | ||
| 146 | + mockMvc.perform(post("/api/usr/user") | ||
| 147 | + .header(HttpHeaders.AUTHORIZATION, adminBearer()) | ||
| 148 | + .contentType(MediaType.APPLICATION_JSON) | ||
| 149 | + .content(objectMapper.writeValueAsString(dto))) | ||
| 150 | + .andExpect(jsonPath("$.code").value(40001)); | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + @Test | ||
| 154 | + @Order(7) | ||
| 155 | + void createUser_normalUserToken_returns40301() throws Exception { | ||
| 156 | + UserCreateDTO dto = validDTO("U-IT-NORMAL", "it-user-normal"); | ||
| 157 | + String normalToken = TestJwtFactory.normalUserToken(tokenProvider, 999, "alice"); | ||
| 158 | + mockMvc.perform(post("/api/usr/user") | ||
| 159 | + .header(HttpHeaders.AUTHORIZATION, "Bearer " + normalToken) | ||
| 160 | + .contentType(MediaType.APPLICATION_JSON) | ||
| 161 | + .content(objectMapper.writeValueAsString(dto))) | ||
| 162 | + .andExpect(status().isForbidden()) | ||
| 163 | + .andExpect(jsonPath("$.code").value(40301)); | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + @Test | ||
| 167 | + @Order(8) | ||
| 168 | + void createUser_noAuthHeader_returns40101() throws Exception { | ||
| 169 | + UserCreateDTO dto = validDTO("U-IT-NOAUTH", "it-user-noauth"); | ||
| 170 | + mockMvc.perform(post("/api/usr/user") | ||
| 171 | + .contentType(MediaType.APPLICATION_JSON) | ||
| 172 | + .content(objectMapper.writeValueAsString(dto))) | ||
| 173 | + .andExpect(status().isUnauthorized()) | ||
| 174 | + .andExpect(jsonPath("$.code").value(40101)); | ||
| 175 | + } | ||
| 83 | } | 176 | } |