刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package jnpf.fileStorage.dms;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.dmsEntity.storage.enums.DmsWorkflowFileType;
import jnpf.dmsEntity.storage.form.DmsWorkflowFileForm;
import jnpf.dmsEntity.storage.vo.DmsFileUploadVO;
import jnpf.dmsEntity.storage.vo.DmsWorkflowFileUploadVO;
import jnpf.exception.DataException;
import org.junit.Test;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
 
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
public class DmsCommonFileReaderTest {
    private final ObjectMapper objectMapper = new ObjectMapper();
    private final DmsCommonFileStorage commonFileStorage = mock(DmsCommonFileStorage.class);
    private final DmsCommonFileReader reader = new DmsCommonFileReader(
            objectMapper, commonFileStorage);
 
    @Test
    public void parsesFileCommonRelativeAndAbsoluteUrls() {
        DmsCommonFileReader.SourceFile relative = reader.parse(
                "/api/file/Image/annex/stored%20file.docx?s=ticket");
        DmsCommonFileReader.SourceFile absolute = reader.parse(
                "https://files.example.com/api/file/Image/temporary/id.pdf?fullfilename=a.pdf");
 
        assertEquals("annex", relative.getType());
        assertEquals("stored file.docx", relative.getFileName());
        assertEquals("temporary", absolute.getType());
        assertEquals("id.pdf", absolute.getFileName());
    }
 
    @Test
    public void rejectsUrlsOutsideFileCommonAndEncodedPathTraversal() {
        assertThrows(DataException.class, () -> reader.parse("https://example.com/file.docx"));
        assertThrows(DataException.class,
                () -> reader.parse("/api/file/Image/annex/%2E%2E%2Fsecret.docx"));
    }
 
    @Test
    @SuppressWarnings("unchecked")
    public void readsStoredContentAndRestoresOriginalNameFromFileJson() {
        byte[] content = "controlled document".getBytes(StandardCharsets.UTF_8);
        Map<String, Object> fileJson = new LinkedHashMap<>();
        fileJson.put("fileId", "stored-file.docx");
        fileJson.put("name", "质量手册.docx");
 
        when(commonFileStorage.exists("annex", "stored-file.docx")).thenReturn(true);
        doAnswer(invocation -> {
            Consumer<InputStream> consumer = invocation.getArgument(2);
            consumer.accept(new ByteArrayInputStream(content));
            return null;
        }).when(commonFileStorage).download(eq("annex"), eq("stored-file.docx"), any());
 
        String result = reader.withFile(
                "/api/file/Image/annex/stored-file.docx", fileJson, "质量手册",
                file -> verifyFile(file, content));
 
        assertEquals("stored", result);
    }
 
    @Test
    @SuppressWarnings("unchecked")
    public void readsDownloadUrlUsingFileIdFromFileJson() {
        byte[] content = "approval page".getBytes(StandardCharsets.UTF_8);
        String url = "/api/file/Download?encryption=ticket&name=%E7%AD%BE%E6%89%B9%E9%A1%B52.0.docx";
        Map<String, Object> fileJson = new LinkedHashMap<>();
        fileJson.put("fileId", "6aad3081cdb2f4d33f7ad915.docx");
        fileJson.put("name", "签批页2.0.docx");
        fileJson.put("url", url);
 
        when(commonFileStorage.exists("annex", "6aad3081cdb2f4d33f7ad915.docx"))
                .thenReturn(true);
        doAnswer(invocation -> {
            Consumer<InputStream> consumer = invocation.getArgument(2);
            consumer.accept(new ByteArrayInputStream(content));
            return null;
        }).when(commonFileStorage).download(
                eq("annex"), eq("6aad3081cdb2f4d33f7ad915.docx"), any());
 
        String result = reader.withFile(url, fileJson, "签批页",
                file -> verifyFile(file, content, "签批页2.0.docx"));
 
        assertEquals("stored", result);
    }
 
    @Test
    public void rejectsDownloadUrlWithoutSafeFileId() {
        String url = "/api/file/Download?encryption=ticket";
        assertThrows(DataException.class,
                () -> reader.withFile(url, new LinkedHashMap<>(), "签批页", file -> null));
 
        Map<String, Object> unsafe = new LinkedHashMap<>();
        unsafe.put("fileId", "../secret.docx");
        unsafe.put("url", url);
        assertThrows(DataException.class,
                () -> reader.withFile(url, unsafe, "签批页", file -> null));
    }
 
    @Test
    public void acceptsFrontendAliasesAndSerializesFlatResponseWithFileJson() throws Exception {
        DmsWorkflowFileForm form = objectMapper.readValue(
                "{\"filetTpe\":\"修定\",\"fileJson\":{\"name\":\"a.docx\"}}",
                DmsWorkflowFileForm.class);
        assertEquals(DmsWorkflowFileType.REVISE, form.getFileType());
        assertEquals("a.docx", ((Map<?, ?>) form.getFileJson()).get("name"));
 
        DmsFileUploadVO file = new DmsFileUploadVO();
        file.setDocumentNo("DOC-001");
        Map<String, Object> fileJson = new LinkedHashMap<>();
        fileJson.put("name", "a.docx");
        DmsWorkflowFileUploadVO response = new DmsWorkflowFileUploadVO(file, fileJson);
        JsonNode json = objectMapper.readTree(objectMapper.writeValueAsString(response));
 
        assertEquals("DOC-001", json.get("documentNo").asText());
        assertEquals("a.docx", json.get("file_json").get("name").asText());
        assertFalse(json.has("file"));
        assertSame(fileJson, response.getFileJson());
    }
 
    private String verifyFile(MultipartFile file, byte[] expected) {
        return verifyFile(file, expected, "质量手册.docx");
    }
 
    private String verifyFile(MultipartFile file, byte[] expected, String expectedName) {
        try {
            assertEquals(expectedName, file.getOriginalFilename());
            assertArrayEquals(expected, file.getBytes());
            return "stored";
        } catch (Exception exception) {
            throw new AssertionError(exception);
        }
    }
}