Commit 59bc9c13ba294fbd13685257d677a3fda29d9f28
1 parent
b532f99f
feat(usr): UserController.createUser + happy path 集成测试 REQ-USR-001
Showing
2 changed files
with
113 additions
and
0 deletions
backend/src/main/java/com/xly/test4/module/usr/controller/UserController.java
0 → 100644
| 1 | +package com.xly.test4.module.usr.controller; | |
| 2 | + | |
| 3 | +import com.xly.test4.common.response.Result; | |
| 4 | +import com.xly.test4.module.usr.dto.UserCreateDTO; | |
| 5 | +import com.xly.test4.module.usr.service.UserService; | |
| 6 | +import com.xly.test4.module.usr.vo.UserCreateVO; | |
| 7 | +import jakarta.validation.Valid; | |
| 8 | +import org.springframework.security.access.prepost.PreAuthorize; | |
| 9 | +import org.springframework.web.bind.annotation.PostMapping; | |
| 10 | +import org.springframework.web.bind.annotation.RequestBody; | |
| 11 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 12 | +import org.springframework.web.bind.annotation.RestController; | |
| 13 | + | |
| 14 | +@RestController | |
| 15 | +@RequestMapping("/api/usr/user") | |
| 16 | +public class UserController { | |
| 17 | + | |
| 18 | + private final UserService userService; | |
| 19 | + | |
| 20 | + public UserController(UserService userService) { | |
| 21 | + this.userService = userService; | |
| 22 | + } | |
| 23 | + | |
| 24 | + // REQ-USR-001: 增加用户 | |
| 25 | + @PostMapping | |
| 26 | + @PreAuthorize("hasAuthority('usr:user:create')") | |
| 27 | + public Result<UserCreateVO> createUser(@Valid @RequestBody UserCreateDTO dto) { | |
| 28 | + return Result.success(userService.createUser(dto)); | |
| 29 | + } | |
| 30 | +} | ... | ... |
backend/src/test/java/com/xly/test4/module/usr/controller/UserControllerIT.java
0 → 100644
| 1 | +package com.xly.test4.module.usr.controller; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | +import com.xly.test4.common.security.JwtTokenProvider; | |
| 6 | +import com.xly.test4.module.usr.dto.UserCreateDTO; | |
| 7 | +import com.xly.test4.module.usr.entity.User; | |
| 8 | +import com.xly.test4.module.usr.mapper.UserMapper; | |
| 9 | +import com.xly.test4.module.usr.mapper.UserPermissionMapper; | |
| 10 | +import com.xly.test4.support.TestJwtFactory; | |
| 11 | +import org.junit.jupiter.api.Test; | |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; | |
| 15 | +import org.springframework.http.HttpHeaders; | |
| 16 | +import org.springframework.http.MediaType; | |
| 17 | +import org.springframework.test.web.servlet.MockMvc; | |
| 18 | + | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 22 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | |
| 23 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | |
| 24 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
| 25 | + | |
| 26 | +@SpringBootTest | |
| 27 | +@AutoConfigureMockMvc | |
| 28 | +class UserControllerIT { | |
| 29 | + | |
| 30 | + @Autowired | |
| 31 | + private MockMvc mockMvc; | |
| 32 | + | |
| 33 | + @Autowired | |
| 34 | + private ObjectMapper objectMapper; | |
| 35 | + | |
| 36 | + @Autowired | |
| 37 | + private JwtTokenProvider tokenProvider; | |
| 38 | + | |
| 39 | + @Autowired | |
| 40 | + private UserMapper userMapper; | |
| 41 | + | |
| 42 | + @Autowired | |
| 43 | + private UserPermissionMapper userPermissionMapper; | |
| 44 | + | |
| 45 | + private String adminBearer() { | |
| 46 | + // 取 DB 中种子 admin 的 iIncrement 一并放进 token,便于审计字段(若需要) | |
| 47 | + User admin = userMapper.selectOne(new LambdaQueryWrapper<User>() | |
| 48 | + .eq(User::getSUserName, "admin")); | |
| 49 | + return "Bearer " + TestJwtFactory.adminToken(tokenProvider, admin.getIIncrement()); | |
| 50 | + } | |
| 51 | + | |
| 52 | + @Test | |
| 53 | + void createUser_validRequestWithAdminToken_returns200WithUserIdAndUserCode() throws Exception { | |
| 54 | + UserCreateDTO dto = new UserCreateDTO(); | |
| 55 | + dto.setUserCode("U-IT-001"); | |
| 56 | + dto.setUserName("it-user-001"); | |
| 57 | + dto.setEmployeeId(null); | |
| 58 | + dto.setUserType("NORMAL"); | |
| 59 | + dto.setLanguage("zh-CN"); | |
| 60 | + dto.setCanEditDoc(false); | |
| 61 | + dto.setPassword("Pass1234"); | |
| 62 | + dto.setPermissionIds(List.of()); | |
| 63 | + | |
| 64 | + mockMvc.perform(post("/api/usr/user") | |
| 65 | + .header(HttpHeaders.AUTHORIZATION, adminBearer()) | |
| 66 | + .contentType(MediaType.APPLICATION_JSON) | |
| 67 | + .content(objectMapper.writeValueAsString(dto))) | |
| 68 | + .andExpect(status().isOk()) | |
| 69 | + .andExpect(jsonPath("$.code").value(200)) | |
| 70 | + .andExpect(jsonPath("$.data.userId").isNumber()) | |
| 71 | + .andExpect(jsonPath("$.data.userCode").value("U-IT-001")); | |
| 72 | + | |
| 73 | + User created = userMapper.selectOne(new LambdaQueryWrapper<User>() | |
| 74 | + .eq(User::getSUserName, "it-user-001")); | |
| 75 | + assertThat(created).isNotNull(); | |
| 76 | + assertThat(created.getSCreatedBy()).isEqualTo("admin"); | |
| 77 | + assertThat(created.getSBrandsId()).isEqualTo("BR-DEFAULT"); | |
| 78 | + assertThat(created.getSSubsidiaryId()).isEqualTo("SUB-DEFAULT"); | |
| 79 | + assertThat(created.getIIsDisabled()).isZero(); | |
| 80 | + assertThat(created.getSPasswordHash()).startsWith("$2").hasSize(60); | |
| 81 | + assertThat(created.getSPasswordHash()).isNotEqualTo("Pass1234"); | |
| 82 | + } | |
| 83 | +} | ... | ... |