-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachment_test.go
82 lines (68 loc) · 1.73 KB
/
attachment_test.go
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
package cloudcms
import (
"bytes"
"io"
"os"
"testing"
)
func TestAttachments(t *testing.T) {
session, err := ConnectDefault()
if err != nil {
t.Fatal(err)
}
repository, err := session.CreateRepository(nil)
if err != nil {
t.Fatal(err)
}
repositoryId := ExtractId(&repository)
defer session.DeleteRepository(repositoryId)
branchId := "master"
nodeId, err := session.CreateNode(repositoryId, branchId, JsonObject{"title": "nodule"}, nil)
if err != nil {
t.Fatal(err)
}
fileBytes, err := os.ReadFile("res/cloudcms.png")
if err != nil {
t.Fatal(err)
}
err = session.UploadAttachment(repositoryId, branchId, nodeId, "default", bytes.NewReader(fileBytes), "image/png", "image.png")
if err != nil {
t.Fatal(err)
}
dl, err := session.DownloadAttachment(repositoryId, branchId, nodeId, "default")
if err != nil {
t.Fatal(err)
}
defer dl.Close()
dlBytes, err := io.ReadAll(dl)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(dlBytes, fileBytes) {
t.Fatal("Downloaded and uploaded files are not the same!")
}
atts, err := session.ListAttachments(repositoryId, branchId, nodeId)
if err != nil {
t.Fatal(err)
}
if len(atts.rows) != 1 {
t.Fatalf("Wrong number of attachments: %d", len(atts.rows))
}
err = session.DeleteAttachment(repositoryId, branchId, nodeId, "default")
if err != nil {
t.Fatal(err)
}
atts, err = session.ListAttachments(repositoryId, branchId, nodeId)
if err != nil {
t.Fatal(err)
}
if len(atts.rows) != 0 {
t.Fatalf("Wrong number of attachments: %d", len(atts.rows))
}
dl, err = session.DownloadAttachment(repositoryId, branchId, nodeId, "default")
if err == nil {
// shouldn't have found attachment
defer dl.Close()
t.Fatal("Attachment not successfully deleted")
}
}