Fix attachment scanning for literal text and multiline links
This commit is contained in:
1 parent
c08bb8a182
commit
b4054189cf
7 files changed
+463
-222
No files matched your search
@@ -85,3 +85,86 @@ func TestReadTokenMayExportButCannotWrite(t *testing.T) {
|
||||
t.Fatalf("只读令牌获得写权限:%d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportDoesNotTreatTextAndFootnotesAsAssets(t *testing.T) {
|
||||
for _, content := range []string{
|
||||
"普通文字 ](private.png)\n",
|
||||
"正文[^1]\n\n[^1]: private.png\n",
|
||||
"\\[转义文字](private.png)\n",
|
||||
"[未闭合链接](private.png\n",
|
||||
"`[代码](private.png)`\n",
|
||||
"正文[^1]\n\n[^1]: 这是脚注内容。\n",
|
||||
} {
|
||||
t.Run(content, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.write("文章.md", content)
|
||||
if err := os.WriteFile(filepath.Join(f.root, "private.png"), []byte("未选中的私有文件"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response := f.do(f.admin, "POST", "/api/v1/export", `{"paths":["文章.md"]}`, nil)
|
||||
defer response.Body.Close()
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.StatusCode != 200 || !strings.HasPrefix(response.Header.Get("Content-Type"), "text/markdown") || string(body) != content {
|
||||
t.Fatalf("应只导出原文,不夹带无关文件:%d %s", response.StatusCode, body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportIncludesAssetsInMultilineLinksAndContainers(t *testing.T) {
|
||||
for _, content := range []string{
|
||||
"\n",
|
||||
"> \n",
|
||||
"> ![图片][pic]\n>\n> [pic]: assets/image.png\n",
|
||||
"> > ![图片][pic]\n> >\n> > [pic]: assets/image.png\n",
|
||||
"- ![图片][pic]\n\n [pic]: assets/image.png\n",
|
||||
"![图片][pic]\n\n[pic]:\n assets/image.png \"说明\"\n",
|
||||
"正文[^1]\n\n[^1]: \n",
|
||||
"![图片][pic]\n\n[pic]: assets/image.png \"[不是链接](private.png)\"\n",
|
||||
"![图片][pic]\n\n[pic]: assets/image.png\n \"[不是链接](private.png)\"\n",
|
||||
"![图片][pic]\n\n[pic]: assets/image.png \"说明\n[fake]: private.png\n\"\n",
|
||||
} {
|
||||
t.Run(content, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.write("文章.md", content)
|
||||
if err := os.MkdirAll(filepath.Join(f.root, "assets"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(f.root, "assets/image.png"), []byte("图片内容"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response := f.do(f.admin, "POST", "/api/v1/export", `{"paths":["文章.md"]}`, nil)
|
||||
defer response.Body.Close()
|
||||
data, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.StatusCode != 200 {
|
||||
t.Fatalf("导出失败:%d %s", response.StatusCode, data)
|
||||
}
|
||||
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||
if err != nil {
|
||||
t.Fatal("附件不应被漏收:", err)
|
||||
}
|
||||
files := map[string]string{}
|
||||
for _, file := range reader.File {
|
||||
r, err := file.Open()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := io.ReadAll(r)
|
||||
r.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files[file.Name] = string(body)
|
||||
}
|
||||
if len(files) != 2 || files["文章.md"] != content || files["assets/image.png"] != "图片内容" {
|
||||
t.Fatalf("原文或附件不符:%v", files)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user