This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
144 lines (117 loc) · 3.25 KB
/
client_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
package gphoto
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
CookieJsonFile = "cookie.json"
)
func GetBinaryTime() []byte {
return []byte(fmt.Sprintf("%d", time.Now().Unix()))
}
func GenNewSampleFile(orginalPath string) string {
file, err := os.Open(orginalPath)
if err != nil {
panic(err)
}
defer file.Close()
stats, _ := file.Stat()
bTime := GetBinaryTime()
file.WriteAt(bTime, stats.Size()-int64(len(bTime)))
filePath := path.Dir(orginalPath) + "/" + string(bTime) + "_" + path.Base(orginalPath)
sampleFile, err := os.Create(filePath)
io.Copy(sampleFile, file)
return filePath
}
func getTestCookiesFromENV(t testing.TB) []*http.Cookie {
photoCookiesBase64 := os.Getenv("GPHOTO_COOKIES_BASE64")
if photoCookiesBase64 == "" {
t.Fatal("ENV GPHOTO_COOKIES_BASE64 can not be empty")
}
photoCookies, err := base64.URLEncoding.DecodeString(photoCookiesBase64)
require.NoError(t, err)
var cookies []*http.Cookie
if err := json.Unmarshal([]byte(photoCookies), &cookies); err != nil {
t.Log(err)
}
return cookies
}
func TestUpload(t *testing.T) {
sampleFile := GenNewSampleFile("./sample_data/sample.mp4")
defer os.Remove(sampleFile)
client := NewClient(getTestCookiesFromENV(t)...)
t.Run("UploadSuccessWithoutProgressHandler", func(t *testing.T) {
photo, err := client.Upload(sampleFile, "sample.mp4", "", nil)
if err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, photo.ID)
assert.NotEmpty(t, photo.AlbumID)
assert.NotEmpty(t, photo.URL)
assert.True(t, strings.HasPrefix(photo.URL, "https://lh3.googleusercontent.com/"))
fmt.Println(photo.URL, photo.ID)
assert.Equal(t, photo.Name, "sample.mp4")
})
t.Run("UploadSuccessWithoutProgressHandlerAndFileName", func(t *testing.T) {
photo, err := client.Upload(sampleFile, "", "", nil)
if err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, photo.ID)
assert.NotEmpty(t, photo.AlbumID)
assert.NotEmpty(t, photo.URL)
assert.NotEmpty(t, photo.Name)
assert.Equal(t, path.Base(sampleFile), photo.Name)
})
t.Run("UploadSuccessWithProgressHandler", func(t *testing.T) {
var current int64
var total int64
progressHandler := func(c int64, t int64) {
//fmt.Printf("current %d , total %d ", current, total)
// I'm lazy to write this test. But it's work
current = c
total = t
}
photo, err := client.Upload(sampleFile, "", "", progressHandler)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, current, total)
assert.NotEmpty(t, photo.ID)
assert.NotEmpty(t, photo.AlbumID)
assert.NotEmpty(t, photo.URL)
assert.NotEmpty(t, photo.Name)
})
}
func BenchmarkReUpload(b *testing.B) {
for n := 0; n < b.N; n++ {
sampleFile := GenNewSampleFile("./sample_data/sample.mp4")
defer os.Remove(sampleFile)
client := NewClient(getTestCookiesFromENV(b)...)
if _, err := client.Upload(sampleFile, "", "", nil); err != nil {
b.Fatal(err)
}
}
}
func TestLogin(t *testing.T) {
t.Skip()
user := os.Getenv("GOOGLE_USERNAME")
pass := os.Getenv("GOOGLE_PASSWORD")
if len(user) == 0 || len(pass) == 0 {
t.Fatal("User or passowrd is empty")
}
c := NewClient()
if err := c.Login(user, pass); err != nil {
t.Fatal(err)
}
}