forked from ggordan/go-onedrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_test.go
255 lines (223 loc) · 7.47 KB
/
item_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package onedrive
import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func parseTime(t string) time.Time {
pt, err := time.Parse(time.RFC3339Nano, t)
if err != nil {
panic(err)
}
return pt
}
func validFixtureFromItemID(itemID string) string {
switch itemID {
case "", "root":
return "fixtures/drive.valid.default.json"
default:
return fmt.Sprintf("fixtures/item.%s.valid.json", itemID)
}
}
var userIdentity = &Identity{
DisplayName: "Gordan Grasarevic",
ID: "0123456789abc",
}
var deviceIdentity = &Identity{
DisplayName: "test app",
ID: "test-id",
}
var appIdentity = &Identity{
DisplayName: "OneDrive website",
ID: "44048800",
}
func newItem(name, id string, size int64) *Item {
return &Item{
CreatedDateTime: parseTime("2015-03-08T03:26:46.443Z"),
CTag: "ctag",
ETag: "etag",
ID: id,
LastModifiedDateTime: parseTime("2015-03-09T12:05:17.333Z"),
Name: name,
Size: size,
WebURL: "https://onedrive.live.com/redir?page=self&resid=" + id,
}
}
func newBaseItem(name, id string, size int64) *Item {
return &Item{
Name: name,
ID: id,
Size: size,
CTag: "ctag",
ETag: "etag",
WebURL: "https://onedrive.live.com/redir?page=self&resid=" + id,
CreatedDateTime: parseTime("2015-03-08T03:26:46.443Z"),
LastModifiedDateTime: parseTime("2015-03-09T12:05:17.333Z"),
ContentDownloadURL: "https://download-url.com/someid",
CreatedBy: &IdentitySet{
User: userIdentity,
},
LastModifiedBy: &IdentitySet{
User: userIdentity,
Application: appIdentity,
},
ParentReference: &ItemReference{
DriveID: "0123456789abc",
ID: "0123456789abc!104",
Path: "/drive/root:/Test folder 1",
},
}
}
func newAudioItem(name, id string, size int64, audio *AudioFacet, file *FileFacet) *Item {
item := newBaseItem(name, id, size)
item.Audio = audio
item.File = file
return item
}
func newFolderItem(name, id string, size int64, folder *FolderFacet) *Item {
item := newBaseItem(name, id, size)
item.ContentDownloadURL = ""
item.ParentReference = nil
item.Folder = folder
return item
}
func newImageItem(name, id string, size int64, image *ImageFacet, file *FileFacet) *Item {
item := newBaseItem(name, id, size)
item.Image = image
item.File = file
return item
}
func newPhotoItem(name, id string, size int64, image *ImageFacet, file *FileFacet, photo *PhotoFacet) *Item {
item := newBaseItem(name, id, size)
item.Image = image
item.File = file
item.Photo = photo
return item
}
func newVideoItem(name, id string, size int64, file *FileFacet, photo *PhotoFacet, location *LocationFacet, video *VideoFacet) *Item {
item := newBaseItem(name, id, size)
item.Location = location
item.Photo = photo
item.File = file
item.Video = video
return item
}
func TestItem_Get(t *testing.T) {
setup()
defer teardown()
audioItem := newAudioItem("01 Perth.mp3", "0123456789abc!121", 7904129,
newAudioFacet("Bon Iver", "Bon Iver", "Bon Iver", 238, "Justin Vernon", "", 1, 1, 262138, "\u00a7", false, true, "Perth", 1, 0, 2011),
newFileFacet("audio/mpeg", newHashesFacet("61AA4245BAB442EB18920B293C3E24B44457E665", "CEF984EA")),
)
folderItem := newFolderItem("root", "0123456789abc!101", 10655823, newFolderFacet(3))
folderItem.ParentReference = &ItemReference{
DriveID: "0123456789abc",
ID: "0123456789abc!104",
Path: "/drive/root:/Test folder 1",
}
imageItem := newImageItem("sydney_opera_house_2011-1920x1080.jpg", "0123456789abc!110", 666657,
newImageFacet(1080, 1920),
newFileFacet("image/jpeg", newHashesFacet("6968B0F0934762EC44ADBC90959FAC6F03FBE211", "FEBB5160")),
)
photoItem := newPhotoItem("IMG_2538.JPG", "0123456789abc!119", 403305,
newImageFacet(480, 720),
newFileFacet("image/jpeg", newHashesFacet("D528F485B3A594A36F00ED7633DC2AE1C442A93D", "4DD1C268")),
newPhotoFacet(parseTime("2013-11-28T11:57:27Z"), "Canon", "Canon EOS 600D", 9.0, 200.0, 1.0, 18.0, 0),
)
videoItem := newVideoItem("Video 10-03-2015 20 34 37.mov", "0123456789abc!123", 4114667,
newFileFacet("video/mp4", newHashesFacet("990944543C492C90A703A31BFFEED09BBFCB65BC", "CBBE2450")),
newPhotoFacet(parseTime("2015-03-10T13:34:35Z"), "Apple", "iPhone 5", 0.0, 0.0, 0.0, 0.0, 0),
newLocationFacet(7.824, 51.5074, -0.2377),
newVideoFacet(16382248, 1833, 1920, 1080),
)
tt := []struct {
itemID string
expectedStatus int
expectedOut *Item
}{
{"audio", 200, audioItem},
{"folder", 200, folderItem},
{"image", 200, imageItem},
{"photo", 200, photoItem},
{"video", 200, videoItem},
}
for i, tst := range tt {
mux.HandleFunc("/me/drive/items/"+tst.itemID, fileWrapperHandler(validFixtureFromItemID(tst.itemID), http.StatusOK))
item, _, err := oneDrive.Items.Get(context.Background(), ItemOption{ItemID: tst.itemID, Scope: MeScope{}})
if err != nil {
t.Fatalf("Problem fetching the default drive: %s", err.Error())
}
if !reflect.DeepEqual(item, tst.expectedOut) {
t.Errorf("[%d] Got \n%v Expected \n%v", i, *item, *tst.expectedOut)
}
}
}
func TestGetItemInvalid(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/me/drive/items/missing", fileWrapperHandler("fixtures/request.invalid.notFound.json", http.StatusNotFound))
missingDrive, resp, err := oneDrive.Items.Get(context.Background(), ItemOption{ItemID: "missing", Scope: MeScope{}})
if missingDrive != nil {
t.Fatalf("A drive was returned when an error was expected: %v", resp)
}
expectedErr := &Error{
Code: "itemNotFound",
Message: "Item Does Not Exist",
InnerError: &Error{
Code: "itemDoesNotExist",
InnerError: &Error{
Code: "folderDoesNotExist",
},
},
}
if !reflect.DeepEqual(expectedErr, err) {
t.Errorf("Got %v Expected %v", err, expectedErr)
}
}
func TestListItemChildren(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/drive/items/some-id/children", fileWrapperHandler("fixtures/item.children.valid.json", http.StatusOK))
items, _, err := oneDrive.Items.ListChildren(context.Background(), "some-id")
if err != nil {
t.Fatal(err)
}
if got, want := len(items.Collection), 3; got != want {
t.Fatalf("Got %d Expected %d", got, want)
}
if got, want := items.Collection[0].Folder.ChildCount, int64(10); got != want {
t.Fatalf("Got %d Expected %d folder child items", got, want)
}
if got, want := items.Collection[1].Name, "Test folder 2"; got != want {
t.Fatalf("Got %q Expected %q", got, want)
}
}
func TestListItemChildrenInvalid(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/drive/items/some-id/children", fileWrapperHandler("fixtures/item.children.invalid.json", http.StatusOK))
_, resp, err := oneDrive.Items.ListChildren(context.Background(), "some-id")
if err == nil {
t.Fatalf("Expected error, got : %v", resp)
}
}
func TestCreateFolder(t *testing.T) {
setup()
defer teardown()
folderItem := newFolderItem("root", "0123456789abc!101", 10655823, newFolderFacet(3))
mux.HandleFunc("/drive/items/0123456789abc!104/children/root", fileWrapperHandler("fixtures/item.folder.valid.json", http.StatusOK))
item, _, err := oneDrive.Items.CreateFolder(context.Background(), "0123456789abc!104", "root")
if err != nil {
t.Fatalf("An error occurred while attempting to create a folder: %s", err)
}
if got, want := item, folderItem; reflect.DeepEqual(got, want) {
t.Fatalf("Got %v Expected %v", *got, *want)
}
}
func TestDeleteItem(t *testing.T) {
setup()
defer teardown()
}