forked from xanzy/go-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_webhook_types_test.go
517 lines (392 loc) · 15.2 KB
/
event_webhook_types_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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//
// Copyright 2021, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gitlab
import (
"encoding/json"
"testing"
"time"
)
const (
expectedID = 1
expectedName = "User1"
expectedUsername = "user1"
excpectedAvatar = "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
expectedEmail = "[email protected]"
)
func TestBuildEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/build.json")
var event *BuildEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Build Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("Build Event is null")
}
if event.BuildID != 1977 {
t.Errorf("BuildID is %v, want %v", event.BuildID, 1977)
}
if event.User.ID != 42 {
t.Errorf("User ID is %d, want %d", event.User.ID, 42)
}
if event.User.Name != expectedName {
t.Errorf("Username is %s, want %s", event.User.Name, expectedName)
}
if event.BuildCreatedAt != "2021-02-23T02:41:37.886Z" {
t.Errorf("BuildCreatedAt is %s, want %s", event.User.Name, expectedName)
}
}
func TestDeploymentEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/deployment.json")
var event *DeploymentEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Deployment Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("Deployment Event is null")
}
if event.Project.ID != 30 {
t.Errorf("Project.ID is %v, want %v", event.Project.ID, 30)
}
if event.User.ID != 42 {
t.Errorf("User ID is %d, want %d", event.User.ID, 42)
}
if event.User.Name != expectedName {
t.Errorf("Username is %s, want %s", event.User.Name, expectedName)
}
if event.CommitTitle != "Add new file" {
t.Errorf("CommitTitle is %s, want %s", event.CommitTitle, "Add new file")
}
}
func TestIssueCommentEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/note_issue.json")
var event *IssueCommentEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Issue Comment Event can not unmarshaled: %v\n ", err.Error())
}
if event.ObjectKind != string(NoteEventTargetType) {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, NoteEventTargetType)
}
if event.ProjectID != 5 {
t.Errorf("ProjectID is %v, want %v", event.ProjectID, 5)
}
if event.User.ID != 42 {
t.Errorf("User ID is %d, want %d", event.User.ID, 42)
}
if event.ObjectAttributes.NoteableType != "Issue" {
t.Errorf("NoteableType is %v, want %v", event.ObjectAttributes.NoteableType, "Issue")
}
if event.Issue.Title != "test_issue" {
t.Errorf("Issue title is %v, want %v", event.Issue.Title, "test_issue")
}
if len(event.Issue.Labels) == 0 || event.Issue.Labels[0].ID != 25 {
t.Errorf("Label id is null")
}
}
func TestIssueEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/issue.json")
var event *IssueEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Issue Event can not unmarshaled: %v\n ", err.Error())
}
if event.Project.ID != 1 {
t.Errorf("Project.ID is %v, want %v", event.Project.ID, 1)
}
if event.User.ID != 42 {
t.Errorf("User ID is %d, want %d", event.User.ID, 42)
}
if event.Assignee.Username != "user1" {
t.Errorf("Assignee username is %s, want %s", event.Assignee.Username, "user1")
}
if event.Changes.TotalTimeSpent.Previous != 8100 {
t.Errorf("Changes.TotalTimeSpent.Previous is %v , want %v", event.Changes.TotalTimeSpent.Previous, 8100)
}
if event.Changes.TotalTimeSpent.Current != 9900 {
t.Errorf("Changes.TotalTimeSpent.Current is %v , want %v", event.Changes.TotalTimeSpent.Current, 8100)
}
}
func TestMergeEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/merge_request.json")
var event *MergeEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Merge Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("Merge Event is null")
}
if event.ObjectAttributes.ID != 99 {
t.Errorf("ObjectAttributes.ID is %v, want %v", event.ObjectAttributes.ID, 99)
}
if event.ObjectAttributes.Source.Homepage != "http://example.com/awesome_space/awesome_project" {
t.Errorf("ObjectAttributes.Source.Homepage is %v, want %v", event.ObjectAttributes.Source.Homepage, "http://example.com/awesome_space/awesome_project")
}
if event.ObjectAttributes.LastCommit.ID != "da1560886d4f094c3e6c9ef40349f7d38b5d27d7" {
t.Errorf("ObjectAttributes.LastCommit.ID is %v, want %s", event.ObjectAttributes.LastCommit.ID, "da1560886d4f094c3e6c9ef40349f7d38b5d27d7")
}
if event.ObjectAttributes.Assignee.Name != expectedName {
t.Errorf("Assignee.Name is %v, want %v", event.ObjectAttributes.Assignee.Name, expectedName)
}
if event.ObjectAttributes.Assignee.Username != expectedUsername {
t.Errorf("ObjectAttributes is %v, want %v", event.ObjectAttributes.Assignee.Username, expectedUsername)
}
if event.User.ID != 42 {
t.Errorf("User ID is %d, want %d", event.User.ID, 42)
}
if event.User.Name != expectedName {
t.Errorf("Username is %s, want %s", event.User.Name, expectedName)
}
if event.User.Email != "[email protected]" {
t.Errorf("User email is %s, want %s", event.User.Email, "[email protected]")
}
if event.ObjectAttributes.LastCommit.Timestamp == nil {
t.Errorf("Timestamp isn't nil")
}
if name := event.ObjectAttributes.LastCommit.Author.Name; name != "GitLab dev user" {
t.Errorf("Commit Username is %s, want %s", name, "GitLab dev user")
}
if event.ObjectAttributes.BlockingDiscussionsResolved != true {
t.Errorf("BlockingDiscussionsResolved isn't true")
}
if event.Assignees[0].ID != expectedID {
t.Errorf("Assignees[0].ID is %v, want %v", event.Assignees[0].ID, expectedID)
}
if event.Assignees[0].Name != expectedName {
t.Errorf("Assignees[0].Name is %v, want %v", event.Assignees[0].Name, expectedName)
}
if event.Assignees[0].Username != expectedUsername {
t.Errorf("Assignees[0].Username is %v, want %v", event.Assignees[0].Username, expectedName)
}
if event.Assignees[0].AvatarURL != excpectedAvatar {
t.Errorf("Assignees[0].Email is %v, want %v", event.Assignees[0].AvatarURL, excpectedAvatar)
}
if event.Assignees[0].Email != expectedEmail {
t.Errorf("Assignees[0].Email is %v, want %v", event.Assignees[0].Email, expectedEmail)
}
if len(event.Changes.Reviewers.Previous) != 0 {
t.Errorf("Changes.Reviewer.Previeous length is %d, want %d", len(event.Changes.Reviewers.Previous), 0)
}
if len(event.Changes.Reviewers.Current) != 1 {
t.Errorf("Changes.Reviewer.Current length is %d, want %d", len(event.Changes.Reviewers.Current), 1)
}
if event.Changes.Reviewers.Current[0].Username != expectedUsername {
t.Errorf("Changes.Reviewer.Current[0].Username is %v, want %v", event.Changes.Reviewers.Current[0].Username, expectedUsername)
}
}
func TestMergeEventUnmarshalFromGroup(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/group_merge_request.json")
var event *MergeEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Group Merge Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("Group Merge Event is null")
}
if event.ObjectKind != eventObjectKindMergeRequest {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, eventObjectKindMergeRequest)
}
if event.User.Username != expectedUsername {
t.Errorf("User.Username is %v, want %v", event.User.Username, expectedUsername)
}
if event.Project.Name != exampleProjectName {
t.Errorf("Project.Name is %v, want %v", event.Project.Name, exampleProjectName)
}
if event.ObjectAttributes.ID != 15917 {
t.Errorf("ObjectAttributes.ID is %v, want %v", event.ObjectAttributes.ID, 15917)
}
if event.ObjectAttributes.Source.Name != exampleProjectName {
t.Errorf("ObjectAttributes.Source.Name is %v, want %v", event.ObjectAttributes.Source.Name, exampleProjectName)
}
if event.ObjectAttributes.LastCommit.Author.Email != "[email protected]" {
t.Errorf("ObjectAttributes.LastCommit.Author.Email is %v, want %v", event.ObjectAttributes.LastCommit.Author.Email, "[email protected]")
}
if event.Repository.Name != exampleProjectName {
t.Errorf("Repository.Name is %v, want %v", event.Repository.Name, exampleProjectName)
}
if event.Assignee.Username != expectedUsername {
t.Errorf("Assignee.Username is %v, want %v", event.Assignee, expectedUsername)
}
if event.User.Name != expectedName {
t.Errorf("Username is %s, want %s", event.User.Name, expectedName)
}
if event.ObjectAttributes.LastCommit.Timestamp == nil {
t.Errorf("Timestamp isn't nil")
}
if name := event.ObjectAttributes.LastCommit.Author.Name; name != "Test User" {
t.Errorf("Commit Username is %s, want %s", name, "Test User")
}
}
func TestPipelineEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/pipeline.json")
var event *PipelineEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Pipeline Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("Pipeline Event is null")
}
if event.ObjectAttributes.ID != 31 {
t.Errorf("ObjectAttributes.ID is %v, want %v", event.ObjectAttributes.ID, 1977)
}
if event.ObjectAttributes.DetailedStatus != "passed" {
t.Errorf("ObjectAttributes.DetailedStatus is %v, want %v", event.ObjectAttributes.DetailedStatus, "passed")
}
if event.ObjectAttributes.QueuedDuration != 12 {
t.Errorf("ObjectAttributes.QueuedDuration is %v, want %v", event.ObjectAttributes.QueuedDuration, 12)
}
if event.ObjectAttributes.Variables[0].Key != "NESTOR_PROD_ENVIRONMENT" {
t.Errorf("ObjectAttributes.Variables[0].Key is %v, want %v", event.ObjectAttributes.Variables[0].Key, "NESTOR_PROD_ENVIRONMENT")
}
if event.User.ID != 42 {
t.Errorf("User ID is %d, want %d", event.User.ID, 42)
}
if event.User.Name != expectedName {
t.Errorf("Username is %s, want %s", event.User.Name, expectedName)
}
if event.Commit.Timestamp == nil {
t.Errorf("Timestamp isn't nil")
}
if name := event.Commit.Author.Name; name != "User" {
t.Errorf("Commit Username is %s, want %s", name, "User")
}
if len(event.Builds) != 5 {
t.Errorf("Builds length is %d, want %d", len(event.Builds), 5)
}
if event.Builds[0].AllowFailure != true {
t.Errorf("Builds.0.AllowFailure is %v, want %v", event.Builds[0].AllowFailure, true)
}
if event.Builds[0].Environment.Name != "production" {
t.Errorf("Builds.0.Environment.Name is %v, want %v", event.Builds[0].Environment.Name, "production")
}
}
func TestPushEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/push.json")
var event *PushEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Push Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("Push Event is null")
}
if event.ProjectID != 15 {
t.Errorf("ProjectID is %v, want %v", event.ProjectID, 15)
}
if event.UserName != exampleEventUserName {
t.Errorf("Username is %s, want %s", event.UserName, exampleEventUserName)
}
if event.Commits[0] == nil || event.Commits[0].Timestamp == nil {
t.Errorf("Commit Timestamp isn't nil")
}
if event.Commits[0] == nil || event.Commits[0].Message != exampleCommitMessage {
t.Errorf("Commit Message is %s, want %s", event.Commits[0].Message, exampleCommitMessage)
}
if event.Commits[0] == nil || event.Commits[0].Title != exampleCommitTitle {
t.Errorf("Commit Title is %s, want %s", event.Commits[0].Title, exampleCommitTitle)
}
if event.Commits[0] == nil || event.Commits[0].Author.Name != "Jordi Mallach" {
t.Errorf("Commit Username is %s, want %s", event.UserName, "Jordi Mallach")
}
}
func TestReleaseEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/release.json")
var event *ReleaseEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Release Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("Release Event is null")
}
if event.Project.ID != 327622 {
t.Errorf("Project.ID is %v, want %v", event.Project.ID, 327622)
}
if event.Commit.Title != "Merge branch 'example-branch' into 'master'" {
t.Errorf("Commit title is %s, want %s", event.Commit.Title, "Merge branch 'example-branch' into 'master'")
}
if len(event.Assets.Sources) != 4 {
t.Errorf("Asset sources length is %d, want %d", len(event.Assets.Sources), 4)
}
if event.Assets.Sources[0].Format != "zip" {
t.Errorf("First asset source format is %s, want %s", event.Assets.Sources[0].Format, "zip")
}
if len(event.Assets.Links) != 1 {
t.Errorf("Asset links length is %d, want %d", len(event.Assets.Links), 1)
}
if event.Assets.Links[0].Name != "Changelog" {
t.Errorf("First asset link name is %s, want %s", event.Assets.Links[0].Name, "Changelog")
}
if event.Commit.Author.Name != "User" {
t.Errorf("Commit author name is %s, want %s", event.Commit.Author.Name, "User")
}
}
func TestSubGroupEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/subgroup.json")
var event *SubGroupEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("SubGroup Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("SubGroup Event is null")
}
if event.Name != "SubGroup 1" {
t.Errorf("Name is %v, want %v", event.Name, "SubGroup 1")
}
if event.GroupID != 2 {
t.Errorf("GroupID is %v, want %v", event.GroupID, 2)
}
if event.ParentGroupID != 1 {
t.Errorf("ParentGroupID is %v, want %v", event.ParentGroupID, 1)
}
if event.CreatedAt.Format(time.RFC3339) != "2022-01-24T14:23:59Z" {
t.Errorf("CreatedAt is %v, want %v", event.CreatedAt.Format(time.RFC3339), "2022-01-24T14:23:59Z")
}
}
func TestTagEventUnmarshal(t *testing.T) {
jsonObject := loadFixture("testdata/webhooks/tag_push.json")
var event *TagEvent
err := json.Unmarshal(jsonObject, &event)
if err != nil {
t.Errorf("Tag Event can not unmarshaled: %v\n ", err.Error())
}
if event == nil {
t.Errorf("Tag Event is null")
}
if event.ProjectID != 1 {
t.Errorf("ProjectID is %v, want %v", event.ProjectID, 1)
}
if event.UserName != exampleEventUserName {
t.Errorf("Username is %s, want %s", event.UserName, exampleEventUserName)
}
if event.Commits[0] == nil || event.Commits[0].Timestamp == nil {
t.Errorf("Commit Timestamp isn't nil")
}
if event.Commits[0] == nil || event.Commits[0].Message != exampleCommitMessage {
t.Errorf("Commit Message is %s, want %s", event.Commits[0].Message, exampleCommitMessage)
}
if event.Commits[0] == nil || event.Commits[0].Title != exampleCommitTitle {
t.Errorf("Commit Title is %s, want %s", event.Commits[0].Title, exampleCommitTitle)
}
if event.Commits[0] == nil || event.Commits[0].Author.Name != exampleEventUserName {
t.Errorf("Commit Username is %s, want %s", event.UserName, exampleEventUserName)
}
}