|
|
@@ -7,27 +7,32 @@ import com.etotem.cfc.service.ReportMemberPermissionService;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.never;
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
/**
|
|
|
- * E09 越权修复契约单测:POST /api/health/report/delete 删除健康报告 的归属/权限校验。
|
|
|
+ * E09 越权修复契约单测:健康报告 删除/编辑/下载 三个端点的归属/权限校验。
|
|
|
*
|
|
|
- * 契约(对应 HealthReportController.deleteReport):
|
|
|
+ * 契约(对应 HealthReportController 的 deleteReport / editReport / downloadReport):
|
|
|
* - userId 未注入 → Result.error("用户未登录")
|
|
|
- * - reportId 不存在 → Result.error("报告不存在"),不触发 healthReportService.deleteReport
|
|
|
- * - 上传者本人(report.userId == userId)→ Result.success("已删除")
|
|
|
- * - 家庭管理员(report.familyId 匹配且 isFamilyAdmin=true)→ Result.success("已删除")
|
|
|
- * - 其他登录用户(非本人、非管理员)→ Result.error("无权删除该报告"),不触发 deleteReport
|
|
|
+ * - reportId 不存在 → Result.error("报告不存在"),不触发底层写/删操作
|
|
|
+ * - 上传者本人(report.userId == userId 且 subjectId 为空)→ 允许
|
|
|
+ * - 家庭管理员(report.familyId 匹配且 isFamilyAdmin=true 且 subjectId 为空)→ 允许
|
|
|
+ * - 被授权成员(subjectId 非空且 canViewMemberReports=true)→ 允许
|
|
|
+ * - 其他登录用户(非本人、非管理员、未被授权)→ Result.error("无权..."),不触发底层操作
|
|
|
+ * - familyId 与 report.familyId 均为空 → 拒绝
|
|
|
*
|
|
|
* 测试不依赖数据库/外部服务:healthReportService / reportMemberPermissionService 全 mock;
|
|
|
* 不启 Spring 上下文,沿用 HealthPlanDeleteControllerTest 的直接调用模式。
|
|
|
@@ -111,4 +116,174 @@ class HealthReportControllerDeleteAuthzTest {
|
|
|
assertNotEquals(200, r.getCode());
|
|
|
verify(healthReportService, never()).deleteReport(anyLong());
|
|
|
}
|
|
|
+
|
|
|
+ /* ============ editReport:越权编辑 ============ */
|
|
|
+
|
|
|
+ private HealthReport reportWithSubject(Long reportId, Long ownerUserId, Long familyId, Long subjectId) {
|
|
|
+ HealthReport r = report(reportId, ownerUserId, familyId);
|
|
|
+ r.setSubjectId(subjectId);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> editParams(Object reportId, Object subjectId) {
|
|
|
+ Map<String, Object> p = params(reportId);
|
|
|
+ p.put("subjectId", subjectId);
|
|
|
+ p.put("payload", new HashMap<String, Object>());
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 编辑_未登录_返回用户未登录() {
|
|
|
+ Result<?> r = controller.editReport(editParams(100L, null), null, null);
|
|
|
+ assertEquals("用户未登录", r.getMessage());
|
|
|
+ verify(healthReportService, never()).updateReportFromPayload(any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 编辑_报告不存在_返回报告不存在() {
|
|
|
+ when(healthReportService.getReportById(99L)).thenReturn(null);
|
|
|
+ Result<?> r = controller.editReport(editParams(99L, null), 1L, null);
|
|
|
+ assertEquals("报告不存在", r.getMessage());
|
|
|
+ verify(healthReportService, never()).updateReportFromPayload(any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 编辑_本人可编辑() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(reportWithSubject(100L, 10L, 7L, null));
|
|
|
+ Result<?> r = controller.editReport(editParams(100L, null), 10L, 7L);
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertEquals("已保存", r.getData());
|
|
|
+ verify(healthReportService).updateReportFromPayload(eq(100L), any(), any(), eq(10L));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 编辑_管理员可编辑() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(reportWithSubject(100L, 10L, 7L, null));
|
|
|
+ when(reportMemberPermissionService.isFamilyAdmin(20L, 7L)).thenReturn(true);
|
|
|
+ Result<?> r = controller.editReport(editParams(100L, null), 20L, 7L);
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertEquals("已保存", r.getData());
|
|
|
+ verify(healthReportService).updateReportFromPayload(eq(100L), any(), any(), eq(20L));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 编辑_被授权成员可编辑() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(reportWithSubject(100L, 10L, 7L, 50L));
|
|
|
+ when(reportMemberPermissionService.canViewMemberReports(30L, 50L, 7L)).thenReturn(true);
|
|
|
+ Result<?> r = controller.editReport(editParams(100L, null), 30L, 7L);
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertEquals("已保存", r.getData());
|
|
|
+ verify(healthReportService).updateReportFromPayload(eq(100L), any(), any(), eq(30L));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 编辑_非本人非管理员_拒绝编辑() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(reportWithSubject(100L, 10L, 7L, null));
|
|
|
+ when(reportMemberPermissionService.isFamilyAdmin(30L, 7L)).thenReturn(false);
|
|
|
+ Result<?> r = controller.editReport(editParams(100L, null), 30L, 7L);
|
|
|
+ assertEquals("无权编辑该报告", r.getMessage());
|
|
|
+ verify(healthReportService, never()).updateReportFromPayload(any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 编辑_授权成员但不可见_拒绝编辑() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(reportWithSubject(100L, 10L, 7L, 50L));
|
|
|
+ when(reportMemberPermissionService.canViewMemberReports(30L, 50L, 7L)).thenReturn(false);
|
|
|
+ Result<?> r = controller.editReport(editParams(100L, null), 30L, 7L);
|
|
|
+ assertEquals("无权编辑该报告", r.getMessage());
|
|
|
+ verify(healthReportService, never()).updateReportFromPayload(any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 编辑_家庭缺失_拒绝编辑() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(reportWithSubject(100L, 10L, null, null));
|
|
|
+ Result<?> r = controller.editReport(editParams(100L, null), 10L, null);
|
|
|
+ assertEquals("无权编辑该报告", r.getMessage());
|
|
|
+ verify(healthReportService, never()).updateReportFromPayload(any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ /* ============ downloadReport:越权下载 ============ */
|
|
|
+
|
|
|
+ private static final String FILE_URL = "https://cdn.example.com/report.pdf";
|
|
|
+
|
|
|
+ private HealthReport downloadReportData(Long reportId, Long ownerUserId, Long familyId, Long subjectId) {
|
|
|
+ HealthReport r = report(reportId, ownerUserId, familyId);
|
|
|
+ r.setSubjectId(subjectId);
|
|
|
+ r.setFileUrl(FILE_URL);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpServletRequest mockRequest() {
|
|
|
+ return mock(HttpServletRequest.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_未登录_返回用户未登录() {
|
|
|
+ Result<?> r = controller.downloadReport(params(100L), null, null, mockRequest());
|
|
|
+ assertEquals("用户未登录", r.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_报告不存在_返回报告不存在() {
|
|
|
+ when(healthReportService.getReportById(99L)).thenReturn(null);
|
|
|
+ Result<?> r = controller.downloadReport(params(99L), 1L, null, mockRequest());
|
|
|
+ assertEquals("报告不存在", r.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_本人可下载() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(downloadReportData(100L, 10L, 7L, null));
|
|
|
+ Result<Map<String, Object>> r = controller.downloadReport(params(100L), 10L, 7L, mockRequest());
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertEquals(FILE_URL, r.getData().get("fileUrl"));
|
|
|
+ assertEquals("report.pdf", r.getData().get("fileName"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_管理员可下载() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(downloadReportData(100L, 10L, 7L, null));
|
|
|
+ when(reportMemberPermissionService.isFamilyAdmin(20L, 7L)).thenReturn(true);
|
|
|
+ Result<Map<String, Object>> r = controller.downloadReport(params(100L), 20L, 7L, mockRequest());
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertEquals(FILE_URL, r.getData().get("fileUrl"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_被授权成员可下载() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(downloadReportData(100L, 10L, 7L, 50L));
|
|
|
+ when(reportMemberPermissionService.canViewMemberReports(30L, 50L, 7L)).thenReturn(true);
|
|
|
+ Result<Map<String, Object>> r = controller.downloadReport(params(100L), 30L, 7L, mockRequest());
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertEquals(FILE_URL, r.getData().get("fileUrl"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_非授权_拒绝下载() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(downloadReportData(100L, 10L, 7L, null));
|
|
|
+ when(reportMemberPermissionService.isFamilyAdmin(30L, 7L)).thenReturn(false);
|
|
|
+ Result<?> r = controller.downloadReport(params(100L), 30L, 7L, mockRequest());
|
|
|
+ assertEquals("无权下载该报告", r.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_授权成员但不可见_拒绝下载() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(downloadReportData(100L, 10L, 7L, 50L));
|
|
|
+ when(reportMemberPermissionService.canViewMemberReports(30L, 50L, 7L)).thenReturn(false);
|
|
|
+ Result<?> r = controller.downloadReport(params(100L), 30L, 7L, mockRequest());
|
|
|
+ assertEquals("无权下载该报告", r.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_家庭缺失_拒绝下载() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(downloadReportData(100L, 10L, null, null));
|
|
|
+ Result<?> r = controller.downloadReport(params(100L), 10L, null, mockRequest());
|
|
|
+ assertEquals("无权下载该报告", r.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 下载_无文件_返回无文件可下载() {
|
|
|
+ when(healthReportService.getReportById(100L)).thenReturn(report(100L, 10L, 7L));
|
|
|
+ Result<?> r = controller.downloadReport(params(100L), 10L, 7L, mockRequest());
|
|
|
+ assertEquals("该报告无文件可下载", r.getMessage());
|
|
|
+ }
|
|
|
}
|