github.com/google/go-github/v52@v52.0.0/github/repos_test.go (about) 1 // Copyright 2013 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 "errors" 12 "fmt" 13 "net/http" 14 "net/url" 15 "strings" 16 "testing" 17 18 "github.com/google/go-cmp/cmp" 19 ) 20 21 func TestRepositoriesService_List_authenticatedUser(t *testing.T) { 22 client, mux, _, teardown := setup() 23 defer teardown() 24 25 wantAcceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview} 26 mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) { 27 testMethod(t, r, "GET") 28 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 29 fmt.Fprint(w, `[{"id":1},{"id":2}]`) 30 }) 31 32 ctx := context.Background() 33 got, _, err := client.Repositories.List(ctx, "", nil) 34 if err != nil { 35 t.Errorf("Repositories.List returned error: %v", err) 36 } 37 38 want := []*Repository{{ID: Int64(1)}, {ID: Int64(2)}} 39 if !cmp.Equal(got, want) { 40 t.Errorf("Repositories.List returned %+v, want %+v", got, want) 41 } 42 43 const methodName = "List" 44 testBadOptions(t, methodName, func() (err error) { 45 _, _, err = client.Repositories.List(ctx, "\n", &RepositoryListOptions{}) 46 return err 47 }) 48 49 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 50 got, resp, err := client.Repositories.List(ctx, "", nil) 51 if got != nil { 52 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 53 } 54 return resp, err 55 }) 56 } 57 58 func TestRepositoriesService_List_specifiedUser(t *testing.T) { 59 client, mux, _, teardown := setup() 60 defer teardown() 61 62 wantAcceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview} 63 mux.HandleFunc("/users/u/repos", func(w http.ResponseWriter, r *http.Request) { 64 testMethod(t, r, "GET") 65 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 66 testFormValues(t, r, values{ 67 "visibility": "public", 68 "affiliation": "owner,collaborator", 69 "sort": "created", 70 "direction": "asc", 71 "page": "2", 72 }) 73 fmt.Fprint(w, `[{"id":1}]`) 74 }) 75 76 opt := &RepositoryListOptions{ 77 Visibility: "public", 78 Affiliation: "owner,collaborator", 79 Sort: "created", 80 Direction: "asc", 81 ListOptions: ListOptions{Page: 2}, 82 } 83 ctx := context.Background() 84 repos, _, err := client.Repositories.List(ctx, "u", opt) 85 if err != nil { 86 t.Errorf("Repositories.List returned error: %v", err) 87 } 88 89 want := []*Repository{{ID: Int64(1)}} 90 if !cmp.Equal(repos, want) { 91 t.Errorf("Repositories.List returned %+v, want %+v", repos, want) 92 } 93 } 94 95 func TestRepositoriesService_List_specifiedUser_type(t *testing.T) { 96 client, mux, _, teardown := setup() 97 defer teardown() 98 99 wantAcceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview} 100 mux.HandleFunc("/users/u/repos", func(w http.ResponseWriter, r *http.Request) { 101 testMethod(t, r, "GET") 102 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 103 testFormValues(t, r, values{ 104 "type": "owner", 105 }) 106 fmt.Fprint(w, `[{"id":1}]`) 107 }) 108 109 opt := &RepositoryListOptions{ 110 Type: "owner", 111 } 112 ctx := context.Background() 113 repos, _, err := client.Repositories.List(ctx, "u", opt) 114 if err != nil { 115 t.Errorf("Repositories.List returned error: %v", err) 116 } 117 118 want := []*Repository{{ID: Int64(1)}} 119 if !cmp.Equal(repos, want) { 120 t.Errorf("Repositories.List returned %+v, want %+v", repos, want) 121 } 122 } 123 124 func TestRepositoriesService_List_invalidUser(t *testing.T) { 125 client, _, _, teardown := setup() 126 defer teardown() 127 128 ctx := context.Background() 129 _, _, err := client.Repositories.List(ctx, "%", nil) 130 testURLParseError(t, err) 131 } 132 133 func TestRepositoriesService_ListByOrg(t *testing.T) { 134 client, mux, _, teardown := setup() 135 defer teardown() 136 137 wantAcceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview} 138 mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) { 139 testMethod(t, r, "GET") 140 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 141 testFormValues(t, r, values{ 142 "type": "forks", 143 "page": "2", 144 }) 145 fmt.Fprint(w, `[{"id":1}]`) 146 }) 147 148 ctx := context.Background() 149 opt := &RepositoryListByOrgOptions{ 150 Type: "forks", 151 ListOptions: ListOptions{Page: 2}, 152 } 153 got, _, err := client.Repositories.ListByOrg(ctx, "o", opt) 154 if err != nil { 155 t.Errorf("Repositories.ListByOrg returned error: %v", err) 156 } 157 158 want := []*Repository{{ID: Int64(1)}} 159 if !cmp.Equal(got, want) { 160 t.Errorf("Repositories.ListByOrg returned %+v, want %+v", got, want) 161 } 162 163 const methodName = "ListByOrg" 164 testBadOptions(t, methodName, func() (err error) { 165 _, _, err = client.Repositories.ListByOrg(ctx, "\n", opt) 166 return err 167 }) 168 169 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 170 got, resp, err := client.Repositories.ListByOrg(ctx, "o", opt) 171 if got != nil { 172 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 173 } 174 return resp, err 175 }) 176 } 177 178 func TestRepositoriesService_ListByOrg_invalidOrg(t *testing.T) { 179 client, _, _, teardown := setup() 180 defer teardown() 181 182 ctx := context.Background() 183 _, _, err := client.Repositories.ListByOrg(ctx, "%", nil) 184 testURLParseError(t, err) 185 } 186 187 func TestRepositoriesService_ListAll(t *testing.T) { 188 client, mux, _, teardown := setup() 189 defer teardown() 190 191 mux.HandleFunc("/repositories", func(w http.ResponseWriter, r *http.Request) { 192 testMethod(t, r, "GET") 193 testFormValues(t, r, values{ 194 "since": "1", 195 }) 196 fmt.Fprint(w, `[{"id":1}]`) 197 }) 198 199 ctx := context.Background() 200 opt := &RepositoryListAllOptions{1} 201 got, _, err := client.Repositories.ListAll(ctx, opt) 202 if err != nil { 203 t.Errorf("Repositories.ListAll returned error: %v", err) 204 } 205 206 want := []*Repository{{ID: Int64(1)}} 207 if !cmp.Equal(got, want) { 208 t.Errorf("Repositories.ListAll returned %+v, want %+v", got, want) 209 } 210 211 const methodName = "ListAll" 212 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 213 got, resp, err := client.Repositories.ListAll(ctx, &RepositoryListAllOptions{1}) 214 if got != nil { 215 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 216 } 217 return resp, err 218 }) 219 } 220 221 func TestRepositoriesService_Create_user(t *testing.T) { 222 client, mux, _, teardown := setup() 223 defer teardown() 224 225 input := &Repository{ 226 Name: String("n"), 227 Archived: Bool(true), // not passed along. 228 } 229 230 wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} 231 mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) { 232 v := new(createRepoRequest) 233 json.NewDecoder(r.Body).Decode(v) 234 235 testMethod(t, r, "POST") 236 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 237 want := &createRepoRequest{Name: String("n")} 238 if !cmp.Equal(v, want) { 239 t.Errorf("Request body = %+v, want %+v", v, want) 240 } 241 242 fmt.Fprint(w, `{"id":1}`) 243 }) 244 245 ctx := context.Background() 246 got, _, err := client.Repositories.Create(ctx, "", input) 247 if err != nil { 248 t.Errorf("Repositories.Create returned error: %v", err) 249 } 250 251 want := &Repository{ID: Int64(1)} 252 if !cmp.Equal(got, want) { 253 t.Errorf("Repositories.Create returned %+v, want %+v", got, want) 254 } 255 256 const methodName = "Create" 257 testBadOptions(t, methodName, func() (err error) { 258 _, _, err = client.Repositories.Create(ctx, "\n", input) 259 return err 260 }) 261 262 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 263 got, resp, err := client.Repositories.Create(ctx, "", input) 264 if got != nil { 265 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 266 } 267 return resp, err 268 }) 269 } 270 271 func TestRepositoriesService_Create_org(t *testing.T) { 272 client, mux, _, teardown := setup() 273 defer teardown() 274 275 input := &Repository{ 276 Name: String("n"), 277 Archived: Bool(true), // not passed along. 278 } 279 280 wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} 281 mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) { 282 v := new(createRepoRequest) 283 json.NewDecoder(r.Body).Decode(v) 284 285 testMethod(t, r, "POST") 286 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 287 want := &createRepoRequest{Name: String("n")} 288 if !cmp.Equal(v, want) { 289 t.Errorf("Request body = %+v, want %+v", v, want) 290 } 291 292 fmt.Fprint(w, `{"id":1}`) 293 }) 294 295 ctx := context.Background() 296 repo, _, err := client.Repositories.Create(ctx, "o", input) 297 if err != nil { 298 t.Errorf("Repositories.Create returned error: %v", err) 299 } 300 301 want := &Repository{ID: Int64(1)} 302 if !cmp.Equal(repo, want) { 303 t.Errorf("Repositories.Create returned %+v, want %+v", repo, want) 304 } 305 } 306 307 func TestRepositoriesService_CreateFromTemplate(t *testing.T) { 308 client, mux, _, teardown := setup() 309 defer teardown() 310 311 templateRepoReq := &TemplateRepoRequest{ 312 Name: String("n"), 313 } 314 315 mux.HandleFunc("/repos/to/tr/generate", func(w http.ResponseWriter, r *http.Request) { 316 v := new(TemplateRepoRequest) 317 json.NewDecoder(r.Body).Decode(v) 318 319 testMethod(t, r, "POST") 320 testHeader(t, r, "Accept", mediaTypeRepositoryTemplatePreview) 321 want := &TemplateRepoRequest{Name: String("n")} 322 if !cmp.Equal(v, want) { 323 t.Errorf("Request body = %+v, want %+v", v, want) 324 } 325 326 fmt.Fprint(w, `{"id":1,"name":"n"}`) 327 }) 328 329 ctx := context.Background() 330 got, _, err := client.Repositories.CreateFromTemplate(ctx, "to", "tr", templateRepoReq) 331 if err != nil { 332 t.Errorf("Repositories.CreateFromTemplate returned error: %v", err) 333 } 334 335 want := &Repository{ID: Int64(1), Name: String("n")} 336 if !cmp.Equal(got, want) { 337 t.Errorf("Repositories.CreateFromTemplate returned %+v, want %+v", got, want) 338 } 339 340 const methodName = "CreateFromTemplate" 341 testBadOptions(t, methodName, func() (err error) { 342 _, _, err = client.Repositories.CreateFromTemplate(ctx, "\n", "\n", templateRepoReq) 343 return err 344 }) 345 346 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 347 got, resp, err := client.Repositories.CreateFromTemplate(ctx, "to", "tr", templateRepoReq) 348 if got != nil { 349 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 350 } 351 return resp, err 352 }) 353 } 354 355 func TestRepositoriesService_Get(t *testing.T) { 356 client, mux, _, teardown := setup() 357 defer teardown() 358 359 wantAcceptHeaders := []string{mediaTypeCodesOfConductPreview, mediaTypeTopicsPreview, mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} 360 mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { 361 testMethod(t, r, "GET") 362 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 363 fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"},"security_and_analysis":{"advanced_security":{"status":"enabled"},"secret_scanning":{"status":"enabled"},"secret_scanning_push_protection":{"status":"enabled"}}}`) 364 }) 365 366 ctx := context.Background() 367 got, _, err := client.Repositories.Get(ctx, "o", "r") 368 if err != nil { 369 t.Errorf("Repositories.Get returned error: %v", err) 370 } 371 372 want := &Repository{ID: Int64(1), Name: String("n"), Description: String("d"), Owner: &User{Login: String("l")}, License: &License{Key: String("mit")}, SecurityAndAnalysis: &SecurityAndAnalysis{AdvancedSecurity: &AdvancedSecurity{Status: String("enabled")}, SecretScanning: &SecretScanning{String("enabled")}, SecretScanningPushProtection: &SecretScanningPushProtection{String("enabled")}}} 373 if !cmp.Equal(got, want) { 374 t.Errorf("Repositories.Get returned %+v, want %+v", got, want) 375 } 376 377 const methodName = "Get" 378 testBadOptions(t, methodName, func() (err error) { 379 _, _, err = client.Repositories.Get(ctx, "\n", "\n") 380 return err 381 }) 382 383 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 384 got, resp, err := client.Repositories.Get(ctx, "o", "r") 385 if got != nil { 386 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 387 } 388 return resp, err 389 }) 390 } 391 392 func TestRepositoriesService_GetCodeOfConduct(t *testing.T) { 393 client, mux, _, teardown := setup() 394 defer teardown() 395 396 mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { 397 testMethod(t, r, "GET") 398 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) 399 fmt.Fprint(w, `{ 400 "code_of_conduct": { 401 "key": "key", 402 "name": "name", 403 "url": "url", 404 "body": "body" 405 }}`, 406 ) 407 }) 408 409 ctx := context.Background() 410 got, _, err := client.Repositories.GetCodeOfConduct(ctx, "o", "r") 411 if err != nil { 412 t.Errorf("Repositories.GetCodeOfConduct returned error: %v", err) 413 } 414 415 want := &CodeOfConduct{ 416 Key: String("key"), 417 Name: String("name"), 418 URL: String("url"), 419 Body: String("body"), 420 } 421 422 if !cmp.Equal(got, want) { 423 t.Errorf("Repositories.GetCodeOfConduct returned %+v, want %+v", got, want) 424 } 425 426 const methodName = "GetCodeOfConduct" 427 testBadOptions(t, methodName, func() (err error) { 428 _, _, err = client.Repositories.GetCodeOfConduct(ctx, "\n", "\n") 429 return err 430 }) 431 432 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 433 got, resp, err := client.Repositories.GetCodeOfConduct(ctx, "o", "r") 434 if got != nil { 435 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 436 } 437 return resp, err 438 }) 439 } 440 441 func TestRepositoriesService_GetByID(t *testing.T) { 442 client, mux, _, teardown := setup() 443 defer teardown() 444 445 mux.HandleFunc("/repositories/1", func(w http.ResponseWriter, r *http.Request) { 446 testMethod(t, r, "GET") 447 fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"}}`) 448 }) 449 450 ctx := context.Background() 451 got, _, err := client.Repositories.GetByID(ctx, 1) 452 if err != nil { 453 t.Fatalf("Repositories.GetByID returned error: %v", err) 454 } 455 456 want := &Repository{ID: Int64(1), Name: String("n"), Description: String("d"), Owner: &User{Login: String("l")}, License: &License{Key: String("mit")}} 457 if !cmp.Equal(got, want) { 458 t.Errorf("Repositories.GetByID returned %+v, want %+v", got, want) 459 } 460 461 const methodName = "GetByID" 462 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 463 got, resp, err := client.Repositories.GetByID(ctx, 1) 464 if got != nil { 465 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 466 } 467 return resp, err 468 }) 469 } 470 471 func TestRepositoriesService_Edit(t *testing.T) { 472 client, mux, _, teardown := setup() 473 defer teardown() 474 475 i := true 476 input := &Repository{HasIssues: &i} 477 478 wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} 479 mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { 480 v := new(Repository) 481 json.NewDecoder(r.Body).Decode(v) 482 483 testMethod(t, r, "PATCH") 484 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 485 if !cmp.Equal(v, input) { 486 t.Errorf("Request body = %+v, want %+v", v, input) 487 } 488 fmt.Fprint(w, `{"id":1}`) 489 }) 490 491 ctx := context.Background() 492 got, _, err := client.Repositories.Edit(ctx, "o", "r", input) 493 if err != nil { 494 t.Errorf("Repositories.Edit returned error: %v", err) 495 } 496 497 want := &Repository{ID: Int64(1)} 498 if !cmp.Equal(got, want) { 499 t.Errorf("Repositories.Edit returned %+v, want %+v", got, want) 500 } 501 502 const methodName = "Edit" 503 testBadOptions(t, methodName, func() (err error) { 504 _, _, err = client.Repositories.Edit(ctx, "\n", "\n", input) 505 return err 506 }) 507 508 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 509 got, resp, err := client.Repositories.Edit(ctx, "o", "r", input) 510 if got != nil { 511 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 512 } 513 return resp, err 514 }) 515 } 516 517 func TestRepositoriesService_Delete(t *testing.T) { 518 client, mux, _, teardown := setup() 519 defer teardown() 520 521 mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { 522 testMethod(t, r, "DELETE") 523 }) 524 525 ctx := context.Background() 526 _, err := client.Repositories.Delete(ctx, "o", "r") 527 if err != nil { 528 t.Errorf("Repositories.Delete returned error: %v", err) 529 } 530 531 const methodName = "Delete" 532 testBadOptions(t, methodName, func() (err error) { 533 _, err = client.Repositories.Delete(ctx, "\n", "\n") 534 return err 535 }) 536 537 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 538 return client.Repositories.Delete(ctx, "o", "r") 539 }) 540 } 541 542 func TestRepositoriesService_Get_invalidOwner(t *testing.T) { 543 client, _, _, teardown := setup() 544 defer teardown() 545 546 ctx := context.Background() 547 _, _, err := client.Repositories.Get(ctx, "%", "r") 548 testURLParseError(t, err) 549 } 550 551 func TestRepositoriesService_Edit_invalidOwner(t *testing.T) { 552 client, _, _, teardown := setup() 553 defer teardown() 554 555 ctx := context.Background() 556 _, _, err := client.Repositories.Edit(ctx, "%", "r", nil) 557 testURLParseError(t, err) 558 } 559 560 func TestRepositoriesService_GetVulnerabilityAlerts(t *testing.T) { 561 client, mux, _, teardown := setup() 562 defer teardown() 563 564 mux.HandleFunc("/repos/o/r/vulnerability-alerts", func(w http.ResponseWriter, r *http.Request) { 565 testMethod(t, r, "GET") 566 testHeader(t, r, "Accept", mediaTypeRequiredVulnerabilityAlertsPreview) 567 568 w.WriteHeader(http.StatusNoContent) 569 }) 570 571 ctx := context.Background() 572 vulnerabilityAlertsEnabled, _, err := client.Repositories.GetVulnerabilityAlerts(ctx, "o", "r") 573 if err != nil { 574 t.Errorf("Repositories.GetVulnerabilityAlerts returned error: %v", err) 575 } 576 577 if want := true; vulnerabilityAlertsEnabled != want { 578 t.Errorf("Repositories.GetVulnerabilityAlerts returned %+v, want %+v", vulnerabilityAlertsEnabled, want) 579 } 580 581 const methodName = "GetVulnerabilityAlerts" 582 testBadOptions(t, methodName, func() (err error) { 583 _, _, err = client.Repositories.GetVulnerabilityAlerts(ctx, "\n", "\n") 584 return err 585 }) 586 587 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 588 got, resp, err := client.Repositories.GetVulnerabilityAlerts(ctx, "o", "r") 589 if got { 590 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got) 591 } 592 return resp, err 593 }) 594 } 595 596 func TestRepositoriesService_EnableVulnerabilityAlerts(t *testing.T) { 597 client, mux, _, teardown := setup() 598 defer teardown() 599 600 mux.HandleFunc("/repos/o/r/vulnerability-alerts", func(w http.ResponseWriter, r *http.Request) { 601 testMethod(t, r, "PUT") 602 testHeader(t, r, "Accept", mediaTypeRequiredVulnerabilityAlertsPreview) 603 604 w.WriteHeader(http.StatusNoContent) 605 }) 606 607 ctx := context.Background() 608 if _, err := client.Repositories.EnableVulnerabilityAlerts(ctx, "o", "r"); err != nil { 609 t.Errorf("Repositories.EnableVulnerabilityAlerts returned error: %v", err) 610 } 611 612 const methodName = "EnableVulnerabilityAlerts" 613 testBadOptions(t, methodName, func() (err error) { 614 _, err = client.Repositories.EnableVulnerabilityAlerts(ctx, "\n", "\n") 615 return err 616 }) 617 618 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 619 return client.Repositories.EnableVulnerabilityAlerts(ctx, "o", "r") 620 }) 621 } 622 623 func TestRepositoriesService_DisableVulnerabilityAlerts(t *testing.T) { 624 client, mux, _, teardown := setup() 625 defer teardown() 626 627 mux.HandleFunc("/repos/o/r/vulnerability-alerts", func(w http.ResponseWriter, r *http.Request) { 628 testMethod(t, r, "DELETE") 629 testHeader(t, r, "Accept", mediaTypeRequiredVulnerabilityAlertsPreview) 630 631 w.WriteHeader(http.StatusNoContent) 632 }) 633 634 ctx := context.Background() 635 if _, err := client.Repositories.DisableVulnerabilityAlerts(ctx, "o", "r"); err != nil { 636 t.Errorf("Repositories.DisableVulnerabilityAlerts returned error: %v", err) 637 } 638 639 const methodName = "DisableVulnerabilityAlerts" 640 testBadOptions(t, methodName, func() (err error) { 641 _, err = client.Repositories.DisableVulnerabilityAlerts(ctx, "\n", "\n") 642 return err 643 }) 644 645 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 646 return client.Repositories.DisableVulnerabilityAlerts(ctx, "o", "r") 647 }) 648 } 649 650 func TestRepositoriesService_EnableAutomatedSecurityFixes(t *testing.T) { 651 client, mux, _, teardown := setup() 652 defer teardown() 653 654 mux.HandleFunc("/repos/o/r/automated-security-fixes", func(w http.ResponseWriter, r *http.Request) { 655 testMethod(t, r, "PUT") 656 testHeader(t, r, "Accept", mediaTypeRequiredAutomatedSecurityFixesPreview) 657 658 w.WriteHeader(http.StatusNoContent) 659 }) 660 661 ctx := context.Background() 662 if _, err := client.Repositories.EnableAutomatedSecurityFixes(ctx, "o", "r"); err != nil { 663 t.Errorf("Repositories.EnableAutomatedSecurityFixes returned error: %v", err) 664 } 665 } 666 667 func TestRepositoriesService_DisableAutomatedSecurityFixes(t *testing.T) { 668 client, mux, _, teardown := setup() 669 defer teardown() 670 671 mux.HandleFunc("/repos/o/r/automated-security-fixes", func(w http.ResponseWriter, r *http.Request) { 672 testMethod(t, r, "DELETE") 673 testHeader(t, r, "Accept", mediaTypeRequiredAutomatedSecurityFixesPreview) 674 675 w.WriteHeader(http.StatusNoContent) 676 }) 677 678 ctx := context.Background() 679 if _, err := client.Repositories.DisableAutomatedSecurityFixes(ctx, "o", "r"); err != nil { 680 t.Errorf("Repositories.DisableAutomatedSecurityFixes returned error: %v", err) 681 } 682 } 683 684 func TestRepositoriesService_ListContributors(t *testing.T) { 685 client, mux, _, teardown := setup() 686 defer teardown() 687 688 mux.HandleFunc("/repos/o/r/contributors", func(w http.ResponseWriter, r *http.Request) { 689 testMethod(t, r, "GET") 690 testFormValues(t, r, values{ 691 "anon": "true", 692 "page": "2", 693 }) 694 fmt.Fprint(w, `[{"contributions":42}]`) 695 }) 696 697 opts := &ListContributorsOptions{Anon: "true", ListOptions: ListOptions{Page: 2}} 698 ctx := context.Background() 699 contributors, _, err := client.Repositories.ListContributors(ctx, "o", "r", opts) 700 if err != nil { 701 t.Errorf("Repositories.ListContributors returned error: %v", err) 702 } 703 704 want := []*Contributor{{Contributions: Int(42)}} 705 if !cmp.Equal(contributors, want) { 706 t.Errorf("Repositories.ListContributors returned %+v, want %+v", contributors, want) 707 } 708 709 const methodName = "ListContributors" 710 testBadOptions(t, methodName, func() (err error) { 711 _, _, err = client.Repositories.ListContributors(ctx, "\n", "\n", opts) 712 return err 713 }) 714 715 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 716 got, resp, err := client.Repositories.ListContributors(ctx, "o", "r", opts) 717 if got != nil { 718 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 719 } 720 return resp, err 721 }) 722 } 723 724 func TestRepositoriesService_ListLanguages(t *testing.T) { 725 client, mux, _, teardown := setup() 726 defer teardown() 727 728 mux.HandleFunc("/repos/o/r/languages", func(w http.ResponseWriter, r *http.Request) { 729 testMethod(t, r, "GET") 730 fmt.Fprint(w, `{"go":1}`) 731 }) 732 733 ctx := context.Background() 734 languages, _, err := client.Repositories.ListLanguages(ctx, "o", "r") 735 if err != nil { 736 t.Errorf("Repositories.ListLanguages returned error: %v", err) 737 } 738 739 want := map[string]int{"go": 1} 740 if !cmp.Equal(languages, want) { 741 t.Errorf("Repositories.ListLanguages returned %+v, want %+v", languages, want) 742 } 743 744 const methodName = "ListLanguages" 745 testBadOptions(t, methodName, func() (err error) { 746 _, _, err = client.Repositories.ListLanguages(ctx, "\n", "\n") 747 return err 748 }) 749 750 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 751 got, resp, err := client.Repositories.ListLanguages(ctx, "o", "r") 752 if got != nil { 753 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 754 } 755 return resp, err 756 }) 757 } 758 759 func TestRepositoriesService_ListTeams(t *testing.T) { 760 client, mux, _, teardown := setup() 761 defer teardown() 762 763 mux.HandleFunc("/repos/o/r/teams", func(w http.ResponseWriter, r *http.Request) { 764 testMethod(t, r, "GET") 765 testFormValues(t, r, values{"page": "2"}) 766 fmt.Fprint(w, `[{"id":1}]`) 767 }) 768 769 opt := &ListOptions{Page: 2} 770 ctx := context.Background() 771 teams, _, err := client.Repositories.ListTeams(ctx, "o", "r", opt) 772 if err != nil { 773 t.Errorf("Repositories.ListTeams returned error: %v", err) 774 } 775 776 want := []*Team{{ID: Int64(1)}} 777 if !cmp.Equal(teams, want) { 778 t.Errorf("Repositories.ListTeams returned %+v, want %+v", teams, want) 779 } 780 781 const methodName = "ListTeams" 782 testBadOptions(t, methodName, func() (err error) { 783 _, _, err = client.Repositories.ListTeams(ctx, "\n", "\n", opt) 784 return err 785 }) 786 787 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 788 got, resp, err := client.Repositories.ListTeams(ctx, "o", "r", opt) 789 if got != nil { 790 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 791 } 792 return resp, err 793 }) 794 } 795 796 func TestRepositoriesService_ListTags(t *testing.T) { 797 client, mux, _, teardown := setup() 798 defer teardown() 799 800 mux.HandleFunc("/repos/o/r/tags", func(w http.ResponseWriter, r *http.Request) { 801 testMethod(t, r, "GET") 802 testFormValues(t, r, values{"page": "2"}) 803 fmt.Fprint(w, `[{"name":"n", "commit" : {"sha" : "s", "url" : "u"}, "zipball_url": "z", "tarball_url": "t"}]`) 804 }) 805 806 opt := &ListOptions{Page: 2} 807 ctx := context.Background() 808 tags, _, err := client.Repositories.ListTags(ctx, "o", "r", opt) 809 if err != nil { 810 t.Errorf("Repositories.ListTags returned error: %v", err) 811 } 812 813 want := []*RepositoryTag{ 814 { 815 Name: String("n"), 816 Commit: &Commit{ 817 SHA: String("s"), 818 URL: String("u"), 819 }, 820 ZipballURL: String("z"), 821 TarballURL: String("t"), 822 }, 823 } 824 if !cmp.Equal(tags, want) { 825 t.Errorf("Repositories.ListTags returned %+v, want %+v", tags, want) 826 } 827 828 const methodName = "ListTags" 829 testBadOptions(t, methodName, func() (err error) { 830 _, _, err = client.Repositories.ListTags(ctx, "\n", "\n", opt) 831 return err 832 }) 833 834 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 835 got, resp, err := client.Repositories.ListTags(ctx, "o", "r", opt) 836 if got != nil { 837 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 838 } 839 return resp, err 840 }) 841 } 842 843 func TestRepositoriesService_ListBranches(t *testing.T) { 844 client, mux, _, teardown := setup() 845 defer teardown() 846 847 mux.HandleFunc("/repos/o/r/branches", func(w http.ResponseWriter, r *http.Request) { 848 testMethod(t, r, "GET") 849 testFormValues(t, r, values{"page": "2"}) 850 fmt.Fprint(w, `[{"name":"master", "commit" : {"sha" : "a57781", "url" : "https://api.github.com/repos/o/r/commits/a57781"}}]`) 851 }) 852 853 opt := &BranchListOptions{ 854 Protected: nil, 855 ListOptions: ListOptions{Page: 2}, 856 } 857 ctx := context.Background() 858 branches, _, err := client.Repositories.ListBranches(ctx, "o", "r", opt) 859 if err != nil { 860 t.Errorf("Repositories.ListBranches returned error: %v", err) 861 } 862 863 want := []*Branch{{Name: String("master"), Commit: &RepositoryCommit{SHA: String("a57781"), URL: String("https://api.github.com/repos/o/r/commits/a57781")}}} 864 if !cmp.Equal(branches, want) { 865 t.Errorf("Repositories.ListBranches returned %+v, want %+v", branches, want) 866 } 867 868 const methodName = "ListBranches" 869 testBadOptions(t, methodName, func() (err error) { 870 _, _, err = client.Repositories.ListBranches(ctx, "\n", "\n", opt) 871 return err 872 }) 873 874 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 875 got, resp, err := client.Repositories.ListBranches(ctx, "o", "r", opt) 876 if got != nil { 877 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 878 } 879 return resp, err 880 }) 881 } 882 883 func TestRepositoriesService_GetBranch(t *testing.T) { 884 client, mux, _, teardown := setup() 885 defer teardown() 886 887 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) { 888 testMethod(t, r, "GET") 889 fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true}`) 890 }) 891 892 ctx := context.Background() 893 branch, _, err := client.Repositories.GetBranch(ctx, "o", "r", "b", false) 894 if err != nil { 895 t.Errorf("Repositories.GetBranch returned error: %v", err) 896 } 897 898 want := &Branch{ 899 Name: String("n"), 900 Commit: &RepositoryCommit{ 901 SHA: String("s"), 902 Commit: &Commit{ 903 Message: String("m"), 904 }, 905 }, 906 Protected: Bool(true), 907 } 908 909 if !cmp.Equal(branch, want) { 910 t.Errorf("Repositories.GetBranch returned %+v, want %+v", branch, want) 911 } 912 913 const methodName = "GetBranch" 914 testBadOptions(t, methodName, func() (err error) { 915 _, _, err = client.Repositories.GetBranch(ctx, "\n", "\n", "\n", false) 916 return err 917 }) 918 } 919 920 func TestRepositoriesService_GetBranch_BadJSONResponse(t *testing.T) { 921 client, mux, _, teardown := setup() 922 defer teardown() 923 924 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) { 925 testMethod(t, r, "GET") 926 fmt.Fprint(w, `{"name":"n", "commit":{"sha":...truncated`) 927 }) 928 929 ctx := context.Background() 930 if _, _, err := client.Repositories.GetBranch(ctx, "o", "r", "b", false); err == nil { 931 t.Error("Repositories.GetBranch returned no error; wanted JSON error") 932 } 933 } 934 935 func TestRepositoriesService_GetBranch_StatusMovedPermanently_followRedirects(t *testing.T) { 936 client, mux, serverURL, teardown := setup() 937 defer teardown() 938 939 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) { 940 testMethod(t, r, "GET") 941 redirectURL, _ := url.Parse(serverURL + baseURLPath + "/repos/o/r/branches/br") 942 http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently) 943 }) 944 mux.HandleFunc("/repos/o/r/branches/br", func(w http.ResponseWriter, r *http.Request) { 945 testMethod(t, r, "GET") 946 fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true}`) 947 }) 948 ctx := context.Background() 949 branch, resp, err := client.Repositories.GetBranch(ctx, "o", "r", "b", true) 950 if err != nil { 951 t.Errorf("Repositories.GetBranch returned error: %v", err) 952 } 953 if resp.StatusCode != http.StatusOK { 954 t.Errorf("Repositories.GetBranch returned status: %d, want %d", resp.StatusCode, http.StatusOK) 955 } 956 957 want := &Branch{ 958 Name: String("n"), 959 Commit: &RepositoryCommit{ 960 SHA: String("s"), 961 Commit: &Commit{ 962 Message: String("m"), 963 }, 964 }, 965 Protected: Bool(true), 966 } 967 if !cmp.Equal(branch, want) { 968 t.Errorf("Repositories.GetBranch returned %+v, want %+v", branch, want) 969 } 970 } 971 972 func TestRepositoriesService_GetBranch_notFound(t *testing.T) { 973 client, mux, _, teardown := setup() 974 defer teardown() 975 976 mux.HandleFunc("/repos/o/r/branches/b", func(w http.ResponseWriter, r *http.Request) { 977 testMethod(t, r, "GET") 978 http.Error(w, "branch not found", http.StatusNotFound) 979 }) 980 ctx := context.Background() 981 _, resp, err := client.Repositories.GetBranch(ctx, "o", "r", "b", true) 982 if err == nil { 983 t.Error("Repositories.GetBranch returned error: nil") 984 } 985 if resp.StatusCode != http.StatusNotFound { 986 t.Errorf("Repositories.GetBranch returned status: %d, want %d", resp.StatusCode, http.StatusNotFound) 987 } 988 989 // Add custom round tripper 990 client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) { 991 return nil, errors.New("failed to get branch") 992 }) 993 994 const methodName = "GetBranch" 995 testBadOptions(t, methodName, func() (err error) { 996 _, _, err = client.Repositories.GetBranch(ctx, "\n", "\n", "\n", true) 997 return err 998 }) 999 } 1000 1001 func TestRepositoriesService_RenameBranch(t *testing.T) { 1002 client, mux, _, teardown := setup() 1003 defer teardown() 1004 1005 renameBranchReq := "nn" 1006 1007 mux.HandleFunc("/repos/o/r/branches/b/rename", func(w http.ResponseWriter, r *http.Request) { 1008 v := new(renameBranchRequest) 1009 json.NewDecoder(r.Body).Decode(v) 1010 1011 testMethod(t, r, "POST") 1012 want := &renameBranchRequest{NewName: "nn"} 1013 if !cmp.Equal(v, want) { 1014 t.Errorf("Request body = %+v, want %+v", v, want) 1015 } 1016 1017 fmt.Fprint(w, `{"protected":true,"name":"nn"}`) 1018 }) 1019 1020 ctx := context.Background() 1021 got, _, err := client.Repositories.RenameBranch(ctx, "o", "r", "b", renameBranchReq) 1022 if err != nil { 1023 t.Errorf("Repositories.RenameBranch returned error: %v", err) 1024 } 1025 1026 want := &Branch{Name: String("nn"), Protected: Bool(true)} 1027 if !cmp.Equal(got, want) { 1028 t.Errorf("Repositories.RenameBranch returned %+v, want %+v", got, want) 1029 } 1030 1031 const methodName = "RenameBranch" 1032 testBadOptions(t, methodName, func() (err error) { 1033 _, _, err = client.Repositories.RenameBranch(ctx, "\n", "\n", "\n", renameBranchReq) 1034 return err 1035 }) 1036 1037 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1038 got, resp, err := client.Repositories.RenameBranch(ctx, "o", "r", "b", renameBranchReq) 1039 if got != nil { 1040 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1041 } 1042 return resp, err 1043 }) 1044 } 1045 1046 func TestRepositoriesService_GetBranchProtection(t *testing.T) { 1047 client, mux, _, teardown := setup() 1048 defer teardown() 1049 1050 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { 1051 v := new(ProtectionRequest) 1052 json.NewDecoder(r.Body).Decode(v) 1053 1054 testMethod(t, r, "GET") 1055 // TODO: remove custom Accept header when this API fully launches 1056 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) 1057 fmt.Fprintf(w, `{ 1058 "required_status_checks":{ 1059 "strict":true, 1060 "contexts":["continuous-integration"], 1061 "checks": [ 1062 { 1063 "context": "continuous-integration", 1064 "app_id": null 1065 } 1066 ] 1067 }, 1068 "required_pull_request_reviews":{ 1069 "dismissal_restrictions":{ 1070 "users":[{ 1071 "id":3, 1072 "login":"u" 1073 }], 1074 "teams":[{ 1075 "id":4, 1076 "slug":"t" 1077 }], 1078 "apps":[{ 1079 "id":5, 1080 "slug":"a" 1081 }] 1082 }, 1083 "dismiss_stale_reviews":true, 1084 "require_code_owner_reviews":true, 1085 "require_last_push_approval":false, 1086 "required_approving_review_count":1 1087 }, 1088 "enforce_admins":{ 1089 "url":"/repos/o/r/branches/b/protection/enforce_admins", 1090 "enabled":true 1091 }, 1092 "restrictions":{ 1093 "users":[{"id":1,"login":"u"}], 1094 "teams":[{"id":2,"slug":"t"}], 1095 "apps":[{"id":3,"slug":"a"}] 1096 }, 1097 "required_conversation_resolution": { 1098 "enabled": true 1099 }, 1100 "block_creations": { 1101 "enabled": false 1102 }, 1103 "lock_branch": { 1104 "enabled": false 1105 }, 1106 "allow_fork_syncing": { 1107 "enabled": false 1108 } 1109 }`) 1110 }) 1111 1112 ctx := context.Background() 1113 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b") 1114 if err != nil { 1115 t.Errorf("Repositories.GetBranchProtection returned error: %v", err) 1116 } 1117 1118 want := &Protection{ 1119 RequiredStatusChecks: &RequiredStatusChecks{ 1120 Strict: true, 1121 Contexts: []string{"continuous-integration"}, 1122 Checks: []*RequiredStatusCheck{ 1123 { 1124 Context: "continuous-integration", 1125 }, 1126 }, 1127 }, 1128 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{ 1129 DismissStaleReviews: true, 1130 DismissalRestrictions: &DismissalRestrictions{ 1131 Users: []*User{ 1132 {Login: String("u"), ID: Int64(3)}, 1133 }, 1134 Teams: []*Team{ 1135 {Slug: String("t"), ID: Int64(4)}, 1136 }, 1137 Apps: []*App{ 1138 {Slug: String("a"), ID: Int64(5)}, 1139 }, 1140 }, 1141 RequireCodeOwnerReviews: true, 1142 RequiredApprovingReviewCount: 1, 1143 RequireLastPushApproval: false, 1144 }, 1145 EnforceAdmins: &AdminEnforcement{ 1146 URL: String("/repos/o/r/branches/b/protection/enforce_admins"), 1147 Enabled: true, 1148 }, 1149 Restrictions: &BranchRestrictions{ 1150 Users: []*User{ 1151 {Login: String("u"), ID: Int64(1)}, 1152 }, 1153 Teams: []*Team{ 1154 {Slug: String("t"), ID: Int64(2)}, 1155 }, 1156 Apps: []*App{ 1157 {Slug: String("a"), ID: Int64(3)}, 1158 }, 1159 }, 1160 RequiredConversationResolution: &RequiredConversationResolution{ 1161 Enabled: true, 1162 }, 1163 BlockCreations: &BlockCreations{ 1164 Enabled: Bool(false), 1165 }, 1166 LockBranch: &LockBranch{ 1167 Enabled: Bool(false), 1168 }, 1169 AllowForkSyncing: &AllowForkSyncing{ 1170 Enabled: Bool(false), 1171 }, 1172 } 1173 if !cmp.Equal(protection, want) { 1174 t.Errorf("Repositories.GetBranchProtection returned %+v, want %+v", protection, want) 1175 } 1176 1177 const methodName = "GetBranchProtection" 1178 testBadOptions(t, methodName, func() (err error) { 1179 _, _, err = client.Repositories.GetBranchProtection(ctx, "\n", "\n", "\n") 1180 return err 1181 }) 1182 1183 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1184 got, resp, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b") 1185 if got != nil { 1186 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1187 } 1188 return resp, err 1189 }) 1190 } 1191 1192 func TestRepositoriesService_GetBranchProtection_noDismissalRestrictions(t *testing.T) { 1193 client, mux, _, teardown := setup() 1194 defer teardown() 1195 1196 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { 1197 testMethod(t, r, "GET") 1198 // TODO: remove custom Accept header when this API fully launches 1199 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) 1200 fmt.Fprintf(w, `{ 1201 "required_status_checks":{ 1202 "strict":true, 1203 "contexts":["continuous-integration"], 1204 "checks": [ 1205 { 1206 "context": "continuous-integration", 1207 "app_id": null 1208 } 1209 ] 1210 }, 1211 "required_pull_request_reviews":{ 1212 "dismiss_stale_reviews":true, 1213 "require_code_owner_reviews":true, 1214 "required_approving_review_count":1 1215 }, 1216 "enforce_admins":{ 1217 "url":"/repos/o/r/branches/b/protection/enforce_admins", 1218 "enabled":true 1219 }, 1220 "restrictions":{ 1221 "users":[{"id":1,"login":"u"}], 1222 "teams":[{"id":2,"slug":"t"}] 1223 } 1224 }`) 1225 }) 1226 1227 ctx := context.Background() 1228 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b") 1229 if err != nil { 1230 t.Errorf("Repositories.GetBranchProtection returned error: %v", err) 1231 } 1232 1233 want := &Protection{ 1234 RequiredStatusChecks: &RequiredStatusChecks{ 1235 Strict: true, 1236 Contexts: []string{"continuous-integration"}, 1237 Checks: []*RequiredStatusCheck{ 1238 { 1239 Context: "continuous-integration", 1240 }, 1241 }, 1242 }, 1243 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{ 1244 DismissStaleReviews: true, 1245 DismissalRestrictions: nil, 1246 RequireCodeOwnerReviews: true, 1247 RequiredApprovingReviewCount: 1, 1248 }, 1249 EnforceAdmins: &AdminEnforcement{ 1250 URL: String("/repos/o/r/branches/b/protection/enforce_admins"), 1251 Enabled: true, 1252 }, 1253 Restrictions: &BranchRestrictions{ 1254 Users: []*User{ 1255 {Login: String("u"), ID: Int64(1)}, 1256 }, 1257 Teams: []*Team{ 1258 {Slug: String("t"), ID: Int64(2)}, 1259 }, 1260 }, 1261 } 1262 if !cmp.Equal(protection, want) { 1263 t.Errorf("Repositories.GetBranchProtection returned %+v, want %+v", protection, want) 1264 } 1265 } 1266 1267 func TestRepositoriesService_GetBranchProtection_branchNotProtected(t *testing.T) { 1268 client, mux, _, teardown := setup() 1269 defer teardown() 1270 1271 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { 1272 testMethod(t, r, "GET") 1273 1274 w.WriteHeader(http.StatusBadRequest) 1275 fmt.Fprintf(w, `{ 1276 "message": %q, 1277 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection" 1278 }`, githubBranchNotProtected) 1279 }) 1280 1281 ctx := context.Background() 1282 protection, _, err := client.Repositories.GetBranchProtection(ctx, "o", "r", "b") 1283 1284 if protection != nil { 1285 t.Errorf("Repositories.GetBranchProtection returned non-nil protection data") 1286 } 1287 1288 if err != ErrBranchNotProtected { 1289 t.Errorf("Repositories.GetBranchProtection returned an invalid error: %v", err) 1290 } 1291 } 1292 1293 func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) { 1294 client, mux, _, teardown := setup() 1295 defer teardown() 1296 1297 input := &ProtectionRequest{ 1298 RequiredStatusChecks: &RequiredStatusChecks{ 1299 Strict: true, 1300 Contexts: []string{"continuous-integration"}, 1301 }, 1302 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{ 1303 DismissStaleReviews: true, 1304 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{ 1305 Users: &[]string{"uu"}, 1306 Teams: &[]string{"tt"}, 1307 Apps: &[]string{"aa"}, 1308 }, 1309 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{ 1310 Users: []string{"uuu"}, 1311 Teams: []string{"ttt"}, 1312 Apps: []string{"aaa"}, 1313 }, 1314 }, 1315 Restrictions: &BranchRestrictionsRequest{ 1316 Users: []string{"u"}, 1317 Teams: []string{"t"}, 1318 Apps: []string{"a"}, 1319 }, 1320 BlockCreations: Bool(true), 1321 LockBranch: Bool(true), 1322 AllowForkSyncing: Bool(true), 1323 } 1324 1325 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { 1326 v := new(ProtectionRequest) 1327 json.NewDecoder(r.Body).Decode(v) 1328 1329 testMethod(t, r, "PUT") 1330 if !cmp.Equal(v, input) { 1331 t.Errorf("Request body = %+v, want %+v", v, input) 1332 } 1333 1334 // TODO: remove custom Accept header when this API fully launches 1335 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) 1336 fmt.Fprintf(w, `{ 1337 "required_status_checks":{ 1338 "strict":true, 1339 "contexts":["continuous-integration"], 1340 "checks": [ 1341 { 1342 "context": "continuous-integration", 1343 "app_id": null 1344 } 1345 ] 1346 }, 1347 "required_pull_request_reviews":{ 1348 "dismissal_restrictions":{ 1349 "users":[{ 1350 "id":3, 1351 "login":"uu" 1352 }], 1353 "teams":[{ 1354 "id":4, 1355 "slug":"tt" 1356 }], 1357 "apps":[{ 1358 "id":5, 1359 "slug":"aa" 1360 }] 1361 }, 1362 "dismiss_stale_reviews":true, 1363 "require_code_owner_reviews":true, 1364 "bypass_pull_request_allowances": { 1365 "users":[{"id":10,"login":"uuu"}], 1366 "teams":[{"id":20,"slug":"ttt"}], 1367 "apps":[{"id":30,"slug":"aaa"}] 1368 } 1369 }, 1370 "restrictions":{ 1371 "users":[{"id":1,"login":"u"}], 1372 "teams":[{"id":2,"slug":"t"}], 1373 "apps":[{"id":3,"slug":"a"}] 1374 }, 1375 "block_creations": { 1376 "enabled": true 1377 }, 1378 "lock_branch": { 1379 "enabled": true 1380 }, 1381 "allow_fork_syncing": { 1382 "enabled": true 1383 } 1384 }`) 1385 }) 1386 1387 ctx := context.Background() 1388 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input) 1389 if err != nil { 1390 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err) 1391 } 1392 1393 want := &Protection{ 1394 RequiredStatusChecks: &RequiredStatusChecks{ 1395 Strict: true, 1396 Contexts: []string{"continuous-integration"}, 1397 Checks: []*RequiredStatusCheck{ 1398 { 1399 Context: "continuous-integration", 1400 }, 1401 }, 1402 }, 1403 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{ 1404 DismissStaleReviews: true, 1405 DismissalRestrictions: &DismissalRestrictions{ 1406 Users: []*User{ 1407 {Login: String("uu"), ID: Int64(3)}, 1408 }, 1409 Teams: []*Team{ 1410 {Slug: String("tt"), ID: Int64(4)}, 1411 }, 1412 Apps: []*App{ 1413 {Slug: String("aa"), ID: Int64(5)}, 1414 }, 1415 }, 1416 RequireCodeOwnerReviews: true, 1417 BypassPullRequestAllowances: &BypassPullRequestAllowances{ 1418 Users: []*User{ 1419 {Login: String("uuu"), ID: Int64(10)}, 1420 }, 1421 Teams: []*Team{ 1422 {Slug: String("ttt"), ID: Int64(20)}, 1423 }, 1424 Apps: []*App{ 1425 {Slug: String("aaa"), ID: Int64(30)}, 1426 }, 1427 }, 1428 }, 1429 Restrictions: &BranchRestrictions{ 1430 Users: []*User{ 1431 {Login: String("u"), ID: Int64(1)}, 1432 }, 1433 Teams: []*Team{ 1434 {Slug: String("t"), ID: Int64(2)}, 1435 }, 1436 Apps: []*App{ 1437 {Slug: String("a"), ID: Int64(3)}, 1438 }, 1439 }, 1440 BlockCreations: &BlockCreations{ 1441 Enabled: Bool(true), 1442 }, 1443 LockBranch: &LockBranch{ 1444 Enabled: Bool(true), 1445 }, 1446 AllowForkSyncing: &AllowForkSyncing{ 1447 Enabled: Bool(true), 1448 }, 1449 } 1450 if !cmp.Equal(protection, want) { 1451 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want) 1452 } 1453 1454 const methodName = "UpdateBranchProtection" 1455 testBadOptions(t, methodName, func() (err error) { 1456 _, _, err = client.Repositories.UpdateBranchProtection(ctx, "\n", "\n", "\n", input) 1457 return err 1458 }) 1459 1460 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1461 got, resp, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input) 1462 if got != nil { 1463 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1464 } 1465 return resp, err 1466 }) 1467 } 1468 1469 func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) { 1470 client, mux, _, teardown := setup() 1471 defer teardown() 1472 1473 input := &ProtectionRequest{ 1474 RequiredStatusChecks: &RequiredStatusChecks{ 1475 Strict: true, 1476 Checks: []*RequiredStatusCheck{ 1477 { 1478 Context: "continuous-integration", 1479 }, 1480 }, 1481 }, 1482 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{ 1483 DismissStaleReviews: true, 1484 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{ 1485 Users: &[]string{"uu"}, 1486 Teams: &[]string{"tt"}, 1487 Apps: &[]string{"aa"}, 1488 }, 1489 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{ 1490 Users: []string{"uuu"}, 1491 Teams: []string{"ttt"}, 1492 Apps: []string{"aaa"}, 1493 }, 1494 }, 1495 Restrictions: &BranchRestrictionsRequest{ 1496 Users: []string{"u"}, 1497 Teams: []string{"t"}, 1498 Apps: []string{"a"}, 1499 }, 1500 } 1501 1502 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { 1503 v := new(ProtectionRequest) 1504 json.NewDecoder(r.Body).Decode(v) 1505 1506 testMethod(t, r, "PUT") 1507 if !cmp.Equal(v, input) { 1508 t.Errorf("Request body = %+v, want %+v", v, input) 1509 } 1510 1511 // TODO: remove custom Accept header when this API fully launches 1512 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) 1513 fmt.Fprintf(w, `{ 1514 "required_status_checks":{ 1515 "strict":true, 1516 "contexts":["continuous-integration"], 1517 "checks": [ 1518 { 1519 "context": "continuous-integration", 1520 "app_id": null 1521 } 1522 ] 1523 }, 1524 "required_pull_request_reviews":{ 1525 "dismissal_restrictions":{ 1526 "users":[{ 1527 "id":3, 1528 "login":"uu" 1529 }], 1530 "teams":[{ 1531 "id":4, 1532 "slug":"tt" 1533 }], 1534 "apps":[{ 1535 "id":5, 1536 "slug":"aa" 1537 }] 1538 }, 1539 "dismiss_stale_reviews":true, 1540 "require_code_owner_reviews":true, 1541 "bypass_pull_request_allowances": { 1542 "users":[{"id":10,"login":"uuu"}], 1543 "teams":[{"id":20,"slug":"ttt"}], 1544 "apps":[{"id":30,"slug":"aaa"}] 1545 } 1546 }, 1547 "restrictions":{ 1548 "users":[{"id":1,"login":"u"}], 1549 "teams":[{"id":2,"slug":"t"}], 1550 "apps":[{"id":3,"slug":"a"}] 1551 } 1552 }`) 1553 }) 1554 1555 ctx := context.Background() 1556 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input) 1557 if err != nil { 1558 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err) 1559 } 1560 1561 want := &Protection{ 1562 RequiredStatusChecks: &RequiredStatusChecks{ 1563 Strict: true, 1564 Contexts: []string{"continuous-integration"}, 1565 Checks: []*RequiredStatusCheck{ 1566 { 1567 Context: "continuous-integration", 1568 }, 1569 }, 1570 }, 1571 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{ 1572 DismissStaleReviews: true, 1573 DismissalRestrictions: &DismissalRestrictions{ 1574 Users: []*User{ 1575 {Login: String("uu"), ID: Int64(3)}, 1576 }, 1577 Teams: []*Team{ 1578 {Slug: String("tt"), ID: Int64(4)}, 1579 }, 1580 Apps: []*App{ 1581 {Slug: String("aa"), ID: Int64(5)}, 1582 }, 1583 }, 1584 RequireCodeOwnerReviews: true, 1585 BypassPullRequestAllowances: &BypassPullRequestAllowances{ 1586 Users: []*User{ 1587 {Login: String("uuu"), ID: Int64(10)}, 1588 }, 1589 Teams: []*Team{ 1590 {Slug: String("ttt"), ID: Int64(20)}, 1591 }, 1592 Apps: []*App{ 1593 {Slug: String("aaa"), ID: Int64(30)}, 1594 }, 1595 }, 1596 }, 1597 Restrictions: &BranchRestrictions{ 1598 Users: []*User{ 1599 {Login: String("u"), ID: Int64(1)}, 1600 }, 1601 Teams: []*Team{ 1602 {Slug: String("t"), ID: Int64(2)}, 1603 }, 1604 Apps: []*App{ 1605 {Slug: String("a"), ID: Int64(3)}, 1606 }, 1607 }, 1608 } 1609 if !cmp.Equal(protection, want) { 1610 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want) 1611 } 1612 } 1613 1614 func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T) { 1615 client, mux, _, teardown := setup() 1616 defer teardown() 1617 1618 input := &ProtectionRequest{ 1619 RequiredStatusChecks: &RequiredStatusChecks{ 1620 Strict: true, 1621 Checks: []*RequiredStatusCheck{}, 1622 }, 1623 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{ 1624 DismissStaleReviews: true, 1625 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{ 1626 Users: &[]string{"uu"}, 1627 Teams: &[]string{"tt"}, 1628 Apps: &[]string{"aa"}, 1629 }, 1630 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{ 1631 Users: []string{"uuu"}, 1632 Teams: []string{"ttt"}, 1633 Apps: []string{"aaa"}, 1634 }, 1635 }, 1636 Restrictions: &BranchRestrictionsRequest{ 1637 Users: []string{"u"}, 1638 Teams: []string{"t"}, 1639 Apps: []string{"a"}, 1640 }, 1641 } 1642 1643 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { 1644 v := new(ProtectionRequest) 1645 json.NewDecoder(r.Body).Decode(v) 1646 1647 testMethod(t, r, "PUT") 1648 if !cmp.Equal(v, input) { 1649 t.Errorf("Request body = %+v, want %+v", v, input) 1650 } 1651 1652 // TODO: remove custom Accept header when this API fully launches 1653 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) 1654 fmt.Fprintf(w, `{ 1655 "required_status_checks":{ 1656 "strict":true, 1657 "contexts":[], 1658 "checks": [] 1659 }, 1660 "required_pull_request_reviews":{ 1661 "dismissal_restrictions":{ 1662 "users":[{ 1663 "id":3, 1664 "login":"uu" 1665 }], 1666 "teams":[{ 1667 "id":4, 1668 "slug":"tt" 1669 }], 1670 "apps":[{ 1671 "id":5, 1672 "slug":"aa" 1673 }] 1674 }, 1675 "dismiss_stale_reviews":true, 1676 "require_code_owner_reviews":true, 1677 "require_last_push_approval":false, 1678 "bypass_pull_request_allowances": { 1679 "users":[{"id":10,"login":"uuu"}], 1680 "teams":[{"id":20,"slug":"ttt"}], 1681 "apps":[{"id":30,"slug":"aaa"}] 1682 } 1683 }, 1684 "restrictions":{ 1685 "users":[{"id":1,"login":"u"}], 1686 "teams":[{"id":2,"slug":"t"}], 1687 "apps":[{"id":3,"slug":"a"}] 1688 } 1689 }`) 1690 }) 1691 1692 ctx := context.Background() 1693 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input) 1694 if err != nil { 1695 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err) 1696 } 1697 1698 want := &Protection{ 1699 RequiredStatusChecks: &RequiredStatusChecks{ 1700 Strict: true, 1701 Contexts: []string{}, 1702 Checks: []*RequiredStatusCheck{}, 1703 }, 1704 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{ 1705 DismissStaleReviews: true, 1706 DismissalRestrictions: &DismissalRestrictions{ 1707 Users: []*User{ 1708 {Login: String("uu"), ID: Int64(3)}, 1709 }, 1710 Teams: []*Team{ 1711 {Slug: String("tt"), ID: Int64(4)}, 1712 }, 1713 Apps: []*App{ 1714 {Slug: String("aa"), ID: Int64(5)}, 1715 }, 1716 }, 1717 RequireCodeOwnerReviews: true, 1718 BypassPullRequestAllowances: &BypassPullRequestAllowances{ 1719 Users: []*User{ 1720 {Login: String("uuu"), ID: Int64(10)}, 1721 }, 1722 Teams: []*Team{ 1723 {Slug: String("ttt"), ID: Int64(20)}, 1724 }, 1725 Apps: []*App{ 1726 {Slug: String("aaa"), ID: Int64(30)}, 1727 }, 1728 }, 1729 }, 1730 Restrictions: &BranchRestrictions{ 1731 Users: []*User{ 1732 {Login: String("u"), ID: Int64(1)}, 1733 }, 1734 Teams: []*Team{ 1735 {Slug: String("t"), ID: Int64(2)}, 1736 }, 1737 Apps: []*App{ 1738 {Slug: String("a"), ID: Int64(3)}, 1739 }, 1740 }, 1741 } 1742 if !cmp.Equal(protection, want) { 1743 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want) 1744 } 1745 } 1746 1747 func TestRepositoriesService_UpdateBranchProtection_RequireLastPushApproval(t *testing.T) { 1748 client, mux, _, teardown := setup() 1749 defer teardown() 1750 1751 input := &ProtectionRequest{ 1752 RequiredPullRequestReviews: &PullRequestReviewsEnforcementRequest{ 1753 RequireLastPushApproval: Bool(true), 1754 }, 1755 } 1756 1757 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { 1758 v := new(ProtectionRequest) 1759 json.NewDecoder(r.Body).Decode(v) 1760 1761 testMethod(t, r, "PUT") 1762 if !cmp.Equal(v, input) { 1763 t.Errorf("Request body = %+v, want %+v", v, input) 1764 } 1765 1766 fmt.Fprintf(w, `{ 1767 "required_pull_request_reviews":{ 1768 "require_last_push_approval":true 1769 } 1770 }`) 1771 }) 1772 1773 ctx := context.Background() 1774 protection, _, err := client.Repositories.UpdateBranchProtection(ctx, "o", "r", "b", input) 1775 if err != nil { 1776 t.Errorf("Repositories.UpdateBranchProtection returned error: %v", err) 1777 } 1778 1779 want := &Protection{ 1780 RequiredPullRequestReviews: &PullRequestReviewsEnforcement{ 1781 RequireLastPushApproval: true, 1782 }, 1783 } 1784 if !cmp.Equal(protection, want) { 1785 t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want) 1786 } 1787 } 1788 1789 func TestRepositoriesService_RemoveBranchProtection(t *testing.T) { 1790 client, mux, _, teardown := setup() 1791 defer teardown() 1792 1793 mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) { 1794 testMethod(t, r, "DELETE") 1795 w.WriteHeader(http.StatusNoContent) 1796 }) 1797 1798 ctx := context.Background() 1799 _, err := client.Repositories.RemoveBranchProtection(ctx, "o", "r", "b") 1800 if err != nil { 1801 t.Errorf("Repositories.RemoveBranchProtection returned error: %v", err) 1802 } 1803 1804 const methodName = "RemoveBranchProtection" 1805 testBadOptions(t, methodName, func() (err error) { 1806 _, err = client.Repositories.RemoveBranchProtection(ctx, "\n", "\n", "\n") 1807 return err 1808 }) 1809 1810 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1811 return client.Repositories.RemoveBranchProtection(ctx, "o", "r", "b") 1812 }) 1813 } 1814 1815 func TestRepositoriesService_ListLanguages_invalidOwner(t *testing.T) { 1816 client, _, _, teardown := setup() 1817 defer teardown() 1818 1819 ctx := context.Background() 1820 _, _, err := client.Repositories.ListLanguages(ctx, "%", "%") 1821 testURLParseError(t, err) 1822 } 1823 1824 func TestRepositoriesService_License(t *testing.T) { 1825 client, mux, _, teardown := setup() 1826 defer teardown() 1827 1828 mux.HandleFunc("/repos/o/r/license", func(w http.ResponseWriter, r *http.Request) { 1829 testMethod(t, r, "GET") 1830 fmt.Fprint(w, `{"name": "LICENSE", "path": "LICENSE", "license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","featured":true}}`) 1831 }) 1832 1833 ctx := context.Background() 1834 got, _, err := client.Repositories.License(ctx, "o", "r") 1835 if err != nil { 1836 t.Errorf("Repositories.License returned error: %v", err) 1837 } 1838 1839 want := &RepositoryLicense{ 1840 Name: String("LICENSE"), 1841 Path: String("LICENSE"), 1842 License: &License{ 1843 Name: String("MIT License"), 1844 Key: String("mit"), 1845 SPDXID: String("MIT"), 1846 URL: String("https://api.github.com/licenses/mit"), 1847 Featured: Bool(true), 1848 }, 1849 } 1850 1851 if !cmp.Equal(got, want) { 1852 t.Errorf("Repositories.License returned %+v, want %+v", got, want) 1853 } 1854 1855 const methodName = "License" 1856 testBadOptions(t, methodName, func() (err error) { 1857 _, _, err = client.Repositories.License(ctx, "\n", "\n") 1858 return err 1859 }) 1860 1861 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1862 got, resp, err := client.Repositories.License(ctx, "o", "r") 1863 if got != nil { 1864 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1865 } 1866 return resp, err 1867 }) 1868 } 1869 1870 func TestRepositoriesService_GetRequiredStatusChecks(t *testing.T) { 1871 client, mux, _, teardown := setup() 1872 defer teardown() 1873 1874 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) { 1875 v := new(ProtectionRequest) 1876 json.NewDecoder(r.Body).Decode(v) 1877 1878 testMethod(t, r, "GET") 1879 fmt.Fprint(w, `{ 1880 "strict": true, 1881 "contexts": ["x","y","z"], 1882 "checks": [ 1883 { 1884 "context": "x", 1885 "app_id": null 1886 }, 1887 { 1888 "context": "y", 1889 "app_id": null 1890 }, 1891 { 1892 "context": "z", 1893 "app_id": null 1894 } 1895 ] 1896 }`) 1897 }) 1898 1899 ctx := context.Background() 1900 checks, _, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b") 1901 if err != nil { 1902 t.Errorf("Repositories.GetRequiredStatusChecks returned error: %v", err) 1903 } 1904 1905 want := &RequiredStatusChecks{ 1906 Strict: true, 1907 Contexts: []string{"x", "y", "z"}, 1908 Checks: []*RequiredStatusCheck{ 1909 { 1910 Context: "x", 1911 }, 1912 { 1913 Context: "y", 1914 }, 1915 { 1916 Context: "z", 1917 }, 1918 }, 1919 } 1920 if !cmp.Equal(checks, want) { 1921 t.Errorf("Repositories.GetRequiredStatusChecks returned %+v, want %+v", checks, want) 1922 } 1923 1924 const methodName = "GetRequiredStatusChecks" 1925 testBadOptions(t, methodName, func() (err error) { 1926 _, _, err = client.Repositories.GetRequiredStatusChecks(ctx, "\n", "\n", "\n") 1927 return err 1928 }) 1929 1930 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1931 got, resp, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b") 1932 if got != nil { 1933 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1934 } 1935 return resp, err 1936 }) 1937 } 1938 1939 func TestRepositoriesService_GetRequiredStatusChecks_branchNotProtected(t *testing.T) { 1940 client, mux, _, teardown := setup() 1941 defer teardown() 1942 1943 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) { 1944 testMethod(t, r, "GET") 1945 1946 w.WriteHeader(http.StatusBadRequest) 1947 fmt.Fprintf(w, `{ 1948 "message": %q, 1949 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection" 1950 }`, githubBranchNotProtected) 1951 }) 1952 1953 ctx := context.Background() 1954 checks, _, err := client.Repositories.GetRequiredStatusChecks(ctx, "o", "r", "b") 1955 1956 if checks != nil { 1957 t.Errorf("Repositories.GetRequiredStatusChecks returned non-nil status-checks data") 1958 } 1959 1960 if err != ErrBranchNotProtected { 1961 t.Errorf("Repositories.GetRequiredStatusChecks returned an invalid error: %v", err) 1962 } 1963 } 1964 1965 func TestRepositoriesService_UpdateRequiredStatusChecks_Contexts(t *testing.T) { 1966 client, mux, _, teardown := setup() 1967 defer teardown() 1968 1969 input := &RequiredStatusChecksRequest{ 1970 Strict: Bool(true), 1971 Contexts: []string{"continuous-integration"}, 1972 } 1973 1974 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) { 1975 v := new(RequiredStatusChecksRequest) 1976 json.NewDecoder(r.Body).Decode(v) 1977 1978 testMethod(t, r, "PATCH") 1979 if !cmp.Equal(v, input) { 1980 t.Errorf("Request body = %+v, want %+v", v, input) 1981 } 1982 testHeader(t, r, "Accept", mediaTypeV3) 1983 fmt.Fprintf(w, `{ 1984 "strict":true, 1985 "contexts":["continuous-integration"], 1986 "checks": [ 1987 { 1988 "context": "continuous-integration", 1989 "app_id": null 1990 } 1991 ] 1992 }`) 1993 }) 1994 1995 ctx := context.Background() 1996 statusChecks, _, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input) 1997 if err != nil { 1998 t.Errorf("Repositories.UpdateRequiredStatusChecks returned error: %v", err) 1999 } 2000 2001 want := &RequiredStatusChecks{ 2002 Strict: true, 2003 Contexts: []string{"continuous-integration"}, 2004 Checks: []*RequiredStatusCheck{ 2005 { 2006 Context: "continuous-integration", 2007 }, 2008 }, 2009 } 2010 if !cmp.Equal(statusChecks, want) { 2011 t.Errorf("Repositories.UpdateRequiredStatusChecks returned %+v, want %+v", statusChecks, want) 2012 } 2013 2014 const methodName = "UpdateRequiredStatusChecks" 2015 testBadOptions(t, methodName, func() (err error) { 2016 _, _, err = client.Repositories.UpdateRequiredStatusChecks(ctx, "\n", "\n", "\n", input) 2017 return err 2018 }) 2019 2020 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2021 got, resp, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input) 2022 if got != nil { 2023 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2024 } 2025 return resp, err 2026 }) 2027 } 2028 2029 func TestRepositoriesService_UpdateRequiredStatusChecks_Checks(t *testing.T) { 2030 client, mux, _, teardown := setup() 2031 defer teardown() 2032 2033 appID := int64(123) 2034 noAppID := int64(-1) 2035 input := &RequiredStatusChecksRequest{ 2036 Strict: Bool(true), 2037 Checks: []*RequiredStatusCheck{ 2038 { 2039 Context: "continuous-integration", 2040 }, 2041 { 2042 Context: "continuous-integration2", 2043 AppID: &appID, 2044 }, 2045 { 2046 Context: "continuous-integration3", 2047 AppID: &noAppID, 2048 }, 2049 }, 2050 } 2051 2052 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) { 2053 v := new(RequiredStatusChecksRequest) 2054 json.NewDecoder(r.Body).Decode(v) 2055 2056 testMethod(t, r, "PATCH") 2057 if !cmp.Equal(v, input) { 2058 t.Errorf("Request body = %+v, want %+v", v, input) 2059 } 2060 testHeader(t, r, "Accept", mediaTypeV3) 2061 fmt.Fprintf(w, `{ 2062 "strict":true, 2063 "contexts":["continuous-integration"], 2064 "checks": [ 2065 { 2066 "context": "continuous-integration", 2067 "app_id": null 2068 }, 2069 { 2070 "context": "continuous-integration2", 2071 "app_id": 123 2072 }, 2073 { 2074 "context": "continuous-integration3", 2075 "app_id": null 2076 } 2077 ] 2078 }`) 2079 }) 2080 2081 ctx := context.Background() 2082 statusChecks, _, err := client.Repositories.UpdateRequiredStatusChecks(ctx, "o", "r", "b", input) 2083 if err != nil { 2084 t.Errorf("Repositories.UpdateRequiredStatusChecks returned error: %v", err) 2085 } 2086 2087 want := &RequiredStatusChecks{ 2088 Strict: true, 2089 Contexts: []string{"continuous-integration"}, 2090 Checks: []*RequiredStatusCheck{ 2091 { 2092 Context: "continuous-integration", 2093 }, 2094 { 2095 Context: "continuous-integration2", 2096 AppID: &appID, 2097 }, 2098 { 2099 Context: "continuous-integration3", 2100 }, 2101 }, 2102 } 2103 if !cmp.Equal(statusChecks, want) { 2104 t.Errorf("Repositories.UpdateRequiredStatusChecks returned %+v, want %+v", statusChecks, want) 2105 } 2106 } 2107 2108 func TestRepositoriesService_RemoveRequiredStatusChecks(t *testing.T) { 2109 client, mux, _, teardown := setup() 2110 defer teardown() 2111 2112 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks", func(w http.ResponseWriter, r *http.Request) { 2113 testMethod(t, r, "DELETE") 2114 testHeader(t, r, "Accept", mediaTypeV3) 2115 w.WriteHeader(http.StatusNoContent) 2116 }) 2117 2118 ctx := context.Background() 2119 _, err := client.Repositories.RemoveRequiredStatusChecks(ctx, "o", "r", "b") 2120 if err != nil { 2121 t.Errorf("Repositories.RemoveRequiredStatusChecks returned error: %v", err) 2122 } 2123 2124 const methodName = "RemoveRequiredStatusChecks" 2125 testBadOptions(t, methodName, func() (err error) { 2126 _, err = client.Repositories.RemoveRequiredStatusChecks(ctx, "\n", "\n", "\n") 2127 return err 2128 }) 2129 2130 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2131 return client.Repositories.RemoveRequiredStatusChecks(ctx, "o", "r", "b") 2132 }) 2133 } 2134 2135 func TestRepositoriesService_ListRequiredStatusChecksContexts(t *testing.T) { 2136 client, mux, _, teardown := setup() 2137 defer teardown() 2138 2139 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) { 2140 v := new(ProtectionRequest) 2141 json.NewDecoder(r.Body).Decode(v) 2142 2143 testMethod(t, r, "GET") 2144 fmt.Fprint(w, `["x", "y", "z"]`) 2145 }) 2146 2147 ctx := context.Background() 2148 contexts, _, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b") 2149 if err != nil { 2150 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned error: %v", err) 2151 } 2152 2153 want := []string{"x", "y", "z"} 2154 if !cmp.Equal(contexts, want) { 2155 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned %+v, want %+v", contexts, want) 2156 } 2157 2158 const methodName = "ListRequiredStatusChecksContexts" 2159 testBadOptions(t, methodName, func() (err error) { 2160 _, _, err = client.Repositories.ListRequiredStatusChecksContexts(ctx, "\n", "\n", "\n") 2161 return err 2162 }) 2163 2164 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2165 got, resp, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b") 2166 if got != nil { 2167 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2168 } 2169 return resp, err 2170 }) 2171 } 2172 2173 func TestRepositoriesService_ListRequiredStatusChecksContexts_branchNotProtected(t *testing.T) { 2174 client, mux, _, teardown := setup() 2175 defer teardown() 2176 2177 mux.HandleFunc("/repos/o/r/branches/b/protection/required_status_checks/contexts", func(w http.ResponseWriter, r *http.Request) { 2178 testMethod(t, r, "GET") 2179 2180 w.WriteHeader(http.StatusBadRequest) 2181 fmt.Fprintf(w, `{ 2182 "message": %q, 2183 "documentation_url": "https://docs.github.com/rest/repos#get-branch-protection" 2184 }`, githubBranchNotProtected) 2185 }) 2186 2187 ctx := context.Background() 2188 contexts, _, err := client.Repositories.ListRequiredStatusChecksContexts(ctx, "o", "r", "b") 2189 2190 if contexts != nil { 2191 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned non-nil contexts data") 2192 } 2193 2194 if err != ErrBranchNotProtected { 2195 t.Errorf("Repositories.ListRequiredStatusChecksContexts returned an invalid error: %v", err) 2196 } 2197 } 2198 2199 func TestRepositoriesService_GetPullRequestReviewEnforcement(t *testing.T) { 2200 client, mux, _, teardown := setup() 2201 defer teardown() 2202 2203 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) { 2204 testMethod(t, r, "GET") 2205 // TODO: remove custom Accept header when this API fully launches 2206 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) 2207 fmt.Fprintf(w, `{ 2208 "dismissal_restrictions":{ 2209 "users":[{"id":1,"login":"u"}], 2210 "teams":[{"id":2,"slug":"t"}], 2211 "apps":[{"id":3,"slug":"a"}] 2212 }, 2213 "dismiss_stale_reviews":true, 2214 "require_code_owner_reviews":true, 2215 "required_approving_review_count":1 2216 }`) 2217 }) 2218 2219 ctx := context.Background() 2220 enforcement, _, err := client.Repositories.GetPullRequestReviewEnforcement(ctx, "o", "r", "b") 2221 if err != nil { 2222 t.Errorf("Repositories.GetPullRequestReviewEnforcement returned error: %v", err) 2223 } 2224 2225 want := &PullRequestReviewsEnforcement{ 2226 DismissStaleReviews: true, 2227 DismissalRestrictions: &DismissalRestrictions{ 2228 Users: []*User{ 2229 {Login: String("u"), ID: Int64(1)}, 2230 }, 2231 Teams: []*Team{ 2232 {Slug: String("t"), ID: Int64(2)}, 2233 }, 2234 Apps: []*App{ 2235 {Slug: String("a"), ID: Int64(3)}, 2236 }, 2237 }, 2238 RequireCodeOwnerReviews: true, 2239 RequiredApprovingReviewCount: 1, 2240 } 2241 2242 if !cmp.Equal(enforcement, want) { 2243 t.Errorf("Repositories.GetPullRequestReviewEnforcement returned %+v, want %+v", enforcement, want) 2244 } 2245 2246 const methodName = "GetPullRequestReviewEnforcement" 2247 testBadOptions(t, methodName, func() (err error) { 2248 _, _, err = client.Repositories.GetPullRequestReviewEnforcement(ctx, "\n", "\n", "\n") 2249 return err 2250 }) 2251 2252 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2253 got, resp, err := client.Repositories.GetPullRequestReviewEnforcement(ctx, "o", "r", "b") 2254 if got != nil { 2255 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2256 } 2257 return resp, err 2258 }) 2259 } 2260 2261 func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) { 2262 client, mux, _, teardown := setup() 2263 defer teardown() 2264 2265 input := &PullRequestReviewsEnforcementUpdate{ 2266 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{ 2267 Users: &[]string{"u"}, 2268 Teams: &[]string{"t"}, 2269 Apps: &[]string{"a"}, 2270 }, 2271 } 2272 2273 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) { 2274 v := new(PullRequestReviewsEnforcementUpdate) 2275 json.NewDecoder(r.Body).Decode(v) 2276 2277 testMethod(t, r, "PATCH") 2278 if !cmp.Equal(v, input) { 2279 t.Errorf("Request body = %+v, want %+v", v, input) 2280 } 2281 // TODO: remove custom Accept header when this API fully launches 2282 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) 2283 fmt.Fprintf(w, `{ 2284 "dismissal_restrictions":{ 2285 "users":[{"id":1,"login":"u"}], 2286 "teams":[{"id":2,"slug":"t"}], 2287 "apps":[{"id":3,"slug":"a"}] 2288 }, 2289 "dismiss_stale_reviews":true, 2290 "require_code_owner_reviews":true, 2291 "required_approving_review_count":3 2292 }`) 2293 }) 2294 2295 ctx := context.Background() 2296 enforcement, _, err := client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "o", "r", "b", input) 2297 if err != nil { 2298 t.Errorf("Repositories.UpdatePullRequestReviewEnforcement returned error: %v", err) 2299 } 2300 2301 want := &PullRequestReviewsEnforcement{ 2302 DismissStaleReviews: true, 2303 DismissalRestrictions: &DismissalRestrictions{ 2304 Users: []*User{ 2305 {Login: String("u"), ID: Int64(1)}, 2306 }, 2307 Teams: []*Team{ 2308 {Slug: String("t"), ID: Int64(2)}, 2309 }, 2310 Apps: []*App{ 2311 {Slug: String("a"), ID: Int64(3)}, 2312 }, 2313 }, 2314 RequireCodeOwnerReviews: true, 2315 RequiredApprovingReviewCount: 3, 2316 } 2317 if !cmp.Equal(enforcement, want) { 2318 t.Errorf("Repositories.UpdatePullRequestReviewEnforcement returned %+v, want %+v", enforcement, want) 2319 } 2320 2321 const methodName = "UpdatePullRequestReviewEnforcement" 2322 testBadOptions(t, methodName, func() (err error) { 2323 _, _, err = client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "\n", "\n", "\n", input) 2324 return err 2325 }) 2326 2327 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2328 got, resp, err := client.Repositories.UpdatePullRequestReviewEnforcement(ctx, "o", "r", "b", input) 2329 if got != nil { 2330 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2331 } 2332 return resp, err 2333 }) 2334 } 2335 2336 func TestRepositoriesService_DisableDismissalRestrictions(t *testing.T) { 2337 client, mux, _, teardown := setup() 2338 defer teardown() 2339 2340 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) { 2341 testMethod(t, r, "PATCH") 2342 // TODO: remove custom Accept header when this API fully launches 2343 testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) 2344 testBody(t, r, `{"dismissal_restrictions":{}}`+"\n") 2345 fmt.Fprintf(w, `{"dismiss_stale_reviews":true,"require_code_owner_reviews":true,"required_approving_review_count":1}`) 2346 }) 2347 2348 ctx := context.Background() 2349 enforcement, _, err := client.Repositories.DisableDismissalRestrictions(ctx, "o", "r", "b") 2350 if err != nil { 2351 t.Errorf("Repositories.DisableDismissalRestrictions returned error: %v", err) 2352 } 2353 2354 want := &PullRequestReviewsEnforcement{ 2355 DismissStaleReviews: true, 2356 DismissalRestrictions: nil, 2357 RequireCodeOwnerReviews: true, 2358 RequiredApprovingReviewCount: 1, 2359 } 2360 if !cmp.Equal(enforcement, want) { 2361 t.Errorf("Repositories.DisableDismissalRestrictions returned %+v, want %+v", enforcement, want) 2362 } 2363 2364 const methodName = "DisableDismissalRestrictions" 2365 testBadOptions(t, methodName, func() (err error) { 2366 _, _, err = client.Repositories.DisableDismissalRestrictions(ctx, "\n", "\n", "\n") 2367 return err 2368 }) 2369 2370 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2371 got, resp, err := client.Repositories.DisableDismissalRestrictions(ctx, "o", "r", "b") 2372 if got != nil { 2373 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2374 } 2375 return resp, err 2376 }) 2377 } 2378 2379 func TestRepositoriesService_RemovePullRequestReviewEnforcement(t *testing.T) { 2380 client, mux, _, teardown := setup() 2381 defer teardown() 2382 2383 mux.HandleFunc("/repos/o/r/branches/b/protection/required_pull_request_reviews", func(w http.ResponseWriter, r *http.Request) { 2384 testMethod(t, r, "DELETE") 2385 w.WriteHeader(http.StatusNoContent) 2386 }) 2387 2388 ctx := context.Background() 2389 _, err := client.Repositories.RemovePullRequestReviewEnforcement(ctx, "o", "r", "b") 2390 if err != nil { 2391 t.Errorf("Repositories.RemovePullRequestReviewEnforcement returned error: %v", err) 2392 } 2393 2394 const methodName = "RemovePullRequestReviewEnforcement" 2395 testBadOptions(t, methodName, func() (err error) { 2396 _, err = client.Repositories.RemovePullRequestReviewEnforcement(ctx, "\n", "\n", "\n") 2397 return err 2398 }) 2399 2400 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2401 return client.Repositories.RemovePullRequestReviewEnforcement(ctx, "o", "r", "b") 2402 }) 2403 } 2404 2405 func TestRepositoriesService_GetAdminEnforcement(t *testing.T) { 2406 client, mux, _, teardown := setup() 2407 defer teardown() 2408 2409 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) { 2410 testMethod(t, r, "GET") 2411 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true}`) 2412 }) 2413 2414 ctx := context.Background() 2415 enforcement, _, err := client.Repositories.GetAdminEnforcement(ctx, "o", "r", "b") 2416 if err != nil { 2417 t.Errorf("Repositories.GetAdminEnforcement returned error: %v", err) 2418 } 2419 2420 want := &AdminEnforcement{ 2421 URL: String("/repos/o/r/branches/b/protection/enforce_admins"), 2422 Enabled: true, 2423 } 2424 2425 if !cmp.Equal(enforcement, want) { 2426 t.Errorf("Repositories.GetAdminEnforcement returned %+v, want %+v", enforcement, want) 2427 } 2428 2429 const methodName = "GetAdminEnforcement" 2430 testBadOptions(t, methodName, func() (err error) { 2431 _, _, err = client.Repositories.GetAdminEnforcement(ctx, "\n", "\n", "\n") 2432 return err 2433 }) 2434 2435 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2436 got, resp, err := client.Repositories.GetAdminEnforcement(ctx, "o", "r", "b") 2437 if got != nil { 2438 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2439 } 2440 return resp, err 2441 }) 2442 } 2443 2444 func TestRepositoriesService_AddAdminEnforcement(t *testing.T) { 2445 client, mux, _, teardown := setup() 2446 defer teardown() 2447 2448 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) { 2449 testMethod(t, r, "POST") 2450 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true}`) 2451 }) 2452 2453 ctx := context.Background() 2454 enforcement, _, err := client.Repositories.AddAdminEnforcement(ctx, "o", "r", "b") 2455 if err != nil { 2456 t.Errorf("Repositories.AddAdminEnforcement returned error: %v", err) 2457 } 2458 2459 want := &AdminEnforcement{ 2460 URL: String("/repos/o/r/branches/b/protection/enforce_admins"), 2461 Enabled: true, 2462 } 2463 if !cmp.Equal(enforcement, want) { 2464 t.Errorf("Repositories.AddAdminEnforcement returned %+v, want %+v", enforcement, want) 2465 } 2466 2467 const methodName = "AddAdminEnforcement" 2468 testBadOptions(t, methodName, func() (err error) { 2469 _, _, err = client.Repositories.AddAdminEnforcement(ctx, "\n", "\n", "\n") 2470 return err 2471 }) 2472 2473 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2474 got, resp, err := client.Repositories.AddAdminEnforcement(ctx, "o", "r", "b") 2475 if got != nil { 2476 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2477 } 2478 return resp, err 2479 }) 2480 } 2481 2482 func TestRepositoriesService_RemoveAdminEnforcement(t *testing.T) { 2483 client, mux, _, teardown := setup() 2484 defer teardown() 2485 2486 mux.HandleFunc("/repos/o/r/branches/b/protection/enforce_admins", func(w http.ResponseWriter, r *http.Request) { 2487 testMethod(t, r, "DELETE") 2488 w.WriteHeader(http.StatusNoContent) 2489 }) 2490 2491 ctx := context.Background() 2492 _, err := client.Repositories.RemoveAdminEnforcement(ctx, "o", "r", "b") 2493 if err != nil { 2494 t.Errorf("Repositories.RemoveAdminEnforcement returned error: %v", err) 2495 } 2496 2497 const methodName = "RemoveAdminEnforcement" 2498 testBadOptions(t, methodName, func() (err error) { 2499 _, err = client.Repositories.RemoveAdminEnforcement(ctx, "\n", "\n", "\n") 2500 return err 2501 }) 2502 2503 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2504 return client.Repositories.RemoveAdminEnforcement(ctx, "o", "r", "b") 2505 }) 2506 } 2507 2508 func TestRepositoriesService_GetSignaturesProtectedBranch(t *testing.T) { 2509 client, mux, _, teardown := setup() 2510 defer teardown() 2511 2512 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) { 2513 testMethod(t, r, "GET") 2514 testHeader(t, r, "Accept", mediaTypeSignaturePreview) 2515 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/required_signatures","enabled":false}`) 2516 }) 2517 2518 ctx := context.Background() 2519 signature, _, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", "b") 2520 if err != nil { 2521 t.Errorf("Repositories.GetSignaturesProtectedBranch returned error: %v", err) 2522 } 2523 2524 want := &SignaturesProtectedBranch{ 2525 URL: String("/repos/o/r/branches/b/protection/required_signatures"), 2526 Enabled: Bool(false), 2527 } 2528 2529 if !cmp.Equal(signature, want) { 2530 t.Errorf("Repositories.GetSignaturesProtectedBranch returned %+v, want %+v", signature, want) 2531 } 2532 2533 const methodName = "GetSignaturesProtectedBranch" 2534 testBadOptions(t, methodName, func() (err error) { 2535 _, _, err = client.Repositories.GetSignaturesProtectedBranch(ctx, "\n", "\n", "\n") 2536 return err 2537 }) 2538 2539 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2540 got, resp, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", "b") 2541 if got != nil { 2542 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2543 } 2544 return resp, err 2545 }) 2546 } 2547 2548 func TestRepositoriesService_RequireSignaturesOnProtectedBranch(t *testing.T) { 2549 client, mux, _, teardown := setup() 2550 defer teardown() 2551 2552 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) { 2553 testMethod(t, r, "POST") 2554 testHeader(t, r, "Accept", mediaTypeSignaturePreview) 2555 fmt.Fprintf(w, `{"url":"/repos/o/r/branches/b/protection/required_signatures","enabled":true}`) 2556 }) 2557 2558 ctx := context.Background() 2559 signature, _, err := client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "o", "r", "b") 2560 if err != nil { 2561 t.Errorf("Repositories.RequireSignaturesOnProtectedBranch returned error: %v", err) 2562 } 2563 2564 want := &SignaturesProtectedBranch{ 2565 URL: String("/repos/o/r/branches/b/protection/required_signatures"), 2566 Enabled: Bool(true), 2567 } 2568 2569 if !cmp.Equal(signature, want) { 2570 t.Errorf("Repositories.RequireSignaturesOnProtectedBranch returned %+v, want %+v", signature, want) 2571 } 2572 2573 const methodName = "RequireSignaturesOnProtectedBranch" 2574 testBadOptions(t, methodName, func() (err error) { 2575 _, _, err = client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "\n", "\n", "\n") 2576 return err 2577 }) 2578 2579 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2580 got, resp, err := client.Repositories.RequireSignaturesOnProtectedBranch(ctx, "o", "r", "b") 2581 if got != nil { 2582 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2583 } 2584 return resp, err 2585 }) 2586 } 2587 2588 func TestRepositoriesService_OptionalSignaturesOnProtectedBranch(t *testing.T) { 2589 client, mux, _, teardown := setup() 2590 defer teardown() 2591 2592 mux.HandleFunc("/repos/o/r/branches/b/protection/required_signatures", func(w http.ResponseWriter, r *http.Request) { 2593 testMethod(t, r, "DELETE") 2594 testHeader(t, r, "Accept", mediaTypeSignaturePreview) 2595 w.WriteHeader(http.StatusNoContent) 2596 }) 2597 2598 ctx := context.Background() 2599 _, err := client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "o", "r", "b") 2600 if err != nil { 2601 t.Errorf("Repositories.OptionalSignaturesOnProtectedBranch returned error: %v", err) 2602 } 2603 2604 const methodName = "OptionalSignaturesOnProtectedBranch" 2605 testBadOptions(t, methodName, func() (err error) { 2606 _, err = client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "\n", "\n", "\n") 2607 return err 2608 }) 2609 2610 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2611 return client.Repositories.OptionalSignaturesOnProtectedBranch(ctx, "o", "r", "b") 2612 }) 2613 } 2614 2615 func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctions(t *testing.T) { 2616 req := PullRequestReviewsEnforcementRequest{} 2617 2618 got, err := json.Marshal(req) 2619 if err != nil { 2620 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err) 2621 } 2622 2623 want := `{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}` 2624 if want != string(got) { 2625 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want) 2626 } 2627 2628 req = PullRequestReviewsEnforcementRequest{ 2629 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{}, 2630 } 2631 2632 got, err = json.Marshal(req) 2633 if err != nil { 2634 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err) 2635 } 2636 2637 want = `{"dismissal_restrictions":{},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}` 2638 if want != string(got) { 2639 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want) 2640 } 2641 2642 req = PullRequestReviewsEnforcementRequest{ 2643 DismissalRestrictionsRequest: &DismissalRestrictionsRequest{ 2644 Users: &[]string{}, 2645 Teams: &[]string{}, 2646 Apps: &[]string{}, 2647 }, 2648 RequireLastPushApproval: Bool(true), 2649 } 2650 2651 got, err = json.Marshal(req) 2652 if err != nil { 2653 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err) 2654 } 2655 2656 want = `{"dismissal_restrictions":{"users":[],"teams":[],"apps":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0,"require_last_push_approval":true}` 2657 if want != string(got) { 2658 t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want) 2659 } 2660 } 2661 2662 func TestRepositoriesService_ListAllTopics(t *testing.T) { 2663 client, mux, _, teardown := setup() 2664 defer teardown() 2665 2666 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) { 2667 testMethod(t, r, "GET") 2668 testHeader(t, r, "Accept", mediaTypeTopicsPreview) 2669 fmt.Fprint(w, `{"names":["go", "go-github", "github"]}`) 2670 }) 2671 2672 ctx := context.Background() 2673 got, _, err := client.Repositories.ListAllTopics(ctx, "o", "r") 2674 if err != nil { 2675 t.Fatalf("Repositories.ListAllTopics returned error: %v", err) 2676 } 2677 2678 want := []string{"go", "go-github", "github"} 2679 if !cmp.Equal(got, want) { 2680 t.Errorf("Repositories.ListAllTopics returned %+v, want %+v", got, want) 2681 } 2682 2683 const methodName = "ListAllTopics" 2684 testBadOptions(t, methodName, func() (err error) { 2685 _, _, err = client.Repositories.ListAllTopics(ctx, "\n", "\n") 2686 return err 2687 }) 2688 2689 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2690 got, resp, err := client.Repositories.ListAllTopics(ctx, "o", "r") 2691 if got != nil { 2692 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2693 } 2694 return resp, err 2695 }) 2696 } 2697 2698 func TestRepositoriesService_ListAllTopics_emptyTopics(t *testing.T) { 2699 client, mux, _, teardown := setup() 2700 defer teardown() 2701 2702 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) { 2703 testMethod(t, r, "GET") 2704 testHeader(t, r, "Accept", mediaTypeTopicsPreview) 2705 fmt.Fprint(w, `{"names":[]}`) 2706 }) 2707 2708 ctx := context.Background() 2709 got, _, err := client.Repositories.ListAllTopics(ctx, "o", "r") 2710 if err != nil { 2711 t.Fatalf("Repositories.ListAllTopics returned error: %v", err) 2712 } 2713 2714 want := []string{} 2715 if !cmp.Equal(got, want) { 2716 t.Errorf("Repositories.ListAllTopics returned %+v, want %+v", got, want) 2717 } 2718 } 2719 2720 func TestRepositoriesService_ReplaceAllTopics(t *testing.T) { 2721 client, mux, _, teardown := setup() 2722 defer teardown() 2723 2724 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) { 2725 testMethod(t, r, "PUT") 2726 testHeader(t, r, "Accept", mediaTypeTopicsPreview) 2727 fmt.Fprint(w, `{"names":["go", "go-github", "github"]}`) 2728 }) 2729 2730 ctx := context.Background() 2731 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{"go", "go-github", "github"}) 2732 if err != nil { 2733 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err) 2734 } 2735 2736 want := []string{"go", "go-github", "github"} 2737 if !cmp.Equal(got, want) { 2738 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want) 2739 } 2740 2741 const methodName = "ReplaceAllTopics" 2742 testBadOptions(t, methodName, func() (err error) { 2743 _, _, err = client.Repositories.ReplaceAllTopics(ctx, "\n", "\n", []string{"\n", "\n", "\n"}) 2744 return err 2745 }) 2746 2747 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2748 got, resp, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{"go", "go-github", "github"}) 2749 if got != nil { 2750 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2751 } 2752 return resp, err 2753 }) 2754 } 2755 2756 func TestRepositoriesService_ReplaceAllTopics_nilSlice(t *testing.T) { 2757 client, mux, _, teardown := setup() 2758 defer teardown() 2759 2760 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) { 2761 testMethod(t, r, "PUT") 2762 testHeader(t, r, "Accept", mediaTypeTopicsPreview) 2763 testBody(t, r, `{"names":[]}`+"\n") 2764 fmt.Fprint(w, `{"names":[]}`) 2765 }) 2766 2767 ctx := context.Background() 2768 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", nil) 2769 if err != nil { 2770 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err) 2771 } 2772 2773 want := []string{} 2774 if !cmp.Equal(got, want) { 2775 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want) 2776 } 2777 } 2778 2779 func TestRepositoriesService_ReplaceAllTopics_emptySlice(t *testing.T) { 2780 client, mux, _, teardown := setup() 2781 defer teardown() 2782 2783 mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) { 2784 testMethod(t, r, "PUT") 2785 testHeader(t, r, "Accept", mediaTypeTopicsPreview) 2786 testBody(t, r, `{"names":[]}`+"\n") 2787 fmt.Fprint(w, `{"names":[]}`) 2788 }) 2789 2790 ctx := context.Background() 2791 got, _, err := client.Repositories.ReplaceAllTopics(ctx, "o", "r", []string{}) 2792 if err != nil { 2793 t.Fatalf("Repositories.ReplaceAllTopics returned error: %v", err) 2794 } 2795 2796 want := []string{} 2797 if !cmp.Equal(got, want) { 2798 t.Errorf("Repositories.ReplaceAllTopics returned %+v, want %+v", got, want) 2799 } 2800 } 2801 2802 func TestRepositoriesService_ListAppRestrictions(t *testing.T) { 2803 client, mux, _, teardown := setup() 2804 defer teardown() 2805 2806 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) { 2807 testMethod(t, r, "GET") 2808 }) 2809 2810 ctx := context.Background() 2811 _, _, err := client.Repositories.ListAppRestrictions(ctx, "o", "r", "b") 2812 if err != nil { 2813 t.Errorf("Repositories.ListAppRestrictions returned error: %v", err) 2814 } 2815 2816 const methodName = "ListAppRestrictions" 2817 testBadOptions(t, methodName, func() (err error) { 2818 _, _, err = client.Repositories.ListAppRestrictions(ctx, "\n", "\n", "\n") 2819 return err 2820 }) 2821 2822 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2823 got, resp, err := client.Repositories.ListAppRestrictions(ctx, "o", "r", "b") 2824 if got != nil { 2825 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2826 } 2827 return resp, err 2828 }) 2829 } 2830 2831 func TestRepositoriesService_ReplaceAppRestrictions(t *testing.T) { 2832 client, mux, _, teardown := setup() 2833 defer teardown() 2834 2835 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) { 2836 testMethod(t, r, "PUT") 2837 fmt.Fprint(w, `[{ 2838 "name": "octocat" 2839 }]`) 2840 }) 2841 input := []string{"octocat"} 2842 ctx := context.Background() 2843 got, _, err := client.Repositories.ReplaceAppRestrictions(ctx, "o", "r", "b", input) 2844 if err != nil { 2845 t.Errorf("Repositories.ReplaceAppRestrictions returned error: %v", err) 2846 } 2847 want := []*App{ 2848 {Name: String("octocat")}, 2849 } 2850 if !cmp.Equal(got, want) { 2851 t.Errorf("Repositories.ReplaceAppRestrictions returned %+v, want %+v", got, want) 2852 } 2853 2854 const methodName = "ReplaceAppRestrictions" 2855 testBadOptions(t, methodName, func() (err error) { 2856 _, _, err = client.Repositories.ReplaceAppRestrictions(ctx, "\n", "\n", "\n", input) 2857 return err 2858 }) 2859 2860 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2861 got, resp, err := client.Repositories.ReplaceAppRestrictions(ctx, "o", "r", "b", input) 2862 if got != nil { 2863 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2864 } 2865 return resp, err 2866 }) 2867 } 2868 2869 func TestRepositoriesService_AddAppRestrictions(t *testing.T) { 2870 client, mux, _, teardown := setup() 2871 defer teardown() 2872 2873 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) { 2874 testMethod(t, r, "POST") 2875 fmt.Fprint(w, `[{ 2876 "name": "octocat" 2877 }]`) 2878 }) 2879 input := []string{"octocat"} 2880 ctx := context.Background() 2881 got, _, err := client.Repositories.AddAppRestrictions(ctx, "o", "r", "b", input) 2882 if err != nil { 2883 t.Errorf("Repositories.AddAppRestrictions returned error: %v", err) 2884 } 2885 want := []*App{ 2886 {Name: String("octocat")}, 2887 } 2888 if !cmp.Equal(got, want) { 2889 t.Errorf("Repositories.AddAppRestrictions returned %+v, want %+v", got, want) 2890 } 2891 2892 const methodName = "AddAppRestrictions" 2893 testBadOptions(t, methodName, func() (err error) { 2894 _, _, err = client.Repositories.AddAppRestrictions(ctx, "\n", "\n", "\n", input) 2895 return err 2896 }) 2897 2898 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2899 got, resp, err := client.Repositories.AddAppRestrictions(ctx, "o", "r", "b", input) 2900 if got != nil { 2901 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2902 } 2903 return resp, err 2904 }) 2905 } 2906 2907 func TestRepositoriesService_RemoveAppRestrictions(t *testing.T) { 2908 client, mux, _, teardown := setup() 2909 defer teardown() 2910 2911 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/apps", func(w http.ResponseWriter, r *http.Request) { 2912 testMethod(t, r, "DELETE") 2913 fmt.Fprint(w, `[]`) 2914 }) 2915 input := []string{"octocat"} 2916 ctx := context.Background() 2917 got, _, err := client.Repositories.RemoveAppRestrictions(ctx, "o", "r", "b", input) 2918 if err != nil { 2919 t.Errorf("Repositories.RemoveAppRestrictions returned error: %v", err) 2920 } 2921 want := []*App{} 2922 if !cmp.Equal(got, want) { 2923 t.Errorf("Repositories.RemoveAppRestrictions returned %+v, want %+v", got, want) 2924 } 2925 2926 const methodName = "RemoveAppRestrictions" 2927 testBadOptions(t, methodName, func() (err error) { 2928 _, _, err = client.Repositories.RemoveAppRestrictions(ctx, "\n", "\n", "\n", input) 2929 return err 2930 }) 2931 2932 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2933 got, resp, err := client.Repositories.RemoveAppRestrictions(ctx, "o", "r", "b", input) 2934 if got != nil { 2935 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2936 } 2937 return resp, err 2938 }) 2939 } 2940 2941 func TestRepositoriesService_ListTeamRestrictions(t *testing.T) { 2942 client, mux, _, teardown := setup() 2943 defer teardown() 2944 2945 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/teams", func(w http.ResponseWriter, r *http.Request) { 2946 testMethod(t, r, "GET") 2947 }) 2948 2949 ctx := context.Background() 2950 _, _, err := client.Repositories.ListTeamRestrictions(ctx, "o", "r", "b") 2951 if err != nil { 2952 t.Errorf("Repositories.ListTeamRestrictions returned error: %v", err) 2953 } 2954 2955 const methodName = "ListTeamRestrictions" 2956 testBadOptions(t, methodName, func() (err error) { 2957 _, _, err = client.Repositories.ListTeamRestrictions(ctx, "\n", "\n", "\n") 2958 return err 2959 }) 2960 2961 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2962 got, resp, err := client.Repositories.ListTeamRestrictions(ctx, "o", "r", "b") 2963 if got != nil { 2964 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2965 } 2966 return resp, err 2967 }) 2968 } 2969 2970 func TestRepositoriesService_ReplaceTeamRestrictions(t *testing.T) { 2971 client, mux, _, teardown := setup() 2972 defer teardown() 2973 2974 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/teams", func(w http.ResponseWriter, r *http.Request) { 2975 testMethod(t, r, "PUT") 2976 fmt.Fprint(w, `[{ 2977 "name": "octocat" 2978 }]`) 2979 }) 2980 input := []string{"octocat"} 2981 ctx := context.Background() 2982 got, _, err := client.Repositories.ReplaceTeamRestrictions(ctx, "o", "r", "b", input) 2983 if err != nil { 2984 t.Errorf("Repositories.ReplaceTeamRestrictions returned error: %v", err) 2985 } 2986 want := []*Team{ 2987 {Name: String("octocat")}, 2988 } 2989 if !cmp.Equal(got, want) { 2990 t.Errorf("Repositories.ReplaceTeamRestrictions returned %+v, want %+v", got, want) 2991 } 2992 2993 const methodName = "ReplaceTeamRestrictions" 2994 testBadOptions(t, methodName, func() (err error) { 2995 _, _, err = client.Repositories.ReplaceTeamRestrictions(ctx, "\n", "\n", "\n", input) 2996 return err 2997 }) 2998 2999 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3000 got, resp, err := client.Repositories.ReplaceTeamRestrictions(ctx, "o", "r", "b", input) 3001 if got != nil { 3002 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3003 } 3004 return resp, err 3005 }) 3006 } 3007 3008 func TestRepositoriesService_AddTeamRestrictions(t *testing.T) { 3009 client, mux, _, teardown := setup() 3010 defer teardown() 3011 3012 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/teams", func(w http.ResponseWriter, r *http.Request) { 3013 testMethod(t, r, "POST") 3014 fmt.Fprint(w, `[{ 3015 "name": "octocat" 3016 }]`) 3017 }) 3018 input := []string{"octocat"} 3019 ctx := context.Background() 3020 got, _, err := client.Repositories.AddTeamRestrictions(ctx, "o", "r", "b", input) 3021 if err != nil { 3022 t.Errorf("Repositories.AddTeamRestrictions returned error: %v", err) 3023 } 3024 want := []*Team{ 3025 {Name: String("octocat")}, 3026 } 3027 if !cmp.Equal(got, want) { 3028 t.Errorf("Repositories.AddTeamRestrictions returned %+v, want %+v", got, want) 3029 } 3030 3031 const methodName = "AddTeamRestrictions" 3032 testBadOptions(t, methodName, func() (err error) { 3033 _, _, err = client.Repositories.AddTeamRestrictions(ctx, "\n", "\n", "\n", input) 3034 return err 3035 }) 3036 3037 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3038 got, resp, err := client.Repositories.AddTeamRestrictions(ctx, "o", "r", "b", input) 3039 if got != nil { 3040 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3041 } 3042 return resp, err 3043 }) 3044 } 3045 3046 func TestRepositoriesService_RemoveTeamRestrictions(t *testing.T) { 3047 client, mux, _, teardown := setup() 3048 defer teardown() 3049 3050 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/teams", func(w http.ResponseWriter, r *http.Request) { 3051 testMethod(t, r, "DELETE") 3052 fmt.Fprint(w, `[]`) 3053 }) 3054 input := []string{"octocat"} 3055 ctx := context.Background() 3056 got, _, err := client.Repositories.RemoveTeamRestrictions(ctx, "o", "r", "b", input) 3057 if err != nil { 3058 t.Errorf("Repositories.RemoveTeamRestrictions returned error: %v", err) 3059 } 3060 want := []*Team{} 3061 if !cmp.Equal(got, want) { 3062 t.Errorf("Repositories.RemoveTeamRestrictions returned %+v, want %+v", got, want) 3063 } 3064 3065 const methodName = "RemoveTeamRestrictions" 3066 testBadOptions(t, methodName, func() (err error) { 3067 _, _, err = client.Repositories.RemoveTeamRestrictions(ctx, "\n", "\n", "\n", input) 3068 return err 3069 }) 3070 3071 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3072 got, resp, err := client.Repositories.RemoveTeamRestrictions(ctx, "o", "r", "b", input) 3073 if got != nil { 3074 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3075 } 3076 return resp, err 3077 }) 3078 } 3079 3080 func TestRepositoriesService_ListUserRestrictions(t *testing.T) { 3081 client, mux, _, teardown := setup() 3082 defer teardown() 3083 3084 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/users", func(w http.ResponseWriter, r *http.Request) { 3085 testMethod(t, r, "GET") 3086 }) 3087 3088 ctx := context.Background() 3089 _, _, err := client.Repositories.ListUserRestrictions(ctx, "o", "r", "b") 3090 if err != nil { 3091 t.Errorf("Repositories.ListUserRestrictions returned error: %v", err) 3092 } 3093 3094 const methodName = "ListUserRestrictions" 3095 testBadOptions(t, methodName, func() (err error) { 3096 _, _, err = client.Repositories.ListUserRestrictions(ctx, "\n", "\n", "\n") 3097 return err 3098 }) 3099 3100 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3101 got, resp, err := client.Repositories.ListUserRestrictions(ctx, "o", "r", "b") 3102 if got != nil { 3103 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3104 } 3105 return resp, err 3106 }) 3107 } 3108 3109 func TestRepositoriesService_ReplaceUserRestrictions(t *testing.T) { 3110 client, mux, _, teardown := setup() 3111 defer teardown() 3112 3113 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/users", func(w http.ResponseWriter, r *http.Request) { 3114 testMethod(t, r, "PUT") 3115 fmt.Fprint(w, `[{ 3116 "name": "octocat" 3117 }]`) 3118 }) 3119 input := []string{"octocat"} 3120 ctx := context.Background() 3121 got, _, err := client.Repositories.ReplaceUserRestrictions(ctx, "o", "r", "b", input) 3122 if err != nil { 3123 t.Errorf("Repositories.ReplaceUserRestrictions returned error: %v", err) 3124 } 3125 want := []*User{ 3126 {Name: String("octocat")}, 3127 } 3128 if !cmp.Equal(got, want) { 3129 t.Errorf("Repositories.ReplaceUserRestrictions returned %+v, want %+v", got, want) 3130 } 3131 3132 const methodName = "ReplaceUserRestrictions" 3133 testBadOptions(t, methodName, func() (err error) { 3134 _, _, err = client.Repositories.ReplaceUserRestrictions(ctx, "\n", "\n", "\n", input) 3135 return err 3136 }) 3137 3138 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3139 got, resp, err := client.Repositories.ReplaceUserRestrictions(ctx, "o", "r", "b", input) 3140 if got != nil { 3141 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3142 } 3143 return resp, err 3144 }) 3145 } 3146 3147 func TestRepositoriesService_AddUserRestrictions(t *testing.T) { 3148 client, mux, _, teardown := setup() 3149 defer teardown() 3150 3151 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/users", func(w http.ResponseWriter, r *http.Request) { 3152 testMethod(t, r, "POST") 3153 fmt.Fprint(w, `[{ 3154 "name": "octocat" 3155 }]`) 3156 }) 3157 input := []string{"octocat"} 3158 ctx := context.Background() 3159 got, _, err := client.Repositories.AddUserRestrictions(ctx, "o", "r", "b", input) 3160 if err != nil { 3161 t.Errorf("Repositories.AddUserRestrictions returned error: %v", err) 3162 } 3163 want := []*User{ 3164 {Name: String("octocat")}, 3165 } 3166 if !cmp.Equal(got, want) { 3167 t.Errorf("Repositories.AddUserRestrictions returned %+v, want %+v", got, want) 3168 } 3169 3170 const methodName = "AddUserRestrictions" 3171 testBadOptions(t, methodName, func() (err error) { 3172 _, _, err = client.Repositories.AddUserRestrictions(ctx, "\n", "\n", "\n", input) 3173 return err 3174 }) 3175 3176 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3177 got, resp, err := client.Repositories.AddUserRestrictions(ctx, "o", "r", "b", input) 3178 if got != nil { 3179 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3180 } 3181 return resp, err 3182 }) 3183 } 3184 3185 func TestRepositoriesService_RemoveUserRestrictions(t *testing.T) { 3186 client, mux, _, teardown := setup() 3187 defer teardown() 3188 3189 mux.HandleFunc("/repos/o/r/branches/b/protection/restrictions/users", func(w http.ResponseWriter, r *http.Request) { 3190 testMethod(t, r, "DELETE") 3191 fmt.Fprint(w, `[]`) 3192 }) 3193 input := []string{"octocat"} 3194 ctx := context.Background() 3195 got, _, err := client.Repositories.RemoveUserRestrictions(ctx, "o", "r", "b", input) 3196 if err != nil { 3197 t.Errorf("Repositories.RemoveUserRestrictions returned error: %v", err) 3198 } 3199 want := []*User{} 3200 if !cmp.Equal(got, want) { 3201 t.Errorf("Repositories.RemoveUserRestrictions returned %+v, want %+v", got, want) 3202 } 3203 3204 const methodName = "RemoveUserRestrictions" 3205 testBadOptions(t, methodName, func() (err error) { 3206 _, _, err = client.Repositories.RemoveUserRestrictions(ctx, "\n", "\n", "\n", input) 3207 return err 3208 }) 3209 3210 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3211 got, resp, err := client.Repositories.RemoveUserRestrictions(ctx, "o", "r", "b", input) 3212 if got != nil { 3213 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3214 } 3215 return resp, err 3216 }) 3217 } 3218 3219 func TestRepositoriesService_Transfer(t *testing.T) { 3220 client, mux, _, teardown := setup() 3221 defer teardown() 3222 3223 input := TransferRequest{NewOwner: "a", TeamID: []int64{123}} 3224 3225 mux.HandleFunc("/repos/o/r/transfer", func(w http.ResponseWriter, r *http.Request) { 3226 var v TransferRequest 3227 json.NewDecoder(r.Body).Decode(&v) 3228 3229 testMethod(t, r, "POST") 3230 if !cmp.Equal(v, input) { 3231 t.Errorf("Request body = %+v, want %+v", v, input) 3232 } 3233 3234 fmt.Fprint(w, `{"owner":{"login":"a"}}`) 3235 }) 3236 3237 ctx := context.Background() 3238 got, _, err := client.Repositories.Transfer(ctx, "o", "r", input) 3239 if err != nil { 3240 t.Errorf("Repositories.Transfer returned error: %v", err) 3241 } 3242 3243 want := &Repository{Owner: &User{Login: String("a")}} 3244 if !cmp.Equal(got, want) { 3245 t.Errorf("Repositories.Transfer returned %+v, want %+v", got, want) 3246 } 3247 3248 const methodName = "Transfer" 3249 testBadOptions(t, methodName, func() (err error) { 3250 _, _, err = client.Repositories.Transfer(ctx, "\n", "\n", input) 3251 return err 3252 }) 3253 3254 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3255 got, resp, err := client.Repositories.Transfer(ctx, "o", "r", input) 3256 if got != nil { 3257 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3258 } 3259 return resp, err 3260 }) 3261 } 3262 3263 func TestRepositoriesService_Dispatch(t *testing.T) { 3264 client, mux, _, teardown := setup() 3265 defer teardown() 3266 3267 var input DispatchRequestOptions 3268 3269 mux.HandleFunc("/repos/o/r/dispatches", func(w http.ResponseWriter, r *http.Request) { 3270 var v DispatchRequestOptions 3271 json.NewDecoder(r.Body).Decode(&v) 3272 3273 testMethod(t, r, "POST") 3274 if !cmp.Equal(v, input) { 3275 t.Errorf("Request body = %+v, want %+v", v, input) 3276 } 3277 3278 fmt.Fprint(w, `{"owner":{"login":"a"}}`) 3279 }) 3280 3281 ctx := context.Background() 3282 3283 testCases := []interface{}{ 3284 nil, 3285 struct { 3286 Foo string 3287 }{ 3288 Foo: "test", 3289 }, 3290 struct { 3291 Bar int 3292 }{ 3293 Bar: 42, 3294 }, 3295 struct { 3296 Foo string 3297 Bar int 3298 Baz bool 3299 }{ 3300 Foo: "test", 3301 Bar: 42, 3302 Baz: false, 3303 }, 3304 } 3305 3306 for _, tc := range testCases { 3307 if tc == nil { 3308 input = DispatchRequestOptions{EventType: "go"} 3309 } else { 3310 bytes, _ := json.Marshal(tc) 3311 payload := json.RawMessage(bytes) 3312 input = DispatchRequestOptions{EventType: "go", ClientPayload: &payload} 3313 } 3314 3315 got, _, err := client.Repositories.Dispatch(ctx, "o", "r", input) 3316 if err != nil { 3317 t.Errorf("Repositories.Dispatch returned error: %v", err) 3318 } 3319 3320 want := &Repository{Owner: &User{Login: String("a")}} 3321 if !cmp.Equal(got, want) { 3322 t.Errorf("Repositories.Dispatch returned %+v, want %+v", got, want) 3323 } 3324 } 3325 3326 const methodName = "Dispatch" 3327 testBadOptions(t, methodName, func() (err error) { 3328 _, _, err = client.Repositories.Dispatch(ctx, "\n", "\n", input) 3329 return err 3330 }) 3331 3332 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 3333 got, resp, err := client.Repositories.Dispatch(ctx, "o", "r", input) 3334 if got != nil { 3335 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 3336 } 3337 return resp, err 3338 }) 3339 } 3340 3341 func TestAdvancedSecurity_Marshal(t *testing.T) { 3342 testJSONMarshal(t, &AdvancedSecurity{}, "{}") 3343 3344 u := &AdvancedSecurity{ 3345 Status: String("status"), 3346 } 3347 3348 want := `{ 3349 "status": "status" 3350 }` 3351 3352 testJSONMarshal(t, u, want) 3353 } 3354 3355 func TestAuthorizedActorsOnly_Marshal(t *testing.T) { 3356 testJSONMarshal(t, &AuthorizedActorsOnly{}, "{}") 3357 3358 u := &AuthorizedActorsOnly{ 3359 From: Bool(true), 3360 } 3361 3362 want := `{ 3363 "from" : true 3364 }` 3365 3366 testJSONMarshal(t, u, want) 3367 } 3368 3369 func TestDispatchRequestOptions_Marshal(t *testing.T) { 3370 testJSONMarshal(t, &DispatchRequestOptions{}, "{}") 3371 3372 cp := json.RawMessage(`{"testKey":"testValue"}`) 3373 u := &DispatchRequestOptions{ 3374 EventType: "test_event_type", 3375 ClientPayload: &cp, 3376 } 3377 3378 want := `{ 3379 "event_type": "test_event_type", 3380 "client_payload": { 3381 "testKey": "testValue" 3382 } 3383 }` 3384 3385 testJSONMarshal(t, u, want) 3386 } 3387 3388 func TestTransferRequest_Marshal(t *testing.T) { 3389 testJSONMarshal(t, &TransferRequest{}, "{}") 3390 3391 u := &TransferRequest{ 3392 NewOwner: "testOwner", 3393 TeamID: []int64{1, 2}, 3394 } 3395 3396 want := `{ 3397 "new_owner": "testOwner", 3398 "team_ids": [1,2] 3399 }` 3400 3401 testJSONMarshal(t, u, want) 3402 } 3403 3404 func TestSignaturesProtectedBranch_Marshal(t *testing.T) { 3405 testJSONMarshal(t, &SignaturesProtectedBranch{}, "{}") 3406 3407 u := &SignaturesProtectedBranch{ 3408 URL: String("https://www.testURL.in"), 3409 Enabled: Bool(false), 3410 } 3411 3412 want := `{ 3413 "url": "https://www.testURL.in", 3414 "enabled": false 3415 }` 3416 3417 testJSONMarshal(t, u, want) 3418 3419 u2 := &SignaturesProtectedBranch{ 3420 URL: String("testURL"), 3421 Enabled: Bool(true), 3422 } 3423 3424 want2 := `{ 3425 "url": "testURL", 3426 "enabled": true 3427 }` 3428 3429 testJSONMarshal(t, u2, want2) 3430 } 3431 3432 func TestDismissalRestrictionsRequest_Marshal(t *testing.T) { 3433 testJSONMarshal(t, &DismissalRestrictionsRequest{}, "{}") 3434 3435 u := &DismissalRestrictionsRequest{ 3436 Users: &[]string{"user1", "user2"}, 3437 Teams: &[]string{"team1", "team2"}, 3438 Apps: &[]string{"app1", "app2"}, 3439 } 3440 3441 want := `{ 3442 "users": ["user1","user2"], 3443 "teams": ["team1","team2"], 3444 "apps": ["app1","app2"] 3445 }` 3446 3447 testJSONMarshal(t, u, want) 3448 } 3449 3450 func TestAdminEnforcement_Marshal(t *testing.T) { 3451 testJSONMarshal(t, &AdminEnforcement{}, "{}") 3452 3453 u := &AdminEnforcement{ 3454 URL: String("https://www.test-url.in"), 3455 Enabled: false, 3456 } 3457 3458 want := `{ 3459 "url": "https://www.test-url.in", 3460 "enabled": false 3461 }` 3462 3463 testJSONMarshal(t, u, want) 3464 } 3465 3466 func TestPullRequestReviewsEnforcementUpdate_Marshal(t *testing.T) { 3467 testJSONMarshal(t, &PullRequestReviewsEnforcementUpdate{}, "{}") 3468 3469 u := &PullRequestReviewsEnforcementUpdate{ 3470 BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{ 3471 Users: []string{"user1", "user2"}, 3472 Teams: []string{"team1", "team2"}, 3473 Apps: []string{"app1", "app2"}, 3474 }, 3475 DismissStaleReviews: Bool(false), 3476 RequireCodeOwnerReviews: Bool(true), 3477 RequiredApprovingReviewCount: 2, 3478 } 3479 3480 want := `{ 3481 "bypass_pull_request_allowances": { 3482 "users": ["user1","user2"], 3483 "teams": ["team1","team2"], 3484 "apps": ["app1","app2"] 3485 }, 3486 "dismiss_stale_reviews": false, 3487 "require_code_owner_reviews": true, 3488 "required_approving_review_count": 2 3489 }` 3490 3491 testJSONMarshal(t, u, want) 3492 } 3493 3494 func TestRequiredStatusCheck_Marshal(t *testing.T) { 3495 testJSONMarshal(t, &RequiredStatusCheck{}, "{}") 3496 3497 u := &RequiredStatusCheck{ 3498 Context: "ctx", 3499 AppID: Int64(1), 3500 } 3501 3502 want := `{ 3503 "context": "ctx", 3504 "app_id": 1 3505 }` 3506 3507 testJSONMarshal(t, u, want) 3508 }