github.com/google/go-github/v49@v49.1.0/github/teams_discussion_comments_test.go (about) 1 // Copyright 2018 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 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 // "Team Discussion Comments" endpoint, when using a teamID. 20 func tdcEndpointByID(orgID, teamID, discussionNumber, commentNumber string) string { 21 out := fmt.Sprintf("/organizations/%v/team/%v/discussions/%v/comments", orgID, teamID, discussionNumber) 22 if commentNumber != "" { 23 return fmt.Sprintf("%v/%v", out, commentNumber) 24 } 25 return out 26 } 27 28 // "Team Discussion Comments" endpoint, when using a team slug. 29 func tdcEndpointBySlug(org, slug, dicsuccionsNumber, commentNumber string) string { 30 out := fmt.Sprintf("/orgs/%v/teams/%v/discussions/%v/comments", org, slug, dicsuccionsNumber) 31 if commentNumber != "" { 32 return fmt.Sprintf("%v/%v", out, commentNumber) 33 } 34 return out 35 } 36 37 func TestTeamsService_ListComments(t *testing.T) { 38 client, mux, _, teardown := setup() 39 defer teardown() 40 41 handleFunc := func(w http.ResponseWriter, r *http.Request) { 42 testMethod(t, r, "GET") 43 testFormValues(t, r, values{ 44 "direction": "desc", 45 }) 46 fmt.Fprintf(w, 47 `[ 48 { 49 "author": { 50 "login": "author", 51 "id": 0, 52 "avatar_url": "https://avatars1.githubusercontent.com/u/0?v=4", 53 "gravatar_id": "", 54 "url": "https://api.github.com/users/author", 55 "html_url": "https://github.com/author", 56 "followers_url": "https://api.github.com/users/author/followers", 57 "following_url": "https://api.github.com/users/author/following{/other_user}", 58 "gists_url": "https://api.github.com/users/author/gists{/gist_id}", 59 "starred_url": "https://api.github.com/users/author/starred{/owner}{/repo}", 60 "subscriptions_url": "https://api.github.com/users/author/subscriptions", 61 "organizations_url": "https://api.github.com/users/author/orgs", 62 "repos_url": "https://api.github.com/users/author/repos", 63 "events_url": "https://api.github.com/users/author/events{/privacy}", 64 "received_events_url": "https://api.github.com/users/author/received_events", 65 "type": "User", 66 "site_admin": false 67 }, 68 "body": "comment", 69 "body_html": "<p>comment</p>", 70 "body_version": "version", 71 "created_at": "2018-01-01T00:00:00Z", 72 "last_edited_at": null, 73 "discussion_url": "https://api.github.com/teams/2/discussions/3", 74 "html_url": "https://github.com/orgs/1/teams/2/discussions/3/comments/4", 75 "node_id": "node", 76 "number": 4, 77 "updated_at": "2018-01-01T00:00:00Z", 78 "url": "https://api.github.com/teams/2/discussions/3/comments/4" 79 } 80 ]`) 81 } 82 83 want := []*DiscussionComment{ 84 { 85 Author: &User{ 86 Login: String("author"), 87 ID: Int64(0), 88 AvatarURL: String("https://avatars1.githubusercontent.com/u/0?v=4"), 89 GravatarID: String(""), 90 URL: String("https://api.github.com/users/author"), 91 HTMLURL: String("https://github.com/author"), 92 FollowersURL: String("https://api.github.com/users/author/followers"), 93 FollowingURL: String("https://api.github.com/users/author/following{/other_user}"), 94 GistsURL: String("https://api.github.com/users/author/gists{/gist_id}"), 95 StarredURL: String("https://api.github.com/users/author/starred{/owner}{/repo}"), 96 SubscriptionsURL: String("https://api.github.com/users/author/subscriptions"), 97 OrganizationsURL: String("https://api.github.com/users/author/orgs"), 98 ReposURL: String("https://api.github.com/users/author/repos"), 99 EventsURL: String("https://api.github.com/users/author/events{/privacy}"), 100 ReceivedEventsURL: String("https://api.github.com/users/author/received_events"), 101 Type: String("User"), 102 SiteAdmin: Bool(false), 103 }, 104 Body: String("comment"), 105 BodyHTML: String("<p>comment</p>"), 106 BodyVersion: String("version"), 107 CreatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}, 108 LastEditedAt: nil, 109 DiscussionURL: String("https://api.github.com/teams/2/discussions/3"), 110 HTMLURL: String("https://github.com/orgs/1/teams/2/discussions/3/comments/4"), 111 NodeID: String("node"), 112 Number: Int(4), 113 UpdatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}, 114 URL: String("https://api.github.com/teams/2/discussions/3/comments/4"), 115 }, 116 } 117 118 e := tdcEndpointByID("1", "2", "3", "") 119 mux.HandleFunc(e, handleFunc) 120 121 ctx := context.Background() 122 commentsByID, _, err := client.Teams.ListCommentsByID(ctx, 1, 2, 3, 123 &DiscussionCommentListOptions{Direction: "desc"}) 124 if err != nil { 125 t.Errorf("Teams.ListCommentsByID returned error: %v", err) 126 } 127 128 if !cmp.Equal(commentsByID, want) { 129 t.Errorf("Teams.ListCommentsByID returned %+v, want %+v", commentsByID, want) 130 } 131 132 e = tdcEndpointBySlug("a", "b", "3", "") 133 mux.HandleFunc(e, handleFunc) 134 135 commentsBySlug, _, err := client.Teams.ListCommentsBySlug(ctx, "a", "b", 3, 136 &DiscussionCommentListOptions{Direction: "desc"}) 137 if err != nil { 138 t.Errorf("Teams.ListCommentsBySlug returned error: %v", err) 139 } 140 141 if !cmp.Equal(commentsBySlug, want) { 142 t.Errorf("Teams.ListCommentsBySlug returned %+v, want %+v", commentsBySlug, want) 143 } 144 145 methodName := "ListCommentsByID" 146 testBadOptions(t, methodName, func() (err error) { 147 _, _, err = client.Teams.ListCommentsByID(ctx, -1, -2, -3, 148 &DiscussionCommentListOptions{Direction: "desc"}) 149 return err 150 }) 151 152 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 153 got, resp, err := client.Teams.ListCommentsByID(ctx, 1, 2, 3, 154 &DiscussionCommentListOptions{Direction: "desc"}) 155 if got != nil { 156 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 157 } 158 return resp, err 159 }) 160 161 methodName = "ListCommentsBySlug" 162 testBadOptions(t, methodName, func() (err error) { 163 _, _, err = client.Teams.ListCommentsBySlug(ctx, "a\na", "b\nb", -3, 164 &DiscussionCommentListOptions{Direction: "desc"}) 165 return err 166 }) 167 168 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 169 got, resp, err := client.Teams.ListCommentsBySlug(ctx, "a", "b", 3, 170 &DiscussionCommentListOptions{Direction: "desc"}) 171 if got != nil { 172 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 173 } 174 return resp, err 175 }) 176 } 177 178 func TestTeamsService_GetComment(t *testing.T) { 179 client, mux, _, teardown := setup() 180 defer teardown() 181 182 handlerFunc := func(w http.ResponseWriter, r *http.Request) { 183 testMethod(t, r, "GET") 184 fmt.Fprint(w, `{"number":4}`) 185 } 186 want := &DiscussionComment{Number: Int(4)} 187 188 e := tdcEndpointByID("1", "2", "3", "4") 189 mux.HandleFunc(e, handlerFunc) 190 191 ctx := context.Background() 192 commentByID, _, err := client.Teams.GetCommentByID(ctx, 1, 2, 3, 4) 193 if err != nil { 194 t.Errorf("Teams.GetCommentByID returned error: %v", err) 195 } 196 197 if !cmp.Equal(commentByID, want) { 198 t.Errorf("Teams.GetCommentByID returned %+v, want %+v", commentByID, want) 199 } 200 201 e = tdcEndpointBySlug("a", "b", "3", "4") 202 mux.HandleFunc(e, handlerFunc) 203 204 commentBySlug, _, err := client.Teams.GetCommentBySlug(ctx, "a", "b", 3, 4) 205 if err != nil { 206 t.Errorf("Teams.GetCommentBySlug returned error: %v", err) 207 } 208 209 if !cmp.Equal(commentBySlug, want) { 210 t.Errorf("Teams.GetCommentBySlug returned %+v, want %+v", commentBySlug, want) 211 } 212 213 methodName := "GetCommentByID" 214 testBadOptions(t, methodName, func() (err error) { 215 _, _, err = client.Teams.GetCommentByID(ctx, -1, -2, -3, -4) 216 return err 217 }) 218 219 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 220 got, resp, err := client.Teams.GetCommentByID(ctx, 1, 2, 3, 4) 221 if got != nil { 222 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 223 } 224 return resp, err 225 }) 226 227 methodName = "ListCommentsBySlug" 228 testBadOptions(t, methodName, func() (err error) { 229 _, _, err = client.Teams.GetCommentBySlug(ctx, "a\na", "b\nb", -3, -4) 230 return err 231 }) 232 233 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 234 got, resp, err := client.Teams.GetCommentBySlug(ctx, "a", "b", 3, 4) 235 if got != nil { 236 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 237 } 238 return resp, err 239 }) 240 } 241 242 func TestTeamsService_CreateComment(t *testing.T) { 243 client, mux, _, teardown := setup() 244 defer teardown() 245 246 input := DiscussionComment{Body: String("c")} 247 248 handlerFunc := func(w http.ResponseWriter, r *http.Request) { 249 v := new(DiscussionComment) 250 json.NewDecoder(r.Body).Decode(v) 251 252 testMethod(t, r, "POST") 253 if !cmp.Equal(v, &input) { 254 t.Errorf("Request body = %+v, want %+v", v, input) 255 } 256 257 fmt.Fprint(w, `{"number":4}`) 258 } 259 want := &DiscussionComment{Number: Int(4)} 260 261 e := tdcEndpointByID("1", "2", "3", "") 262 mux.HandleFunc(e, handlerFunc) 263 264 ctx := context.Background() 265 commentByID, _, err := client.Teams.CreateCommentByID(ctx, 1, 2, 3, input) 266 if err != nil { 267 t.Errorf("Teams.CreateCommentByID returned error: %v", err) 268 } 269 270 if !cmp.Equal(commentByID, want) { 271 t.Errorf("Teams.CreateCommentByID returned %+v, want %+v", commentByID, want) 272 } 273 274 e = tdcEndpointBySlug("a", "b", "3", "") 275 mux.HandleFunc(e, handlerFunc) 276 277 commentBySlug, _, err := client.Teams.CreateCommentBySlug(ctx, "a", "b", 3, input) 278 if err != nil { 279 t.Errorf("Teams.CreateCommentBySlug returned error: %v", err) 280 } 281 282 if !cmp.Equal(commentBySlug, want) { 283 t.Errorf("Teams.CreateCommentBySlug returned %+v, want %+v", commentBySlug, want) 284 } 285 286 methodName := "CreateCommentByID" 287 testBadOptions(t, methodName, func() (err error) { 288 _, _, err = client.Teams.CreateCommentByID(ctx, -1, -2, -3, input) 289 return err 290 }) 291 292 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 293 got, resp, err := client.Teams.CreateCommentByID(ctx, 1, 2, 3, input) 294 if got != nil { 295 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 296 } 297 return resp, err 298 }) 299 300 methodName = "CreateCommentBySlug" 301 testBadOptions(t, methodName, func() (err error) { 302 _, _, err = client.Teams.CreateCommentBySlug(ctx, "a\na", "b\nb", -3, input) 303 return err 304 }) 305 306 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 307 got, resp, err := client.Teams.CreateCommentBySlug(ctx, "a", "b", 3, input) 308 if got != nil { 309 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 310 } 311 return resp, err 312 }) 313 } 314 315 func TestTeamsService_EditComment(t *testing.T) { 316 client, mux, _, teardown := setup() 317 defer teardown() 318 319 input := DiscussionComment{Body: String("e")} 320 handlerFunc := func(w http.ResponseWriter, r *http.Request) { 321 v := new(DiscussionComment) 322 json.NewDecoder(r.Body).Decode(v) 323 324 testMethod(t, r, "PATCH") 325 if !cmp.Equal(v, &input) { 326 t.Errorf("Request body = %+v, want %+v", v, input) 327 } 328 329 fmt.Fprint(w, `{"number":4}`) 330 } 331 want := &DiscussionComment{Number: Int(4)} 332 333 e := tdcEndpointByID("1", "2", "3", "4") 334 mux.HandleFunc(e, handlerFunc) 335 336 ctx := context.Background() 337 commentByID, _, err := client.Teams.EditCommentByID(ctx, 1, 2, 3, 4, input) 338 if err != nil { 339 t.Errorf("Teams.EditCommentByID returned error: %v", err) 340 } 341 342 if !cmp.Equal(commentByID, want) { 343 t.Errorf("Teams.EditCommentByID returned %+v, want %+v", commentByID, want) 344 } 345 346 e = tdcEndpointBySlug("a", "b", "3", "4") 347 mux.HandleFunc(e, handlerFunc) 348 349 commentBySlug, _, err := client.Teams.EditCommentBySlug(ctx, "a", "b", 3, 4, input) 350 if err != nil { 351 t.Errorf("Teams.EditCommentBySlug returned error: %v", err) 352 } 353 354 if !cmp.Equal(commentBySlug, want) { 355 t.Errorf("Teams.EditCommentBySlug returned %+v, want %+v", commentBySlug, want) 356 } 357 358 methodName := "EditCommentByID" 359 testBadOptions(t, methodName, func() (err error) { 360 _, _, err = client.Teams.EditCommentByID(ctx, -1, -2, -3, -4, input) 361 return err 362 }) 363 364 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 365 got, resp, err := client.Teams.EditCommentByID(ctx, 1, 2, 3, 4, input) 366 if got != nil { 367 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 368 } 369 return resp, err 370 }) 371 372 methodName = "EditCommentBySlug" 373 testBadOptions(t, methodName, func() (err error) { 374 _, _, err = client.Teams.EditCommentBySlug(ctx, "a\na", "b\nb", -3, -4, input) 375 return err 376 }) 377 378 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 379 got, resp, err := client.Teams.EditCommentBySlug(ctx, "a", "b", 3, 4, input) 380 if got != nil { 381 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 382 } 383 return resp, err 384 }) 385 } 386 387 func TestTeamsService_DeleteComment(t *testing.T) { 388 client, mux, _, teardown := setup() 389 defer teardown() 390 391 handlerFunc := func(w http.ResponseWriter, r *http.Request) { 392 testMethod(t, r, "DELETE") 393 } 394 395 e := tdcEndpointByID("1", "2", "3", "4") 396 mux.HandleFunc(e, handlerFunc) 397 398 ctx := context.Background() 399 _, err := client.Teams.DeleteCommentByID(ctx, 1, 2, 3, 4) 400 if err != nil { 401 t.Errorf("Teams.DeleteCommentByID returned error: %v", err) 402 } 403 404 e = tdcEndpointBySlug("a", "b", "3", "4") 405 mux.HandleFunc(e, handlerFunc) 406 407 _, err = client.Teams.DeleteCommentBySlug(ctx, "a", "b", 3, 4) 408 if err != nil { 409 t.Errorf("Teams.DeleteCommentBySlug returned error: %v", err) 410 } 411 412 methodName := "DeleteCommentByID" 413 testBadOptions(t, methodName, func() (err error) { 414 _, err = client.Teams.DeleteCommentByID(ctx, -1, -2, -3, -4) 415 return err 416 }) 417 418 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 419 resp, err := client.Teams.DeleteCommentByID(ctx, 1, 2, 3, 4) 420 return resp, err 421 }) 422 423 methodName = "DeleteCommentBySlug" 424 testBadOptions(t, methodName, func() (err error) { 425 _, err = client.Teams.DeleteCommentBySlug(ctx, "a\na", "b\nb", -3, -4) 426 return err 427 }) 428 429 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 430 resp, err := client.Teams.DeleteCommentBySlug(ctx, "a", "b", 3, 4) 431 return resp, err 432 }) 433 } 434 435 func TestDiscussionComment_Marshal(t *testing.T) { 436 testJSONMarshal(t, &DiscussionComment{}, "{}") 437 438 u := &DiscussionComment{ 439 Author: &User{}, 440 Body: String("body"), 441 BodyHTML: String("body html"), 442 BodyVersion: String("body version"), 443 CreatedAt: &Timestamp{referenceTime}, 444 LastEditedAt: &Timestamp{referenceTime}, 445 DiscussionURL: String("url"), 446 HTMLURL: String("html url"), 447 NodeID: String("node"), 448 Number: Int(1), 449 UpdatedAt: &Timestamp{referenceTime}, 450 URL: String("url"), 451 Reactions: &Reactions{ 452 TotalCount: Int(10), 453 PlusOne: Int(1), 454 MinusOne: Int(1), 455 Laugh: Int(1), 456 Confused: Int(1), 457 Heart: Int(2), 458 Hooray: Int(5), 459 Rocket: Int(3), 460 Eyes: Int(9), 461 URL: String("url"), 462 }, 463 } 464 465 want := `{ 466 "author":{}, 467 "body":"body", 468 "body_html":"body html", 469 "body_version":"body version", 470 "created_at":` + referenceTimeStr + `, 471 "last_edited_at":` + referenceTimeStr + `, 472 "discussion_url":"url", 473 "html_url":"html url", 474 "node_id":"node", 475 "number":1, 476 "updated_at":` + referenceTimeStr + `, 477 "url":"url", 478 "reactions":{ 479 "total_count": 10, 480 "+1": 1, 481 "-1": 1, 482 "laugh": 1, 483 "confused": 1, 484 "heart": 2, 485 "hooray": 5, 486 "rocket": 3, 487 "eyes": 9, 488 "url":"url" 489 } 490 }` 491 492 testJSONMarshal(t, u, want) 493 }