github.com/google/go-github/v33@v33.0.0/github/reactions_test.go (about) 1 // Copyright 2016 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 "fmt" 11 "net/http" 12 "reflect" 13 "testing" 14 ) 15 16 func TestReaction_Marshal(t *testing.T) { 17 testJSONMarshal(t, &Reaction{}, "{}") 18 19 r := &Reaction{ 20 ID: Int64(1), 21 User: nil, 22 NodeID: String("n"), 23 Content: String("+1"), 24 } 25 26 want := `{ 27 "id": 1, 28 "node_id": "n", 29 "content": "+1" 30 }` 31 32 testJSONMarshal(t, r, want) 33 } 34 35 func TestReactions_Marshal(t *testing.T) { 36 testJSONMarshal(t, &Reactions{}, "{}") 37 38 r := &Reactions{ 39 TotalCount: Int(1), 40 PlusOne: Int(1), 41 MinusOne: Int(1), 42 Laugh: Int(1), 43 Confused: Int(1), 44 Heart: Int(1), 45 Hooray: Int(1), 46 Rocket: Int(1), 47 Eyes: Int(1), 48 URL: String("u"), 49 } 50 51 want := `{ 52 "total_count": 1, 53 "+1": 1, 54 "-1": 1, 55 "laugh": 1, 56 "confused": 1, 57 "heart": 1, 58 "hooray": 1, 59 "rocket": 1, 60 "eyes": 1, 61 "url": "u" 62 }` 63 64 testJSONMarshal(t, r, want) 65 } 66 67 func TestReactionsService_ListCommentReactions(t *testing.T) { 68 client, mux, _, teardown := setup() 69 defer teardown() 70 71 mux.HandleFunc("/repos/o/r/comments/1/reactions", func(w http.ResponseWriter, r *http.Request) { 72 testMethod(t, r, "GET") 73 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 74 75 testFormValues(t, r, values{"content": "+1"}) 76 fmt.Fprint(w, `[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`) 77 }) 78 79 opt := &ListCommentReactionOptions{Content: "+1"} 80 reactions, _, err := client.Reactions.ListCommentReactions(context.Background(), "o", "r", 1, opt) 81 if err != nil { 82 t.Errorf("ListCommentReactions returned error: %v", err) 83 } 84 want := []*Reaction{{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")}} 85 if !reflect.DeepEqual(reactions, want) { 86 t.Errorf("ListCommentReactions = %+v, want %+v", reactions, want) 87 } 88 } 89 90 func TestReactionsService_CreateCommentReaction(t *testing.T) { 91 client, mux, _, teardown := setup() 92 defer teardown() 93 94 mux.HandleFunc("/repos/o/r/comments/1/reactions", func(w http.ResponseWriter, r *http.Request) { 95 testMethod(t, r, "POST") 96 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 97 98 w.WriteHeader(http.StatusCreated) 99 w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) 100 }) 101 102 got, _, err := client.Reactions.CreateCommentReaction(context.Background(), "o", "r", 1, "+1") 103 if err != nil { 104 t.Errorf("CreateCommentReaction returned error: %v", err) 105 } 106 want := &Reaction{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")} 107 if !reflect.DeepEqual(got, want) { 108 t.Errorf("CreateCommentReaction = %+v, want %+v", got, want) 109 } 110 } 111 112 func TestReactionsService_ListIssueReactions(t *testing.T) { 113 client, mux, _, teardown := setup() 114 defer teardown() 115 116 mux.HandleFunc("/repos/o/r/issues/1/reactions", func(w http.ResponseWriter, r *http.Request) { 117 testMethod(t, r, "GET") 118 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 119 120 w.WriteHeader(http.StatusOK) 121 w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) 122 }) 123 124 got, _, err := client.Reactions.ListIssueReactions(context.Background(), "o", "r", 1, nil) 125 if err != nil { 126 t.Errorf("ListIssueReactions returned error: %v", err) 127 } 128 want := []*Reaction{{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")}} 129 if !reflect.DeepEqual(got, want) { 130 t.Errorf("ListIssueReactions = %+v, want %+v", got, want) 131 } 132 } 133 134 func TestReactionsService_CreateIssueReaction(t *testing.T) { 135 client, mux, _, teardown := setup() 136 defer teardown() 137 138 mux.HandleFunc("/repos/o/r/issues/1/reactions", func(w http.ResponseWriter, r *http.Request) { 139 testMethod(t, r, "POST") 140 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 141 142 w.WriteHeader(http.StatusCreated) 143 w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) 144 }) 145 146 got, _, err := client.Reactions.CreateIssueReaction(context.Background(), "o", "r", 1, "+1") 147 if err != nil { 148 t.Errorf("CreateIssueReaction returned error: %v", err) 149 } 150 want := &Reaction{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")} 151 if !reflect.DeepEqual(got, want) { 152 t.Errorf("CreateIssueReaction = %+v, want %+v", got, want) 153 } 154 } 155 156 func TestReactionsService_ListIssueCommentReactions(t *testing.T) { 157 client, mux, _, teardown := setup() 158 defer teardown() 159 160 mux.HandleFunc("/repos/o/r/issues/comments/1/reactions", func(w http.ResponseWriter, r *http.Request) { 161 testMethod(t, r, "GET") 162 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 163 164 w.WriteHeader(http.StatusOK) 165 w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) 166 }) 167 168 got, _, err := client.Reactions.ListIssueCommentReactions(context.Background(), "o", "r", 1, nil) 169 if err != nil { 170 t.Errorf("ListIssueCommentReactions returned error: %v", err) 171 } 172 want := []*Reaction{{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")}} 173 if !reflect.DeepEqual(got, want) { 174 t.Errorf("ListIssueCommentReactions = %+v, want %+v", got, want) 175 } 176 } 177 178 func TestReactionsService_CreateIssueCommentReaction(t *testing.T) { 179 client, mux, _, teardown := setup() 180 defer teardown() 181 182 mux.HandleFunc("/repos/o/r/issues/comments/1/reactions", func(w http.ResponseWriter, r *http.Request) { 183 testMethod(t, r, "POST") 184 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 185 186 w.WriteHeader(http.StatusCreated) 187 w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) 188 }) 189 190 got, _, err := client.Reactions.CreateIssueCommentReaction(context.Background(), "o", "r", 1, "+1") 191 if err != nil { 192 t.Errorf("CreateIssueCommentReaction returned error: %v", err) 193 } 194 want := &Reaction{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")} 195 if !reflect.DeepEqual(got, want) { 196 t.Errorf("CreateIssueCommentReaction = %+v, want %+v", got, want) 197 } 198 } 199 200 func TestReactionsService_ListPullRequestCommentReactions(t *testing.T) { 201 client, mux, _, teardown := setup() 202 defer teardown() 203 204 mux.HandleFunc("/repos/o/r/pulls/comments/1/reactions", func(w http.ResponseWriter, r *http.Request) { 205 testMethod(t, r, "GET") 206 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 207 208 w.WriteHeader(http.StatusOK) 209 w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) 210 }) 211 212 got, _, err := client.Reactions.ListPullRequestCommentReactions(context.Background(), "o", "r", 1, nil) 213 if err != nil { 214 t.Errorf("ListPullRequestCommentReactions returned error: %v", err) 215 } 216 want := []*Reaction{{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")}} 217 if !reflect.DeepEqual(got, want) { 218 t.Errorf("ListPullRequestCommentReactions = %+v, want %+v", got, want) 219 } 220 } 221 222 func TestReactionsService_CreatePullRequestCommentReaction(t *testing.T) { 223 client, mux, _, teardown := setup() 224 defer teardown() 225 226 mux.HandleFunc("/repos/o/r/pulls/comments/1/reactions", func(w http.ResponseWriter, r *http.Request) { 227 testMethod(t, r, "POST") 228 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 229 230 w.WriteHeader(http.StatusCreated) 231 w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) 232 }) 233 234 got, _, err := client.Reactions.CreatePullRequestCommentReaction(context.Background(), "o", "r", 1, "+1") 235 if err != nil { 236 t.Errorf("CreatePullRequestCommentReaction returned error: %v", err) 237 } 238 want := &Reaction{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")} 239 if !reflect.DeepEqual(got, want) { 240 t.Errorf("CreatePullRequestCommentReaction = %+v, want %+v", got, want) 241 } 242 } 243 244 func TestReactionsService_ListTeamDiscussionReactions(t *testing.T) { 245 client, mux, _, teardown := setup() 246 defer teardown() 247 248 mux.HandleFunc("/teams/1/discussions/2/reactions", func(w http.ResponseWriter, r *http.Request) { 249 testMethod(t, r, "GET") 250 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 251 252 w.WriteHeader(http.StatusOK) 253 w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) 254 }) 255 256 got, _, err := client.Reactions.ListTeamDiscussionReactions(context.Background(), 1, 2, nil) 257 if err != nil { 258 t.Errorf("ListTeamDiscussionReactions returned error: %v", err) 259 } 260 want := []*Reaction{{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")}} 261 if !reflect.DeepEqual(got, want) { 262 t.Errorf("ListTeamDiscussionReactions = %+v, want %+v", got, want) 263 } 264 } 265 266 func TestReactionsService_CreateTeamDiscussionReaction(t *testing.T) { 267 client, mux, _, teardown := setup() 268 defer teardown() 269 270 mux.HandleFunc("/teams/1/discussions/2/reactions", func(w http.ResponseWriter, r *http.Request) { 271 testMethod(t, r, "POST") 272 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 273 274 w.WriteHeader(http.StatusCreated) 275 w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) 276 }) 277 278 got, _, err := client.Reactions.CreateTeamDiscussionReaction(context.Background(), 1, 2, "+1") 279 if err != nil { 280 t.Errorf("CreateTeamDiscussionReaction returned error: %v", err) 281 } 282 want := &Reaction{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")} 283 if !reflect.DeepEqual(got, want) { 284 t.Errorf("CreateTeamDiscussionReaction = %+v, want %+v", got, want) 285 } 286 } 287 288 func TestReactionService_ListTeamDiscussionCommentReactions(t *testing.T) { 289 client, mux, _, teardown := setup() 290 defer teardown() 291 292 mux.HandleFunc("/teams/1/discussions/2/comments/3/reactions", func(w http.ResponseWriter, r *http.Request) { 293 testMethod(t, r, "GET") 294 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 295 296 w.WriteHeader(http.StatusOK) 297 w.Write([]byte(`[{"id":1,"user":{"login":"l","id":2},"content":"+1"}]`)) 298 }) 299 300 got, _, err := client.Reactions.ListTeamDiscussionCommentReactions(context.Background(), 1, 2, 3, nil) 301 if err != nil { 302 t.Errorf("ListTeamDiscussionCommentReactions returned error: %v", err) 303 } 304 want := []*Reaction{{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")}} 305 if !reflect.DeepEqual(got, want) { 306 t.Errorf("ListTeamDiscussionCommentReactions = %+v, want %+v", got, want) 307 } 308 } 309 310 func TestReactionService_CreateTeamDiscussionCommentReaction(t *testing.T) { 311 client, mux, _, teardown := setup() 312 defer teardown() 313 314 mux.HandleFunc("/teams/1/discussions/2/comments/3/reactions", func(w http.ResponseWriter, r *http.Request) { 315 testMethod(t, r, "POST") 316 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 317 318 w.WriteHeader(http.StatusCreated) 319 w.Write([]byte(`{"id":1,"user":{"login":"l","id":2},"content":"+1"}`)) 320 }) 321 322 got, _, err := client.Reactions.CreateTeamDiscussionCommentReaction(context.Background(), 1, 2, 3, "+1") 323 if err != nil { 324 t.Errorf("CreateTeamDiscussionCommentReaction returned error: %v", err) 325 } 326 want := &Reaction{ID: Int64(1), User: &User{Login: String("l"), ID: Int64(2)}, Content: String("+1")} 327 if !reflect.DeepEqual(got, want) { 328 t.Errorf("CreateTeamDiscussionCommentReaction = %+v, want %+v", got, want) 329 } 330 } 331 332 func TestReactionsService_DeleteCommitCommentReaction(t *testing.T) { 333 client, mux, _, teardown := setup() 334 defer teardown() 335 336 mux.HandleFunc("/repos/o/r/comments/1/reactions/2", func(w http.ResponseWriter, r *http.Request) { 337 testMethod(t, r, "DELETE") 338 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 339 340 w.WriteHeader(http.StatusNoContent) 341 }) 342 343 if _, err := client.Reactions.DeleteCommentReaction(context.Background(), "o", "r", 1, 2); err != nil { 344 t.Errorf("DeleteCommentReaction returned error: %v", err) 345 } 346 } 347 348 func TestReactionsService_DeleteCommitCommentReactionByRepoID(t *testing.T) { 349 client, mux, _, teardown := setup() 350 defer teardown() 351 352 mux.HandleFunc("/repositories/1/comments/2/reactions/3", func(w http.ResponseWriter, r *http.Request) { 353 testMethod(t, r, "DELETE") 354 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 355 356 w.WriteHeader(http.StatusNoContent) 357 }) 358 359 if _, err := client.Reactions.DeleteCommentReactionByID(context.Background(), 1, 2, 3); err != nil { 360 t.Errorf("DeleteCommentReactionByRepoID returned error: %v", err) 361 } 362 } 363 364 func TestReactionsService_DeleteIssueReaction(t *testing.T) { 365 client, mux, _, teardown := setup() 366 defer teardown() 367 368 mux.HandleFunc("/repos/o/r/issues/1/reactions/2", func(w http.ResponseWriter, r *http.Request) { 369 testMethod(t, r, "DELETE") 370 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 371 372 w.WriteHeader(http.StatusNoContent) 373 }) 374 375 if _, err := client.Reactions.DeleteIssueReaction(context.Background(), "o", "r", 1, 2); err != nil { 376 t.Errorf("DeleteIssueReaction returned error: %v", err) 377 } 378 } 379 380 func TestReactionsService_DeleteIssueReactionByRepoID(t *testing.T) { 381 client, mux, _, teardown := setup() 382 defer teardown() 383 384 mux.HandleFunc("/repositories/1/issues/2/reactions/3", func(w http.ResponseWriter, r *http.Request) { 385 testMethod(t, r, "DELETE") 386 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 387 388 w.WriteHeader(http.StatusNoContent) 389 }) 390 391 if _, err := client.Reactions.DeleteIssueReactionByID(context.Background(), 1, 2, 3); err != nil { 392 t.Errorf("DeleteIssueReactionByRepoID returned error: %v", err) 393 } 394 } 395 396 func TestReactionsService_DeleteIssueCommentReaction(t *testing.T) { 397 client, mux, _, teardown := setup() 398 defer teardown() 399 400 mux.HandleFunc("/repos/o/r/issues/comments/1/reactions/2", func(w http.ResponseWriter, r *http.Request) { 401 testMethod(t, r, "DELETE") 402 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 403 404 w.WriteHeader(http.StatusNoContent) 405 }) 406 407 if _, err := client.Reactions.DeleteIssueCommentReaction(context.Background(), "o", "r", 1, 2); err != nil { 408 t.Errorf("DeleteIssueCommentReaction returned error: %v", err) 409 } 410 } 411 412 func TestReactionsService_DeleteIssueCommentReactionByRepoID(t *testing.T) { 413 client, mux, _, teardown := setup() 414 defer teardown() 415 416 mux.HandleFunc("/repositories/1/issues/comments/2/reactions/3", func(w http.ResponseWriter, r *http.Request) { 417 testMethod(t, r, "DELETE") 418 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 419 420 w.WriteHeader(http.StatusNoContent) 421 }) 422 423 if _, err := client.Reactions.DeleteIssueCommentReactionByID(context.Background(), 1, 2, 3); err != nil { 424 t.Errorf("DeleteIssueCommentReactionByRepoID returned error: %v", err) 425 } 426 } 427 428 func TestReactionsService_DeletePullRequestCommentReaction(t *testing.T) { 429 client, mux, _, teardown := setup() 430 defer teardown() 431 432 mux.HandleFunc("/repos/o/r/pulls/comments/1/reactions/2", func(w http.ResponseWriter, r *http.Request) { 433 testMethod(t, r, "DELETE") 434 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 435 436 w.WriteHeader(http.StatusNoContent) 437 }) 438 439 if _, err := client.Reactions.DeletePullRequestCommentReaction(context.Background(), "o", "r", 1, 2); err != nil { 440 t.Errorf("DeletePullRequestCommentReaction returned error: %v", err) 441 } 442 } 443 444 func TestReactionsService_DeletePullRequestCommentReactionByRepoID(t *testing.T) { 445 client, mux, _, teardown := setup() 446 defer teardown() 447 448 mux.HandleFunc("/repositories/1/pulls/comments/2/reactions/3", func(w http.ResponseWriter, r *http.Request) { 449 testMethod(t, r, "DELETE") 450 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 451 452 w.WriteHeader(http.StatusNoContent) 453 }) 454 455 if _, err := client.Reactions.DeletePullRequestCommentReactionByID(context.Background(), 1, 2, 3); err != nil { 456 t.Errorf("DeletePullRequestCommentReactionByRepoID returned error: %v", err) 457 } 458 } 459 460 func TestReactionsService_DeleteTeamDiscussionReaction(t *testing.T) { 461 client, mux, _, teardown := setup() 462 defer teardown() 463 464 mux.HandleFunc("/orgs/o/teams/s/discussions/1/reactions/2", func(w http.ResponseWriter, r *http.Request) { 465 testMethod(t, r, "DELETE") 466 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 467 468 w.WriteHeader(http.StatusNoContent) 469 }) 470 471 if _, err := client.Reactions.DeleteTeamDiscussionReaction(context.Background(), "o", "s", 1, 2); err != nil { 472 t.Errorf("DeleteTeamDiscussionReaction returned error: %v", err) 473 } 474 } 475 476 func TestReactionsService_DeleteTeamDiscussionReactionByTeamIDAndOrgID(t *testing.T) { 477 client, mux, _, teardown := setup() 478 defer teardown() 479 480 mux.HandleFunc("/organizations/1/team/2/discussions/3/reactions/4", func(w http.ResponseWriter, r *http.Request) { 481 testMethod(t, r, "DELETE") 482 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 483 484 w.WriteHeader(http.StatusNoContent) 485 }) 486 487 if _, err := client.Reactions.DeleteTeamDiscussionReactionByOrgIDAndTeamID(context.Background(), 1, 2, 3, 4); err != nil { 488 t.Errorf("DeleteTeamDiscussionReactionByTeamIDAndOrgID returned error: %v", err) 489 } 490 } 491 492 func TestReactionsService_DeleteTeamDiscussionCommentReaction(t *testing.T) { 493 client, mux, _, teardown := setup() 494 defer teardown() 495 496 mux.HandleFunc("/orgs/o/teams/s/discussions/1/comments/2/reactions/3", func(w http.ResponseWriter, r *http.Request) { 497 testMethod(t, r, "DELETE") 498 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 499 500 w.WriteHeader(http.StatusNoContent) 501 }) 502 503 if _, err := client.Reactions.DeleteTeamDiscussionCommentReaction(context.Background(), "o", "s", 1, 2, 3); err != nil { 504 t.Errorf("DeleteTeamDiscussionCommentReaction returned error: %v", err) 505 } 506 } 507 508 func TestReactionsService_DeleteTeamDiscussionCommentReactionByTeamIDAndOrgID(t *testing.T) { 509 client, mux, _, teardown := setup() 510 defer teardown() 511 512 mux.HandleFunc("/organizations/1/team/2/discussions/3/comments/4/reactions/5", func(w http.ResponseWriter, r *http.Request) { 513 testMethod(t, r, "DELETE") 514 testHeader(t, r, "Accept", mediaTypeReactionsPreview) 515 516 w.WriteHeader(http.StatusNoContent) 517 }) 518 519 if _, err := client.Reactions.DeleteTeamDiscussionCommentReactionByOrgIDAndTeamID(context.Background(), 1, 2, 3, 4, 5); err != nil { 520 t.Errorf("DeleteTeamDiscussionCommentReactionByTeamIDAndOrgID returned error: %v", err) 521 } 522 }