forked from openshift-pipelines/pipelines-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_test.go
467 lines (446 loc) · 13 KB
/
events_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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package github
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/google/go-github/v42/github"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
ghtesthelper "github.com/openshift-pipelines/pipelines-as-code/pkg/test/github"
"gotest.tools/v3/assert"
"gotest.tools/v3/env"
"gotest.tools/v3/fs"
rtesting "knative.dev/pkg/reconciler/testing"
)
// script kiddies, don't get too excited, this has been randomly generated with random words
const fakePrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC6GorZBeri0eVERMZQDFh5E1RMPjFk9AevaWr27yJse6eiUlos
gY2L2vcZKLOrdvVR+TLLapIMFfg1E1qVr1iTHP3IiSCs1uW6NKDmxEQc9Uf/fG9c
i56tGmTVxLkC94AvlVFmgxtWfHdP3lF2O0EcfRyIi6EIbGkWDqWQVEQG2wIDAQAB
AoGAaKOd6FK0dB5Si6Uj4ERgxosAvfHGMh4n6BAc7YUd1ONeKR2myBl77eQLRaEm
DMXRP+sfDVL5lUQRED62ky1JXlDc0TmdLiO+2YVyXI5Tbej0Q6wGVC25/HedguUX
fw+MdKe8jsOOXVRLrJ2GfpKZ2CmOKGTm/hyrFa10TmeoTxkCQQDa4fvqZYD4vOwZ
CplONnVk+PyQETj+mAyUiBnHEeLpztMImNLVwZbrmMHnBtCNx5We10oCLW+Qndfw
Xi4LgliVAkEA2amSV+TZiUVQmm5j9yzon0rt1FK+cmVWfRS/JAUXyvl+Xh/J+7Gu
QzoEGJNAnzkUIZuwhTfNRWlzURWYA8BVrwJAZFQhfJd6PomaTwAktU0REm9ulTrP
vSNE4PBhoHX6ZOGAqfgi7AgIfYVPm+3rupE5a82TBtx8vvUa/fqtcGkW4QJAaL9t
WPUeJyx/XMJxQzuOe1JA4CQt2LmiBLHeRoRY7ephgQSFXKYmed3KqNT8jWOXp5DY
Q1QWaigUQdpFfNCrqwJBANLgWaJV722PhQXOCmR+INvZ7ksIhJVcq/x1l2BYOLw2
QsncVExbMiPa9Oclo5qLuTosS8qwHm1MJEytp3/SkB8=
-----END RSA PRIVATE KEY-----`
var sampleRepo = &github.Repository{
Owner: &github.User{
Login: github.String("owner"),
},
Name: github.String("reponame"),
DefaultBranch: github.String("defaultbranch"),
HTMLURL: github.String("https://github.com/owner/repo"),
}
var samplePRevent = github.PullRequestEvent{
PullRequest: &github.PullRequest{
Head: &github.PullRequestBranch{
SHA: github.String("sampleHeadsha"),
Ref: github.String("headred"),
},
Base: &github.PullRequestBranch{
SHA: github.String("basesha"),
Ref: github.String("baseref"),
},
User: &github.User{
Login: github.String("user"),
},
},
Repo: sampleRepo,
}
var samplePR = github.PullRequest{
Number: github.Int(54321),
Head: &github.PullRequestBranch{
SHA: github.String("samplePRsha"),
Repo: sampleRepo,
},
}
// TODO: better testing matrix against only public function like we do for bitbucket-cloud
func TestPayLoadFix(t *testing.T) {
b, err := ioutil.ReadFile("testdata/pull_request_with_newlines.json")
assert.NilError(t, err)
ctx, _ := rtesting.SetupFakeContext(t)
fakeclient, mux, _, teardown := ghtesthelper.SetupGH()
defer teardown()
mux.HandleFunc("/repos/repo/owner/commits/SHA", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`)
})
mux.HandleFunc("/repos/repo/owner/git/commits/SHA", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`)
})
gprovider := Provider{
Client: fakeclient,
}
logger := getLogger()
event := &info.Event{
EventType: "pull_request",
TriggerTarget: "pull_request",
}
run := ¶ms.Run{
Clients: clients.Clients{
Log: logger,
},
Info: info.Info{
Event: event,
},
}
_, err = gprovider.ParsePayload(ctx, run, string(b))
assert.NilError(t, err)
// would bomb out on "assertion failed: error is not nil: invalid character
// '\n' in string literal" if we don't fix the payload
assert.NilError(t, err)
}
func TestParsePayLoad(t *testing.T) {
fakeclient, mux, _, teardown := ghtesthelper.SetupGH()
defer teardown()
tests := []struct {
name string
wantErrString string
eventType string
payloadEventStruct interface{}
jeez string
triggerTarget string
githubClient *github.Client
muxReplies map[string]interface{}
shaRet string
}{
{
name: "bad/unknow event",
wantErrString: "unknown X-Github-Event",
eventType: "unknown",
},
{
name: "bad/invalid json",
wantErrString: "invalid character",
eventType: "pull_request",
jeez: "xxxx",
},
{
name: "bad/not supported",
wantErrString: "this event is not supported",
eventType: "pull_request_review_comment",
payloadEventStruct: github.PullRequestReviewCommentEvent{Action: github.String("created")},
},
{
name: "bad/check run only issue recheck supported",
wantErrString: "only issue recheck is supported",
eventType: "check_run",
triggerTarget: "nonopetitrobot",
payloadEventStruct: github.CheckRunEvent{Action: github.String("created")},
githubClient: fakeclient,
},
{
name: "bad/check run only with github apps",
wantErrString: "only supported with github apps",
eventType: "check_run",
payloadEventStruct: github.CheckRunEvent{Action: github.String("created")},
},
{
name: "bad/issue comment retest only with github apps",
wantErrString: "only supported with github apps",
eventType: "issue_comment",
payloadEventStruct: github.IssueCommentEvent{Action: github.String("created")},
},
{
name: "bad/issue comment not coming from pull request",
eventType: "issue_comment",
githubClient: fakeclient,
payloadEventStruct: github.IssueCommentEvent{Issue: &github.Issue{}},
wantErrString: "issue comment is not coming from a pull_request",
},
{
name: "bad/issue comment invalid pullrequest",
eventType: "issue_comment",
githubClient: fakeclient,
payloadEventStruct: github.IssueCommentEvent{Issue: &github.Issue{
PullRequestLinks: &github.PullRequestLinks{
HTMLURL: github.String("/bad"),
},
}},
wantErrString: "bad pull request number",
},
{
name: "bad/rerequest error fetching PR",
githubClient: fakeclient,
eventType: "check_run",
triggerTarget: "issue-recheck",
wantErrString: "404",
payloadEventStruct: github.CheckRunEvent{
Repo: sampleRepo,
CheckRun: &github.CheckRun{
CheckSuite: &github.CheckSuite{
PullRequests: []*github.PullRequest{&samplePR},
},
},
},
shaRet: "samplePRsha",
},
{
name: "good/rerequest on pull request",
eventType: "check_run",
githubClient: fakeclient,
triggerTarget: "issue-recheck",
payloadEventStruct: github.CheckRunEvent{
Repo: sampleRepo,
CheckRun: &github.CheckRun{
CheckSuite: &github.CheckSuite{
PullRequests: []*github.PullRequest{&samplePR},
},
},
},
muxReplies: map[string]interface{}{"/repos/owner/reponame/pulls/54321": samplePR},
shaRet: "samplePRsha",
},
{
name: "good/rerequest on push",
eventType: "check_run",
githubClient: fakeclient,
triggerTarget: "issue-recheck",
payloadEventStruct: github.CheckRunEvent{
Repo: sampleRepo,
CheckRun: &github.CheckRun{
CheckSuite: &github.CheckSuite{
HeadSHA: github.String("headSHACheckSuite"),
},
},
},
shaRet: "headSHACheckSuite",
},
{
name: "good/issue comment",
eventType: "issue_comment",
githubClient: fakeclient,
payloadEventStruct: github.IssueCommentEvent{
Issue: &github.Issue{
PullRequestLinks: &github.PullRequestLinks{
HTMLURL: github.String("/666"),
},
},
Repo: sampleRepo,
},
muxReplies: map[string]interface{}{"/repos/owner/reponame/pulls/666": samplePR},
shaRet: "samplePRsha",
},
{
name: "good/pull request",
eventType: "pull_request",
triggerTarget: "pull_request",
payloadEventStruct: samplePRevent,
shaRet: "sampleHeadsha",
},
{
name: "good/push",
eventType: "push",
triggerTarget: "push",
payloadEventStruct: github.PushEvent{
Repo: &github.PushEventRepository{
Owner: &github.User{Login: github.String("owner")},
Name: github.String("pushRepo"),
},
HeadCommit: &github.HeadCommit{ID: github.String("SHAPush")},
},
shaRet: "SHAPush",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
for key, value := range tt.muxReplies {
mux.HandleFunc(key, func(rw http.ResponseWriter, r *http.Request) {
bjeez, _ := json.Marshal(value)
fmt.Fprint(rw, string(bjeez))
})
}
gprovider := Provider{
Client: tt.githubClient,
}
logger := getLogger()
event := &info.Event{
EventType: tt.eventType,
TriggerTarget: tt.triggerTarget,
}
run := ¶ms.Run{
Clients: clients.Clients{
Log: logger,
},
Info: info.Info{
Event: event,
},
}
bjeez, _ := json.Marshal(tt.payloadEventStruct)
jeez := string(bjeez)
if tt.jeez != "" {
jeez = tt.jeez
}
ret, err := gprovider.ParsePayload(ctx, run, jeez)
if tt.wantErrString != "" {
assert.ErrorContains(t, err, tt.wantErrString)
return
}
assert.NilError(t, err)
assert.Assert(t, ret != nil)
assert.Equal(t, tt.shaRet, ret.SHA)
})
}
}
func TestAppTokenGeneration(t *testing.T) {
fakeGithubAuthURL := "https://fake.gitub.auth/api/v3/"
tests := []struct {
name string
wantErr bool
nilClient bool
envs map[string]string
wsSecretFiles map[string]string
resultBaseURL string
}{
{
name: "Env not set",
envs: map[string]string{
"FOO": "bar",
},
nilClient: true,
},
{
name: "Bad Account ID",
envs: map[string]string{
"PAC_INSTALLATION_ID": "bad",
"PAC_WORKSPACE_SECRET": "xxx",
},
wantErr: true,
},
{
name: "bad/Workspace path doesn't exist",
envs: map[string]string{
"PAC_INSTALLATION_ID": "11111",
"PAC_WORKSPACE_SECRET": "not/here",
},
wantErr: true,
},
{
name: "bad/Application ID in workspace doesn't exist",
envs: map[string]string{
"PAC_INSTALLATION_ID": "11111",
"PAC_WORKSPACE_SECRET": "here",
},
wsSecretFiles: map[string]string{
"nogithub-application-id": "Foo",
},
wantErr: true,
},
{
name: "bad/wrong ApplicationID",
envs: map[string]string{
"PAC_INSTALLATION_ID": "11111",
"PAC_WORKSPACE_SECRET": "here",
},
wsSecretFiles: map[string]string{
"github-application-id": "BAD",
},
wantErr: true,
},
{
name: "bad/Private Key not present",
envs: map[string]string{
"PAC_INSTALLATION_ID": "11111",
"PAC_WORKSPACE_SECRET": "here",
},
wsSecretFiles: map[string]string{
"github-application-id": "2222",
},
wantErr: true,
},
{
name: "Bad/Private Key chelou",
envs: map[string]string{
"PAC_INSTALLATION_ID": "11111",
"PAC_WORKSPACE_SECRET": "here",
"PAC_GIT_PROVIDER_APIURL": "foo.bar.com",
},
wsSecretFiles: map[string]string{
"github-application-id": "2222",
"github-private-key": "hello",
},
wantErr: true,
},
{
name: "Good/ghe base domain",
envs: map[string]string{
"PAC_INSTALLATION_ID": "11111",
"PAC_WORKSPACE_SECRET": "here",
"PAC_GIT_PROVIDER_APIURL": "foo.bar.com",
},
wsSecretFiles: map[string]string{
"github-application-id": "2222",
"github-private-key": fakePrivateKey,
},
resultBaseURL: fakeGithubAuthURL,
},
{
name: "Good/full ghe url",
envs: map[string]string{
"PAC_INSTALLATION_ID": "11111",
"PAC_WORKSPACE_SECRET": "here",
"PAC_GIT_PROVIDER_APIURL": "https://alpha.beta.com",
},
wsSecretFiles: map[string]string{
"github-application-id": "2222",
"github-private-key": fakePrivateKey,
},
resultBaseURL: fakeGithubAuthURL,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if s, ok := tt.envs["PAC_WORKSPACE_SECRET"]; ok && s == "here" {
d := fs.NewDir(t, "workspace-secret", fs.WithFiles(tt.wsSecretFiles))
defer d.Remove()
tt.envs["PAC_WORKSPACE_SECRET"] = d.Path()
}
_, mux, url, teardown := ghtesthelper.SetupGH()
defer teardown()
mux.HandleFunc(fmt.Sprintf("/app/installations/%s/access_tokens", tt.envs["PAC_INSTALLATION_ID"]), func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`)
})
tt.envs["PAC_GIT_PROVIDER_APIURL"] = fakeGithubAuthURL
tt.envs["PAC_GIT_PROVIDER_TOKEN_APIURL"] = url + "/api/v3"
envRemove := env.PatchAll(t, tt.envs)
defer envRemove()
ctx, _ := rtesting.SetupFakeContext(t)
jeez, _ := json.Marshal(samplePRevent)
gprovider := Provider{}
logger := getLogger()
event := &info.Event{
EventType: "pull_request",
TriggerTarget: "pull_request",
}
run := ¶ms.Run{
Clients: clients.Clients{
Log: logger,
},
Info: info.Info{
Event: event,
Pac: &info.PacOpts{},
},
}
_, err := gprovider.ParsePayload(ctx, run, string(jeez))
if tt.wantErr {
assert.Assert(t, err != nil)
return
}
assert.NilError(t, err)
if tt.nilClient {
assert.Assert(t, gprovider.Client == nil)
return
}
assert.Assert(t, gprovider.Client != nil)
assert.Equal(t, gprovider.Client.BaseURL.String(), tt.resultBaseURL)
})
}
}