|
|
@@ -0,0 +1,260 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
+import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
|
|
+import org.springframework.core.type.classreading.MetadataReader;
|
|
|
+import org.springframework.core.type.classreading.MetadataReaderFactory;
|
|
|
+import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
+import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测:匿名访问鉴权门禁(能力键 {@code auth.guard.anonymous},B-227-1 P0 + B-315-1 P1)。
|
|
|
+ *
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/e2e-live/auth-anonymous-guard.spec.js} —— TC-AU-U01/U02/U03
|
|
|
+ * (notification/list,read + search/global 匿名应 401/403,B-227-1 P0 缺陷守卫)、
|
|
|
+ * TC-AU-U11/U12(coupon/list,claim 匿名应 401/403,B-315-1 P1 缺陷守卫)。</li>
|
|
|
+ * <li>设计:{@code docs/tdd/orchestration/task-fix-b227-b315-anonymous-guard-2026-11-20.md}
|
|
|
+ * §3.1/§3.2 —— {@code JwtInterceptor.LOGIN_REQUIRED_PATHS} 收录通知族、全局搜索、
|
|
|
+ * 优惠券族;read 的「他人 id 不可见」归属校验由
|
|
|
+ * {@code NotificationCenterViewContractTest} 第 176 行覆盖(已绿,此处不重复)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状(origin/cfclub 核实)</b>:{@code LOGIN_REQUIRED_PATHS} 未收录
|
|
|
+ * {@code /api/notification/}、{@code /api/search/global}、{@code /api/coupon/};三者既不在
|
|
|
+ * {@code LOGIN_REQUIRED_PATHS} 也不在 {@code PUBLIC_PATHS},落入
|
|
|
+ * {@code setAnonymousUser(request) + setAnonymousSecurityContext()} 默认匿名放行分支
|
|
|
+ * ({@code preHandle} 返回 true,userId=-1 直达控制器 → 200 假成功 / 业务 500)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 断言</b>:以无 token 的 POST 请求调用 {@code preHandle},断言返回 false(→401),
|
|
|
+ * 并以反射读取 {@code LOGIN_REQUIRED_PATHS} 断言 5 个端点被收录。当前全部失败(红)。
|
|
|
+ * GREEN 阶段实现名单收录后自动转绿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>边界</b>:设计 §3.3 明确「本任务不新增『默认拒绝』」(误伤 articles/mini-game 等历史
|
|
|
+ * 匿名端点),故本测试<b>不</b>断言「白名单之外一律 401」,只断言上述 5 个具体端点必须被拒绝。</p>
|
|
|
+ *
|
|
|
+ * <p><b>包路径约束</b>:设计注明「实现仅在 feature 分支、不得假定包路径」,故本测试不 import
|
|
|
+ * {@code JwtInterceptor},而是先按候选 FQCN 查找、再按简单名扫描 classpath 定位,避免假定包路径。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试风格</b>:纯反射 + Servlet Mock 对象,无 Spring 容器 / 无 DB / 无外部服务,
|
|
|
+ * 与同批 RED 契约测试({@code TeamProfitHookContractTest} /
|
|
|
+ * {@code AuthSessionMutexContractRedTest} 等)一致。</p>
|
|
|
+ */
|
|
|
+class AuthGuardAnonymousContractTest {
|
|
|
+
|
|
|
+ /** 候选 FQCN;命中任一即用,均不命中再按简单名扫描 classpath(不假定包路径) */
|
|
|
+ private static final String[] JWT_INTERCEPTOR_CANDIDATES = {
|
|
|
+ "com.etotem.cfc.config.JwtInterceptor",
|
|
|
+ "com.etotem.cfc.interceptor.JwtInterceptor",
|
|
|
+ "com.etotem.cfc.security.JwtInterceptor",
|
|
|
+ "com.etotem.cfc.filter.JwtInterceptor"
|
|
|
+ };
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // 正常流 · 对照基线(当前已实现 · GREEN,证明测试装置有效)
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("基线:已收录的私有端点(/api/user/info)匿名 POST 必须被 preHandle 拒绝 → 401")
|
|
|
+ void control_userInfo_匿名应被拒绝() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertAnonymousRejected(clazz, "/api/user/info", "CONTROL");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("基线:JwtInterceptor 类可定位(候选 FQCN / 简单名扫描)")
|
|
|
+ void jwtInterceptor_classLocatable() {
|
|
|
+ assertNotNull(findJwtInterceptorClass(),
|
|
|
+ "[auth.guard.anonymous] 无法定位 JwtInterceptor 类:契约要求 JwtInterceptor"
|
|
|
+ + " 对匿名请求执行登录门禁(LOGIN_REQUIRED_PATHS 收录)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // B-227-1(P0)· 行为流:3 个私有点匿名必须被拒绝(预期 RED)
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("TC-AU-U01:POST /api/notification/list 匿名应被拒绝(401/403) · B-227-1")
|
|
|
+ void TC_AU_U01_notificationList_匿名应被拒绝() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertAnonymousRejected(clazz, "/api/notification/list", "TC-AU-U01");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("TC-AU-U02:POST /api/notification/read 匿名应被拒绝(401/403) · B-227-1")
|
|
|
+ void TC_AU_U02_notificationRead_匿名应被拒绝() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertAnonymousRejected(clazz, "/api/notification/read", "TC-AU-U02");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("TC-AU-U03:POST /api/search/global 匿名应被拒绝(401/403) · B-227-1")
|
|
|
+ void TC_AU_U03_searchGlobal_匿名应被拒绝() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertAnonymousRejected(clazz, "/api/search/global", "TC-AU-U03");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // B-315-1(P1)· 行为流:优惠券 list/claim 匿名必须被拒绝(预期 RED)
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("TC-AU-U11:POST /api/coupon/list 匿名应被拒绝(401/403) · B-315-1")
|
|
|
+ void TC_AU_U11_couponList_匿名应被拒绝() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertAnonymousRejected(clazz, "/api/coupon/list", "TC-AU-U11");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("TC-AU-U12:POST /api/coupon/claim 匿名应被拒绝(401/403) · B-315-1(写接口不得入业务层)")
|
|
|
+ void TC_AU_U12_couponClaim_匿名应被拒绝() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertAnonymousRejected(clazz, "/api/coupon/claim", "TC-AU-U12");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // 结构流:LOGIN_REQUIRED_PATHS 必须收录 5 个端点(预期 RED)
|
|
|
+ // 用 equals || startsWith 匹配,兼容「族前缀」与「逐端点」两种实现写法
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("结构:LOGIN_REQUIRED_PATHS 应覆盖通知族 list/read(B-227-1)")
|
|
|
+ void loginRequiredPaths_覆盖通知族List和Read() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertLoginRequiredCovers(clazz, "/api/notification/list", "TC-AU-U01",
|
|
|
+ "设计 §3.1:应收录 /api/notification/ 前缀(或逐端点)覆盖通知列表");
|
|
|
+ assertLoginRequiredCovers(clazz, "/api/notification/read", "TC-AU-U02",
|
|
|
+ "设计 §3.1:应收录 /api/notification/ 前缀(或逐端点)覆盖已读标记");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("结构:LOGIN_REQUIRED_PATHS 应覆盖 /api/search/global(B-227-1)")
|
|
|
+ void loginRequiredPaths_覆盖全局搜索() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertLoginRequiredCovers(clazz, "/api/search/global", "TC-AU-U03",
|
|
|
+ "设计 §3.1:全局搜索为私有数据,必须收录 LOGIN_REQUIRED_PATHS");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("结构:LOGIN_REQUIRED_PATHS 应覆盖优惠券 list/claim(B-315-1)")
|
|
|
+ void loginRequiredPaths_覆盖券List和Claim() throws Exception {
|
|
|
+ Class<?> clazz = requireInterceptorClass();
|
|
|
+ assertLoginRequiredCovers(clazz, "/api/coupon/list", "TC-AU-U11",
|
|
|
+ "设计 §3.2:券列表为用户私有读,必须收录(或 /api/coupon/ 前缀)");
|
|
|
+ assertLoginRequiredCovers(clazz, "/api/coupon/claim", "TC-AU-U12",
|
|
|
+ "设计 §3.2:券领取为用户私有写,必须收录(或 /api/coupon/ 前缀)");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // helpers
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ /** 无 token 的 POST 请求调用 preHandle,断言被拒绝(返回 false → 401)。 */
|
|
|
+ private void assertAnonymousRejected(Class<?> clazz, String path, String tc) throws Exception {
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest("POST", path);
|
|
|
+ MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
+ Object interceptor = clazz.getDeclaredConstructor().newInstance();
|
|
|
+ Method preHandle = clazz.getMethod("preHandle",
|
|
|
+ HttpServletRequest.class, HttpServletResponse.class, Object.class);
|
|
|
+
|
|
|
+ Object result;
|
|
|
+ try {
|
|
|
+ result = preHandle.invoke(interceptor, request, response, new Object());
|
|
|
+ } catch (java.lang.reflect.InvocationTargetException e) {
|
|
|
+ Throwable cause = e.getCause();
|
|
|
+ fail("[" + tc + "] POST " + path + " 调用 preHandle 抛异常,契约无法验证: " + cause);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ assertFalse(Boolean.TRUE.equals(result),
|
|
|
+ "[" + tc + "] POST " + path + " 未携带 Authorization 时 JwtInterceptor.preHandle 应返回 false"
|
|
|
+ + "(→401),当前返回 " + result
|
|
|
+ + "(落入 setAnonymousUser 默认匿名放行 → 控制器 200 假成功/业务 500,"
|
|
|
+ + "B-227-1/B-315-1 缺陷守卫)。");
|
|
|
+ assertTrue(response.getStatus() == HttpServletResponse.SC_UNAUTHORIZED
|
|
|
+ || response.getStatus() == HttpServletResponse.SC_FORBIDDEN,
|
|
|
+ "[" + tc + "] POST " + path + " 匿名被拒时应写回 401/403,当前 status=" + response.getStatus());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 反射读取 LOGIN_REQUIRED_PATHS,断言 endpoint 被 equals || startsWith 覆盖。 */
|
|
|
+ private void assertLoginRequiredCovers(Class<?> clazz, String endpoint, String tc, String hint) throws Exception {
|
|
|
+ Field field = clazz.getDeclaredField("LOGIN_REQUIRED_PATHS");
|
|
|
+ field.setAccessible(true);
|
|
|
+ Object value = field.get(null);
|
|
|
+ assertNotNull(value, "[" + tc + "] JwtInterceptor.LOGIN_REQUIRED_PATHS 字段为 null。");
|
|
|
+
|
|
|
+ String[] paths = (String[]) value;
|
|
|
+ boolean covered = false;
|
|
|
+ StringBuilder seen = new StringBuilder();
|
|
|
+ for (String p : paths) {
|
|
|
+ if (seen.length() > 0) {
|
|
|
+ seen.append(", ");
|
|
|
+ }
|
|
|
+ seen.append(p);
|
|
|
+ if (endpoint.equals(p) || endpoint.startsWith(p)) {
|
|
|
+ covered = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(covered,
|
|
|
+ "[" + tc + "] 契约要求 " + endpoint + " 在 LOGIN_REQUIRED_PATHS 中(" + hint
|
|
|
+ + ");当前名单=[" + seen + "] —— 未收录 → 默认匿名放行 → 200 直通(缺陷守卫红)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> requireInterceptorClass() {
|
|
|
+ Class<?> clazz = findJwtInterceptorClass();
|
|
|
+ assertNotNull(clazz,
|
|
|
+ "[auth.guard.anonymous] 无法定位 JwtInterceptor 类:契约要求匿名请求由 JwtInterceptor"
|
|
|
+ + " 登录门禁拒绝。实现仅在 feature 分支,本测试按候选 FQCN + 简单名扫描定位。");
|
|
|
+ return clazz;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> findJwtInterceptorClass() {
|
|
|
+ for (String cn : JWT_INTERCEPTOR_CANDIDATES) {
|
|
|
+ Class<?> c = loadClass(cn);
|
|
|
+ if (c != null) {
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 简单名扫描兜底:不假定包路径(实现若在 feature 分支改包仍可定位)
|
|
|
+ try {
|
|
|
+ PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
|
+ MetadataReaderFactory factory = new CachingMetadataReaderFactory(resolver.getClassLoader());
|
|
|
+ Resource[] resources = resolver.getResources("classpath*:**/JwtInterceptor.class");
|
|
|
+ for (Resource r : resources) {
|
|
|
+ if (!"JwtInterceptor.class".equals(r.getFilename())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ MetadataReader reader = factory.getMetadataReader(r);
|
|
|
+ Class<?> c = loadClass(reader.getClassMetadata().getClassName());
|
|
|
+ if (c != null) {
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ // 扫描失败交给 requireInterceptorClass 的断言给出清晰失败信息
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|