1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "testing"
14 "time"
15
16 "github.com/google/go-cmp/cmp"
17 )
18
19 func TestTeamsService_ListDiscussionsByID(t *testing.T) {
20 client, mux, _, teardown := setup()
21 defer teardown()
22
23 mux.HandleFunc("/organizations/1/team/2/discussions", func(w http.ResponseWriter, r *http.Request) {
24 testMethod(t, r, "GET")
25 testFormValues(t, r, values{
26 "direction": "desc",
27 "page": "2",
28 })
29 fmt.Fprintf(w,
30 `[
31 {
32 "author": {
33 "login": "author",
34 "id": 0,
35 "avatar_url": "https://avatars1.githubusercontent.com/u/0?v=4",
36 "gravatar_id": "",
37 "url": "https://api.github.com/users/author",
38 "html_url": "https://github.com/author",
39 "followers_url": "https://api.github.com/users/author/followers",
40 "following_url": "https://api.github.com/users/author/following{/other_user}",
41 "gists_url": "https://api.github.com/users/author/gists{/gist_id}",
42 "starred_url": "https://api.github.com/users/author/starred{/owner}{/repo}",
43 "subscriptions_url": "https://api.github.com/users/author/subscriptions",
44 "organizations_url": "https://api.github.com/users/author/orgs",
45 "repos_url": "https://api.github.com/users/author/repos",
46 "events_url": "https://api.github.com/users/author/events{/privacy}",
47 "received_events_url": "https://api.github.com/users/author/received_events",
48 "type": "User",
49 "site_admin": false
50 },
51 "body": "test",
52 "body_html": "<p>test</p>",
53 "body_version": "version",
54 "comments_count": 1,
55 "comments_url": "https://api.github.com/teams/2/discussions/3/comments",
56 "created_at": "2018-01-01T00:00:00Z",
57 "last_edited_at": null,
58 "html_url": "https://github.com/orgs/1/teams/2/discussions/3",
59 "node_id": "node",
60 "number": 3,
61 "pinned": false,
62 "private": false,
63 "team_url": "https://api.github.com/teams/2",
64 "title": "test",
65 "updated_at": "2018-01-01T00:00:00Z",
66 "url": "https://api.github.com/teams/2/discussions/3"
67 }
68 ]`)
69 })
70 ctx := context.Background()
71 discussions, _, err := client.Teams.ListDiscussionsByID(ctx, 1, 2, &DiscussionListOptions{"desc", ListOptions{Page: 2}})
72 if err != nil {
73 t.Errorf("Teams.ListDiscussionsByID returned error: %v", err)
74 }
75
76 want := []*TeamDiscussion{
77 {
78 Author: &User{
79 Login: String("author"),
80 ID: Int64(0),
81 AvatarURL: String("https://avatars1.githubusercontent.com/u/0?v=4"),
82 GravatarID: String(""),
83 URL: String("https://api.github.com/users/author"),
84 HTMLURL: String("https://github.com/author"),
85 FollowersURL: String("https://api.github.com/users/author/followers"),
86 FollowingURL: String("https://api.github.com/users/author/following{/other_user}"),
87 GistsURL: String("https://api.github.com/users/author/gists{/gist_id}"),
88 StarredURL: String("https://api.github.com/users/author/starred{/owner}{/repo}"),
89 SubscriptionsURL: String("https://api.github.com/users/author/subscriptions"),
90 OrganizationsURL: String("https://api.github.com/users/author/orgs"),
91 ReposURL: String("https://api.github.com/users/author/repos"),
92 EventsURL: String("https://api.github.com/users/author/events{/privacy}"),
93 ReceivedEventsURL: String("https://api.github.com/users/author/received_events"),
94 Type: String("User"),
95 SiteAdmin: Bool(false),
96 },
97 Body: String("test"),
98 BodyHTML: String("<p>test</p>"),
99 BodyVersion: String("version"),
100 CommentsCount: Int(1),
101 CommentsURL: String("https://api.github.com/teams/2/discussions/3/comments"),
102 CreatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)},
103 LastEditedAt: nil,
104 HTMLURL: String("https://github.com/orgs/1/teams/2/discussions/3"),
105 NodeID: String("node"),
106 Number: Int(3),
107 Pinned: Bool(false),
108 Private: Bool(false),
109 TeamURL: String("https://api.github.com/teams/2"),
110 Title: String("test"),
111 UpdatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)},
112 URL: String("https://api.github.com/teams/2/discussions/3"),
113 },
114 }
115 if !cmp.Equal(discussions, want) {
116 t.Errorf("Teams.ListDiscussionsByID returned %+v, want %+v", discussions, want)
117 }
118
119 const methodName = "ListDiscussionsByID"
120 testBadOptions(t, methodName, func() (err error) {
121 _, _, err = client.Teams.ListDiscussionsByID(ctx, -1, -2, nil)
122 return err
123 })
124
125 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
126 got, resp, err := client.Teams.ListDiscussionsByID(ctx, 1, 2, &DiscussionListOptions{"desc", ListOptions{Page: 2}})
127 if got != nil {
128 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
129 }
130 return resp, err
131 })
132 }
133
134 func TestTeamsService_ListDiscussionsBySlug(t *testing.T) {
135 client, mux, _, teardown := setup()
136 defer teardown()
137
138 mux.HandleFunc("/orgs/o/teams/s/discussions", func(w http.ResponseWriter, r *http.Request) {
139 testMethod(t, r, "GET")
140 testFormValues(t, r, values{
141 "direction": "desc",
142 "page": "2",
143 })
144 fmt.Fprintf(w,
145 `[
146 {
147 "author": {
148 "login": "author",
149 "id": 0,
150 "avatar_url": "https://avatars1.githubusercontent.com/u/0?v=4",
151 "gravatar_id": "",
152 "url": "https://api.github.com/users/author",
153 "html_url": "https://github.com/author",
154 "followers_url": "https://api.github.com/users/author/followers",
155 "following_url": "https://api.github.com/users/author/following{/other_user}",
156 "gists_url": "https://api.github.com/users/author/gists{/gist_id}",
157 "starred_url": "https://api.github.com/users/author/starred{/owner}{/repo}",
158 "subscriptions_url": "https://api.github.com/users/author/subscriptions",
159 "organizations_url": "https://api.github.com/users/author/orgs",
160 "repos_url": "https://api.github.com/users/author/repos",
161 "events_url": "https://api.github.com/users/author/events{/privacy}",
162 "received_events_url": "https://api.github.com/users/author/received_events",
163 "type": "User",
164 "site_admin": false
165 },
166 "body": "test",
167 "body_html": "<p>test</p>",
168 "body_version": "version",
169 "comments_count": 1,
170 "comments_url": "https://api.github.com/teams/2/discussions/3/comments",
171 "created_at": "2018-01-01T00:00:00Z",
172 "last_edited_at": null,
173 "html_url": "https://github.com/orgs/1/teams/2/discussions/3",
174 "node_id": "node",
175 "number": 3,
176 "pinned": false,
177 "private": false,
178 "team_url": "https://api.github.com/teams/2",
179 "title": "test",
180 "updated_at": "2018-01-01T00:00:00Z",
181 "url": "https://api.github.com/teams/2/discussions/3"
182 }
183 ]`)
184 })
185 ctx := context.Background()
186 discussions, _, err := client.Teams.ListDiscussionsBySlug(ctx, "o", "s", &DiscussionListOptions{"desc", ListOptions{Page: 2}})
187 if err != nil {
188 t.Errorf("Teams.ListDiscussionsBySlug returned error: %v", err)
189 }
190
191 want := []*TeamDiscussion{
192 {
193 Author: &User{
194 Login: String("author"),
195 ID: Int64(0),
196 AvatarURL: String("https://avatars1.githubusercontent.com/u/0?v=4"),
197 GravatarID: String(""),
198 URL: String("https://api.github.com/users/author"),
199 HTMLURL: String("https://github.com/author"),
200 FollowersURL: String("https://api.github.com/users/author/followers"),
201 FollowingURL: String("https://api.github.com/users/author/following{/other_user}"),
202 GistsURL: String("https://api.github.com/users/author/gists{/gist_id}"),
203 StarredURL: String("https://api.github.com/users/author/starred{/owner}{/repo}"),
204 SubscriptionsURL: String("https://api.github.com/users/author/subscriptions"),
205 OrganizationsURL: String("https://api.github.com/users/author/orgs"),
206 ReposURL: String("https://api.github.com/users/author/repos"),
207 EventsURL: String("https://api.github.com/users/author/events{/privacy}"),
208 ReceivedEventsURL: String("https://api.github.com/users/author/received_events"),
209 Type: String("User"),
210 SiteAdmin: Bool(false),
211 },
212 Body: String("test"),
213 BodyHTML: String("<p>test</p>"),
214 BodyVersion: String("version"),
215 CommentsCount: Int(1),
216 CommentsURL: String("https://api.github.com/teams/2/discussions/3/comments"),
217 CreatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)},
218 LastEditedAt: nil,
219 HTMLURL: String("https://github.com/orgs/1/teams/2/discussions/3"),
220 NodeID: String("node"),
221 Number: Int(3),
222 Pinned: Bool(false),
223 Private: Bool(false),
224 TeamURL: String("https://api.github.com/teams/2"),
225 Title: String("test"),
226 UpdatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)},
227 URL: String("https://api.github.com/teams/2/discussions/3"),
228 },
229 }
230 if !cmp.Equal(discussions, want) {
231 t.Errorf("Teams.ListDiscussionsBySlug returned %+v, want %+v", discussions, want)
232 }
233
234 const methodName = "ListDiscussionsBySlug"
235 testBadOptions(t, methodName, func() (err error) {
236 _, _, err = client.Teams.ListDiscussionsBySlug(ctx, "o\no", "s\ns", nil)
237 return err
238 })
239
240 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
241 got, resp, err := client.Teams.ListDiscussionsBySlug(ctx, "o", "s", &DiscussionListOptions{"desc", ListOptions{Page: 2}})
242 if got != nil {
243 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
244 }
245 return resp, err
246 })
247 }
248
249 func TestTeamsService_GetDiscussionByID(t *testing.T) {
250 client, mux, _, teardown := setup()
251 defer teardown()
252
253 mux.HandleFunc("/organizations/1/team/2/discussions/3", func(w http.ResponseWriter, r *http.Request) {
254 testMethod(t, r, "GET")
255 fmt.Fprint(w, `{"number":3}`)
256 })
257
258 ctx := context.Background()
259 discussion, _, err := client.Teams.GetDiscussionByID(ctx, 1, 2, 3)
260 if err != nil {
261 t.Errorf("Teams.GetDiscussionByID returned error: %v", err)
262 }
263
264 want := &TeamDiscussion{Number: Int(3)}
265 if !cmp.Equal(discussion, want) {
266 t.Errorf("Teams.GetDiscussionByID returned %+v, want %+v", discussion, want)
267 }
268
269 const methodName = "GetDiscussionByID"
270 testBadOptions(t, methodName, func() (err error) {
271 _, _, err = client.Teams.GetDiscussionByID(ctx, -1, -2, -3)
272 return err
273 })
274
275 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
276 got, resp, err := client.Teams.GetDiscussionByID(ctx, 1, 2, 3)
277 if got != nil {
278 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
279 }
280 return resp, err
281 })
282 }
283
284 func TestTeamsService_GetDiscussionBySlug(t *testing.T) {
285 client, mux, _, teardown := setup()
286 defer teardown()
287
288 mux.HandleFunc("/orgs/o/teams/s/discussions/3", func(w http.ResponseWriter, r *http.Request) {
289 testMethod(t, r, "GET")
290 fmt.Fprint(w, `{"number":3}`)
291 })
292
293 ctx := context.Background()
294 discussion, _, err := client.Teams.GetDiscussionBySlug(ctx, "o", "s", 3)
295 if err != nil {
296 t.Errorf("Teams.GetDiscussionBySlug returned error: %v", err)
297 }
298
299 want := &TeamDiscussion{Number: Int(3)}
300 if !cmp.Equal(discussion, want) {
301 t.Errorf("Teams.GetDiscussionBySlug returned %+v, want %+v", discussion, want)
302 }
303
304 const methodName = "GetDiscussionBySlug"
305 testBadOptions(t, methodName, func() (err error) {
306 _, _, err = client.Teams.GetDiscussionBySlug(ctx, "o\no", "s\ns", -3)
307 return err
308 })
309
310 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
311 got, resp, err := client.Teams.GetDiscussionBySlug(ctx, "o", "s", 3)
312 if got != nil {
313 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
314 }
315 return resp, err
316 })
317 }
318
319 func TestTeamsService_CreateDiscussionByID(t *testing.T) {
320 client, mux, _, teardown := setup()
321 defer teardown()
322
323 input := TeamDiscussion{Title: String("c_t"), Body: String("c_b")}
324
325 mux.HandleFunc("/organizations/1/team/2/discussions", func(w http.ResponseWriter, r *http.Request) {
326 v := new(TeamDiscussion)
327 json.NewDecoder(r.Body).Decode(v)
328
329 testMethod(t, r, "POST")
330 if !cmp.Equal(v, &input) {
331 t.Errorf("Request body = %+v, want %+v", v, input)
332 }
333
334 fmt.Fprint(w, `{"number":3}`)
335 })
336
337 ctx := context.Background()
338 comment, _, err := client.Teams.CreateDiscussionByID(ctx, 1, 2, input)
339 if err != nil {
340 t.Errorf("Teams.CreateDiscussionByID returned error: %v", err)
341 }
342
343 want := &TeamDiscussion{Number: Int(3)}
344 if !cmp.Equal(comment, want) {
345 t.Errorf("Teams.CreateDiscussionByID returned %+v, want %+v", comment, want)
346 }
347
348 const methodName = "CreateDiscussionByID"
349 testBadOptions(t, methodName, func() (err error) {
350 _, _, err = client.Teams.CreateDiscussionByID(ctx, -1, -2, input)
351 return err
352 })
353
354 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
355 got, resp, err := client.Teams.CreateDiscussionByID(ctx, 1, 2, input)
356 if got != nil {
357 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
358 }
359 return resp, err
360 })
361 }
362
363 func TestTeamsService_CreateDiscussionBySlug(t *testing.T) {
364 client, mux, _, teardown := setup()
365 defer teardown()
366
367 input := TeamDiscussion{Title: String("c_t"), Body: String("c_b")}
368
369 mux.HandleFunc("/orgs/o/teams/s/discussions", func(w http.ResponseWriter, r *http.Request) {
370 v := new(TeamDiscussion)
371 json.NewDecoder(r.Body).Decode(v)
372
373 testMethod(t, r, "POST")
374 if !cmp.Equal(v, &input) {
375 t.Errorf("Request body = %+v, want %+v", v, input)
376 }
377
378 fmt.Fprint(w, `{"number":3}`)
379 })
380
381 ctx := context.Background()
382 comment, _, err := client.Teams.CreateDiscussionBySlug(ctx, "o", "s", input)
383 if err != nil {
384 t.Errorf("Teams.CreateDiscussionBySlug returned error: %v", err)
385 }
386
387 want := &TeamDiscussion{Number: Int(3)}
388 if !cmp.Equal(comment, want) {
389 t.Errorf("Teams.CreateDiscussionBySlug returned %+v, want %+v", comment, want)
390 }
391
392 const methodName = "CreateDiscussionBySlug"
393 testBadOptions(t, methodName, func() (err error) {
394 _, _, err = client.Teams.CreateDiscussionBySlug(ctx, "o\no", "s\ns", input)
395 return err
396 })
397
398 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
399 got, resp, err := client.Teams.CreateDiscussionBySlug(ctx, "o", "s", input)
400 if got != nil {
401 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
402 }
403 return resp, err
404 })
405 }
406
407 func TestTeamsService_EditDiscussionByID(t *testing.T) {
408 client, mux, _, teardown := setup()
409 defer teardown()
410
411 input := TeamDiscussion{Title: String("e_t"), Body: String("e_b")}
412
413 mux.HandleFunc("/organizations/1/team/2/discussions/3", func(w http.ResponseWriter, r *http.Request) {
414 v := new(TeamDiscussion)
415 json.NewDecoder(r.Body).Decode(v)
416
417 testMethod(t, r, "PATCH")
418 if !cmp.Equal(v, &input) {
419 t.Errorf("Request body = %+v, want %+v", v, input)
420 }
421
422 fmt.Fprint(w, `{"number":3}`)
423 })
424
425 ctx := context.Background()
426 comment, _, err := client.Teams.EditDiscussionByID(ctx, 1, 2, 3, input)
427 if err != nil {
428 t.Errorf("Teams.EditDiscussionByID returned error: %v", err)
429 }
430
431 want := &TeamDiscussion{Number: Int(3)}
432 if !cmp.Equal(comment, want) {
433 t.Errorf("Teams.EditDiscussionByID returned %+v, want %+v", comment, want)
434 }
435
436 const methodName = "EditDiscussionByID"
437 testBadOptions(t, methodName, func() (err error) {
438 _, _, err = client.Teams.EditDiscussionByID(ctx, -1, -2, -3, input)
439 return err
440 })
441
442 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
443 got, resp, err := client.Teams.EditDiscussionByID(ctx, 1, 2, 3, input)
444 if got != nil {
445 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
446 }
447 return resp, err
448 })
449 }
450
451 func TestTeamsService_EditDiscussionBySlug(t *testing.T) {
452 client, mux, _, teardown := setup()
453 defer teardown()
454
455 input := TeamDiscussion{Title: String("e_t"), Body: String("e_b")}
456
457 mux.HandleFunc("/orgs/o/teams/s/discussions/3", func(w http.ResponseWriter, r *http.Request) {
458 v := new(TeamDiscussion)
459 json.NewDecoder(r.Body).Decode(v)
460
461 testMethod(t, r, "PATCH")
462 if !cmp.Equal(v, &input) {
463 t.Errorf("Request body = %+v, want %+v", v, input)
464 }
465
466 fmt.Fprint(w, `{"number":3}`)
467 })
468
469 ctx := context.Background()
470 comment, _, err := client.Teams.EditDiscussionBySlug(ctx, "o", "s", 3, input)
471 if err != nil {
472 t.Errorf("Teams.EditDiscussionBySlug returned error: %v", err)
473 }
474
475 want := &TeamDiscussion{Number: Int(3)}
476 if !cmp.Equal(comment, want) {
477 t.Errorf("Teams.EditDiscussionBySlug returned %+v, want %+v", comment, want)
478 }
479
480 const methodName = "EditDiscussionBySlug"
481 testBadOptions(t, methodName, func() (err error) {
482 _, _, err = client.Teams.EditDiscussionBySlug(ctx, "o\no", "s\ns", -3, input)
483 return err
484 })
485
486 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
487 got, resp, err := client.Teams.EditDiscussionBySlug(ctx, "o", "s", 3, input)
488 if got != nil {
489 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
490 }
491 return resp, err
492 })
493 }
494
495 func TestTeamsService_DeleteDiscussionByID(t *testing.T) {
496 client, mux, _, teardown := setup()
497 defer teardown()
498
499 mux.HandleFunc("/organizations/1/team/2/discussions/3", func(w http.ResponseWriter, r *http.Request) {
500 testMethod(t, r, "DELETE")
501 })
502
503 ctx := context.Background()
504 _, err := client.Teams.DeleteDiscussionByID(ctx, 1, 2, 3)
505 if err != nil {
506 t.Errorf("Teams.DeleteDiscussionByID returned error: %v", err)
507 }
508
509 const methodName = "DeleteDiscussionByID"
510 testBadOptions(t, methodName, func() (err error) {
511 _, err = client.Teams.DeleteDiscussionByID(ctx, -1, -2, -3)
512 return err
513 })
514
515 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
516 return client.Teams.DeleteDiscussionByID(ctx, 1, 2, 3)
517 })
518 }
519
520 func TestTeamsService_DeleteDiscussionBySlug(t *testing.T) {
521 client, mux, _, teardown := setup()
522 defer teardown()
523
524 mux.HandleFunc("/orgs/o/teams/s/discussions/3", func(w http.ResponseWriter, r *http.Request) {
525 testMethod(t, r, "DELETE")
526 })
527
528 ctx := context.Background()
529 _, err := client.Teams.DeleteDiscussionBySlug(ctx, "o", "s", 3)
530 if err != nil {
531 t.Errorf("Teams.DeleteDiscussionBySlug returned error: %v", err)
532 }
533
534 const methodName = "DeleteDiscussionBySlug"
535 testBadOptions(t, methodName, func() (err error) {
536 _, err = client.Teams.DeleteDiscussionBySlug(ctx, "o\no", "s\ns", -3)
537 return err
538 })
539
540 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
541 return client.Teams.DeleteDiscussionBySlug(ctx, "o", "s", 3)
542 })
543 }
544
545 func TestTeamDiscussion_Marshal(t *testing.T) {
546 testJSONMarshal(t, &TeamDiscussion{}, "{}")
547
548 u := &TeamDiscussion{
549 Author: &User{
550 Login: String("author"),
551 ID: Int64(0),
552 URL: String("https://api.github.com/users/author"),
553 AvatarURL: String("https://avatars1.githubusercontent.com/u/0?v=4"),
554 GravatarID: String(""),
555 CreatedAt: &Timestamp{referenceTime},
556 SuspendedAt: &Timestamp{referenceTime},
557 },
558 Body: String("test"),
559 BodyHTML: String("<p>test</p>"),
560 BodyVersion: String("version"),
561 CommentsCount: Int(1),
562 CommentsURL: String("https://api.github.com/teams/2/discussions/3/comments"),
563 CreatedAt: &Timestamp{referenceTime},
564 LastEditedAt: &Timestamp{referenceTime},
565 }
566
567 want := `{
568 "author": {
569 "login": "author",
570 "id": 0,
571 "avatar_url": "https://avatars1.githubusercontent.com/u/0?v=4",
572 "gravatar_id": "",
573 "url": "https://api.github.com/users/author",
574 "created_at": ` + referenceTimeStr + `,
575 "suspended_at": ` + referenceTimeStr + `
576 },
577 "body": "test",
578 "body_html": "<p>test</p>",
579 "body_version": "version",
580 "comments_count": 1,
581 "comments_url": "https://api.github.com/teams/2/discussions/3/comments",
582 "created_at": ` + referenceTimeStr + `,
583 "last_edited_at": ` + referenceTimeStr + `
584 }`
585
586 testJSONMarshal(t, u, want)
587 }
588
View as plain text