Debian Codegraph @

putio/files_test.go

raw symbols SCIP
1 package putio
2
3 import (
4 "bytes"
5 "context"
6 "fmt"
7 "io"
8 "net/http"
9 "strings"
10 "testing"
11 "time"
12 )
13
14 func TestFiles_Get(t *testing.T) {
15 setup()
16 defer teardown()
17
18 fixture := `
19 {
20 "file": {
21 "content_type": "text/plain",
22 "crc32": "66a1512f",
23 "created_at": "2013-09-07T21:32:03",
24 "first_accessed_at": null,
25 "icon": "https://put.io/images/file_types/text.png",
26 "id": 6546533,
27 "is_mp4_available": false,
28 "is_shared": false,
29 "name": "MyFile.txt",
30 "opensubtitles_hash": null,
31 "parent_id": 123,
32 "screenshot": null,
33 "size": 92
34 },
35 "status": "OK"
36 }
37 `
38
39 mux.HandleFunc("/v2/files/1", func(w http.ResponseWriter, r *http.Request) {
40 testMethod(t, r, "GET")
41 fmt.Fprintln(w, fixture)
42 })
43 mux.HandleFunc("/v2/files/2", http.NotFound)
44
45 file, err := client.Files.Get(context.Background(), 1)
46 if err != nil {
47 t.Error(err)
48 }
49
50 if file.Size != 92 {
51 t.Errorf("got: %v, want: 92", file.Size)
52 }
53
54 // non-existent file
55 _, err = client.Files.Get(context.Background(), 2)
56 if err == nil {
57 t.Fatal("must not return nil")
58 }
59 if err, ok := err.(*ErrorResponse); ok && err.Response.StatusCode != 404 {
60 t.Errorf("error: %s, excepted: 404", err)
61 }
62 }
63
64 func TestFiles_List(t *testing.T) {
65 setup()
66 defer teardown()
67
68 fixture := `
69 {
70 "files": [
71 {
72 "content_type": "text/plain",
73 "crc32": "66a1512f",
74 "created_at": "2013-09-07T21:32:03",
75 "first_accessed_at": null,
76 "icon": "https://put.io/images/file_types/text.png",
77 "id": 6546533,
78 "is_mp4_available": false,
79 "is_shared": false,
80 "name": "MyFile.txt",
81 "opensubtitles_hash": null,
82 "parent_id": 123,
83 "screenshot": null,
84 "size": 92
85 },
86 {
87 "content_type": "video/x-matroska",
88 "crc32": "cb97ba70",
89 "created_at": "2013-09-07T21:32:03",
90 "first_accessed_at": "2013-09-07T21:32:13",
91 "icon": "https://put.io/thumbnails/aF5rkZVtYV9pV1iWimSOZWJjWWFaXGZdaZBmY2OJY4uJlV5pj5FiXg%3D%3D.jpg",
92 "id": 7645645,
93 "is_mp4_available": false,
94 "is_shared": false,
95 "name": "MyVideo.mkv",
96 "opensubtitles_hash": "acc2785ffa573c69",
97 "parent_id": 123,
98 "screenshot": "https://put.io/screenshots/aF5rkZVtYV9pV1iWimSOZWJjWWFaXGZdaZBmY2OJY4uJlV5pj5FiXg%3D%3D.jpg",
99 "size": 1155197659
100 }
101 ],
102 "parent": {
103 "content_type": "application/x-directory",
104 "crc32": null,
105 "created_at": "2013-09-07T21:32:03",
106 "first_accessed_at": null,
107 "icon": "https://put.io/images/file_types/folder.png",
108 "id": 123,
109 "is_mp4_available": false,
110 "is_shared": false,
111 "name": "MyFolder",
112 "opensubtitles_hash": null,
113 "parent_id": 0,
114 "screenshot": null,
115 "size": 1155197751
116 },
117 "status": "OK"
118 }
119 `
120 mux.HandleFunc("/v2/files/list", func(w http.ResponseWriter, r *http.Request) {
121 testMethod(t, r, "GET")
122
123 // not found handler
124 parentID := r.URL.Query().Get("parent_id")
125 if parentID == "2" {
126 http.NotFound(w, r)
127 return
128 }
129
130 fmt.Fprintln(w, fixture)
131 })
132
133 files, parent, err := client.Files.List(context.Background(), 0)
134 if err != nil {
135 t.Error(err)
136 }
137
138 if len(files) != 2 {
139 t.Errorf("got: %v, want: 2", len(files))
140 }
141 if parent.ID != 123 {
142 t.Errorf("got: %v, want: 123", parent.ID)
143 }
144
145 // non-existent parent folder
146 _, _, err = client.Files.List(context.Background(), 2)
147 if err == nil {
148 t.Fatal("must not return nil")
149 }
150 if err, ok := err.(*ErrorResponse); ok && err.Response.StatusCode != 404 {
151 t.Errorf("error: %s, excepted: 404", err)
152 }
153 }
154
155 func TestFiles_CreateFolder(t *testing.T) {
156 setup()
157 defer teardown()
158
159 fixture := `
160
161 {
162 "file": {
163 "content_type": "application/x-directory",
164 "crc32": null,
165 "created_at": "2016-07-15T09:21:03",
166 "extension": null,
167 "file_type": "FOLDER",
168 "first_accessed_at": null,
169 "folder_type": "REGULAR",
170 "icon": "https://api.put.io/images/file_types/folder.png",
171 "id": 415105276,
172 "is_hidden": false,
173 "is_mp4_available": false,
174 "is_shared": false,
175 "name": "foobar",
176 "opensubtitles_hash": null,
177 "parent_id": 0,
178 "screenshot": null,
179 "size": 0
180 },
181 "status": "OK"
182 }
183 `
184 mux.HandleFunc("/v2/files/create-folder", func(w http.ResponseWriter, r *http.Request) {
185 testMethod(t, r, "POST")
186 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
187 fmt.Fprintln(w, fixture)
188 })
189
190 file, err := client.Files.CreateFolder(context.Background(), "foobar", 0)
191 if err != nil {
192 t.Error(err)
193 }
194
195 if file.Name != "foobar" {
196 t.Errorf("got: %v, want: foobar", file.Name)
197 }
198
199 // empty folder name
200 _, err = client.Files.CreateFolder(context.Background(), "", 0)
201 if err == nil {
202 t.Errorf("empty folder name accepted")
203 }
204 }
205
206 func TestFiles_Delete(t *testing.T) {
207 setup()
208 defer teardown()
209
210 mux.HandleFunc("/v2/files/delete", func(w http.ResponseWriter, r *http.Request) {
211 testMethod(t, r, "POST")
212 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
213 fmt.Fprintln(w, `{"status": "OK"}`)
214 })
215
216 err := client.Files.Delete(context.Background(), 1, 2, 3)
217 if err != nil {
218 t.Error(err)
219 }
220
221 // empty params
222 err = client.Files.Delete(context.Background())
223 if err == nil {
224 t.Errorf("empty parameters accepted")
225 }
226 }
227
228 func TestFiles_Rename(t *testing.T) {
229 setup()
230 defer teardown()
231
232 mux.HandleFunc("/v2/files/rename", func(w http.ResponseWriter, r *http.Request) {
233 testMethod(t, r, "POST")
234 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
235 fmt.Fprintln(w, `{"status":"OK"}`)
236 })
237
238 err := client.Files.Rename(context.Background(), 1, "bar")
239 if err != nil {
240 t.Error(err)
241 }
242
243 // empty name
244 err = client.Files.Rename(context.Background(), 1, "")
245 if err == nil {
246 t.Errorf("empty filename accepted")
247 }
248 }
249
250 func TestFiles_Move(t *testing.T) {
251 setup()
252 defer teardown()
253
254 mux.HandleFunc("/v2/files/move", func(w http.ResponseWriter, r *http.Request) {
255 testMethod(t, r, "POST")
256 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
257 fmt.Fprintln(w, `{"status":"OK"}`)
258 })
259
260 // move 1, 2, 3, 4 and 5 to root directory (0).
261 err := client.Files.Move(context.Background(), 0, 1, 2, 3, 4, 5)
262 if err != nil {
263 t.Error(err)
264 }
265
266 // no files
267 err = client.Files.Move(context.Background(), 0)
268 if err == nil {
269 t.Errorf("no files given and it is accepted")
270 }
271 }
272
273 func TestFiles_Search(t *testing.T) {
274 setup()
275 defer teardown()
276
277 fixture := `
278 {
279 "files": [
280 {
281 "content_type": "video/x-msvideo",
282 "crc32": "812ed74d",
283 "created_at": "2013-04-30T21:40:04",
284 "extension": "avi",
285 "file_type": "VIDEO",
286 "first_accessed_at": "2013-12-24T09:18:58",
287 "folder_type": "REGULAR",
288 "icon": "https://some-valid-screenhost-url.com",
289 "id": 79905833,
290 "is_hidden": false,
291 "is_mp4_available": true,
292 "is_shared": false,
293 "name": "some-file.mkv",
294 "opensubtitles_hash": "fb5414fd9b9e1e38",
295 "parent_id": 79905827,
296 "screenshot": "https://some-valid-screenhost-url.com",
297 "sender_name": "hafifuyku",
298 "size": 738705408,
299 "start_from": 0
300 },
301 {
302 "content_type": "application/x-directory",
303 "crc32": null,
304 "created_at": "2013-04-30T21:40:03",
305 "extension": null,
306 "file_type": "FOLDER",
307 "first_accessed_at": null,
308 "folder_type": "REGULAR",
309 "icon": "https://some-valid-screenhost-url.com",
310 "id": 79905827,
311 "is_hidden": false,
312 "is_mp4_available": false,
313 "is_shared": false,
314 "name": "Movie 43",
315 "opensubtitles_hash": null,
316 "parent_id": 2197,
317 "screenshot": null,
318 "sender_name": "hafifuyku",
319 "size": 738831202
320 },
321 {
322 "content_type": "application/x-directory",
323 "crc32": null,
324 "created_at": "2010-05-19T22:24:21",
325 "extension": null,
326 "file_type": "FOLDER",
327 "first_accessed_at": null,
328 "folder_type": "REGULAR",
329 "icon": "https://some-valid-screenhost-url.com",
330 "id": 5659875,
331 "is_hidden": false,
332 "is_mp4_available": false,
333 "is_shared": false,
334 "name": "MOVIE",
335 "opensubtitles_hash": null,
336 "parent_id": 0,
337 "screenshot": null,
338 "sender_name": "emsel",
339 "size": 0
340 }
341 ],
342 "next": null,
343 "status": "OK",
344 "total": 3
345 }
346 `
347 mux.HandleFunc("/v2/files/search/", func(w http.ResponseWriter, r *http.Request) {
348 testMethod(t, r, "GET")
349 fmt.Fprintln(w, fixture)
350 })
351
352 s, err := client.Files.Search(context.Background(), "naber", 1)
353 if err != nil {
354 t.Error(err)
355 }
356
357 if len(s.Files) != 3 {
358 t.Errorf("got: %v, want: 3", len(s.Files))
359 }
360
361 if s.Files[0].Name != "some-file.mkv" {
362 t.Errorf("got: %v, want: some-file.mkv", s.Files[0].Name)
363 }
364
365 // invalid page number
366 _, err = client.Files.Search(context.Background(), "naber", 0)
367 if err == nil {
368 t.Errorf("invalid page number accepted")
369 }
370
371 // empty query
372 _, err = client.Files.Search(context.Background(), "", 1)
373 if err == nil {
374 t.Errorf("empty query accepted")
375 }
376 }
377
378 func TestFiles_SetVideoPosition(t *testing.T) {
379 setup()
380 defer teardown()
381
382 mux.HandleFunc("/v2/files/1/start-from", func(w http.ResponseWriter, r *http.Request) {
383 testMethod(t, r, "POST")
384 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
385 fmt.Fprintln(w, `{"statutus":"OK"}`)
386 })
387
388 err := client.Files.SetVideoPosition(context.Background(), 1, 10)
389 if err != nil {
390 t.Error(err)
391 }
392
393 // negative time
394 err = client.Files.SetVideoPosition(context.Background(), 1, -1)
395 if err == nil {
396 t.Errorf("negative time accepted")
397 }
398 }
399
400 func TestFiles_DeleteVideoPosition(t *testing.T) {
401 setup()
402 defer teardown()
403
404 mux.HandleFunc("/v2/files/1/start-from/delete", func(w http.ResponseWriter, r *http.Request) {
405 testMethod(t, r, "POST")
406 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
407 fmt.Fprintln(w, `{"statutus":"OK"}`)
408 })
409
410 err := client.Files.DeleteVideoPosition(context.Background(), 1)
411 if err != nil {
412 t.Error(err)
413 }
414
415 // negative id
416 err = client.Files.DeleteVideoPosition(context.Background(), -1)
417 if err == nil {
418 t.Errorf("negative file id accepted")
419 }
420 }
421
422 func TestFiles_HLSPlaylist(t *testing.T) {
423 setup()
424 defer teardown()
425
426 sampleHLS := `
427 #EXTM3U
428 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=688301
429 http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/0640_vod.m3u8
430 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=165135
431 http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/0150_vod.m3u8
432 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=262346
433 http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/0240_vod.m3u8
434 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=481677
435 http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/0440_vod.m3u8
436 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=1308077
437 http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/1240_vod.m3u8
438 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=1927853
439 http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/1840_vod.m3u8
440 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=2650941
441 http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/2540_vod.m3u8
442 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=3477293
443 http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/3340_vod.m3u8
444 `
445 mux.HandleFunc("/v2/files/1/hls/media.m3u8", func(w http.ResponseWriter, r *http.Request) {
446 testMethod(t, r, "GET")
447 http.ServeContent(w, r, "media.m3u8", time.Now().UTC(), strings.NewReader(sampleHLS))
448 })
449
450 body, err := client.Files.HLSPlaylist(context.Background(), 1, "all")
451 if err != nil {
452 t.Error(err)
453 }
454 defer body.Close()
455
456 var buf bytes.Buffer
457 _, err = io.Copy(&buf, body)
458 if err != nil {
459 t.Error(err)
460 }
461 if buf.String() != sampleHLS {
462 t.Errorf("got: %v, want: %v", buf.String(), sampleHLS)
463 }
464
465 // negative id
466 _, err = client.Files.HLSPlaylist(context.Background(), -1, "all")
467 if err == nil {
468 t.Errorf("negative file ID accepted")
469 }
470
471 // empty key
472 _, err = client.Files.HLSPlaylist(context.Background(), 1, "")
473 if err == nil {
474 t.Errorf("empty key is accepted")
475 }
476 }
477
478 func TestFiles_Shared(t *testing.T) {
479 setup()
480 defer teardown()
481
482 fixture := `
483 {
484 "shared": [
485 {
486 "file_id": 388029022,
487 "file_name": "cowboy",
488 "shared_with": 1
489 },
490 {
491 "file_id": 388029023,
492 "file_name": "bebop",
493 "shared_with": 1
494 }
495 ],
496 "status": "OK"
497 }
498 `
499 mux.HandleFunc("/v2/files/shared", func(w http.ResponseWriter, r *http.Request) {
500 testMethod(t, r, "GET")
501 fmt.Fprintln(w, fixture)
502 })
503
504 files, err := client.Files.shared(context.Background())
505 if err != nil {
506 t.Error(err)
507 }
508 if len(files) != 2 {
509 t.Errorf("got: %v, want: %v", len(files), 2)
510 }
511
512 if files[0].FileID != 388029022 {
513 t.Errorf("got: %v, want: %v", files[0].FileID, 388029022)
514 }
515 }
516
517 func TestFiles_SharedWith(t *testing.T) {
518 setup()
519 defer teardown()
520
521 fixture := `
522 {
523 "shared-with": [
524 {
525 "share_id": 1,
526 "user_avatar_url": "https://some-valid-avatar-url.com/avatar.jpg",
527 "user_name": "spike"
528 },
529 {
530 "share_id": 2,
531 "user_avatar_url": "https://some-valid-avatar-url.com/avatar2.jpg",
532 "user_name": "edward"
533 }
534 ],
535 "status": "OK"
536 }
537 `
538
539 mux.HandleFunc("/v2/files/1/shared-with", func(w http.ResponseWriter, r *http.Request) {
540 testMethod(t, r, "GET")
541 fmt.Fprintln(w, fixture)
542 })
543
544 files, err := client.Files.sharedWith(context.Background(), 1)
545 if err != nil {
546 t.Error(err)
547 }
548 if len(files) != 2 {
549 t.Errorf("got: %v, want: %v", len(files), 2)
550 }
551 }
552
553 func TestFiles_Subtitles(t *testing.T) {
554 setup()
555 defer teardown()
556
557 fixture := `
558 {
559 "default": "key0",
560 "status": "OK",
561 "subtitles": [
562 {
563 "key": "key0",
564 "language": "Turkish",
565 "language_code": "tur",
566 "name": "Big Buck Bunny",
567 "source": "opensubtitles"
568 },
569 {
570 "key": "key1",
571 "language": "English",
572 "language_code": "eng",
573 "name": "Big Buck Bunny",
574 "source": "opensubtitles"
575 }
576 ]
577 }
578 `
579 mux.HandleFunc("/v2/files/1/subtitles", func(w http.ResponseWriter, r *http.Request) {
580 testMethod(t, r, "GET")
581 fmt.Fprintln(w, fixture)
582 })
583
584 subtitles, err := client.Files.Subtitles(context.Background(), 1)
585 if err != nil {
586 t.Error(err)
587 }
588
589 if len(subtitles) != 2 {
590 t.Errorf("got: %v, want: %v", len(subtitles), 2)
591 }
592
593 if subtitles[0].Key != "key0" {
594 t.Errorf("got: %v, want: %v", subtitles[0].Key, "key0")
595 }
596
597 // negative id
598 _, err = client.Files.Subtitles(context.Background(), -1)
599 if err == nil {
600 t.Errorf("negative file ID accepted")
601 }
602 }
603
604 func TestFiles_DownloadSubtitle(t *testing.T) {
605 setup()
606 defer teardown()
607
608 fileContent := `
609 1
610 00:03:07,834 --> 00:03:09,904
611 Let's go down.
612
613 2
614 00:03:20,474 --> 00:03:24,388
615 - You got some out. How many left?
616 - Three out, eight left.
617 `
618 mux.HandleFunc("/v2/files/1/subtitles/", func(w http.ResponseWriter, r *http.Request) {
619 testMethod(t, r, "GET")
620 // trim leading and trailing slashes and split the url path
621 f := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
622
623 var key string
624 // empty key means `default`
625 if len(f) == 4 {
626 key = "default"
627 }
628 // grab the last item of the path
629 if len(f) == 5 {
630 key = f[len(f)-1]
631 }
632
633 switch key {
634 case "default", "key0":
635 http.ServeContent(w, r, "big.buck.bunny.srt", time.Now().UTC(), strings.NewReader(fileContent))
636 default:
637 http.NotFound(w, r)
638 }
639 })
640
641 // valid file ID and valid key
642 rc, err := client.Files.DownloadSubtitle(context.Background(), 1, "key0", "")
643 if err != nil {
644 t.Error(err)
645 }
646 defer rc.Close()
647
648 var buf bytes.Buffer
649 _, err = io.Copy(&buf, rc)
650 if err != nil {
651 t.Error(err)
652 }
653 if buf.String() != fileContent {
654 t.Errorf("got: %v, want: %v", buf.String(), fileContent)
655 }
656
657 // negative file ID
658 rc, err = client.Files.DownloadSubtitle(context.Background(), -1, "key0", "")
659 if err == nil {
660 defer rc.Close()
661 t.Errorf("negative file ID accepted")
662 }
663
664 // invalid key
665 rc, err = client.Files.DownloadSubtitle(context.Background(), 1, "key3", "")
666 if err == nil {
667 defer rc.Close()
668 t.Errorf("invalid key accepted")
669 }
670
671 // empty key
672 rc, err = client.Files.DownloadSubtitle(context.Background(), 1, "", "")
673 if err != nil {
674 t.Error(err)
675 }
676 defer rc.Close()
677
678 buf.Reset()
679 _, err = io.Copy(&buf, rc)
680 if err != nil {
681 t.Error(err)
682 }
683 if buf.String() != fileContent {
684 t.Errorf("got: %v, want: %v", buf.String(), fileContent)
685 }
686 }