forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks_test.go
More file actions
433 lines (368 loc) · 12.3 KB
/
webhooks_test.go
File metadata and controls
433 lines (368 loc) · 12.3 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package bitbucket
import (
"encoding/json"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWebhooksList_Success(t *testing.T) {
t.Parallel()
var receivedPath string
var receivedMethod string
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedPath = r.URL.Path
receivedMethod = r.Method
respondJSON(w, http.StatusOK, map[string]interface{}{
"values": []interface{}{
map[string]interface{}{
"uuid": "{hook-1}",
"description": "webhook 1",
"url": "https://example.com/hook1",
"active": true,
"events": []interface{}{"repo:push"},
},
},
})
})
defer server.Close()
opts := &WebhooksOptions{Owner: "owner", RepoSlug: "repo"}
webhooks, err := client.Repositories.Webhooks.List(opts)
require.NoError(t, err)
assert.Equal(t, "GET", receivedMethod)
assert.Equal(t, "/2.0/repositories/owner/repo/hooks/", receivedPath)
assert.Len(t, webhooks, 1)
assert.Equal(t, "{hook-1}", webhooks[0].Uuid)
assert.Equal(t, "https://example.com/hook1", webhooks[0].Url)
}
func TestWebhooksGets_Success(t *testing.T) {
t.Parallel()
var receivedPath string
var receivedMethod string
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedPath = r.URL.Path
receivedMethod = r.Method
respondJSON(w, http.StatusOK, paginatedResponse([]interface{}{
map[string]interface{}{
"uuid": "{hook-1}",
"description": "test hook",
"url": "https://example.com",
"active": true,
},
}))
})
defer server.Close()
opts := &WebhooksOptions{Owner: "owner", RepoSlug: "repo"}
result, err := client.Repositories.Webhooks.Gets(opts)
require.NoError(t, err)
assert.Equal(t, "GET", receivedMethod)
assert.Equal(t, "/2.0/repositories/owner/repo/hooks/", receivedPath)
require.NotNil(t, result)
resultMap, ok := result.(map[string]interface{})
require.True(t, ok, "result should be a map")
values, ok := resultMap["values"].([]interface{})
require.True(t, ok, "result should contain values array")
assert.Len(t, values, 1)
firstItem := values[0].(map[string]interface{})
assert.Equal(t, "{hook-1}", firstItem["uuid"])
}
func TestWebhooksGets_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
defer server.Close()
opts := &WebhooksOptions{Owner: "owner", RepoSlug: "repo"}
result, err := client.Repositories.Webhooks.Gets(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestWebhooksCreate_Success(t *testing.T) {
t.Parallel()
var receivedMethod string
var receivedBody map[string]interface{}
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedMethod = r.Method
bodyBytes, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(bodyBytes, &receivedBody)
respondJSON(w, http.StatusCreated, map[string]interface{}{
"uuid": "{new-hook}",
"description": "my webhook",
"url": "https://example.com/hook",
"active": true,
"events": []interface{}{"repo:push"},
})
})
defer server.Close()
opts := &WebhooksOptions{
Owner: "owner",
RepoSlug: "repo",
Description: "my webhook",
Url: "https://example.com/hook",
Active: true,
Events: []string{"repo:push"},
}
webhook, err := client.Repositories.Webhooks.Create(opts)
require.NoError(t, err)
assert.Equal(t, "POST", receivedMethod)
assert.Equal(t, "{new-hook}", webhook.Uuid)
assert.Equal(t, "my webhook", webhook.Description)
assert.Equal(t, "https://example.com/hook", webhook.Url)
assert.True(t, webhook.Active)
require.Len(t, webhook.Events, 1)
assert.Equal(t, "repo:push", webhook.Events[0])
// Verify request body serialization
assert.Equal(t, "my webhook", receivedBody["description"])
assert.Equal(t, "https://example.com/hook", receivedBody["url"])
assert.Equal(t, true, receivedBody["active"])
events := receivedBody["events"].([]interface{})
require.Len(t, events, 1)
assert.Equal(t, "repo:push", events[0])
}
func TestWebhooksCreate_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
})
defer server.Close()
opts := &WebhooksOptions{
Owner: "owner",
RepoSlug: "repo",
Url: "https://example.com/hook",
Events: []string{"repo:push"},
}
result, err := client.Repositories.Webhooks.Create(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestWebhooksGet_Success(t *testing.T) {
t.Parallel()
var receivedPath string
var receivedMethod string
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedPath = r.URL.Path
receivedMethod = r.Method
respondJSON(w, http.StatusOK, map[string]interface{}{
"uuid": "{hook-uuid}",
"description": "webhook",
"url": "https://example.com/hook",
"active": true,
})
})
defer server.Close()
opts := &WebhooksOptions{Owner: "owner", RepoSlug: "repo", Uuid: "{hook-uuid}"}
webhook, err := client.Repositories.Webhooks.Get(opts)
require.NoError(t, err)
assert.Equal(t, "GET", receivedMethod)
assert.Equal(t, "/2.0/repositories/owner/repo/hooks/{hook-uuid}", receivedPath)
assert.Equal(t, "{hook-uuid}", webhook.Uuid)
}
func TestWebhooksUpdate_Success(t *testing.T) {
t.Parallel()
var receivedMethod string
var receivedPath string
var receivedBody map[string]interface{}
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedMethod = r.Method
receivedPath = r.URL.Path
bodyBytes, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(bodyBytes, &receivedBody)
respondJSON(w, http.StatusOK, map[string]interface{}{
"uuid": "{hook-uuid}",
"description": "updated",
"url": "https://example.com/hook-new",
"active": true,
"events": []interface{}{"repo:push", "issue:created"},
})
})
defer server.Close()
opts := &WebhooksOptions{
Owner: "owner",
RepoSlug: "repo",
Uuid: "{hook-uuid}",
Description: "updated",
Url: "https://example.com/hook-new",
Active: true,
Events: []string{"repo:push", "issue:created"},
}
webhook, err := client.Repositories.Webhooks.Update(opts)
require.NoError(t, err)
assert.Equal(t, "PUT", receivedMethod)
assert.Equal(t, "/2.0/repositories/owner/repo/hooks/{hook-uuid}", receivedPath)
assert.Equal(t, "updated", webhook.Description)
assert.Equal(t, "https://example.com/hook-new", webhook.Url)
assert.True(t, webhook.Active)
require.Len(t, webhook.Events, 2)
assert.Equal(t, "repo:push", webhook.Events[0])
assert.Equal(t, "issue:created", webhook.Events[1])
// Verify request body Events array
events := receivedBody["events"].([]interface{})
require.Len(t, events, 2)
assert.Equal(t, "repo:push", events[0])
assert.Equal(t, "issue:created", events[1])
}
func TestWebhooksUpdate_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
defer server.Close()
opts := &WebhooksOptions{
Owner: "owner",
RepoSlug: "repo",
Uuid: "bad-uuid",
Url: "https://example.com",
Events: []string{"repo:push"},
}
result, err := client.Repositories.Webhooks.Update(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestWebhooksDelete_Success(t *testing.T) {
t.Parallel()
var receivedMethod string
var receivedPath string
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
receivedMethod = r.Method
receivedPath = r.URL.Path
w.WriteHeader(http.StatusNoContent)
})
defer server.Close()
opts := &WebhooksOptions{Owner: "owner", RepoSlug: "repo", Uuid: "{hook-uuid}"}
result, err := client.Repositories.Webhooks.Delete(opts)
require.NoError(t, err)
assert.Equal(t, "DELETE", receivedMethod)
assert.Equal(t, "/2.0/repositories/owner/repo/hooks/{hook-uuid}", receivedPath)
assert.Nil(t, result)
}
func TestWebhooksDelete_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
defer server.Close()
opts := &WebhooksOptions{Owner: "owner", RepoSlug: "repo", Uuid: "bad-uuid"}
result, err := client.Repositories.Webhooks.Delete(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestDecodeWebhook_Success(t *testing.T) {
t.Parallel()
response := map[string]interface{}{
"uuid": "{hook}",
"description": "test",
"url": "https://example.com",
"active": true,
"events": []interface{}{"repo:push"},
}
webhook, err := decodeWebhook(response)
require.NoError(t, err)
assert.Equal(t, "{hook}", webhook.Uuid)
assert.Equal(t, "test", webhook.Description)
assert.True(t, webhook.Active)
}
func TestDecodeWebhook_ErrorType(t *testing.T) {
t.Parallel()
response := map[string]interface{}{
"type": "error",
"error": map[string]interface{}{
"message": "webhook not found",
},
}
_, err := decodeWebhook(response)
assert.Error(t, err)
}
func TestDecodeWebhooks_Success(t *testing.T) {
t.Parallel()
response := map[string]interface{}{
"values": []interface{}{
map[string]interface{}{
"uuid": "{hook-1}",
"description": "hook 1",
"url": "https://example.com/1",
},
map[string]interface{}{
"uuid": "{hook-2}",
"description": "hook 2",
"url": "https://example.com/2",
},
},
}
webhooks, err := decodeWebhooks(response)
require.NoError(t, err)
assert.Len(t, webhooks, 2)
assert.Equal(t, "{hook-1}", webhooks[0].Uuid)
assert.Equal(t, "{hook-2}", webhooks[1].Uuid)
}
func TestBuildWebhooksBody(t *testing.T) {
t.Parallel()
webhooks := &Webhooks{}
opts := &WebhooksOptions{
Description: "test hook",
Url: "https://example.com/hook",
Active: true,
Secret: "mysecret",
Events: []string{"repo:push", "pullrequest:created"},
}
data, err := webhooks.buildWebhooksBody(opts)
require.NoError(t, err)
var body map[string]interface{}
require.NoError(t, json.Unmarshal([]byte(data), &body))
assert.Equal(t, "test hook", body["description"])
assert.Equal(t, "https://example.com/hook", body["url"])
assert.Equal(t, true, body["active"])
assert.Equal(t, "mysecret", body["secret"])
events := body["events"].([]interface{})
assert.Len(t, events, 2)
assert.Equal(t, "repo:push", events[0])
assert.Equal(t, "pullrequest:created", events[1])
}
func TestBuildWebhooksBody_EmptyOptionalFields(t *testing.T) {
t.Parallel()
webhooks := &Webhooks{}
opts := &WebhooksOptions{
Events: []string{"repo:push"},
// Description, Url, Secret are all empty strings -> should be excluded
// Active is false (zero value for bool) -> still included because of the always-true condition
}
data, err := webhooks.buildWebhooksBody(opts)
require.NoError(t, err)
var body map[string]interface{}
require.NoError(t, json.Unmarshal([]byte(data), &body))
// Empty string fields should be excluded from the body
_, hasDescription := body["description"]
assert.False(t, hasDescription, "empty description should be excluded")
_, hasUrl := body["url"]
assert.False(t, hasUrl, "empty url should be excluded")
_, hasSecret := body["secret"]
assert.False(t, hasSecret, "empty secret should be excluded")
// Active is always included (the condition `true || false` is always true)
assert.Equal(t, false, body["active"])
// Events should always be present
events := body["events"].([]interface{})
require.Len(t, events, 1)
assert.Equal(t, "repo:push", events[0])
}
func TestWebhooksList_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
defer server.Close()
opts := &WebhooksOptions{Owner: "owner", RepoSlug: "repo"}
result, err := client.Repositories.Webhooks.List(opts)
assert.Error(t, err)
assert.Nil(t, result)
}
func TestWebhooksGet_Error(t *testing.T) {
t.Parallel()
client, server := setupMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
defer server.Close()
opts := &WebhooksOptions{Owner: "owner", RepoSlug: "repo", Uuid: "bad-uuid"}
result, err := client.Repositories.Webhooks.Get(opts)
assert.Error(t, err)
assert.Nil(t, result)
}