Commit 677b3e486a032ffe8cb6cab0040379201bd6597d

Authored by zichun
1 parent 3263e4df

feat(usr): POST /api/v1/auth/login controller + 端到端测试 REQ-USR-001

backend/src/main/java/com/xly/erp/module/usr/controller/AuthController.java 0 → 100644
  1 +package com.xly.erp.module.usr.controller;
  2 +
  3 +import com.xly.erp.common.response.Result;
  4 +import com.xly.erp.module.usr.dto.LoginReq;
  5 +import com.xly.erp.module.usr.service.LoginService;
  6 +import com.xly.erp.module.usr.vo.LoginVo;
  7 +import jakarta.validation.Valid;
  8 +import lombok.RequiredArgsConstructor;
  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 +/**
  15 + * 认证入口。REQ-USR-001:POST /api/v1/auth/login。
  16 + */
  17 +@RestController
  18 +@RequestMapping("/api/v1/auth")
  19 +@RequiredArgsConstructor
  20 +public class AuthController {
  21 +
  22 + private final LoginService loginService;
  23 +
  24 + @PostMapping("/login")
  25 + public Result<LoginVo> login(@RequestBody @Valid LoginReq req) {
  26 + LoginVo vo = loginService.login(req.getUsername(), req.getPassword(), req.getCompanyCode());
  27 + return Result.ok(vo);
  28 + }
  29 +}
