-
Notifications
You must be signed in to change notification settings - Fork 8
/
file_test.go
174 lines (147 loc) · 4.45 KB
/
file_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
package nylas
import (
"context"
"encoding/base64"
"image/png"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
const b64TestPNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg=="
func testPNG() io.Reader {
return base64.NewDecoder(base64.StdEncoding, strings.NewReader(b64TestPNG))
}
func TestFile(t *testing.T) {
accessToken := "accessToken"
id := "br57kcekhf1hsjq04y8aonkit"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertBasicAuth(t, r, accessToken, "")
assertMethodPath(t, r, http.MethodGet, "/files/"+id)
_, _ = w.Write(getFileJSON)
}))
defer ts.Close()
client := NewClient("", "", withTestServer(ts), WithAccessToken(accessToken))
got, err := client.File(context.Background(), id)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := File{
ID: "8cid1lhd0m7x9k5wjrkpufs1a",
Object: "file",
AccountID: "43jf3n4e***",
ContentType: "image/png",
Filename: "test.png",
ContentDisposition: "inline",
ContentID: "abkhjghgjghkjf476387",
Size: 24429,
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("File: (-got +want):\n%s", diff)
}
}
func TestUploadFile(t *testing.T) {
accessToken := "accessToken"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertBasicAuth(t, r, accessToken, "")
assertMethodPath(t, r, http.MethodPost, "/files")
mp, err := r.MultipartReader()
if err != nil {
t.Fatalf("expected multipart: %v", err)
}
p, err := mp.NextPart()
if err != nil && err != io.EOF {
t.Fatalf("next part: %v", err)
}
if p == nil || p.FormName() != "file" {
t.Fatal("expected single field named \"file\"")
}
wantContentType := "image/png"
if ct := p.Header.Get("Content-Type"); ct != wantContentType {
t.Errorf("expected Content-Type to be detected, got: %q; want %q",
ct, wantContentType)
}
if _, err := png.Decode(p); err != nil {
t.Fatalf("part not valid png: %v", err)
}
_, _ = w.Write(uploadFileJSON)
}))
defer ts.Close()
client := NewClient("", "", withTestServer(ts), WithAccessToken(accessToken))
got, err := client.UploadFile(context.Background(), "test.png", testPNG())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := File{
ID: "8cid1lhd0m7x9k5wjrkpufs1a",
Object: "file",
AccountID: "43jf3n4e***",
ContentType: "image/png",
Filename: "test.png",
Size: 24429,
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("File: (-got +want):\n%s", diff)
}
}
func TestDownloadFile(t *testing.T) {
accessToken := "accessToken"
id := "br57kcekhf1hsjq04y8aonkit"
want := []byte(`body`)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertBasicAuth(t, r, accessToken, "")
assertMethodPath(t, r, http.MethodGet, "/files/"+id+"/download")
_, _ = w.Write(want)
}))
defer ts.Close()
client := NewClient("", "", withTestServer(ts), WithAccessToken(accessToken))
file, err := client.DownloadFile(context.Background(), id)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
t.Fatalf("unexpected read error: %v", err)
}
if diff := cmp.Diff(data, want); diff != "" {
t.Errorf("File: (-got +want):\n%s", diff)
}
}
func TestDeleteFile(t *testing.T) {
accessToken := "accessToken"
id := "br57kcekhf1hsjq04y8aonkit"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertBasicAuth(t, r, accessToken, "")
assertMethodPath(t, r, http.MethodDelete, "/files/"+id)
}))
defer ts.Close()
client := NewClient("", "", withTestServer(ts), WithAccessToken(accessToken))
err := client.DeleteFile(context.Background(), id)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
var getFileJSON = []byte(`{
"account_id": "43jf3n4e***",
"content_type": "image/png",
"content_disposition": "inline",
"content_id": "abkhjghgjghkjf476387",
"filename": "test.png",
"id": "8cid1lhd0m7x9k5wjrkpufs1a",
"object": "file",
"size": 24429
}`)
var uploadFileJSON = []byte(`[
{
"account_id": "43jf3n4e***",
"content_type": "image/png",
"filename": "test.png",
"id": "8cid1lhd0m7x9k5wjrkpufs1a",
"object": "file",
"size": 24429
}
]`)