-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwebhooks_test.go
More file actions
209 lines (198 loc) · 7.68 KB
/
Copy pathwebhooks_test.go
File metadata and controls
209 lines (198 loc) · 7.68 KB
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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestWebhooksEndpoints(t *testing.T) {
tests := []TestCase{
// POST /software/webhooks
{
description: "POST webhook with too-short secret returns 422",
query: "POST /v1/software/webhooks",
body: `{"url": "https://example.org/receiver", "secret": "tooshort"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 422,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
assert.Equal(t, "can't create Webhook", response["title"])
},
},
{
description: "POST webhook with valid 16-char secret returns 201",
query: "POST /v1/software/webhooks",
body: `{"url": "https://example.org/receiver", "secret": "1234567890abcdef"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
assertUUID(t, response["id"])
assert.Equal(t, "https://example.org/receiver", response["url"])
},
},
// GET /webhooks/:id
{
query: "GET /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7",
expectedCode: 200,
expectedBody: `{"id":"007bc84a-7e2d-43a0-b7e1-a256d4114aa7","url":"https://1-b.example.org/receiver","createdAt":"2017-05-01T00:00:00Z","updatedAt":"2017-05-01T00:00:00Z"}`,
expectedContentType: "application/json",
},
{
description: "Non-existent webhook",
query: "GET /v1/webhooks/eea19c82-0449-11ed-bd84-d8bbc146d165",
expectedCode: 404,
expectedBody: `{"title":"can't get Webhook","detail":"Webhook was not found","status":404}`,
expectedContentType: "application/problem+json",
},
// PATCH /webhooks/:id
{
query: "PATCH /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7",
body: `{"url": "https://new.example.org/receiver"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
assert.Equal(t, "007bc84a-7e2d-43a0-b7e1-a256d4114aa7", response["id"])
assert.Equal(t, "https://new.example.org/receiver", response["url"])
assert.Equal(t, "2017-05-01T00:00:00Z", response["createdAt"])
assertRFC3339(t, response["updatedAt"])
assertOnlyKeys(t, response, "id", "url", "createdAt", "updatedAt")
},
},
{
description: "PATCH webhook with non-normalized URL",
query: "PATCH /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7",
body: `{"url": "https://www.new.example.org/receiver/"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
assert.Equal(t, "https://new.example.org/receiver", response["url"])
},
},
{
description: "PATCH webhook - wrong token",
query: "PATCH /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7",
body: `{"url": "https://new.example.org/receiver"}`,
headers: map[string][]string{
"Authorization": {badToken},
"Content-Type": {"application/json"},
},
expectedCode: 401,
expectedBody: `{"title":"token authentication failed","status":401}`,
expectedContentType: "application/problem+json",
},
{
description: "PATCH /v1/webhooks with invalid JSON",
query: "PATCH /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7",
body: `INVALID_JSON`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 400,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
assert.Equal(t, `can't update Webhook`, response["title"])
assert.Equal(t, "invalid or malformed JSON", response["detail"])
},
},
{
description: "PATCH /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7 with JSON with extra fields",
query: "PATCH /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7",
body: `{"url": "https://new.example.org/receiver", "EXTRA_FIELD": "extra field not in schema"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't update Webhook","detail":"unknown field in JSON input","status":422}`,
},
{
description: "PATCH webhook with validation errors",
query: "PATCH /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7",
body: `{"url": "INVALID_URL"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 422,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
assert.Equal(t, `can't update Webhook`, response["title"])
assert.Equal(t, "invalid format: url is invalid", response["detail"])
assert.IsType(t, []interface{}{}, response["validationErrors"])
validationErrors := response["validationErrors"].([]interface{})
assert.Equal(t, 1, len(validationErrors))
firstValidationError := validationErrors[0].(map[string]interface{})
for key := range firstValidationError {
assert.Contains(t, []string{"field", "rule", "value"}, key)
}
},
},
{
description: "PATCH /v1/webhooks with empty body",
query: "PATCH /v1/webhooks/007bc84a-7e2d-43a0-b7e1-a256d4114aa7",
body: "",
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 400,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
assert.Equal(t, `can't update Webhook`, response["title"])
assert.Equal(t, "invalid or malformed JSON", response["detail"])
},
},
// DELETE /webhooks/:id
{
description: "Delete non-existent webhook",
query: "DELETE /v1/webhooks/NO_SUCH_WEBHOOK",
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 404,
// This error is different from because it's returned directly from Fiber's
// route constraints, so we don't need to hit the database to find the resource
// because we already know that's not a valid webhook id looking at its format.
expectedBody: `{"title":"Not Found","detail":"Cannot DELETE /v1/webhooks/NO_SUCH_WEBHOOK","status":404}`,
expectedContentType: "application/problem+json",
},
{
description: "DELETE webhook with bad authentication",
query: "DELETE /v1/webhooks/1702cd06-fffb-4d20-8f55-73e2a00ee052",
headers: map[string][]string{
"Authorization": {badToken},
"Content-Type": {"application/json"},
},
expectedCode: 401,
expectedBody: `{"title":"token authentication failed","status":401}`,
expectedContentType: "application/problem+json",
},
{
query: "DELETE /v1/webhooks/24bc1b5d-fe81-47be-9d55-910f820bdd04",
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 204,
expectedBody: "",
expectedContentType: "",
},
}
runTestCases(t, tests)
}