... ...
backend/src/test/java/com/xly/erp/module/usr/controller/AuthControllerTest.java 0 → 100644
  1 +package com.xly.erp.module.usr.controller;
  2 +
  3 +import com.fasterxml.jackson.databind.ObjectMapper;
  4 +import com.xly.erp.common.response.ErrorCode;
  5 +import com.xly.erp.module.usr.dto.LoginReq;
  6 +import com.xly.erp.module.usr.support.LoginTestSeeder;
  7 +import org.junit.jupiter.api.BeforeEach;
  8 +import org.junit.jupiter.api.Test;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  11 +import org.springframework.boot.test.context.SpringBootTest;
  12 +import org.springframework.http.MediaType;
  13 +import org.springframework.jdbc.core.JdbcTemplate;
  14 +import org.springframework.test.context.ActiveProfiles;
  15 +import org.springframework.test.web.servlet.MockMvc;
  16 +
  17 +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  18 +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  19 +
  20 +@SpringBootTest
  21 +@AutoConfigureMockMvc
  22 +@ActiveProfiles("test")
  23 +class AuthControllerTest {
  24 +
  25 + @Autowired private MockMvc mvc;
  26 + @Autowired private ObjectMapper json;
  27 + @Autowired private LoginTestSeeder seeder;
  28 + @Autowired private JdbcTemplate jdbc;
  29 +
  30 + @BeforeEach
  31 + void setUp() {
  32 + seeder.reset();
  33 + }
  34 +
  35 + private String body(String username, String password, String companyCode) throws Exception {
  36 + LoginReq r = new LoginReq();
  37 + r.setUsername(username);
  38 + r.setPassword(password);
  39 + r.setCompanyCode(companyCode);
  40 + return json.writeValueAsString(r);
  41 + }
  42 +
  43 + @Test
  44 + void post_login_success_returns200_andLoginVo() throws Exception {
  45 + mvc.perform(post("/api/v1/auth/login")
  46 + .contentType(MediaType.APPLICATION_JSON)
  47 + .content(body(LoginTestSeeder.USER_OK, LoginTestSeeder.DEFAULT_PASSWORD,
  48 + LoginTestSeeder.COMPANY_OK)))
  49 + .andExpect(status().isOk())
  50 + .andExpect(jsonPath("$.code").value(ErrorCode.OK))
  51 + .andExpect(jsonPath("$.data.accessToken").isNotEmpty())
  52 + .andExpect(jsonPath("$.data.tokenType").value("Bearer"))
  53 + .andExpect(jsonPath("$.data.expiresInSec").value(7200))
  54 + .andExpect(jsonPath("$.data.userInfo.username").value(LoginTestSeeder.USER_OK))
  55 + .andExpect(jsonPath("$.data.userInfo.companyCode").value(LoginTestSeeder.COMPANY_OK));
  56 + }
  57 +
  58 + @Test
  59 + void post_login_badCredentials_returns401_code40101() throws Exception {
  60 + mvc.perform(post("/api/v1/auth/login")
  61 + .contentType(MediaType.APPLICATION_JSON)
  62 + .content(body(LoginTestSeeder.USER_OK, "WrongPass1!", LoginTestSeeder.COMPANY_OK)))
  63 + .andExpect(status().isUnauthorized())
  64 + .andExpect(jsonPath("$.code").value(ErrorCode.BAD_CREDENTIALS));
  65 + }
  66 +
  67 + @Test
  68 + void post_login_unknownUser_returns401_code40101() throws Exception {
  69 + mvc.perform(post("/api/v1/auth/login")
  70 + .contentType(MediaType.APPLICATION_JSON)
  71 + .content(body("nobody", "any", LoginTestSeeder.COMPANY_OK)))
  72 + .andExpect(status().isUnauthorized())
  73 + .andExpect(jsonPath("$.code").value(ErrorCode.BAD_CREDENTIALS));
  74 + }
  75 +
  76 + @Test
  77 + void post_login_lockedAccount_returns423_code42301() throws Exception {
  78 + jdbc.update("UPDATE sys_user SET iFailedLoginCount=5, tLockUntil=DATE_ADD(NOW(), INTERVAL 30 MINUTE) WHERE sUsername=?",
  79 + LoginTestSeeder.USER_OK);
  80 + mvc.perform(post("/api/v1/auth/login")
  81 + .contentType(MediaType.APPLICATION_JSON)
  82 + .content(body(LoginTestSeeder.USER_OK, LoginTestSeeder.DEFAULT_PASSWORD,
  83 + LoginTestSeeder.COMPANY_OK)))
  84 + .andExpect(status().isLocked())
  85 + .andExpect(jsonPath("$.code").value(ErrorCode.ACCOUNT_LOCKED));
  86 + }
  87 +
  88 + @Test
  89 + void post_login_deletedAccount_returns401_code40103() throws Exception {
  90 + mvc.perform(post("/api/v1/auth/login")
  91 + .contentType(MediaType.APPLICATION_JSON)
  92 + .content(body(LoginTestSeeder.USER_DELETED, LoginTestSeeder.DEFAULT_PASSWORD,
  93 + LoginTestSeeder.COMPANY_OK)))
  94 + .andExpect(status().isUnauthorized())
  95 + .andExpect(jsonPath("$.code").value(ErrorCode.ACCOUNT_DELETED));
  96 + }
  97 +
  98 + @Test
  99 + void post_login_unknownCompany_returns400_code40004() throws Exception {
  100 + mvc.perform(post("/api/v1/auth/login")
  101 + .contentType(MediaType.APPLICATION_JSON)
  102 + .content(body(LoginTestSeeder.USER_OK, LoginTestSeeder.DEFAULT_PASSWORD, "NOPE")))
  103 + .andExpect(status().isBadRequest())
  104 + .andExpect(jsonPath("$.code").value(ErrorCode.COMPANY_NOT_FOUND));
  105 + }
  106 +
  107 + @Test
  108 + void post_login_blankUsername_returns400_code40001() throws Exception {
  109 + mvc.perform(post("/api/v1/auth/login")
  110 + .contentType(MediaType.APPLICATION_JSON)
  111 + .content(body("", LoginTestSeeder.DEFAULT_PASSWORD, LoginTestSeeder.COMPANY_OK)))
  112 + .andExpect(status().isBadRequest())
  113 + .andExpect(jsonPath("$.code").value(ErrorCode.BAD_REQUEST));
  114 + }
  115 +}
... ...