This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathfunctions_test.go
238 lines (205 loc) · 7.06 KB
/
functions_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
package webhooks
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"regexp"
"strings"
"testing"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/facebookgo/ensure"
"github.com/facebookgo/jsonpipe"
"github.com/facebookgo/parse"
)
func newFunctionsHarness(t testing.TB) *parsecli.Harness {
h := parsecli.NewHarness(t)
defer h.Stop()
ht := parsecli.TransportFunc(func(r *http.Request) (*http.Response, error) {
var body interface{}
var params map[string]interface{}
if r.Body != nil {
err := json.NewDecoder(r.Body).Decode(¶ms)
if err != nil {
return &http.Response{StatusCode: http.StatusInternalServerError}, err
}
}
switch r.Method {
case "GET":
if r.URL.Path == "/1/hooks/functions/foo" {
body = map[string]interface{}{
"results": []map[string]interface{}{
{"functionName": "foo"},
{"functionName": "foo", "url": "https://api.example.com/foo"},
},
}
} else if r.URL.Path == defaultFunctionsURL {
body = map[string]interface{}{
"results": []map[string]interface{}{
{"functionName": "foo"},
{"functionName": "foo", "url": "https://api.example.com/foo"},
{"functionName": "bar", "url": "https://api.example.com/bar"},
},
}
} else {
return &http.Response{StatusCode: http.StatusBadRequest},
errors.New("no such function hook is defined")
}
case "POST":
ensure.DeepEqual(t, r.URL.Path, defaultFunctionsURL)
switch params["functionName"] {
case "foo":
body = map[string]interface{}{
"functionName": "foo",
"url": "https://api.example.com/foo",
"warning": "function foo already exists",
}
case "bar":
body = map[string]interface{}{
"functionName": "bar",
"url": "https://api.example.com/bar",
}
default:
return &http.Response{StatusCode: http.StatusInternalServerError},
errors.New("invalid function name")
}
case "PUT":
if params["__op"] == "Delete" && strings.HasPrefix(r.URL.Path, "/foo") {
ensure.DeepEqual(t, r.URL.Path, "/1/hooks/functions/foo")
body = map[string]interface{}{"functionName": "foo"}
} else {
switch strings.Replace(r.URL.Path, "/1/hooks/functions/", "", 1) {
case "foo":
ensure.DeepEqual(t, r.URL.Path, "/1/hooks/functions/foo")
body = map[string]interface{}{
"functionName": "foo",
"url": "https://api.example.com/_foo",
"warning": "function foo already exists",
}
case "bar":
ensure.DeepEqual(t, r.URL.Path, "/1/hooks/functions/bar")
body = map[string]interface{}{
"functionName": "bar",
"url": "https://api.example.com/_bar",
}
default:
return &http.Response{StatusCode: http.StatusInternalServerError},
errors.New("invalid function name")
}
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(jsonpipe.Encode(body)),
}, nil
})
h.Env.ParseAPIClient = &parsecli.ParseAPIClient{APIClient: &parse.Client{Transport: ht}}
return h
}
func TestFunctionHookString(t *testing.T) {
t.Parallel()
f := functionHook{FunctionName: "foo"}
ensure.DeepEqual(t, f.String(), `Function name: "foo"`)
f.URL = "https://api.example.com/foo"
ensure.DeepEqual(t,
f.String(),
`Function name: "foo", URL: "https://api.example.com/foo"`)
}
func TestReadFuncParams(t *testing.T) {
t.Parallel()
h := parsecli.NewHarness(t)
defer h.Stop()
h.Env.In = strings.NewReader("\n")
_, err := readFunctionName(h.Env, nil)
ensure.Err(t, err, regexp.MustCompile("Function name cannot be empty"))
h.Env.In = strings.NewReader("foo\n")
hook, err := readFunctionName(h.Env, nil)
ensure.Nil(t, err)
ensure.DeepEqual(t, *hook, functionHook{FunctionName: "foo"})
h.Env.In = strings.NewReader("foo\napi.example.com/foo\n")
hook, err = readFunctionParams(h.Env, nil)
ensure.Nil(t, err)
ensure.DeepEqual(t, *hook, functionHook{
FunctionName: "foo",
URL: "https://api.example.com/foo",
})
}
func TestFunctionHooksRead(t *testing.T) {
t.Parallel()
h := newFunctionsHarness(t)
f := functionHooksCmd{All: true}
ensure.Nil(t, f.functionHooksRead(h.Env, nil))
ensure.DeepEqual(t, h.Out.String(), `The following cloudcode or webhook functions are associated with this app:
Function name: "bar", URL: "https://api.example.com/bar"
Function name: "foo"
Function name: "foo", URL: "https://api.example.com/foo"
`)
f.All = false
h.Env.In = strings.NewReader("foo\napi.example.com/foo\n")
h.Out.Reset()
ensure.Nil(t, f.functionHooksRead(h.Env, nil))
ensure.DeepEqual(t, h.Out.String(), `Please enter the function name: The following functions named: "foo" are associated with your app:
Function name: "foo"
Function name: "foo", URL: "https://api.example.com/foo"
`)
}
func TestFunctionHooksCreate(t *testing.T) {
t.Parallel()
h := newFunctionsHarness(t)
var f functionHooksCmd
h.Env.In = strings.NewReader("foo\napi.example.com/foo\n")
ensure.Nil(t, f.functionHooksCreate(h.Env, nil))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter the function name: URL: https://Successfully created a webhook function "foo" pointing to "https://api.example.com/foo"
`)
ensure.DeepEqual(t, h.Err.String(), "WARNING: function foo already exists\n")
h.Out.Reset()
h.Err.Reset()
h.Env.In = strings.NewReader("bar\napi.example.com/bar\n")
ensure.Nil(t, f.functionHooksCreate(h.Env, nil))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter the function name: URL: https://Successfully created a webhook function "bar" pointing to "https://api.example.com/bar"
`)
ensure.DeepEqual(t, h.Err.String(), "")
}
func TestFunctionHooksUpdate(t *testing.T) {
t.Parallel()
h := newFunctionsHarness(t)
var f functionHooksCmd
h.Env.In = strings.NewReader("foo\napi.example.com/_foo\n")
ensure.Nil(t, f.functionHooksUpdate(h.Env, nil))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter the function name: URL: https://Successfully update the webhook function "foo" to point to "https://api.example.com/_foo"
`)
ensure.DeepEqual(t, h.Err.String(), "WARNING: function foo already exists\n")
h.Out.Reset()
h.Err.Reset()
h.Env.In = strings.NewReader("bar\napi.example.com/_bar\n")
ensure.Nil(t, f.functionHooksUpdate(h.Env, nil))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter the function name: URL: https://Successfully update the webhook function "bar" to point to "https://api.example.com/_bar"
`)
ensure.DeepEqual(t, h.Err.String(), "")
}
func TestFunctionHookDelete(t *testing.T) {
t.Parallel()
h := newFunctionsHarness(t)
f := &functionHooksCmd{interactive: true}
h.Env.In = strings.NewReader("foo\ny\n")
ensure.Nil(t, f.functionHooksDelete(h.Env, nil))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter the function name: Are you sure you want to delete webhook function: "foo" (y/n): Successfully deleted webhook function "foo"
Function "foo" defined in Cloud Code will be used henceforth
`)
h.Out.Reset()
h.Env.In = strings.NewReader("foo\nn\n")
ensure.Nil(t, f.functionHooksDelete(h.Env, nil))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter the function name: Are you sure you want to delete webhook function: "foo" (y/n): `)
}