1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "testing"
14
15 "github.com/google/go-cmp/cmp"
16 )
17
18 func TestRepositoriesService_ListCollaborators(t *testing.T) {
19 client, mux, _, teardown := setup()
20 defer teardown()
21
22 mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) {
23 testMethod(t, r, "GET")
24 testFormValues(t, r, values{"page": "2"})
25 fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
26 })
27
28 opt := &ListCollaboratorsOptions{
29 ListOptions: ListOptions{Page: 2},
30 }
31 ctx := context.Background()
32 users, _, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt)
33 if err != nil {
34 t.Errorf("Repositories.ListCollaborators returned error: %v", err)
35 }
36
37 want := []*User{{ID: Int64(1)}, {ID: Int64(2)}}
38 if !cmp.Equal(users, want) {
39 t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want)
40 }
41
42 const methodName = "ListCollaborators"
43 testBadOptions(t, methodName, func() (err error) {
44 _, _, err = client.Repositories.ListCollaborators(ctx, "\n", "\n", opt)
45 return err
46 })
47
48 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
49 got, resp, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt)
50 if got != nil {
51 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
52 }
53 return resp, err
54 })
55 }
56
57 func TestRepositoriesService_ListCollaborators_withAffiliation(t *testing.T) {
58 client, mux, _, teardown := setup()
59 defer teardown()
60
61 mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) {
62 testMethod(t, r, "GET")
63 testFormValues(t, r, values{"affiliation": "all", "page": "2"})
64 fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
65 })
66
67 opt := &ListCollaboratorsOptions{
68 ListOptions: ListOptions{Page: 2},
69 Affiliation: "all",
70 }
71 ctx := context.Background()
72 users, _, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt)
73 if err != nil {
74 t.Errorf("Repositories.ListCollaborators returned error: %v", err)
75 }
76
77 want := []*User{{ID: Int64(1)}, {ID: Int64(2)}}
78 if !cmp.Equal(users, want) {
79 t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want)
80 }
81
82 const methodName = "ListCollaborators"
83 testBadOptions(t, methodName, func() (err error) {
84 _, _, err = client.Repositories.ListCollaborators(ctx, "\n", "\n", opt)
85 return err
86 })
87
88 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
89 got, resp, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt)
90 if got != nil {
91 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
92 }
93 return resp, err
94 })
95 }
96
97 func TestRepositoriesService_ListCollaborators_invalidOwner(t *testing.T) {
98 client, _, _, teardown := setup()
99 defer teardown()
100
101 ctx := context.Background()
102 _, _, err := client.Repositories.ListCollaborators(ctx, "%", "%", nil)
103 testURLParseError(t, err)
104 }
105
106 func TestRepositoriesService_IsCollaborator_True(t *testing.T) {
107 client, mux, _, teardown := setup()
108 defer teardown()
109
110 mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
111 testMethod(t, r, "GET")
112 w.WriteHeader(http.StatusNoContent)
113 })
114
115 ctx := context.Background()
116 isCollab, _, err := client.Repositories.IsCollaborator(ctx, "o", "r", "u")
117 if err != nil {
118 t.Errorf("Repositories.IsCollaborator returned error: %v", err)
119 }
120
121 if !isCollab {
122 t.Errorf("Repositories.IsCollaborator returned false, want true")
123 }
124
125 const methodName = "IsCollaborator"
126 testBadOptions(t, methodName, func() (err error) {
127 _, _, err = client.Repositories.IsCollaborator(ctx, "\n", "\n", "\n")
128 return err
129 })
130
131 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
132 got, resp, err := client.Repositories.IsCollaborator(ctx, "o", "r", "u")
133 if got {
134 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
135 }
136 return resp, err
137 })
138 }
139
140 func TestRepositoriesService_IsCollaborator_False(t *testing.T) {
141 client, mux, _, teardown := setup()
142 defer teardown()
143
144 mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
145 testMethod(t, r, "GET")
146 w.WriteHeader(http.StatusNotFound)
147 })
148
149 ctx := context.Background()
150 isCollab, _, err := client.Repositories.IsCollaborator(ctx, "o", "r", "u")
151 if err != nil {
152 t.Errorf("Repositories.IsCollaborator returned error: %v", err)
153 }
154
155 if isCollab {
156 t.Errorf("Repositories.IsCollaborator returned true, want false")
157 }
158
159 const methodName = "IsCollaborator"
160 testBadOptions(t, methodName, func() (err error) {
161 _, _, err = client.Repositories.IsCollaborator(ctx, "\n", "\n", "\n")
162 return err
163 })
164
165 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
166 got, resp, err := client.Repositories.IsCollaborator(ctx, "o", "r", "u")
167 if got {
168 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
169 }
170 return resp, err
171 })
172 }
173
174 func TestRepositoriesService_IsCollaborator_invalidUser(t *testing.T) {
175 client, _, _, teardown := setup()
176 defer teardown()
177
178 ctx := context.Background()
179 _, _, err := client.Repositories.IsCollaborator(ctx, "%", "%", "%")
180 testURLParseError(t, err)
181 }
182
183 func TestRepositoryService_GetPermissionLevel(t *testing.T) {
184 client, mux, _, teardown := setup()
185 defer teardown()
186
187 mux.HandleFunc("/repos/o/r/collaborators/u/permission", func(w http.ResponseWriter, r *http.Request) {
188 testMethod(t, r, "GET")
189 fmt.Fprintf(w, `{"permission":"admin","user":{"login":"u"}}`)
190 })
191
192 ctx := context.Background()
193 rpl, _, err := client.Repositories.GetPermissionLevel(ctx, "o", "r", "u")
194 if err != nil {
195 t.Errorf("Repositories.GetPermissionLevel returned error: %v", err)
196 }
197
198 want := &RepositoryPermissionLevel{
199 Permission: String("admin"),
200 User: &User{
201 Login: String("u"),
202 },
203 }
204
205 if !cmp.Equal(rpl, want) {
206 t.Errorf("Repositories.GetPermissionLevel returned %+v, want %+v", rpl, want)
207 }
208
209 const methodName = "GetPermissionLevel"
210 testBadOptions(t, methodName, func() (err error) {
211 _, _, err = client.Repositories.GetPermissionLevel(ctx, "\n", "\n", "\n")
212 return err
213 })
214
215 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
216 got, resp, err := client.Repositories.GetPermissionLevel(ctx, "o", "r", "u")
217 if got != nil {
218 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
219 }
220 return resp, err
221 })
222 }
223
224 func TestRepositoriesService_AddCollaborator(t *testing.T) {
225 client, mux, _, teardown := setup()
226 defer teardown()
227
228 opt := &RepositoryAddCollaboratorOptions{Permission: "admin"}
229 mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
230 v := new(RepositoryAddCollaboratorOptions)
231 json.NewDecoder(r.Body).Decode(v)
232 testMethod(t, r, "PUT")
233 if !cmp.Equal(v, opt) {
234 t.Errorf("Request body = %+v, want %+v", v, opt)
235 }
236 w.WriteHeader(http.StatusOK)
237 w.Write([]byte(`{"permissions": "write","url": "https://api.github.com/user/repository_invitations/1296269","html_url": "https://github.com/octocat/Hello-World/invitations","id":1,"permissions":"write","repository":{"url":"s","name":"r","id":1},"invitee":{"login":"u"},"inviter":{"login":"o"}}`))
238 })
239 ctx := context.Background()
240 collaboratorInvitation, _, err := client.Repositories.AddCollaborator(ctx, "o", "r", "u", opt)
241 if err != nil {
242 t.Errorf("Repositories.AddCollaborator returned error: %v", err)
243 }
244 want := &CollaboratorInvitation{
245 ID: Int64(1),
246 Repo: &Repository{
247 ID: Int64(1),
248 URL: String("s"),
249 Name: String("r"),
250 },
251 Invitee: &User{
252 Login: String("u"),
253 },
254 Inviter: &User{
255 Login: String("o"),
256 },
257 Permissions: String("write"),
258 URL: String("https://api.github.com/user/repository_invitations/1296269"),
259 HTMLURL: String("https://github.com/octocat/Hello-World/invitations"),
260 }
261
262 if !cmp.Equal(collaboratorInvitation, want) {
263 t.Errorf("AddCollaborator returned %+v, want %+v", collaboratorInvitation, want)
264 }
265
266 const methodName = "AddCollaborator"
267 testBadOptions(t, methodName, func() (err error) {
268 _, _, err = client.Repositories.AddCollaborator(ctx, "\n", "\n", "\n", opt)
269 return err
270 })
271
272 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
273 got, resp, err := client.Repositories.AddCollaborator(ctx, "o", "r", "u", opt)
274 if got != nil {
275 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
276 }
277 return resp, err
278 })
279 }
280
281 func TestRepositoriesService_AddCollaborator_invalidUser(t *testing.T) {
282 client, _, _, teardown := setup()
283 defer teardown()
284
285 ctx := context.Background()
286 _, _, err := client.Repositories.AddCollaborator(ctx, "%", "%", "%", nil)
287 testURLParseError(t, err)
288 }
289
290 func TestRepositoriesService_RemoveCollaborator(t *testing.T) {
291 client, mux, _, teardown := setup()
292 defer teardown()
293
294 mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
295 testMethod(t, r, "DELETE")
296 w.WriteHeader(http.StatusNoContent)
297 })
298
299 ctx := context.Background()
300 _, err := client.Repositories.RemoveCollaborator(ctx, "o", "r", "u")
301 if err != nil {
302 t.Errorf("Repositories.RemoveCollaborator returned error: %v", err)
303 }
304
305 const methodName = "RemoveCollaborator"
306 testBadOptions(t, methodName, func() (err error) {
307 _, err = client.Repositories.RemoveCollaborator(ctx, "\n", "\n", "\n")
308 return err
309 })
310
311 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
312 return client.Repositories.RemoveCollaborator(ctx, "o", "r", "u")
313 })
314 }
315
316 func TestRepositoriesService_RemoveCollaborator_invalidUser(t *testing.T) {
317 client, _, _, teardown := setup()
318 defer teardown()
319
320 ctx := context.Background()
321 _, err := client.Repositories.RemoveCollaborator(ctx, "%", "%", "%")
322 testURLParseError(t, err)
323 }
324
325 func TestRepositoryAddCollaboratorOptions_Marshal(t *testing.T) {
326 testJSONMarshal(t, &RepositoryAddCollaboratorOptions{}, "{}")
327
328 r := &RepositoryAddCollaboratorOptions{
329 Permission: "permission",
330 }
331
332 want := `{
333 "permission": "permission"
334 }`
335
336 testJSONMarshal(t, r, want)
337 }
338
339 func TestRepositoryPermissionLevel_Marshal(t *testing.T) {
340 testJSONMarshal(t, &RepositoryPermissionLevel{}, "{}")
341
342 r := &RepositoryPermissionLevel{
343 Permission: String("permission"),
344 User: &User{
345 Login: String("l"),
346 ID: Int64(1),
347 URL: String("u"),
348 AvatarURL: String("a"),
349 GravatarID: String("g"),
350 Name: String("n"),
351 Company: String("c"),
352 Blog: String("b"),
353 Location: String("l"),
354 Email: String("e"),
355 Hireable: Bool(true),
356 Bio: String("b"),
357 TwitterUsername: String("t"),
358 PublicRepos: Int(1),
359 Followers: Int(1),
360 Following: Int(1),
361 CreatedAt: &Timestamp{referenceTime},
362 SuspendedAt: &Timestamp{referenceTime},
363 },
364 }
365
366 want := `{
367 "permission": "permission",
368 "user": {
369 "login": "l",
370 "id": 1,
371 "avatar_url": "a",
372 "gravatar_id": "g",
373 "name": "n",
374 "company": "c",
375 "blog": "b",
376 "location": "l",
377 "email": "e",
378 "hireable": true,
379 "bio": "b",
380 "twitter_username": "t",
381 "public_repos": 1,
382 "followers": 1,
383 "following": 1,
384 "created_at": ` + referenceTimeStr + `,
385 "suspended_at": ` + referenceTimeStr + `,
386 "url": "u"
387 }
388 }`
389
390 testJSONMarshal(t, r, want)
391 }
392
393 func TestCollaboratorInvitation_Marshal(t *testing.T) {
394 testJSONMarshal(t, &CollaboratorInvitation{}, "{}")
395
396 r := &CollaboratorInvitation{
397 ID: Int64(1),
398 Repo: &Repository{
399
400 ID: Int64(1),
401 URL: String("url"),
402 Name: String("n"),
403 },
404 Invitee: &User{
405 Login: String("l"),
406 ID: Int64(1),
407 URL: String("u"),
408 AvatarURL: String("a"),
409 GravatarID: String("g"),
410 Name: String("n"),
411 Company: String("c"),
412 Blog: String("b"),
413 Location: String("l"),
414 Email: String("e"),
415 Hireable: Bool(true),
416 Bio: String("b"),
417 TwitterUsername: String("t"),
418 PublicRepos: Int(1),
419 Followers: Int(1),
420 Following: Int(1),
421 CreatedAt: &Timestamp{referenceTime},
422 SuspendedAt: &Timestamp{referenceTime},
423 },
424 Inviter: &User{
425 Login: String("l"),
426 ID: Int64(1),
427 URL: String("u"),
428 AvatarURL: String("a"),
429 GravatarID: String("g"),
430 Name: String("n"),
431 Company: String("c"),
432 Blog: String("b"),
433 Location: String("l"),
434 Email: String("e"),
435 Hireable: Bool(true),
436 Bio: String("b"),
437 TwitterUsername: String("t"),
438 PublicRepos: Int(1),
439 Followers: Int(1),
440 Following: Int(1),
441 CreatedAt: &Timestamp{referenceTime},
442 SuspendedAt: &Timestamp{referenceTime},
443 },
444 Permissions: String("per"),
445 CreatedAt: &Timestamp{referenceTime},
446 URL: String("url"),
447 HTMLURL: String("hurl"),
448 }
449
450 want := `{
451 "id": 1,
452 "repository": {
453 "id": 1,
454 "name": "n",
455 "url": "url"
456 },
457 "invitee": {
458 "login": "l",
459 "id": 1,
460 "avatar_url": "a",
461 "gravatar_id": "g",
462 "name": "n",
463 "company": "c",
464 "blog": "b",
465 "location": "l",
466 "email": "e",
467 "hireable": true,
468 "bio": "b",
469 "twitter_username": "t",
470 "public_repos": 1,
471 "followers": 1,
472 "following": 1,
473 "created_at": ` + referenceTimeStr + `,
474 "suspended_at": ` + referenceTimeStr + `,
475 "url": "u"
476 },
477 "inviter": {
478 "login": "l",
479 "id": 1,
480 "avatar_url": "a",
481 "gravatar_id": "g",
482 "name": "n",
483 "company": "c",
484 "blog": "b",
485 "location": "l",
486 "email": "e",
487 "hireable": true,
488 "bio": "b",
489 "twitter_username": "t",
490 "public_repos": 1,
491 "followers": 1,
492 "following": 1,
493 "created_at": ` + referenceTimeStr + `,
494 "suspended_at": ` + referenceTimeStr + `,
495 "url": "u"
496 },
497 "permissions": "per",
498 "created_at": ` + referenceTimeStr + `,
499 "url": "url",
500 "html_url": "hurl"
501 }`
502
503 testJSONMarshal(t, r, want)
504 }
505
View as plain text