forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages_test.go
342 lines (323 loc) · 8.06 KB
/
messages_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
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
// Copyright 2016 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"reflect"
"strings"
"testing"
)
func TestValidatePayload(t *testing.T) {
const defaultBody = `{"yo":true}` // All tests below use the default request body and signature.
const defaultSignature = "sha1=126f2c800419c60137ce748d7672e77b65cf16d6"
secretKey := []byte("0123456789abcdef")
tests := []struct {
signature string
eventID string
event string
wantEventID string
wantEvent string
wantPayload string
}{
// The following tests generate expected errors:
{}, // Missing signature
{signature: "yo"}, // Missing signature prefix
{signature: "sha1=yo"}, // Signature not hex string
{signature: "sha1=012345"}, // Invalid signature
// The following tests expect err=nil:
{
signature: defaultSignature,
eventID: "dead-beef",
event: "ping",
wantEventID: "dead-beef",
wantEvent: "ping",
wantPayload: defaultBody,
},
{
signature: defaultSignature,
event: "ping",
wantEvent: "ping",
wantPayload: defaultBody,
},
{
signature: "sha256=b1f8020f5b4cd42042f807dd939015c4a418bc1ff7f604dd55b0a19b5d953d9b",
event: "ping",
wantEvent: "ping",
wantPayload: defaultBody,
},
{
signature: "sha512=8456767023c1195682e182a23b3f5d19150ecea598fde8cb85918f7281b16079471b1329f92b912c4d8bd7455cb159777db8f29608b20c7c87323ba65ae62e1f",
event: "ping",
wantEvent: "ping",
wantPayload: defaultBody,
},
}
for _, test := range tests {
buf := bytes.NewBufferString(defaultBody)
req, err := http.NewRequest("GET", "http://localhost/event", buf)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
if test.signature != "" {
req.Header.Set(signatureHeader, test.signature)
}
req.Header.Set("Content-Type", "application/json")
got, err := ValidatePayload(req, secretKey)
if err != nil {
if test.wantPayload != "" {
t.Errorf("ValidatePayload(%#v): err = %v, want nil", test, err)
}
continue
}
if string(got) != test.wantPayload {
t.Errorf("ValidatePayload = %q, want %q", got, test.wantPayload)
}
}
}
func TestValidatePayload_FormGet(t *testing.T) {
payload := `{"yo":true}`
signature := "sha1=3374ef144403e8035423b23b02e2c9d7a4c50368"
secretKey := []byte("0123456789abcdef")
form := url.Values{}
form.Add("payload", payload)
req, err := http.NewRequest("POST", "http://localhost/event", strings.NewReader(form.Encode()))
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
req.PostForm = form
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set(signatureHeader, signature)
got, err := ValidatePayload(req, secretKey)
if err != nil {
t.Errorf("ValidatePayload(%#v): err = %v, want nil", payload, err)
}
if string(got) != payload {
t.Errorf("ValidatePayload = %q, want %q", got, payload)
}
// check that if payload is invalid we get error
req.Header.Set(signatureHeader, "invalid signature")
if _, err = ValidatePayload(req, nil); err == nil {
t.Error("ValidatePayload = nil, want err")
}
}
func TestValidatePayload_FormPost(t *testing.T) {
payload := `{"yo":true}`
signature := "sha1=3374ef144403e8035423b23b02e2c9d7a4c50368"
secretKey := []byte("0123456789abcdef")
form := url.Values{}
form.Set("payload", payload)
buf := bytes.NewBufferString(form.Encode())
req, err := http.NewRequest("POST", "http://localhost/event", buf)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set(signatureHeader, signature)
got, err := ValidatePayload(req, secretKey)
if err != nil {
t.Errorf("ValidatePayload(%#v): err = %v, want nil", payload, err)
}
if string(got) != payload {
t.Errorf("ValidatePayload = %q, want %q", got, payload)
}
// check that if payload is invalid we get error
req.Header.Set(signatureHeader, "invalid signature")
if _, err = ValidatePayload(req, nil); err == nil {
t.Error("ValidatePayload = nil, want err")
}
}
func TestValidatePayload_InvalidContentType(t *testing.T) {
req, err := http.NewRequest("POST", "http://localhost/event", nil)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
req.Header.Set("Content-Type", "invalid content type")
if _, err = ValidatePayload(req, nil); err == nil {
t.Error("ValidatePayload = nil, want err")
}
}
func TestParseWebHook(t *testing.T) {
tests := []struct {
payload interface{}
messageType string
}{
{
payload: &CheckRunEvent{},
messageType: "check_run",
},
{
payload: &CheckSuiteEvent{},
messageType: "check_suite",
},
{
payload: &CommitCommentEvent{},
messageType: "commit_comment",
},
{
payload: &CreateEvent{},
messageType: "create",
},
{
payload: &DeleteEvent{},
messageType: "delete",
},
{
payload: &DeploymentEvent{},
messageType: "deployment",
},
{
payload: &DeploymentStatusEvent{},
messageType: "deployment_status",
},
{
payload: &ForkEvent{},
messageType: "fork",
},
{
payload: &GollumEvent{},
messageType: "gollum",
},
{
payload: &InstallationEvent{},
messageType: "installation",
},
{
payload: &InstallationRepositoriesEvent{},
messageType: "installation_repositories",
},
{
payload: &IssueCommentEvent{},
messageType: "issue_comment",
},
{
payload: &IssuesEvent{},
messageType: "issues",
},
{
payload: &LabelEvent{},
messageType: "label",
},
{
payload: &MarketplacePurchaseEvent{},
messageType: "marketplace_purchase",
},
{
payload: &MemberEvent{},
messageType: "member",
},
{
payload: &MembershipEvent{},
messageType: "membership",
},
{
payload: &MilestoneEvent{},
messageType: "milestone",
},
{
payload: &OrganizationEvent{},
messageType: "organization",
},
{
payload: &OrgBlockEvent{},
messageType: "org_block",
},
{
payload: &PageBuildEvent{},
messageType: "page_build",
},
{
payload: &PingEvent{},
messageType: "ping",
},
{
payload: &ProjectEvent{},
messageType: "project",
},
{
payload: &ProjectCardEvent{},
messageType: "project_card",
},
{
payload: &ProjectColumnEvent{},
messageType: "project_column",
},
{
payload: &PublicEvent{},
messageType: "public",
},
{
payload: &PullRequestEvent{},
messageType: "pull_request",
},
{
payload: &PullRequestReviewEvent{},
messageType: "pull_request_review",
},
{
payload: &PullRequestReviewCommentEvent{},
messageType: "pull_request_review_comment",
},
{
payload: &PushEvent{},
messageType: "push",
},
{
payload: &ReleaseEvent{},
messageType: "release",
},
{
payload: &RepositoryEvent{},
messageType: "repository",
},
{
payload: &RepositoryVulnerabilityAlertEvent{},
messageType: "repository_vulnerability_alert",
},
{
payload: &StatusEvent{},
messageType: "status",
},
{
payload: &TeamEvent{},
messageType: "team",
},
{
payload: &TeamAddEvent{},
messageType: "team_add",
},
{
payload: &WatchEvent{},
messageType: "watch",
},
}
for _, test := range tests {
p, err := json.Marshal(test.payload)
if err != nil {
t.Fatalf("Marshal(%#v): %v", test.payload, err)
}
got, err := ParseWebHook(test.messageType, p)
if err != nil {
t.Fatalf("ParseWebHook: %v", err)
}
if want := test.payload; !reflect.DeepEqual(got, want) {
t.Errorf("ParseWebHook(%#v, %#v) = %#v, want %#v", test.messageType, p, got, want)
}
}
}
func TestDeliveryID(t *testing.T) {
id := "8970a780-244e-11e7-91ca-da3aabcb9793"
req, err := http.NewRequest("POST", "http://localhost", nil)
if err != nil {
t.Fatalf("DeliveryID: %v", err)
}
req.Header.Set("X-Github-Delivery", id)
got := DeliveryID(req)
if got != id {
t.Errorf("DeliveryID(%#v) = %q, want %q", req, got, id)
}
}