云
9 小时以前 795bc269ba9acfae74620059c4ed64bd121aa288
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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<SecurityFrameworkUtils> 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<SecurityFrameworkUtils> 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<SecurityFrameworkUtils> 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());
        }
    }
}