This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
uploads_test.go
350 lines (276 loc) · 9.31 KB
/
uploads_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package strava
import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"io/ioutil"
"reflect"
"strings"
"time"
"testing"
)
func TestUploadsGet(t *testing.T) {
client := newCassetteClient(testToken, "upload_get")
upload, err := NewUploadsService(client).Get(46440854).Do()
expected := &UploadDetailed{}
expected.Id = 46440854
expected.ExternalId = "25FA60D8-15CF-472E-8C86-228B16320F41"
expected.Error = ""
expected.Status = "The created activity has been deleted."
expected.ActivityId = 0
if err != nil {
t.Fatalf("service error: %v", err)
}
if !reflect.DeepEqual(upload, expected) {
t.Errorf("should match\n%v\n%v", upload, expected)
}
// from here on out just check the request parameters
s := NewUploadsService(newStoreRequestClient())
// path
s.Get(123).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/uploads/123" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
if transport.request.URL.RawQuery != "" {
t.Errorf("request query incorrect, got %v", transport.request.URL.RawQuery)
}
}
func TestUploadsCreate(t *testing.T) {
data := strings.NewReader(rawGPXDataForTesting())
client := newCassetteClient("special token with write permissions here", "upload_create")
upload, err := NewUploadsService(client).Create(FileDataTypes.GPX, "", data).
Private().
Do()
expected := &UploadSummary{}
expected.Id = 141032026
expected.ExternalId = "golibraryupload.gpx"
expected.Error = ""
expected.Status = "Your activity is still being processed."
expected.ActivityId = 0
if err != nil {
t.Fatalf("service error: %v", err)
}
if !reflect.DeepEqual(upload, expected) {
t.Errorf("should match\n%v\n%v", upload, expected)
}
// upload already gzipped data, first gzip our test data
gzDataBuffer := &bytes.Buffer{}
gzWriter := gzip.NewWriter(gzDataBuffer)
io.Copy(gzWriter, strings.NewReader(rawGPXDataForTesting()))
gzWriter.Close()
client = newCassetteClient("special token with write permissions here", "upload_create_gz")
upload, err = NewUploadsService(client).Create(FileDataTypes.GPXGZ, "upload", gzDataBuffer).
Private().
Do()
expected = &UploadSummary{}
expected.Id = 141038217
expected.ExternalId = "upload.gpx"
expected.Error = ""
expected.Status = "Your activity is still being processed."
expected.ActivityId = 0
if err != nil {
t.Fatalf("service error: %v", err)
}
if !reflect.DeepEqual(upload, expected) {
t.Errorf("should match\n%v\n%v", upload, expected)
}
// bad reader
var r badReader
s := NewUploadsService(newStoreRequestClient())
upload, err = NewUploadsService(client).Create(FileDataTypes.GPX, "", r).
Private().
Do()
if err == nil {
t.Error("should return error for bad reader")
}
s = NewUploadsService(newStoreRequestClient())
upload, err = NewUploadsService(client).Create(FileDataTypes.GPXGZ, "", r).
Private().
Do()
if err == nil {
t.Error("should return error for bad reader")
}
// test unauthorized if no write permissions
data2 := strings.NewReader(rawGPXDataForTesting())
client = newCassetteClient(testToken, "upload_create_unauthorized")
upload, err = NewUploadsService(client).Create(FileDataTypes.GPX, "", data2).
Private().
Do()
if upload != nil {
t.Error("should return nil upload on error")
}
if err == nil {
t.Error("should return error when using unauthorized token")
}
e, ok := err.(Error)
if !ok {
t.Fatal("should return strava error type")
}
if e.Message != "Authorization Error" {
t.Error("should return authorization error")
}
// path
s = NewUploadsService(newStoreRequestClient())
s.Create(FileDataTypes.GPX, "", strings.NewReader(rawGPXDataForTesting())).Do()
transport := s.client.httpClient.Transport.(*storeRequestTransport)
if transport.request.URL.Path != "/api/v3/uploads" {
t.Errorf("request path incorrect, got %v", transport.request.URL.Path)
}
// from here on out just check the request parameters
// data type
s = NewUploadsService(newStoreRequestClient())
s.Create(FileDataTypes.GPXGZ, "", strings.NewReader(rawGPXDataForTesting())).
Do()
body := s.client.httpClient.Transport.(*storeRequestTransport).request.Body
content, _ := ioutil.ReadAll(body)
if !strings.Contains(string(content), "\"data_type\"\r\n\r\ngpx.gz") {
t.Errorf("should include data type in request")
}
// activity type
s = NewUploadsService(newStoreRequestClient())
s.Create(FileDataTypes.GPX, "", strings.NewReader(rawGPXDataForTesting())).
ActivityType(ActivityTypes.AlpineSki).
Do()
body = s.client.httpClient.Transport.(*storeRequestTransport).request.Body
content, _ = ioutil.ReadAll(body)
if !strings.Contains(string(content), "\"activity_type\"\r\n\r\nAlpineSki") {
t.Errorf("should include activity type in request")
}
// name
s = NewUploadsService(newStoreRequestClient())
s.Create(FileDataTypes.GPX, "", strings.NewReader(rawGPXDataForTesting())).
Name("foo").
Do()
body = s.client.httpClient.Transport.(*storeRequestTransport).request.Body
content, _ = ioutil.ReadAll(body)
if !strings.Contains(string(content), "\"name\"\r\n\r\nfoo") {
t.Errorf("should include name in request")
}
// description
s = NewUploadsService(newStoreRequestClient())
s.Create(FileDataTypes.GPX, "", strings.NewReader(rawGPXDataForTesting())).
Description("foo").
Do()
body = s.client.httpClient.Transport.(*storeRequestTransport).request.Body
content, _ = ioutil.ReadAll(body)
if !strings.Contains(string(content), "\"description\"\r\n\r\nfoo") {
t.Errorf("should include description value in request")
}
// private
s = NewUploadsService(newStoreRequestClient())
s.Create(FileDataTypes.GPX, "", strings.NewReader(rawGPXDataForTesting())).
Private().
Do()
body = s.client.httpClient.Transport.(*storeRequestTransport).request.Body
content, _ = ioutil.ReadAll(body)
if !strings.Contains(string(content), "\"private\"\r\n\r\n1") {
t.Errorf("should include private in request")
}
// trainer
s = NewUploadsService(newStoreRequestClient())
s.Create(FileDataTypes.GPX, "", strings.NewReader(rawGPXDataForTesting())).
Trainer().
Do()
body = s.client.httpClient.Transport.(*storeRequestTransport).request.Body
content, _ = ioutil.ReadAll(body)
if !strings.Contains(string(content), "\"trainer\"\r\n\r\n1") {
t.Errorf("should include trainer in request")
}
// external id
s = NewUploadsService(newStoreRequestClient())
s.Create(FileDataTypes.GPX, "", strings.NewReader(rawGPXDataForTesting())).
ExternalId("foo").
Do()
body = s.client.httpClient.Transport.(*storeRequestTransport).request.Body
content, _ = ioutil.ReadAll(body)
if !strings.Contains(string(content), "\"external_id\"\r\n\r\nfoo") {
t.Errorf("should include external id in request")
}
}
func TestUploadsBadJSON(t *testing.T) {
var err error
s := NewUploadsService(NewStubResponseClient("bad json"))
_, err = s.Get(123).Do()
if err == nil {
t.Error("should return a bad json error")
}
_, err = s.Create(FileDataTypes.FIT, "", strings.NewReader("data")).Do()
if err == nil {
t.Error("should return a bad json error")
}
}
func TestFileDataTypeIsGzipped(t *testing.T) {
if FileDataTypes.FIT.isGzipped() {
t.Error("should not be gzipped type")
}
if FileDataTypes.TCX.isGzipped() {
t.Error("should not be gzipped type")
}
if FileDataTypes.GPX.isGzipped() {
t.Error("should not be gzipped type")
}
if !FileDataTypes.FITGZ.isGzipped() {
t.Error("should be gzipped type")
}
if !FileDataTypes.TCXGZ.isGzipped() {
t.Error("should be gzipped type")
}
if !FileDataTypes.GPXGZ.isGzipped() {
t.Error("should be gzipped type")
}
}
func TestFileDataTypeToGzippedType(t *testing.T) {
if FileDataTypes.FIT.toGzippedType() != FileDataTypes.FITGZ {
t.Error("should convert to proper gzipped type")
}
if FileDataTypes.TCX.toGzippedType() != FileDataTypes.TCXGZ {
t.Error("should convert to proper gzipped type")
}
if FileDataTypes.GPX.toGzippedType() != FileDataTypes.GPXGZ {
t.Error("should convert to proper gzipped type")
}
if FileDataTypes.FITGZ.toGzippedType() != FileDataTypes.FITGZ {
t.Error("should return self if already gzipped type")
}
if FileDataTypes.TCXGZ.toGzippedType() != FileDataTypes.TCXGZ {
t.Error("should return self if already gzipped type")
}
if FileDataTypes.GPXGZ.toGzippedType() != FileDataTypes.GPXGZ {
t.Error("should return self if already gzipped type")
}
rand := FileDataType("random")
if rand.toGzippedType() != rand {
t.Error("should return self if random file data type")
}
}
func rawGPXDataForTesting() string {
format := "2006-01-02T15:04:05Z"
now := time.Now()
return fmt.Sprintf(`
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="strava.com iPhone" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<time>%s</time>
</metadata>
<trk>
<name>Morning Ride</name>
<trkseg>
<trkpt lat="37.7737810" lon="-122.4669790">
<ele>72.2</ele>
<time>%s</time>
</trkpt>
<trkpt lat="37.7737580" lon="-122.4669550">
<ele>72.2</ele>
<time>%s</time>
</trkpt>
</trkseg>
</trk>
</gpx>`, now.Format(format), now.Format(format), now.Add(2*time.Second).Format(format))
}
type badReader int
func (badReader) Read(b []byte) (int, error) {
return 0, errors.New("bad reader")
}