-
Notifications
You must be signed in to change notification settings - Fork 6
/
assembly_test.go
266 lines (209 loc) · 5.42 KB
/
assembly_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
package transloadit
import (
"encoding/json"
"os"
"strings"
"testing"
)
var assemblyURL string
func TestStartAssembly_Success(t *testing.T) {
client := setup(t)
assembly := NewAssembly()
file, err := os.Open("./fixtures/lol_cat.jpg")
if err != nil {
t.Fatal(err)
}
assembly.AddReader("image", "lol_cat.jpg", file)
assembly.AddFile("image2", "./fixtures/mona_lisa.jpg")
assembly.AddStep("resize", map[string]interface{}{
"robot": "/image/resize",
"width": 75,
"height": 75,
"resize_strategy": "pad",
"background": "#000000",
"imagemagick_stack": "v3.0.0",
})
assembly.NotifyURL = "https://example.com/"
assembly.Fields["string_test"] = "foo"
assembly.Fields["number_test"] = 100
info, err := client.StartAssembly(ctx, assembly)
if err != nil {
t.Fatal(err)
}
if info.AssemblyID == "" {
t.Fatal("response doesn't contain assembly_id")
}
if info.NotifyURL != "https://example.com/" {
t.Fatal("wrong notify url")
}
if info.Fields["string_test"] != "foo" {
t.Fatal("wrong field string_test")
}
// Go's JSON package parses numbers as a float by default, so we
// need 100 to be a float for comparison.
if info.Fields["number_test"] != float64(100) {
t.Fatal("wrong field number_test")
}
if len(info.Uploads) != 2 {
t.Fatal("wrong number of uploads")
}
if info.Uploads[0].Name == "lol_cat.jpg" {
if info.Uploads[0].Field != "image" {
t.Fatal("wrong field name")
}
} else if info.Uploads[1].Name == "lol_cat.jpg" {
if info.Uploads[1].Field != "image" {
t.Fatal("wrong field name")
}
} else {
t.Fatal("lol_cat.jpg not found in uploads")
}
assemblyURL = info.AssemblyURL
}
func TestGetAssembly(t *testing.T) {
client := setup(t)
assembly, err := client.GetAssembly(ctx, assemblyURL)
if err != nil {
t.Fatal(err)
}
if assembly.AssemblyID == "" {
t.Fatal("assembly id not contained")
}
if assembly.AssemblyURL != assemblyURL {
t.Fatal("assembly urls don't match")
}
}
func TestStartAssembly_Failure(t *testing.T) {
t.Parallel()
config := DefaultConfig
config.AuthKey = "does not exist"
config.AuthSecret = "does not matter"
client := NewClient(config)
assembly := NewAssembly()
file, err := os.Open("./fixtures/lol_cat.jpg")
if err != nil {
t.Fatal(err)
}
assembly.AddReader("image", "lol_cat.jpg", file)
assembly.AddStep("resize", map[string]interface{}{
"robot": "/image/resize",
"width": 75,
"height": 75,
"resize_strategy": "pad",
"background": "#000000",
"imagemagick_stack": "v3.0.0",
})
_, err = client.StartAssembly(ctx, assembly)
reqErr := err.(RequestError)
if reqErr.Code != "GET_ACCOUNT_UNKNOWN_AUTH_KEY" {
t.Fatal("wrong error code in response")
}
if reqErr.Message == "" {
t.Fatal("error message should not be empty")
}
}
func TestStartAssembly_Template(t *testing.T) {
setupTemplates(t)
// Delete Template
defer tearDownTemplate(t)
client := setup(t)
assembly := NewAssembly()
assembly.TemplateID = templateIDOptimizeResize
info, err := client.StartAssembly(ctx, assembly)
if err != nil {
t.Fatal(err)
}
if info.AssemblyID == "" {
t.Fatalf("response doesn't contain assembly_id. %s", info.Error)
}
if !strings.Contains(info.Params, templateIDOptimizeResize) {
t.Fatal("template id not as parameter submitted")
}
}
func TestStartAssemblyReplay(t *testing.T) {
t.Parallel()
client := setup(t)
assembly := NewAssemblyReplay(assemblyURL)
assembly.NotifyURL = "https://example.com/"
assembly.ReparseTemplate = true
assembly.AddStep("convert", map[string]interface{}{
"robot": "/html/convert",
"url": "https://transloadit.com/",
})
info, err := client.StartAssemblyReplay(ctx, assembly)
if err != nil {
t.Fatal(err)
}
if info.Ok != "ASSEMBLY_REPLAYING" {
t.Fatal("wrong status code returned")
}
if info.NotifyURL != "https://example.com/" {
t.Fatal("wrong notify url")
}
}
func TestCancelAssembly(t *testing.T) {
t.Parallel()
client := setup(t)
assembly := NewAssembly()
assembly.AddStep("convert", map[string]interface{}{
"robot": "/html/convert",
"url": "https://transloadit.com/",
})
info, err := client.StartAssembly(ctx, assembly)
if err != nil {
t.Fatal(err)
}
if info.AssemblyURL == "" {
t.Fatal("response doesn't contain assembly_url")
}
info, err = client.CancelAssembly(ctx, info.AssemblyURL)
if err != nil {
t.Fatal(err)
}
if info.Ok != "ASSEMBLY_CANCELED" {
t.Fatal("incorrect assembly status")
}
}
func TestListAssemblies(t *testing.T) {
t.Parallel()
client := setup(t)
assemblies, err := client.ListAssemblies(ctx, &ListOptions{
PageSize: 3,
})
if err != nil {
t.Fatal(err)
}
if len(assemblies.Assemblies) < 3 {
t.Fatal("wrong number of assemblies")
}
if assemblies.Count == 0 {
t.Fatal("wrong count")
}
if assemblies.Assemblies[0].AssemblyID == "" {
t.Fatal("wrong template name")
}
}
func TestInteger_MarshalJSON(t *testing.T) {
var info AssemblyInfo
err := json.Unmarshal([]byte(`{"bytes_expected":55}`), &info)
if err != nil {
t.Fatal(err)
}
if info.BytesExpected != 55 {
t.Fatal("wrong integer parsed")
}
err = json.Unmarshal([]byte(`{"bytes_expected":null}`), &info)
if err != nil {
t.Fatal(err)
}
if info.BytesExpected != 0 {
t.Fatal("wrong default value for null")
}
err = json.Unmarshal([]byte(`{"bytes_expected":""}`), &info)
if err != nil {
t.Fatal(err)
}
if info.BytesExpected != 0 {
t.Fatal("wrong default value for string")
}
}