-
Notifications
You must be signed in to change notification settings - Fork 6
/
transloadit_test.go
executable file
·149 lines (119 loc) · 3.22 KB
/
transloadit_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
package transloadit
import (
"context"
"fmt"
"math/rand"
"net/url"
"os"
"strings"
"testing"
"time"
)
var ctx = context.Background()
var templatesSetup bool
var templateIDOptimizeResize string
func TestNewClient_MissingAuthKey(t *testing.T) {
t.Parallel()
defer func() {
err := recover().(string)
if !strings.Contains(err, "missing AuthKey") {
t.Fatal("error should contain message")
}
}()
_ = NewClient(DefaultConfig)
}
func TestNewClient_MissingAuthSecret(t *testing.T) {
t.Parallel()
defer func() {
err := recover().(string)
if !strings.Contains(err, "missing AuthSecret") {
t.Fatal("error should contain message")
}
}()
config := DefaultConfig
config.AuthKey = "fooo"
_ = NewClient(config)
}
func TestNewClient_Success(t *testing.T) {
t.Parallel()
config := DefaultConfig
config.AuthKey = "fooo"
config.AuthSecret = "bar"
_ = NewClient(config)
}
func setup(t *testing.T) Client {
config := DefaultConfig
config.AuthKey = os.Getenv("TRANSLOADIT_KEY")
config.AuthSecret = os.Getenv("TRANSLOADIT_SECRET")
client := NewClient(config)
return client
}
func setupTemplates(t *testing.T) {
if templatesSetup {
return
}
client := setup(t)
template := NewTemplate()
template.Name = generateTemplateName()
template.AddStep("optimize", map[string]interface{}{
"robot": "/image/optimize",
"use": ":original",
})
template.AddStep("image/resize", map[string]interface{}{
"background": "#000000",
"height": 75,
"resize_strategy": "pad",
"robot": "/image/resize",
"width": 75,
"use": "optimize",
"imagemagick_stack": "v3.0.0",
})
id, err := client.CreateTemplate(ctx, template)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Created template '%s' (%s) for testing.\n", template.Name, id)
templateIDOptimizeResize = id
templatesSetup = true
}
func tearDownTemplate(t *testing.T) {
if !templatesSetup {
return
}
client := setup(t)
if err := client.DeleteTemplate(ctx, templateIDOptimizeResize); err != nil {
t.Fatalf("Error to delete template %s: %s", templateIDOptimizeResize, err)
}
templateIDOptimizeResize = ""
templatesSetup = false
}
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func generateTemplateName() string {
b := make([]rune, 16)
for i := range b {
b[i] = letters[seededRand.Intn(len(letters))]
}
return "gosdk-" + string(b)
}
func TestCreateSignedSmartCDNUrl(t *testing.T) {
client := NewClient(Config{
AuthKey: "foo_key",
AuthSecret: "foo_secret",
})
params := url.Values{}
params.Add("foo", "bar")
params.Add("aaa", "42") // This must be sorted before `foo`
params.Add("aaa", "21")
url := client.CreateSignedSmartCDNUrl(SignedSmartCDNUrlOptions{
Workspace: "foo_workspace",
Template: "foo_template",
Input: "foo/input",
URLParams: params,
ExpiresAt: time.Date(2024, 5, 1, 1, 0, 0, 0, time.UTC),
})
expected := "https://foo_workspace.tlcdn.com/foo_template/foo%2Finput?aaa=42&aaa=21&auth_key=foo_key&exp=1714525200000&foo=bar&sig=sha256%3A9a8df3bb28eea621b46ec808a250b7903b2546be7e66c048956d4f30b8da7519"
if url != expected {
t.Errorf("Expected URL:\n%s\nGot:\n%s", expected, url)
}
}