forked from jpillora/go-dropbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles_test.go
236 lines (196 loc) · 6.21 KB
/
files_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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package dropbox
import (
"bytes"
"crypto/rand"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFiles_Upload(t *testing.T) {
c := client()
file, err := os.Open("Readme.md")
assert.NoError(t, err, "error opening file")
defer file.Close()
out, err := c.Files.Upload(&UploadInput{
Mute: true,
Mode: WriteModeOverwrite,
Path: "/Readme.md",
Reader: file,
})
assert.NoError(t, err, "error uploading file")
assert.Equal(t, "/readme.md", out.PathLower)
}
func TestFiles_Download(t *testing.T) {
c := client()
out, err := c.Files.Download(&DownloadInput{"/Readme.md"})
assert.NoError(t, err, "error downloading")
defer out.Body.Close()
fi, err := os.Lstat("Readme.md")
assert.NoError(t, err, "error getting local file info")
assert.Equal(t, fi.Size(), out.Length, "Readme.md length mismatch")
remote, err := ioutil.ReadAll(out.Body)
assert.NoError(t, err, "error reading remote")
local, err := ioutil.ReadFile("Readme.md")
assert.NoError(t, err, "error reading local")
assert.Equal(t, local, remote, "Readme.md mismatch")
}
func TestFiles_GetMetadata(t *testing.T) {
c := client()
out, err := c.Files.GetMetadata(&GetMetadataInput{
Path: "/Readme.md",
})
assert.NoError(t, err)
assert.Equal(t, "file", out.Tag)
}
func TestFiles_ListFolder(t *testing.T) {
t.Parallel()
c := client()
out, err := c.Files.ListFolder(&ListFolderInput{
Path: "/list",
})
assert.NoError(t, err)
assert.Equal(t, 2000, len(out.Entries))
assert.True(t, out.HasMore)
}
func TestFiles_ListFolder_root(t *testing.T) {
t.Parallel()
c := client()
_, err := c.Files.ListFolder(&ListFolderInput{
Path: "/",
})
assert.NoError(t, err)
}
func TestFiles_Search(t *testing.T) {
c := client()
out, err := c.Files.Search(&SearchInput{
Path: "/",
Query: "hello",
})
assert.NoError(t, err)
assert.Equal(t, 2, len(out.Matches))
}
func TestFiles_Delete(t *testing.T) {
c := client()
out, err := c.Files.Delete(&DeleteInput{
Path: "/Readme.md",
})
assert.NoError(t, err)
assert.Equal(t, "/readme.md", out.PathLower)
}
// A gray, 64 by 64 px PNG
var grayPng = []byte{
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49,
0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x40, 0x08, 0x02,
0x00, 0x00, 0x00, 0x25, 0x0b, 0xe6, 0x89, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41,
0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 0x00, 0x00,
0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, 0x00,
0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0e, 0xc3, 0x00, 0x00, 0x0e, 0xc3,
0x01, 0xc7, 0x6f, 0xa8, 0x64, 0x00, 0x00, 0x00, 0x18, 0x74, 0x45, 0x58, 0x74,
0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x00, 0x70, 0x61, 0x69, 0x6e,
0x74, 0x2e, 0x6e, 0x65, 0x74, 0x20, 0x34, 0x2e, 0x30, 0x2e, 0x36, 0xfc, 0x8c,
0x63, 0xdf, 0x00, 0x00, 0x00, 0x4c, 0x49, 0x44, 0x41, 0x54, 0x68, 0xde, 0xed,
0xcf, 0x31, 0x0d, 0x00, 0x00, 0x08, 0x03, 0x30, 0xe6, 0x7e, 0xb2, 0x91, 0xc0,
0x4d, 0xd2, 0x3a, 0x68, 0xda, 0xce, 0x67, 0x11, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0xb8, 0x2c, 0x4a, 0x27, 0x66, 0x41, 0xb9, 0xd3, 0xef, 0xa3, 0x00,
0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
}
func TestFiles_GetThumbnail(t *testing.T) {
c := client()
// REVIEW(bg): This feels a bit sloppy...
{
buf := bytes.NewBuffer(grayPng)
_, err := c.Files.Upload(&UploadInput{
Mute: true,
Mode: WriteModeOverwrite,
Path: "/gray.png",
Reader: buf,
})
assert.NoError(t, err, "error uploading file")
}
out, err := c.Files.GetThumbnail(&GetThumbnailInput{"/gray.png", GetThumbnailFormatJPEG, GetThumbnailSizeW32H32})
assert.NoError(t, err)
if err != nil {
return
}
defer out.Body.Close()
assert.NotEmpty(t, out.Length, "length should not be 0")
buf := make([]byte, 11)
_, err = out.Body.Read(buf)
assert.NoError(t, err)
assert.Equal(t, []byte{
0xff, 0xd8, // JPEG SOI marker
0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, // JFIF tag
}, buf, "should have jpeg header")
}
func TestFiles_GetPreview(t *testing.T) {
c := client()
out, err := c.Files.GetPreview(&GetPreviewInput{"/sample.ppt"})
defer out.Body.Close()
assert.NoError(t, err)
assert.NotEmpty(t, out.Length, "length should not be 0")
buf := make([]byte, 4)
_, err = out.Body.Read(buf)
assert.NoError(t, err)
assert.Equal(t, []byte{0x25, 0x50, 0x44, 0x46}, buf, "should have pdf magic number")
}
func TestFiles_ListRevisions(t *testing.T) {
c := client()
out, err := c.Files.ListRevisions(&ListRevisionsInput{Path: "/sample.ppt"})
assert.NoError(t, err)
assert.NotEmpty(t, out.Entries)
assert.False(t, out.IsDeleted)
}
func TestFiles_UploadStartAppendFinish(t *testing.T) {
c := client()
//680KB "file" in 200KB chunks
size := int64(680e3)
b := make([]byte, int(size))
_, err := rand.Read(b)
if err != nil {
t.Fatal("crypto fail")
}
//start
start, err := c.Files.UploadSessionStart(&UploadSessionStartInput{
Reader: bytes.NewReader(b[:200]),
})
if err != nil {
t.Errorf("error upload session start: %s", err)
return
}
//initialise cursor with id and offset
cursID := start.UploadSessionCursor.ID
//append twice
if err = c.Files.UploadSessionAppend(&UploadSessionAppendInput{
Cursor: UploadSessionCursor{ID: cursID, Offset: 200},
Reader: bytes.NewReader(b[200:400]),
}); err != nil {
t.Errorf("error upload session append 1: %s", err)
return
}
if err = c.Files.UploadSessionAppend(&UploadSessionAppendInput{
Cursor: UploadSessionCursor{ID: cursID, Offset: 400},
Reader: bytes.NewReader(b[400:600]),
}); err != nil {
t.Errorf("error upload session append 2: %s", err)
return
}
//finish
fin, err := c.Files.UploadSessionFinish(&UploadSessionFinishInput{
Cursor: UploadSessionCursor{ID: cursID, Offset: 600},
Commit: UploadInput{
Path: "/more-noise.txt",
Mode: WriteModeOverwrite,
Mute: true,
Reader: bytes.NewReader(b[600:]),
},
})
if err != nil {
t.Errorf("error upload session finish: %s", err)
return
}
assert.Equal(t, int64(fin.Size), size, "should be the correct size")
}