Przeglądaj źródła

test: ExportController PDF导出测试覆盖(4条)

新增 ExportController 集成测试,覆盖全部代码路径:缺失recordId、非VIP用户、非记录所有者、成功导出PDF。ControllerIntegrationTest 测试数从49增至53。

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
liaoxg 5 miesięcy temu
rodzic
commit
6670832789

+ 74 - 0
num-server/src/test/java/com/etotem/num/controller/ControllerIntegrationTest.java

@@ -18,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.http.MediaType;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.test.web.servlet.MockMvc;
 
 
@@ -70,6 +71,9 @@ class ControllerIntegrationTest {
     @MockBean
     @MockBean
     private AnnotationService annotationService;
     private AnnotationService annotationService;
 
 
+    @MockBean
+    private ExportService exportService;
+
     // Real JWT tokens for testing
     // Real JWT tokens for testing
     private String userToken;
     private String userToken;
     private String adminToken;
     private String adminToken;
@@ -842,4 +846,74 @@ class ControllerIntegrationTest {
                 .andExpect(jsonPath("$.code").value(0))
                 .andExpect(jsonPath("$.code").value(0))
                 .andExpect(jsonPath("$.data.talentTendency.title").value("天赋倾向"));
                 .andExpect(jsonPath("$.data.talentTendency.title").value("天赋倾向"));
     }
     }
+
+    // ==================== ExportController ====================
+
+    @Test
+    void testExportPdf_missingRecordId() throws Exception {
+        mockMvc.perform(post("/api/export/pdf")
+                        .header("Authorization", "Bearer " + userToken)
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{}"))
+                .andExpect(status().isBadRequest())
+                .andExpect(jsonPath("$.code").value(1003))
+                .andExpect(jsonPath("$.message").value("Missing recordId"));
+    }
+
+    @Test
+    void testExportPdf_nonVip() throws Exception {
+        when(userService.isVip(100L)).thenReturn(false);
+
+        mockMvc.perform(post("/api/export/pdf")
+                        .header("Authorization", "Bearer " + userToken)
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{\"recordId\":1}"))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(1009))
+                .andExpect(jsonPath("$.message").value("请升级能量师后可导出 PDF 报告"));
+    }
+
+    @Test
+    void testExportPdf_notOwner() throws Exception {
+        when(userService.isVip(100L)).thenReturn(true);
+        User mockUser = new User();
+        mockUser.setId(100L);
+        when(userService.getById(100L)).thenReturn(mockUser);
+        ChartRecord mockRecord = new ChartRecord();
+        mockRecord.setUserId(999L);
+        when(chartService.getById(1L)).thenReturn(mockRecord);
+
+        mockMvc.perform(post("/api/export/pdf")
+                        .header("Authorization", "Bearer " + userToken)
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{\"recordId\":1}"))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(1002))
+                .andExpect(jsonPath("$.message").value("无权操作此记录"));
+    }
+
+    @Test
+    void testExportPdf_success() throws Exception {
+        when(userService.isVip(100L)).thenReturn(true);
+        User mockUser = new User();
+        mockUser.setId(100L);
+        when(userService.getById(100L)).thenReturn(mockUser);
+        ChartRecord mockRecord = new ChartRecord();
+        mockRecord.setUserId(100L);
+        when(chartService.getById(1L)).thenReturn(mockRecord);
+
+        java.io.File tmpFile = java.io.File.createTempFile("test-export-", ".pdf");
+        tmpFile.deleteOnExit();
+        when(exportService.generatePdf(eq(100L), eq(1L), any(), any()))
+                .thenReturn(tmpFile.getAbsolutePath());
+
+        mockMvc.perform(post("/api/export/pdf")
+                        .header("Authorization", "Bearer " + userToken)
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{\"recordId\":1}"))
+                .andExpect(status().isOk())
+                .andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION,
+                        "attachment; filename=\"" + tmpFile.getName() + "\""))
+                .andExpect(content().contentType(MediaType.APPLICATION_PDF_VALUE));
+    }
 }
 }