forked from xanzy/go-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_webhook_types_test.go
191 lines (144 loc) · 5.32 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
package gitlab
import (
"encoding/json"
"testing"
)
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 != "John Smith" {
t.Errorf("Username is %s, want %s", event.UserName, "John Smith")
}
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].Author.Name != "Jordi Mallach" {
t.Errorf("Commit Username is %s, want %s", event.UserName, "Jordi Mallach")
}
}
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 != "User1" {
t.Errorf("Assignee.Name is %v, want %v", event.ObjectAttributes.ID, "User1")
}
if event.ObjectAttributes.Assignee.Username != "user1" {
t.Errorf("ObjectAttributes is %v, want %v", event.ObjectAttributes.Assignee.Username, "user1")
}
if event.User.Name == "" {
t.Errorf("Username is %s, want %s", event.User.Name, "Administrator")
}
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")
}
}
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 is %v, want %v", event.ObjectAttributes.ID, 1977)
}
if event.User.Name == "" {
t.Errorf("Username is %s, want %s", event.User.Name, "Administrator")
}
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")
}
}
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)
}
}
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 != "merge_request" {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, "merge_request")
}
if event.User.Username != "root" {
t.Errorf("User.Username is %v, want %v", event.User.Username, "root")
}
if event.Project.Name != "example-project" {
t.Errorf("Project.Name is %v, want %v", event.Project.Name, "example-project")
}
if event.ObjectAttributes.ID != 15917 {
t.Errorf("ObjectAttributes.ID is %v, want %v", event.ObjectAttributes.ID, 15917)
}
if event.ObjectAttributes.Source.Name != "example-project" {
t.Errorf("ObjectAttributes.Source.Name is %v, want %v", event.ObjectAttributes.Source.Name, "example-project")
}
if event.ObjectAttributes.LastCommit.Author.Email != "test.user@mail.com" {
t.Errorf("ObjectAttributes.LastCommit.Author.Email is %v, want %v", event.ObjectAttributes.LastCommit.Author.Email, "test.user@mail.com")
}
if event.Repository.Name != "example-project" {
t.Errorf("Repository.Name is %v, want %v", event.Repository.Name, "example-project")
}
if event.Assignee.Username != "root" {
t.Errorf("Assignee.Username is %v, want %v", event.Assignee, "root")
}
if event.User.Name == "" {
t.Errorf("Username is %s, want %s", event.User.Name, "Administrator")
}
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")
}
}