Commit 8f600c602ec7a3a2e2953a737733b08ca35daeea
1 parent
e765fc29
feat(usr): PUT /api/usr/users/{userId} REQ-USR-002
REQ-USR-002
Showing
2 changed files
with
43 additions
and
0 deletions
backend/src/main/java/com/example/erp/module/usr/controller/UserController.java
| ... | ... | @@ -5,11 +5,13 @@ import com.example.erp.common.vo.PageVO; |
| 5 | 5 | import com.example.erp.config.UserPrincipal; |
| 6 | 6 | import com.example.erp.module.usr.dto.UserCreateReqDTO; |
| 7 | 7 | import com.example.erp.module.usr.dto.UserListQueryDTO; |
| 8 | +import com.example.erp.module.usr.dto.UserUpdateReqDTO; | |
| 8 | 9 | import com.example.erp.module.usr.service.UserService; |
| 9 | 10 | import com.example.erp.module.usr.vo.PermissionGroupVO; |
| 10 | 11 | import com.example.erp.module.usr.vo.StaffVO; |
| 11 | 12 | import com.example.erp.module.usr.vo.UserCreateRespVO; |
| 12 | 13 | import com.example.erp.module.usr.vo.UserListItemVO; |
| 14 | +import com.example.erp.module.usr.vo.UserUpdateRespVO; | |
| 13 | 15 | import jakarta.validation.Valid; |
| 14 | 16 | import lombok.RequiredArgsConstructor; |
| 15 | 17 | import org.springframework.security.core.annotation.AuthenticationPrincipal; |
| ... | ... | @@ -49,6 +51,15 @@ public class UserController { |
| 49 | 51 | return Result.ok(userService.getUserList(q, principal.brandId())); |
| 50 | 52 | } |
| 51 | 53 | |
| 54 | + // REQ-USR-002: 修改用户 | |
| 55 | + @PutMapping("/users/{userId}") | |
| 56 | + public Result<UserUpdateRespVO> updateUser( | |
| 57 | + @PathVariable String userId, | |
| 58 | + @Valid @RequestBody UserUpdateReqDTO req, | |
| 59 | + @AuthenticationPrincipal UserPrincipal principal) { | |
| 60 | + return Result.ok(userService.updateUser(userId, req, principal)); | |
| 61 | + } | |
| 62 | + | |
| 52 | 63 | @GetMapping("/users/staffs") |
| 53 | 64 | public Result<List<StaffVO>> getStaffs(@AuthenticationPrincipal UserPrincipal principal) { |
| 54 | 65 | return Result.ok(userService.getStaffs(principal.brandId())); | ... | ... |
backend/src/test/java/com/example/erp/module/usr/UserControllerTest.java
| ... | ... | @@ -9,8 +9,10 @@ import com.example.erp.module.usr.controller.UserController; |
| 9 | 9 | import com.example.erp.module.usr.service.UserService; |
| 10 | 10 | import com.example.erp.common.vo.PageVO; |
| 11 | 11 | import com.example.erp.module.usr.dto.UserListQueryDTO; |
| 12 | +import com.example.erp.module.usr.dto.UserUpdateReqDTO; | |
| 12 | 13 | import com.example.erp.module.usr.vo.UserCreateRespVO; |
| 13 | 14 | import com.example.erp.module.usr.vo.UserListItemVO; |
| 15 | +import com.example.erp.module.usr.vo.UserUpdateRespVO; | |
| 14 | 16 | import com.example.erp.module.usr.vo.StaffVO; |
| 15 | 17 | import com.example.erp.module.usr.vo.PermissionGroupVO; |
| 16 | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| ... | ... | @@ -128,6 +130,36 @@ class UserControllerTest { |
| 128 | 130 | } |
| 129 | 131 | |
| 130 | 132 | @Test |
| 133 | + void updateUser_withToken_returns200() throws Exception { | |
| 134 | + UserUpdateRespVO resp = new UserUpdateRespVO(); | |
| 135 | + resp.setUserId("u-target"); | |
| 136 | + resp.setUsername("alice"); | |
| 137 | + resp.setUpdatedAt(java.time.LocalDateTime.now()); | |
| 138 | + when(userService.updateUser(anyString(), any(UserUpdateReqDTO.class), any())).thenReturn(resp); | |
| 139 | + | |
| 140 | + UserUpdateReqDTO body = new UserUpdateReqDTO(); | |
| 141 | + body.setUserType("普通用户"); | |
| 142 | + body.setLanguage("中文"); | |
| 143 | + body.setPermGroupIds(List.of()); | |
| 144 | + | |
| 145 | + mockMvc.perform(put("/api/usr/users/u-target").with(superAdmin()) | |
| 146 | + .contentType(org.springframework.http.MediaType.APPLICATION_JSON) | |
| 147 | + .content(objectMapper.writeValueAsString(body))) | |
| 148 | + .andExpect(status().isOk()) | |
| 149 | + .andExpect(jsonPath("$.code").value(200)) | |
| 150 | + .andExpect(jsonPath("$.data.userId").value("u-target")) | |
| 151 | + .andExpect(jsonPath("$.data.username").value("alice")); | |
| 152 | + } | |
| 153 | + | |
| 154 | + @Test | |
| 155 | + void updateUser_noAuth_returns401() throws Exception { | |
| 156 | + mockMvc.perform(put("/api/usr/users/u-target") | |
| 157 | + .contentType(org.springframework.http.MediaType.APPLICATION_JSON) | |
| 158 | + .content("{}")) | |
| 159 | + .andExpect(status().isUnauthorized()); | |
| 160 | + } | |
| 161 | + | |
| 162 | + @Test | |
| 131 | 163 | void getPermissionGroups_returns200() throws Exception { |
| 132 | 164 | PermissionGroupVO g = new PermissionGroupVO(); |
| 133 | 165 | g.setSId("g1"); | ... | ... |