2026-04-09 15:16:49 +08:00
|
|
|
|
package com.label.integration;
|
|
|
|
|
|
|
|
|
|
|
|
import com.label.AbstractIntegrationTest;
|
|
|
|
|
|
import com.label.common.result.Result;
|
2026-04-14 13:28:10 +08:00
|
|
|
|
import com.label.dto.LoginRequest;
|
2026-04-09 15:16:49 +08:00
|
|
|
|
import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
|
|
|
|
import org.springframework.core.ParameterizedTypeReference;
|
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-14 13:28:10 +08:00
|
|
|
|
* 璁よ瘉娴佺▼闆嗘垚娴嬭瘯锛圲S1锛夈€? *
|
|
|
|
|
|
* 娴嬭瘯鍦烘櫙锛? * 1. 姝g‘瀵嗙爜鐧诲綍 鈫?杩斿洖 token
|
|
|
|
|
|
* 2. 閿欒瀵嗙爜鐧诲綍 鈫?401
|
|
|
|
|
|
* 3. 涓嶅瓨鍦ㄧ殑鍏徃浠g爜 鈫?401
|
|
|
|
|
|
* 4. 鏈夋晥 Token 璁块棶 /api/auth/me 鈫?200锛岃繑鍥炵敤鎴蜂俊鎭? * 5. 涓诲姩閫€鍑哄悗锛屽師 Token 璁块棶 /api/auth/me 鈫?401
|
2026-04-09 15:16:49 +08:00
|
|
|
|
*
|
2026-04-14 13:28:10 +08:00
|
|
|
|
* 娴嬭瘯鏁版嵁鏉ヨ嚜 init.sql 绉嶅瓙锛圖EMO 鍏徃 / admin / admin123锛? */
|
2026-04-09 15:16:49 +08:00
|
|
|
|
public class AuthIntegrationTest extends AbstractIntegrationTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private TestRestTemplate restTemplate;
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
// ------------------------------------------------------------------ 鐧诲綍娴嬭瘯 --
|
2026-04-09 15:16:49 +08:00
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
|
@DisplayName("姝g‘瀵嗙爜鐧诲綍 鈫?杩斿洖 token")
|
2026-04-09 15:16:49 +08:00
|
|
|
|
void login_withCorrectCredentials_returnsToken() {
|
|
|
|
|
|
ResponseEntity<Map> response = doLogin("DEMO", "admin", "admin123");
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
|
|
|
|
|
|
Map<?, ?> body = response.getBody();
|
|
|
|
|
|
assertThat(body).isNotNull();
|
|
|
|
|
|
assertThat(body.get("code")).isEqualTo("SUCCESS");
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
|
Map<String, Object> data = (Map<String, Object>) body.get("data");
|
|
|
|
|
|
assertThat(data.get("token")).isNotNull().isInstanceOf(String.class);
|
|
|
|
|
|
assertThat((String) data.get("token")).isNotBlank();
|
|
|
|
|
|
assertThat(data.get("role")).isEqualTo("ADMIN");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
|
@DisplayName("閿欒瀵嗙爜鐧诲綍 鈫?401 Unauthorized")
|
2026-04-09 15:16:49 +08:00
|
|
|
|
void login_withWrongPassword_returns401() {
|
|
|
|
|
|
ResponseEntity<Map> response = doLogin("DEMO", "admin", "wrong_password");
|
|
|
|
|
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
|
@DisplayName("涓嶅瓨鍦ㄧ殑鍏徃浠g爜 鈫?401 Unauthorized")
|
2026-04-09 15:16:49 +08:00
|
|
|
|
void login_withUnknownCompany_returns401() {
|
|
|
|
|
|
ResponseEntity<Map> response = doLogin("NONEXIST", "admin", "admin123");
|
|
|
|
|
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
// ------------------------------------------------------------------ /me 娴嬭瘯 --
|
2026-04-09 15:16:49 +08:00
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
|
@DisplayName("鏈夋晥 Token 璁块棶 /api/auth/me 鈫?200锛岃繑鍥炵敤鎴蜂俊鎭?)
|
2026-04-09 15:16:49 +08:00
|
|
|
|
void me_withValidToken_returns200WithUserInfo() {
|
|
|
|
|
|
String token = loginAndGetToken("DEMO", "admin", "admin123");
|
|
|
|
|
|
assertThat(token).isNotBlank();
|
|
|
|
|
|
|
|
|
|
|
|
ResponseEntity<Map> response = restTemplate.exchange(
|
|
|
|
|
|
baseUrl("/api/auth/me"),
|
|
|
|
|
|
HttpMethod.GET,
|
|
|
|
|
|
bearerRequest(token),
|
|
|
|
|
|
Map.class);
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
|
Map<String, Object> data = (Map<String, Object>) response.getBody().get("data");
|
|
|
|
|
|
assertThat(data.get("username")).isEqualTo("admin");
|
|
|
|
|
|
assertThat(data.get("role")).isEqualTo("ADMIN");
|
|
|
|
|
|
assertThat(data.get("companyId")).isNotNull();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
|
@DisplayName("鏃?Token 璁块棶 /api/auth/me 鈫?401")
|
2026-04-09 15:16:49 +08:00
|
|
|
|
void me_withNoToken_returns401() {
|
|
|
|
|
|
ResponseEntity<String> response = restTemplate.getForEntity(
|
|
|
|
|
|
baseUrl("/api/auth/me"), String.class);
|
|
|
|
|
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
// ------------------------------------------------------------------ 閫€鍑烘祴璇?--
|
2026-04-09 15:16:49 +08:00
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-04-14 13:28:10 +08:00
|
|
|
|
@DisplayName("涓诲姩閫€鍑哄悗锛屽師 Token 璁块棶 /api/auth/me 鈫?401")
|
2026-04-09 15:16:49 +08:00
|
|
|
|
void logout_thenMe_returns401() {
|
|
|
|
|
|
String token = loginAndGetToken("DEMO", "admin", "admin123");
|
|
|
|
|
|
assertThat(token).isNotBlank();
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
// 纭鐧诲綍鏈夋晥
|
2026-04-09 15:16:49 +08:00
|
|
|
|
ResponseEntity<Map> meResponse = restTemplate.exchange(
|
|
|
|
|
|
baseUrl("/api/auth/me"),
|
|
|
|
|
|
HttpMethod.GET,
|
|
|
|
|
|
bearerRequest(token),
|
|
|
|
|
|
Map.class);
|
|
|
|
|
|
assertThat(meResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
// 閫€鍑? ResponseEntity<Map> logoutResponse = restTemplate.exchange(
|
2026-04-09 15:16:49 +08:00
|
|
|
|
baseUrl("/api/auth/logout"),
|
|
|
|
|
|
HttpMethod.POST,
|
|
|
|
|
|
bearerRequest(token),
|
|
|
|
|
|
Map.class);
|
|
|
|
|
|
assertThat(logoutResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
// 閫€鍑哄悗鍐嶈闂?/me 鈫?401
|
2026-04-09 15:16:49 +08:00
|
|
|
|
ResponseEntity<Map> meAfterLogout = restTemplate.exchange(
|
|
|
|
|
|
baseUrl("/api/auth/me"),
|
|
|
|
|
|
HttpMethod.GET,
|
|
|
|
|
|
bearerRequest(token),
|
|
|
|
|
|
Map.class);
|
|
|
|
|
|
assertThat(meAfterLogout.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
// ------------------------------------------------------------------ 宸ュ叿鏂规硶 --
|
2026-04-09 15:16:49 +08:00
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
/** 鍙戣捣鐧诲綍璇锋眰锛岃繑鍥炲師濮?ResponseEntity */
|
2026-04-09 15:16:49 +08:00
|
|
|
|
private ResponseEntity<Map> doLogin(String companyCode, String username, String password) {
|
|
|
|
|
|
LoginRequest req = new LoginRequest();
|
|
|
|
|
|
req.setCompanyCode(companyCode);
|
|
|
|
|
|
req.setUsername(username);
|
|
|
|
|
|
req.setPassword(password);
|
|
|
|
|
|
return restTemplate.postForEntity(baseUrl("/api/auth/login"), req, Map.class);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
/** 鐧诲綍骞舵彁鍙?token 瀛楃涓诧紱澶辫触鏃惰繑鍥?null */
|
2026-04-09 15:16:49 +08:00
|
|
|
|
private String loginAndGetToken(String companyCode, String username, String password) {
|
|
|
|
|
|
ResponseEntity<Map> response = doLogin(companyCode, username, password);
|
|
|
|
|
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
|
Map<String, Object> data = (Map<String, Object>) response.getBody().get("data");
|
|
|
|
|
|
return (String) data.get("token");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:28:10 +08:00
|
|
|
|
/** 鏋勯€犲甫 Bearer Token 鐨勮姹傚疄浣擄紙鏃?body锛?*/
|
2026-04-09 15:16:49 +08:00
|
|
|
|
private HttpEntity<Void> bearerRequest(String token) {
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
|
headers.set("Authorization", "Bearer " + token);
|
|
|
|
|
|
return new HttpEntity<>(headers);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|