-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
174 lines (143 loc) · 4.83 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 go_hubspot
import (
"bytes"
"encoding/json"
"fmt"
"github.com/google/go-cmp/cmp"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func getMockFileAPI(mockClient *IHTTPClientMock) HubspotFileAPI {
return HubspotFileAPI{
URLTemplate: "https://api.hubapi.com/files/v3/files%s",
APIKey: "apiKey",
PortalID: "portalId",
httpClient: mockClient,
}
}
func TestMakeFilePublic(t *testing.T) {
mockHubspotHTTPClient := IHTTPClientMock{
DoFunc: func(req *http.Request) (*http.Response, error) {
expectedRequestUrl := "https://api.hubapi.com/files/v3/files/fileid"
expectedOptions := FileUploadOptions{Access: "PUBLIC_NOT_INDEXABLE"}
if req.URL.String() != expectedRequestUrl {
t.Errorf("Unexpected URL: %s", req.URL.String())
return nil, nil
}
var options FileUploadOptions
err := json.NewDecoder(req.Body).Decode(&options)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return nil, nil
}
if !cmp.Equal(options, expectedOptions) {
t.Errorf("Options expected: %#v\nGot: %#v", expectedOptions, options)
return nil, nil
}
w := httptest.NewRecorder()
w.WriteHeader(200)
w.Write([]byte(`{
"id":"59358383010",
"createdAt":"2021-11-09T16:23:40.166Z",
"updatedAt":"2021-11-09T16:23:40.166Z",
"archived":false,
"parentFolderId":"59348125351",
"name":"fileName",
"path":"/folderPath/Test123",
"size":16,
"type":"OTHER",
"extension":"",
"defaultHostingUrl":"https://f.hubspotusercontent10.net/hubfs/8458264/folderPath/Test123",
"url":"https://f.hubspotusercontent10.net/hubfs/8458264/folderPath/Test123",
"isUsableInContent":true,
"access":"PUBLIC_NOT_INDEXABLE"
}`))
return w.Result(), nil
},
}
expectedLink := "https://f.hubspotusercontent10.net/hubfs/8458264/folderPath/Test123"
api := getMockFileAPI(&mockHubspotHTTPClient)
gotLink, err := api.MakeFilePublic("fileid")
if err != nil {
t.Errorf("Error making file public: %s", err.Error())
}
if expectedLink != gotLink {
t.Errorf("File upload returned unexpected link, expected: %s got: %s", expectedLink, gotLink)
}
}
func TestUploadFile(t *testing.T) {
expectedLink := "https://app.hubspot.com/file-preview/portalId/file/59358383010"
fileContent := []byte("Hello World File Content")
mockHubspotHTTPClient := IHTTPClientMock{
DoFunc: func(req *http.Request) (resp *http.Response, err error) {
url := fmt.Sprintf("%s", req.URL)
w := httptest.NewRecorder()
if url == "https://api.hubapi.com/files/v3/files" {
if req.Method != "POST" {
t.Errorf("Unexpected method: expected POST, got: %s", req.Method)
}
expectedOptions := FileUploadOptions{
Access: "PUBLIC_NOT_INDEXABLE",
Overwrite: true,
DuplicateValidationStrategy: "NONE",
DuplicateValidationScope: "EXACT_FOLDER",
}
var gotOptions FileUploadOptions
err := json.Unmarshal([]byte(req.FormValue("options")), &gotOptions)
if err != nil {
t.Errorf("Error unmarshalling request options to a FileUploadOptions struct: %s" + err.Error())
}
if !cmp.Equal(expectedOptions, gotOptions) {
t.Errorf("Unexpected options for file upload expected:\n %v\ngot:\n%v", expectedOptions, gotOptions)
}
file, _, err := req.FormFile("file")
defer file.Close()
if err != nil {
t.Errorf("Error getting file from form request: %s", err.Error())
}
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, file); err != nil {
return nil, err
}
if !cmp.Equal(buf.Bytes(), fileContent) {
t.Errorf("Incorrect file information uploaded")
}
gotFolderPath := req.FormValue("folderPath")
if gotFolderPath != "folderPath" {
t.Errorf("Incorrect folderPath used, expected: folderPath, got: %s", req.FormValue("folderPath"))
}
w.WriteHeader(201)
w.Write([]byte(`{
"id":"59358383010",
"createdAt":"2021-11-09T16:23:40.166Z",
"updatedAt":"2021-11-09T16:23:40.166Z",
"archived":false,
"parentFolderId":"59348125351",
"name":"fileName",
"path":"/folderPath/Test123",
"size":16,
"type":"OTHER",
"extension":"",
"defaultHostingUrl":"https://f.hubspotusercontent10.net/hubfs/8458264/folderPath/Test123",
"url":"https://f.hubspotusercontent10.net/hubfs/8458264/folderPath/Test123",
"isUsableInContent":true,
"access":"PRIVATE"
}`))
} else {
t.Errorf("Unexpected URL: %s", url)
return nil, nil
}
return w.Result(), nil
},
}
api := getMockFileAPI(&mockHubspotHTTPClient)
gotLink, err := api.UploadFile(fileContent, "folderPath", "fileName")
if err != nil {
t.Errorf("Error uploading file: %s", err.Error())
}
if expectedLink != gotLink {
t.Errorf("File upload returned unexpected link, expected: %s got: %s", expectedLink, gotLink)
}
}