package cn.iocoder.yudao.module.system.service.storage; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.module.system.dal.dataobject.storage.SystemAgentImportSessionDO; import cn.iocoder.yudao.module.system.dal.dataobject.storage.SystemStorageAttachmentDO; import cn.iocoder.yudao.module.system.dal.dataobject.storage.SystemStorageBlobDO; import cn.iocoder.yudao.module.system.dal.mysql.storage.SystemAgentImportSessionMapper; import cn.iocoder.yudao.module.system.dal.mysql.storage.SystemStorageAttachmentMapper; import cn.iocoder.yudao.module.system.framework.agent.AgentBridgeException; import cn.iocoder.yudao.module.system.framework.storage.config.StorageProperties; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; import java.nio.file.Files; import java.nio.file.Path; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class SystemAgentImportBridgeServiceTest { @Mock private SystemAgentImportSessionMapper sessionMapper; @Mock private SystemStorageAttachmentMapper attachmentMapper; @Mock private SystemStorageBlobService blobService; @Mock private SystemStorageAttachmentService attachmentService; @Mock private StorageProperties storageProperties; @InjectMocks private SystemAgentImportBridgeService service; @Test void foreignSessionCannotReadAttachment() { SystemStorageAttachmentDO attachment = new SystemStorageAttachmentDO(); attachment.setRecordType("agent_import_session"); attachment.setRecordId(21L); attachment.setStorageBlobId(90L); when(attachmentMapper.selectById(5L)).thenReturn(attachment); SystemAgentImportSessionDO session = new SystemAgentImportSessionDO(); session.setOwnerUserId(8L); session.setStatus("active"); when(sessionMapper.selectById(21L)).thenReturn(session); try (MockedStatic auth = mockStatic(SecurityFrameworkUtils.class)) { auth.when(SecurityFrameworkUtils::getLoginUserId).thenReturn(7L); assertThrows(AgentBridgeException.class, () -> service.requireOwnedAttachmentRef(5L)); } verifyNoInteractions(blobService); } @Test void ownSessionCannotBindForeignUpload() { SystemAgentImportSessionDO session = new SystemAgentImportSessionDO(); session.setOwnerUserId(7L); session.setStatus("active"); when(sessionMapper.selectById(21L)).thenReturn(session); SystemStorageBlobDO blob = new SystemStorageBlobDO(); blob.setCreator("8"); blob.setByteSize(100L); when(blobService.getStorageBlob(90L)).thenReturn(blob); try (MockedStatic auth = mockStatic(SecurityFrameworkUtils.class)) { auth.when(SecurityFrameworkUtils::getLoginUserId).thenReturn(7L); assertThrows(AgentBridgeException.class, () -> service.bindAttachment(21L, 90L)); } verifyNoInteractions(attachmentService); } @Test void ownerReadsBoundAttachment(@TempDir Path tempDir) throws Exception { Path blobDir = Files.createDirectories(tempDir.resolve("2026/1022")); byte[] content = "code,name\nA,物料A\n".getBytes(java.nio.charset.StandardCharsets.UTF_8); Files.write(blobDir.resolve("uuid-materials.csv"), content); when(storageProperties.getPath()).thenReturn(tempDir.toString()); SystemStorageAttachmentDO attachment = new SystemStorageAttachmentDO(); attachment.setRecordType("agent_import_session"); attachment.setRecordId(21L); attachment.setStorageBlobId(90L); when(attachmentMapper.selectById(5L)).thenReturn(attachment); SystemAgentImportSessionDO session = new SystemAgentImportSessionDO(); session.setOwnerUserId(7L); session.setStatus("active"); when(sessionMapper.selectById(21L)).thenReturn(session); SystemStorageBlobDO blob = new SystemStorageBlobDO(); blob.setCreator("7"); blob.setByteSize((long) content.length); blob.setOriginalFilename("materials.csv"); blob.setPath("2026/1022"); blob.setUidFilename("uuid-materials.csv"); when(blobService.getStorageBlob(90L)).thenReturn(blob); try (MockedStatic auth = mockStatic(SecurityFrameworkUtils.class)) { auth.when(SecurityFrameworkUtils::getLoginUserId).thenReturn(7L); SystemAgentAttachmentRef ref = service.requireOwnedAttachmentRef(5L); assertEquals("materials.csv", ref.originalFilename()); assertEquals(21L, ref.sessionId()); assertEquals(64, service.sha256OfAttachment(5L).length()); } } }