test(phase-0): v1.1 验收契约预置(AC-7~12/16~19)与两处棘轮台账登记
后端 9 支 @Disabled 契约只引用现存签名(mvn -o test-compile rc=0),逐条
钉住审计 ✅ 缺口:清空全表删、每日档按 id 越权、出厂默认下匿名可写的
interrupted/upload、12 格共享执行器与拒绝即内联、心跳抢锁变 40902、
批查整实体与无优雅停机、PENDING 被 Math.min(4) 钉死永不终态、
视频详情 SQL 双转义、无 statement timeout、删除多写无事务、
深 OFFSET/ORDER BY RAND/心跳整行/菜单每请求重算、补检 WARN 风暴与无 MDC。
两前端 3 支用 { skip } 预置 AC-16/17/18(网络错误登出、全局兜底与脱敏
上报、单次 PUT 无分片、element-plus 回首屏、列表无竞态门)。
台账两处按官方口径重钉,非放宽:source-pin-ledger 用仓内
UPDATE_SOURCE_PIN_BASELINE=1 生成(含他人 3 支零钉子件记账);
SourceScanTest 点名台账 10→22 名/8→16 支,逐个出处写在注释里。
This commit is contained in:
+138
@@ -0,0 +1,138 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockFilterChain;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* AC-8(R4/审计 S0-7 ✅):匿名可达的写接口必须收紧——{@code /api/tasks/{id}/interrupted}
|
||||
* 与 {@code /api/files/upload} 在无凭据时必须 401,内部令牌或合法 JWT 才放行。
|
||||
*
|
||||
* <p>现状(三条都是可读的事实):
|
||||
* <ul>
|
||||
* <li>{@code AdminApiGuardFilter.java:80-107} 把 {@code /api/files}(:87)与
|
||||
* {@code /api/tasks}(:100,注释 :98-99 正好点名 interrupted)放进
|
||||
* {@code USER_TOOL_PREFIXES},而这组前缀要吃 {@code userToolGuardEnabled}(:141);</li>
|
||||
* <li>{@code application.yml:603} 出厂默认 {@code user-tool-guard-enabled:false}
|
||||
* ({@code AdminGuardSwitchConfigTest} 已把这枚默认钉死)⇒ 整组前缀默认不吃凭据;</li>
|
||||
* <li>{@code TaskHeartbeatController.java:41-52} 与 {@code FileUploadController.java:34-59}
|
||||
* 自身没有任何方法级校验——匿名遍历 taskId 就能把 RUNNING 任务置 FAILED,匿名上传能塞满临时盘。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>因此本用例把两条端点当作 {@code SELF_SERVICE_PREFIXES}(:113-128,关开关也拦)那一类
|
||||
* 来验收:{@code userToolGuardEnabled=false} 这个总闸**不许**成为它们的豁免理由。
|
||||
* 401 的形状沿用既有口径:HTTP 200 + body.code=401(见 {@code AdminApiGuardFilterTest:286-301}
|
||||
* 与 {@code writeFailure:293-298}),不许拿 status 冒充。
|
||||
*/
|
||||
@Disabled("Pending Phase 1: AC-8 匿名写接口收紧(interrupted / files/upload)")
|
||||
@DisplayName("AC-8 匿名写接口必须要求内部令牌或合法 JWT")
|
||||
class PendingPhase1AnonymousWriteGuardTest {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
/** 出厂默认形状:总开关开着、user-tool 那组关着、前缀名单为空。 */
|
||||
private AdminApiGuardFilter factoryDefaults(AdminAuthSupport authSupport) {
|
||||
AdminApiGuardFilter filter = new AdminApiGuardFilter(authSupport, objectMapper);
|
||||
ReflectionTestUtils.setField(filter, "enabled", true);
|
||||
ReflectionTestUtils.setField(filter, "exemptPrefixes", "");
|
||||
ReflectionTestUtils.setField(filter, "userToolGuardEnabled", false);
|
||||
ReflectionTestUtils.setField(filter, "userToolGuardPrefixes", "");
|
||||
return filter;
|
||||
}
|
||||
|
||||
private JsonNode bodyOf(MockHttpServletResponse response) throws Exception {
|
||||
return objectMapper.readTree(response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 匿名打断任务必须被拒() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.isTrustedInternalToken(any())).thenReturn(false);
|
||||
AdminApiGuardFilter filter = factoryDefaults(authSupport);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/tasks/7001/interrupted");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).as("S0-7:匿名请求不许打到控制器").isNull();
|
||||
assertThat(bodyOf(response).path("code").asInt())
|
||||
.as("401 口径写在 body.code(HTTP 200 + 业务码),与全局异常处理一致")
|
||||
.isEqualTo(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 匿名上传文件必须被拒() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.isTrustedInternalToken(any())).thenReturn(false);
|
||||
AdminApiGuardFilter filter = factoryDefaults(authSupport);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/files/upload");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).as("S0-7:2GB 全局上限不等于有鉴权,匿名上传必须拦").isNull();
|
||||
assertThat(bodyOf(response).path("code").asInt()).isEqualTo(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 坏令牌同样必须被拒() throws Exception {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
when(authSupport.isTrustedInternalToken(any())).thenReturn(false);
|
||||
when(authSupport.requireUserOrInternal(any())).thenThrow(new BusinessException(401, "未登录"));
|
||||
AdminApiGuardFilter filter = factoryDefaults(authSupport);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/tasks/7001/interrupted");
|
||||
request.addHeader("Authorization", "Bearer not-a-jwt");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(chain.getRequest()).isNull();
|
||||
assertThat(bodyOf(response).path("code").asInt()).isEqualTo(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 内部令牌与合法会话必须放行() throws Exception {
|
||||
AdminAuthSupport internal = mock(AdminAuthSupport.class);
|
||||
when(internal.isTrustedInternalToken(any())).thenReturn(true);
|
||||
MockFilterChain internalChain = new MockFilterChain();
|
||||
factoryDefaults(internal).doFilter(
|
||||
new MockHttpServletRequest("POST", "/api/tasks/7001/interrupted"),
|
||||
new MockHttpServletResponse(), internalChain);
|
||||
assertThat(internalChain.getRequest())
|
||||
.as("蓝图 §3.1:4.0.66 客户端带内部令牌,兼容窗口内必须能用")
|
||||
.isNotNull();
|
||||
|
||||
AdminAuthSupport jwt = mock(AdminAuthSupport.class);
|
||||
when(jwt.isTrustedInternalToken(any())).thenReturn(false);
|
||||
when(jwt.requireUserOrInternal(any())).thenReturn(new AdminUserEntity());
|
||||
MockFilterChain jwtChain = new MockFilterChain();
|
||||
factoryDefaults(jwt).doFilter(
|
||||
new MockHttpServletRequest("POST", "/api/files/upload"),
|
||||
new MockHttpServletResponse(), jwtChain);
|
||||
assertThat(jwtChain.getRequest()).as("合法 JWT 必须照常上传").isNotNull();
|
||||
verify(jwt).requireUserOrInternal(any());
|
||||
verify(internal, never()).requireUserOrInternal(any());
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import com.nanri.aiimage.testutil.SourceScan;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* AC-21(R8/审计 §5、PERF_REPORT §6):补检 WARN 必须按 job/轮次收敛,全站日志必须能按请求关联。
|
||||
*
|
||||
* <p>两条实测证据:
|
||||
* <ul>
|
||||
* <li><b>补检 WARN 风暴</b>:{@code AppearancePatentDeliverySupport.java:123-124} 对同一
|
||||
* task/job 每轮补检打一条 {@code 交付缺口触发补检轮} WARN;
|
||||
* {@code docs/audit/PERF_REPORT.md:189} 实录 task 32609/job 23345 三分钟 478 条
|
||||
* (≈9,560 条/小时),任务长期停在"有 12 行未完成检测,补检一轮后重新组装"的循环里。
|
||||
* 同文件 {@code :167-201} 的 {@code scheduleFallbackRecheck} 已经有轮次闸
|
||||
* ({@code taskCacheService.recheckRounds/countRecheckRound}),
|
||||
* AC 要的正是把同一对计数键接到 {@code requestRecheckForMissingRows} 上:
|
||||
* 要么按 job/轮次限流,要么一条汇总。</li>
|
||||
* <li><b>无 MDC</b>:{@code RequestTraceFilter.java:47-51} 已经解析出
|
||||
* {@code X-Request-Id / X-Amzn-Trace-Id / Traceparent},但只塞进日志参数
|
||||
* (:63-79),从不 {@code MDC.put}——全仓 {@code grep MDC} 零命中;
|
||||
* {@code logback-spring.xml:7-8} 的两条 pattern 里也没有 {@code %X{...}},
|
||||
* 于是 571 处 log 无法按请求关联(审计 §5.2 判断:"补 MDC 比加日志量更有效")。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>客户端/worker 的单文件大小上限与保留口径统一,另有两支预置:
|
||||
* {@code app_client/tests/test_pending_phase1_日志脱敏与轮转.py}(AC-1,含大小轮转)与
|
||||
* {@code scripts/tests/test_pending_phase5_worker日志轮转与保留.py}(AC-21 的 worker 侧)。
|
||||
*/
|
||||
@Disabled("Pending Phase 5: AC-21 补检 WARN 限流/汇总与 MDC 关联")
|
||||
@DisplayName("AC-21 日志量级与请求关联契约")
|
||||
class PendingPhase5LogVolumeAndMdcTest {
|
||||
|
||||
@Test
|
||||
void 补检轮_WARN_必须按_job_或轮次收敛() {
|
||||
String support = SourceScan.withoutComments(
|
||||
SourceScan.sourceText("AppearancePatentDeliverySupport.java"));
|
||||
String body = SourceScan.methodBody(support, "requestRecheckForMissingRows");
|
||||
|
||||
assertThat(body)
|
||||
.as("§5/PERF §6:每轮一条 WARN 就是 9,560 条/小时;必须复用 :182/:197 那对轮次计数键")
|
||||
.containsAnyOf("recheckRounds", "countRecheckRound", "throttle", "summary");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 请求入口必须把_requestId_写进_MDC() {
|
||||
String filter = SourceScan.withoutComments(SourceScan.sourceText("RequestTraceFilter.java"));
|
||||
|
||||
assertThat(filter)
|
||||
.as("§5.2:requestId 已解析却不入 MDC,571 处日志无法按请求关联")
|
||||
.contains("MDC.put");
|
||||
assertThat(filter)
|
||||
.as("退出请求必须清 MDC(线程复用会把上一请求的 id 带给下一条日志)")
|
||||
.contains("MDC.clear");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 日志_pattern_必须带出_MDC_位() throws Exception {
|
||||
Path moduleRoot = com.nanri.aiimage.testutil.SourceScan.mainSourcesRoot()
|
||||
.resolve("../../src/main/resources/logback-spring.xml")
|
||||
.normalize();
|
||||
String xml = Files.readString(moduleRoot);
|
||||
|
||||
assertThat(xml)
|
||||
.as("pattern 里没有 %X{...} 时,MDC 写了也看不见(FILE/CONSOLE 两条都要带)")
|
||||
.contains("%X{requestId}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user