Skip to content

Commit d60319f

Browse files
authored
Merge pull request xanzy#1256 from venky333/test/events
add tests for events
2 parents 0c36e0e + 19e8996 commit d60319f

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed

events_test.go

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestUsersService_ListUserContributionEvents(t *testing.T) {
12+
mux, server, client := setup(t)
13+
defer teardown(server)
14+
15+
mux.HandleFunc("/api/v4/users/1/events", func(w http.ResponseWriter, r *http.Request) {
16+
testMethod(t, r, http.MethodGet)
17+
fmt.Fprintf(w, `
18+
[
19+
{
20+
"id": 3,
21+
"title": null,
22+
"project_id": 15,
23+
"action_name": "closed",
24+
"target_id": 830,
25+
"target_type": "Issue",
26+
"author_id": 1,
27+
"target_title": "Public project search field",
28+
"author": {
29+
"name": "Venkatesh Thalluri",
30+
"username": "venky333",
31+
"id": 1,
32+
"state": "active",
33+
"avatar_url": "http://localhost:3000/uploads/user/avatar/1/fox_avatar.png",
34+
"web_url": "http://localhost:3000/venky333"
35+
},
36+
"author_username": "venky333"
37+
}
38+
]
39+
`)
40+
})
41+
42+
want := []*ContributionEvent{
43+
{
44+
ID: 3,
45+
Title: "",
46+
ProjectID: 15,
47+
ActionName: "closed",
48+
TargetID: 830,
49+
TargetIID: 0,
50+
TargetType: "Issue",
51+
AuthorID: 1,
52+
TargetTitle: "Public project search field",
53+
Note: nil,
54+
Author: struct {
55+
Name string `json:"name"`
56+
Username string `json:"username"`
57+
ID int `json:"id"`
58+
State string `json:"state"`
59+
AvatarURL string `json:"avatar_url"`
60+
WebURL string `json:"web_url"`
61+
}{
62+
Name: "Venkatesh Thalluri",
63+
Username: "venky333",
64+
ID: 1,
65+
State: "active",
66+
AvatarURL: "http://localhost:3000/uploads/user/avatar/1/fox_avatar.png",
67+
WebURL: "http://localhost:3000/venky333",
68+
},
69+
AuthorUsername: "venky333",
70+
},
71+
}
72+
73+
ces, resp, err := client.Users.ListUserContributionEvents(1, nil, nil)
74+
require.NoError(t, err)
75+
require.NotNil(t, resp)
76+
require.Equal(t, want, ces)
77+
78+
ces, resp, err = client.Users.ListUserContributionEvents(1.01, nil, nil)
79+
require.EqualError(t, err, "invalid ID type 1.01, the ID must be an int or a string")
80+
require.Nil(t, resp)
81+
require.Nil(t, ces)
82+
83+
ces, resp, err = client.Users.ListUserContributionEvents(1, nil, nil, errorOption)
84+
require.EqualError(t, err, "RequestOptionFunc returns an error")
85+
require.Nil(t, resp)
86+
require.Nil(t, ces)
87+
88+
ces, resp, err = client.Users.ListUserContributionEvents(3, nil, nil)
89+
require.Error(t, err)
90+
require.Nil(t, ces)
91+
require.Equal(t, http.StatusNotFound, resp.StatusCode)
92+
}
93+
94+
func TestEventsService_ListCurrentUserContributionEvents(t *testing.T) {
95+
mux, server, client := setup(t)
96+
defer teardown(server)
97+
98+
mux.HandleFunc("/api/v4/events", func(w http.ResponseWriter, r *http.Request) {
99+
testMethod(t, r, http.MethodGet)
100+
fmt.Fprintf(w, `
101+
[
102+
{
103+
"id": 1,
104+
"title":null,
105+
"project_id":1,
106+
"action_name":"opened",
107+
"target_id":160,
108+
"target_type":"Issue",
109+
"author_id":25,
110+
"target_title":"Qui natus eos odio tempore et quaerat consequuntur ducimus cupiditate quis.",
111+
"author":{
112+
"name":"Venkatesh Thalluri",
113+
"username":"venky333",
114+
"id":25,
115+
"state":"active",
116+
"avatar_url":"http://www.gravatar.com/avatar/97d6d9441ff85fdc730e02a6068d267b?s=80u0026d=identicon",
117+
"web_url":"https://gitlab.example.com/venky333"
118+
},
119+
"author_username":"venky333"
120+
}
121+
]
122+
`)
123+
})
124+
125+
want := []*ContributionEvent{
126+
{
127+
ID: 1,
128+
Title: "",
129+
ProjectID: 1,
130+
ActionName: "opened",
131+
TargetID: 160,
132+
TargetIID: 0,
133+
TargetType: "Issue",
134+
AuthorID: 25,
135+
TargetTitle: "Qui natus eos odio tempore et quaerat consequuntur ducimus cupiditate quis.",
136+
Note: nil,
137+
Author: struct {
138+
Name string `json:"name"`
139+
Username string `json:"username"`
140+
ID int `json:"id"`
141+
State string `json:"state"`
142+
AvatarURL string `json:"avatar_url"`
143+
WebURL string `json:"web_url"`
144+
}{
145+
Name: "Venkatesh Thalluri",
146+
Username: "venky333",
147+
ID: 25,
148+
State: "active",
149+
AvatarURL: "http://www.gravatar.com/avatar/97d6d9441ff85fdc730e02a6068d267b?s=80u0026d=identicon",
150+
WebURL: "https://gitlab.example.com/venky333",
151+
},
152+
AuthorUsername: "venky333",
153+
},
154+
}
155+
156+
ces, resp, err := client.Events.ListCurrentUserContributionEvents(nil, nil)
157+
require.NoError(t, err)
158+
require.NotNil(t, resp)
159+
require.Equal(t, want, ces)
160+
161+
ces, resp, err = client.Events.ListCurrentUserContributionEvents(nil, nil, errorOption)
162+
require.EqualError(t, err, "RequestOptionFunc returns an error")
163+
require.Nil(t, resp)
164+
require.Nil(t, ces)
165+
}
166+
167+
func TestEventsService_ListCurrentUserContributionEvents_StatusNotFound(t *testing.T) {
168+
mux, server, client := setup(t)
169+
defer teardown(server)
170+
171+
mux.HandleFunc("/api/v4/events", func(w http.ResponseWriter, r *http.Request) {
172+
testMethod(t, r, http.MethodGet)
173+
w.WriteHeader(http.StatusNotFound)
174+
})
175+
176+
ces, resp, err := client.Events.ListCurrentUserContributionEvents(nil, nil)
177+
require.Error(t, err)
178+
require.Nil(t, ces)
179+
require.Equal(t, http.StatusNotFound, resp.StatusCode)
180+
}
181+
182+
func TestEventsService_ListProjectVisibleEvents(t *testing.T) {
183+
mux, server, client := setup(t)
184+
defer teardown(server)
185+
186+
mux.HandleFunc("/api/v4/projects/15/events", func(w http.ResponseWriter, r *http.Request) {
187+
testMethod(t, r, http.MethodGet)
188+
fmt.Fprintf(w, `
189+
[
190+
{
191+
"id": 3,
192+
"title": null,
193+
"project_id": 15,
194+
"action_name": "closed",
195+
"target_id": 830,
196+
"target_type": "Issue",
197+
"author_id": 1,
198+
"target_title": "Public project search field",
199+
"author": {
200+
"name": "Venkatesh Thalluri",
201+
"username": "venky333",
202+
"id": 1,
203+
"state": "active",
204+
"avatar_url": "http://localhost:3000/uploads/user/avatar/1/fox_avatar.png",
205+
"web_url": "http://localhost:3000/venky333"
206+
},
207+
"author_username": "venky333"
208+
}
209+
]
210+
`)
211+
})
212+
213+
want := []*ContributionEvent{
214+
{
215+
ID: 3,
216+
Title: "",
217+
ProjectID: 15,
218+
ActionName: "closed",
219+
TargetID: 830,
220+
TargetIID: 0,
221+
TargetType: "Issue",
222+
AuthorID: 1,
223+
TargetTitle: "Public project search field",
224+
Note: nil,
225+
Author: struct {
226+
Name string `json:"name"`
227+
Username string `json:"username"`
228+
ID int `json:"id"`
229+
State string `json:"state"`
230+
AvatarURL string `json:"avatar_url"`
231+
WebURL string `json:"web_url"`
232+
}{
233+
Name: "Venkatesh Thalluri",
234+
Username: "venky333",
235+
ID: 1,
236+
State: "active",
237+
AvatarURL: "http://localhost:3000/uploads/user/avatar/1/fox_avatar.png",
238+
WebURL: "http://localhost:3000/venky333",
239+
},
240+
AuthorUsername: "venky333",
241+
},
242+
}
243+
244+
ces, resp, err := client.Events.ListProjectVisibleEvents(15, nil, nil)
245+
require.NoError(t, err)
246+
require.NotNil(t, resp)
247+
require.Equal(t, want, ces)
248+
249+
ces, resp, err = client.Events.ListProjectVisibleEvents(15.01, nil, nil)
250+
require.EqualError(t, err, "invalid ID type 15.01, the ID must be an int or a string")
251+
require.Nil(t, resp)
252+
require.Nil(t, ces)
253+
254+
ces, resp, err = client.Events.ListProjectVisibleEvents(15, nil, nil, errorOption)
255+
require.EqualError(t, err, "RequestOptionFunc returns an error")
256+
require.Nil(t, resp)
257+
require.Nil(t, ces)
258+
259+
ces, resp, err = client.Events.ListProjectVisibleEvents(3, nil, nil)
260+
require.Error(t, err)
261+
require.Nil(t, ces)
262+
require.Equal(t, http.StatusNotFound, resp.StatusCode)
263+
}

0 commit comments

Comments
 (0)