forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhooks_test.go
217 lines (187 loc) · 5.61 KB
/
webhooks_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
package tests
import (
"errors"
"fmt"
"os"
"testing"
"github.com/diggerhq/go-bitbucket"
)
func TestWebhook(t *testing.T) {
user := os.Getenv("BITBUCKET_TEST_USERNAME")
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
owner := os.Getenv("BITBUCKET_TEST_OWNER")
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
if user == "" {
t.Error("BITBUCKET_TEST_USERNAME is empty.")
}
if pass == "" {
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
}
if owner == "" {
t.Error("BITBUCKET_TEST_OWNER is empty.")
}
if repo == "" {
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
}
c := bitbucket.NewBasicAuth(user, pass)
var webhookResourceUuid string
t.Run("create", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Description: "go-bb-test",
Url: "https://example.com",
Active: false,
Events: []string{bitbucket.RepoPushEvent, bitbucket.IssueCreatedEvent},
}
webhook, err := c.Repositories.Webhooks.Create(opt)
if err != nil {
t.Error(err)
}
if webhook == nil {
t.Error("The webhook could not be created.")
}
if webhook.Description != "go-bb-test" {
t.Error("The webhook `description` attribute does not match the expected value.")
}
if webhook.Url != "https://example.com" {
t.Error("The webhook `url` attribute does not match the expected value.")
}
if webhook.Active != false {
t.Error("The webhook `active` attribute does not match the expected value.")
}
if len(webhook.Events) != 2 {
t.Error("The webhook `events` attribute does not match the expected value.")
}
webhookResourceUuid = webhook.Uuid
})
t.Run("get", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Uuid: webhookResourceUuid,
}
webhook, err := c.Repositories.Webhooks.Get(opt)
if err != nil {
t.Error(err)
}
if webhook == nil {
t.Error("The webhook could not be retrieved.")
}
if webhook.Description != "go-bb-test" {
t.Error("The webhook `description` attribute does not match the expected value.")
}
if webhook.Url != "https://example.com" {
t.Error("The webhook `url` attribute does not match the expected value.")
}
if webhook.Active != false {
t.Error("The webhook `active` attribute does not match the expected value.")
}
if len(webhook.Events) != 2 {
t.Error("The webhook `events` attribute does not match the expected value.")
}
})
t.Run("update", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Uuid: webhookResourceUuid,
Description: "go-bb-test-new",
Url: "https://new-example.com",
Events: []string{bitbucket.RepoPushEvent, bitbucket.IssueCreatedEvent, bitbucket.RepoForkEvent},
}
webhook, err := c.Repositories.Webhooks.Update(opt)
if err != nil {
t.Error(err)
}
if webhook == nil {
t.Error("The webhook could not be retrieved.")
}
if webhook.Description != "go-bb-test-new" {
t.Error("The webhook `description` attribute does not match the expected value.")
}
if webhook.Url != "https://new-example.com" {
t.Error("The webhook `url` attribute does not match the expected value.")
}
if webhook.Active != false {
t.Error("The webhook `active` attribute does not match the expected value.")
}
if len(webhook.Events) != 3 {
t.Error("The webhook `events` attribute does not match the expected value.")
}
})
t.Run("delete", func(t *testing.T) {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Uuid: webhookResourceUuid,
}
_, err := c.Repositories.Webhooks.Delete(opt)
if err != nil {
t.Error(err)
}
})
t.Run("list/gets", func(t *testing.T) {
const expectedNumberOfWebhooks = 20
var webhookUUIDs []string
defer func() {
for _, uuid := range webhookUUIDs {
_, err := c.Repositories.Webhooks.Delete(&bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Uuid: uuid,
})
if err != nil {
t.Errorf("Failed to delete webhook UUID %s: %v", uuid, err)
}
}
}()
for i := 0; i < expectedNumberOfWebhooks; i++ {
opt := &bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
Description: fmt.Sprintf("go-bb-test-%d", i),
Url: fmt.Sprintf("https://example.com/%d", i),
Active: false,
Events: []string{bitbucket.RepoPushEvent, bitbucket.IssueCreatedEvent},
}
webhook, err := c.Repositories.Webhooks.Create(opt)
if err != nil {
t.Errorf("Failed to create webhook %d: %v", i, err)
}
webhookUUIDs = append(webhookUUIDs, webhook.Uuid)
}
// Use a page length of 5 to ensure the auto paging is working
c.Pagelen = 5
getsResponse, err := c.Repositories.Webhooks.Gets(&bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
})
if err != nil {
t.Errorf("Failed to list webhooks: %v", err)
return
}
responseMap, ok := getsResponse.(map[string]interface{})
if !ok {
t.Error(errors.New("response could not be decoded"))
return
}
values := responseMap["values"].([]interface{})
if len(values) != expectedNumberOfWebhooks {
t.Error(fmt.Errorf("Expected %d webhooks but got %d. Response: %v", expectedNumberOfWebhooks, len(values), getsResponse))
return
}
listResponse, err := c.Repositories.Webhooks.List(&bitbucket.WebhooksOptions{
Owner: owner,
RepoSlug: repo,
})
if err != nil {
t.Errorf("Failed to list webhooks: %v", err)
return
}
if len(listResponse) != expectedNumberOfWebhooks {
t.Error(fmt.Errorf("Expected %d webhooks but got %d. Response: %v", expectedNumberOfWebhooks, len(values), listResponse))
return
}
})
}