|
@@ -0,0 +1,36 @@
|
|
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
|
|
+
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 单家庭工作台-鉴权契约:AdminFamilyWorkbenchController 的每个 POST 端点必须
|
|
|
|
|
+ * @PreAuthorize("hasRole(admin)") 且路径挂在 /api/admin/families/ 下。
|
|
|
|
|
+ * 无 token 由 JwtInterceptor(LOGIN_REQUIRED_PATHS 含 /api/admin/**) 回 401;
|
|
|
|
|
+ * 非 admin 由 @PreAuthorize → GlobalExceptionHandler 回 200+{code:403}。
|
|
|
|
|
+ * 纯反射(无 Spring/DB),任何环境可跑。
|
|
|
|
|
+ */
|
|
|
|
|
+class AdminWorkbenchAuthContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 工作台控制器每个POST端点必须admin鉴权且路径在apiAdminFamilies下() {
|
|
|
|
|
+ int found = 0;
|
|
|
|
|
+ for (Method m : AdminFamilyWorkbenchController.class.getDeclaredMethods()) {
|
|
|
|
|
+ PostMapping pm = m.getAnnotation(PostMapping.class);
|
|
|
|
|
+ if (pm == null) continue;
|
|
|
|
|
+ found++;
|
|
|
|
|
+ PreAuthorize pa = m.getAnnotation(PreAuthorize.class);
|
|
|
|
|
+ assertNotNull(pa, m.getName() + " 必须带 @PreAuthorize");
|
|
|
|
|
+ assertTrue(pa.value().contains("admin"), m.getName() + " 必须要求 admin 角色");
|
|
|
|
|
+ assertTrue(pm.value()[0].startsWith("/families/"),
|
|
|
|
|
+ m.getName() + " 路径必须以 /families/ 开头(familyId 来自 URL): " + pm.value()[0]);
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(found > 0, "控制器必须至少有一个 POST 端点");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|