refactor: flatten dto entity and mapper packages

This commit is contained in:
wh
2026-04-14 13:28:10 +08:00
parent 0af19cf1b5
commit 29766ebd28
64 changed files with 1524 additions and 1780 deletions

View File

@@ -2,7 +2,7 @@ package com.label.integration;
import com.label.AbstractIntegrationTest;
import com.label.common.result.Result;
import com.label.module.user.dto.LoginRequest;
import com.label.dto.LoginRequest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -15,26 +15,22 @@ import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 认证流程集成测试US1
* 璁よ瘉娴佺▼闆嗘垚娴嬭瘯锛圲S1锛夈€? *
* 娴嬭瘯鍦烘櫙锛? * 1. 姝g‘瀵嗙爜鐧诲綍 鈫?杩斿洖 token
* 2. 閿欒瀵嗙爜鐧诲綍 鈫?401
* 3. 涓嶅瓨鍦ㄧ殑鍏徃浠g爜 鈫?401
* 4. 鏈夋晥 Token 璁块棶 /api/auth/me 鈫?200锛岃繑鍥炵敤鎴蜂俊鎭? * 5. 涓诲姩閫€鍑哄悗锛屽師 Token 璁块棶 /api/auth/me 鈫?401
*
* 测试场景:
* 1. 正确密码登录 → 返回 token
* 2. 错误密码登录 → 401
* 3. 不存在的公司代码 → 401
* 4. 有效 Token 访问 /api/auth/me → 200返回用户信息
* 5. 主动退出后,原 Token 访问 /api/auth/me → 401
*
* 测试数据来自 init.sql 种子DEMO 公司 / admin / admin123
*/
* 娴嬭瘯鏁版嵁鏉ヨ嚜 init.sql 绉嶅瓙锛圖EMO 鍏徃 / admin / admin123锛? */
public class AuthIntegrationTest extends AbstractIntegrationTest {
@Autowired
private TestRestTemplate restTemplate;
// ------------------------------------------------------------------ 登录测试 --
// ------------------------------------------------------------------ 鐧诲綍娴嬭瘯 --
@Test
@DisplayName("正确密码登录 → 返回 token")
@DisplayName("姝g‘瀵嗙爜鐧诲綍 鈫?杩斿洖 token")
void login_withCorrectCredentials_returnsToken() {
ResponseEntity<Map> response = doLogin("DEMO", "admin", "admin123");
@@ -52,23 +48,23 @@ public class AuthIntegrationTest extends AbstractIntegrationTest {
}
@Test
@DisplayName("错误密码登录 → 401 Unauthorized")
@DisplayName("閿欒瀵嗙爜鐧诲綍 鈫?401 Unauthorized")
void login_withWrongPassword_returns401() {
ResponseEntity<Map> response = doLogin("DEMO", "admin", "wrong_password");
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
@Test
@DisplayName("不存在的公司代码 → 401 Unauthorized")
@DisplayName("涓嶅瓨鍦ㄧ殑鍏徃浠g爜 鈫?401 Unauthorized")
void login_withUnknownCompany_returns401() {
ResponseEntity<Map> response = doLogin("NONEXIST", "admin", "admin123");
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
// ------------------------------------------------------------------ /me 测试 --
// ------------------------------------------------------------------ /me 娴嬭瘯 --
@Test
@DisplayName("有效 Token 访问 /api/auth/me 200,返回用户信息")
@DisplayName("鏈夋晥 Token 璁块棶 /api/auth/me 鈫?200锛岃繑鍥炵敤鎴蜂俊鎭?)
void me_withValidToken_returns200WithUserInfo() {
String token = loginAndGetToken("DEMO", "admin", "admin123");
assertThat(token).isNotBlank();
@@ -89,22 +85,22 @@ public class AuthIntegrationTest extends AbstractIntegrationTest {
}
@Test
@DisplayName("无 Token 访问 /api/auth/me → 401")
@DisplayName("?Token 璁块棶 /api/auth/me ?401")
void me_withNoToken_returns401() {
ResponseEntity<String> response = restTemplate.getForEntity(
baseUrl("/api/auth/me"), String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
// ------------------------------------------------------------------ 退出测试 --
// ------------------------------------------------------------------ 閫€鍑烘祴璇?--
@Test
@DisplayName("主动退出后,原 Token 访问 /api/auth/me → 401")
@DisplayName("涓诲姩閫鍑哄悗锛屽師 Token 璁块棶 /api/auth/me ?401")
void logout_thenMe_returns401() {
String token = loginAndGetToken("DEMO", "admin", "admin123");
assertThat(token).isNotBlank();
// 确认登录有效
// 纭鐧诲綍鏈夋晥
ResponseEntity<Map> meResponse = restTemplate.exchange(
baseUrl("/api/auth/me"),
HttpMethod.GET,
@@ -112,15 +108,14 @@ public class AuthIntegrationTest extends AbstractIntegrationTest {
Map.class);
assertThat(meResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
// 退出
ResponseEntity<Map> logoutResponse = restTemplate.exchange(
// 閫€鍑? ResponseEntity<Map> logoutResponse = restTemplate.exchange(
baseUrl("/api/auth/logout"),
HttpMethod.POST,
bearerRequest(token),
Map.class);
assertThat(logoutResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
// 退出后再访问 /me 401
// 閫€鍑哄悗鍐嶈闂?/me 鈫?401
ResponseEntity<Map> meAfterLogout = restTemplate.exchange(
baseUrl("/api/auth/me"),
HttpMethod.GET,
@@ -129,9 +124,9 @@ public class AuthIntegrationTest extends AbstractIntegrationTest {
assertThat(meAfterLogout.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
}
// ------------------------------------------------------------------ 工具方法 --
// ------------------------------------------------------------------ 宸ュ叿鏂规硶 --
/** 发起登录请求,返回原始 ResponseEntity */
/** 鍙戣捣鐧诲綍璇锋眰锛岃繑鍥炲師濮?ResponseEntity */
private ResponseEntity<Map> doLogin(String companyCode, String username, String password) {
LoginRequest req = new LoginRequest();
req.setCompanyCode(companyCode);
@@ -140,7 +135,7 @@ public class AuthIntegrationTest extends AbstractIntegrationTest {
return restTemplate.postForEntity(baseUrl("/api/auth/login"), req, Map.class);
}
/** 登录并提取 token 字符串;失败时返回 null */
/** 鐧诲綍骞舵彁鍙?token 瀛楃涓诧紱澶辫触鏃惰繑鍥?null */
private String loginAndGetToken(String companyCode, String username, String password) {
ResponseEntity<Map> response = doLogin(companyCode, username, password);
if (!response.getStatusCode().is2xxSuccessful()) {
@@ -151,7 +146,7 @@ public class AuthIntegrationTest extends AbstractIntegrationTest {
return (String) data.get("token");
}
/** 构造带 Bearer Token 的请求实体(无 body */
/** 鏋勯€犲甫 Bearer Token 鐨勮姹傚疄浣擄紙鏃?body锛?*/
private HttpEntity<Void> bearerRequest(String token) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + token);