github.com/google/go-github/v33@v33.0.0/github/teams_test.go (about) 1 // Copyright 2018 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "bytes" 10 "context" 11 "encoding/json" 12 "fmt" 13 "io/ioutil" 14 "net/http" 15 "reflect" 16 "strings" 17 "testing" 18 ) 19 20 func TestTeamsService_ListTeams(t *testing.T) { 21 client, mux, _, teardown := setup() 22 defer teardown() 23 24 mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) { 25 testMethod(t, r, "GET") 26 testFormValues(t, r, values{"page": "2"}) 27 fmt.Fprint(w, `[{"id":1}]`) 28 }) 29 30 opt := &ListOptions{Page: 2} 31 teams, _, err := client.Teams.ListTeams(context.Background(), "o", opt) 32 if err != nil { 33 t.Errorf("Teams.ListTeams returned error: %v", err) 34 } 35 36 want := []*Team{{ID: Int64(1)}} 37 if !reflect.DeepEqual(teams, want) { 38 t.Errorf("Teams.ListTeams returned %+v, want %+v", teams, want) 39 } 40 } 41 42 func TestTeamsService_ListTeams_invalidOrg(t *testing.T) { 43 client, _, _, teardown := setup() 44 defer teardown() 45 46 _, _, err := client.Teams.ListTeams(context.Background(), "%", nil) 47 testURLParseError(t, err) 48 } 49 50 func TestTeamsService_GetTeamByID(t *testing.T) { 51 client, mux, _, teardown := setup() 52 defer teardown() 53 54 mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { 55 testMethod(t, r, "GET") 56 fmt.Fprint(w, `{"id":1, "name":"n", "description": "d", "url":"u", "slug": "s", "permission":"p", "ldap_dn":"cn=n,ou=groups,dc=example,dc=com", "parent":null}`) 57 }) 58 59 team, _, err := client.Teams.GetTeamByID(context.Background(), 1, 1) 60 if err != nil { 61 t.Errorf("Teams.GetTeamByID returned error: %v", err) 62 } 63 64 want := &Team{ID: Int64(1), Name: String("n"), Description: String("d"), URL: String("u"), Slug: String("s"), Permission: String("p"), LDAPDN: String("cn=n,ou=groups,dc=example,dc=com")} 65 if !reflect.DeepEqual(team, want) { 66 t.Errorf("Teams.GetTeamByID returned %+v, want %+v", team, want) 67 } 68 } 69 70 func TestTeamsService_GetTeamByID_notFound(t *testing.T) { 71 client, mux, _, teardown := setup() 72 defer teardown() 73 74 mux.HandleFunc("/organizations/1/team/2", func(w http.ResponseWriter, r *http.Request) { 75 testMethod(t, r, "GET") 76 w.WriteHeader(http.StatusNotFound) 77 }) 78 79 team, resp, err := client.Teams.GetTeamByID(context.Background(), 1, 2) 80 if err == nil { 81 t.Errorf("Expected HTTP 404 response") 82 } 83 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 84 t.Errorf("Teams.GetTeamByID returned status %d, want %d", got, want) 85 } 86 if team != nil { 87 t.Errorf("Teams.GetTeamByID returned %+v, want nil", team) 88 } 89 } 90 91 func TestTeamsService_GetTeamBySlug(t *testing.T) { 92 client, mux, _, teardown := setup() 93 defer teardown() 94 95 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 96 testMethod(t, r, "GET") 97 fmt.Fprint(w, `{"id":1, "name":"n", "description": "d", "url":"u", "slug": "s", "permission":"p", "ldap_dn":"cn=n,ou=groups,dc=example,dc=com", "parent":null}`) 98 }) 99 100 team, _, err := client.Teams.GetTeamBySlug(context.Background(), "o", "s") 101 if err != nil { 102 t.Errorf("Teams.GetTeamBySlug returned error: %v", err) 103 } 104 105 want := &Team{ID: Int64(1), Name: String("n"), Description: String("d"), URL: String("u"), Slug: String("s"), Permission: String("p"), LDAPDN: String("cn=n,ou=groups,dc=example,dc=com")} 106 if !reflect.DeepEqual(team, want) { 107 t.Errorf("Teams.GetTeamBySlug returned %+v, want %+v", team, want) 108 } 109 } 110 111 func TestTeamsService_GetTeamBySlug_invalidOrg(t *testing.T) { 112 client, _, _, teardown := setup() 113 defer teardown() 114 115 _, _, err := client.Teams.GetTeamBySlug(context.Background(), "%", "s") 116 testURLParseError(t, err) 117 } 118 119 func TestTeamsService_GetTeamBySlug_notFound(t *testing.T) { 120 client, mux, _, teardown := setup() 121 defer teardown() 122 123 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 124 testMethod(t, r, "GET") 125 w.WriteHeader(http.StatusNotFound) 126 }) 127 128 team, resp, err := client.Teams.GetTeamBySlug(context.Background(), "o", "s") 129 if err == nil { 130 t.Errorf("Expected HTTP 404 response") 131 } 132 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 133 t.Errorf("Teams.GetTeamBySlug returned status %d, want %d", got, want) 134 } 135 if team != nil { 136 t.Errorf("Teams.GetTeamBySlug returned %+v, want nil", team) 137 } 138 } 139 140 func TestTeamsService_CreateTeam(t *testing.T) { 141 client, mux, _, teardown := setup() 142 defer teardown() 143 144 input := NewTeam{Name: "n", Privacy: String("closed"), RepoNames: []string{"r"}} 145 146 mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) { 147 v := new(NewTeam) 148 json.NewDecoder(r.Body).Decode(v) 149 150 testMethod(t, r, "POST") 151 if !reflect.DeepEqual(v, &input) { 152 t.Errorf("Request body = %+v, want %+v", v, input) 153 } 154 155 fmt.Fprint(w, `{"id":1}`) 156 }) 157 158 team, _, err := client.Teams.CreateTeam(context.Background(), "o", input) 159 if err != nil { 160 t.Errorf("Teams.CreateTeam returned error: %v", err) 161 } 162 163 want := &Team{ID: Int64(1)} 164 if !reflect.DeepEqual(team, want) { 165 t.Errorf("Teams.CreateTeam returned %+v, want %+v", team, want) 166 } 167 } 168 169 func TestTeamsService_CreateTeam_invalidOrg(t *testing.T) { 170 client, _, _, teardown := setup() 171 defer teardown() 172 173 _, _, err := client.Teams.CreateTeam(context.Background(), "%", NewTeam{}) 174 testURLParseError(t, err) 175 } 176 177 func TestTeamsService_EditTeamByID(t *testing.T) { 178 client, mux, _, teardown := setup() 179 defer teardown() 180 181 input := NewTeam{Name: "n", Privacy: String("closed")} 182 183 mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { 184 v := new(NewTeam) 185 json.NewDecoder(r.Body).Decode(v) 186 187 testMethod(t, r, "PATCH") 188 if !reflect.DeepEqual(v, &input) { 189 t.Errorf("Request body = %+v, want %+v", v, input) 190 } 191 192 fmt.Fprint(w, `{"id":1}`) 193 }) 194 195 team, _, err := client.Teams.EditTeamByID(context.Background(), 1, 1, input, false) 196 if err != nil { 197 t.Errorf("Teams.EditTeamByID returned error: %v", err) 198 } 199 200 want := &Team{ID: Int64(1)} 201 if !reflect.DeepEqual(team, want) { 202 t.Errorf("Teams.EditTeamByID returned %+v, want %+v", team, want) 203 } 204 } 205 206 func TestTeamsService_EditTeamByID_RemoveParent(t *testing.T) { 207 client, mux, _, teardown := setup() 208 defer teardown() 209 210 input := NewTeam{Name: "n", Privacy: String("closed")} 211 var body string 212 213 mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { 214 v := new(NewTeam) 215 buf, err := ioutil.ReadAll(r.Body) 216 if err != nil { 217 t.Errorf("Unable to read body: %v", err) 218 } 219 body = string(buf) 220 json.NewDecoder(bytes.NewBuffer(buf)).Decode(v) 221 222 testMethod(t, r, "PATCH") 223 if !reflect.DeepEqual(v, &input) { 224 t.Errorf("Request body = %+v, want %+v", v, input) 225 } 226 227 fmt.Fprint(w, `{"id":1}`) 228 }) 229 230 team, _, err := client.Teams.EditTeamByID(context.Background(), 1, 1, input, true) 231 if err != nil { 232 t.Errorf("Teams.EditTeamByID returned error: %v", err) 233 } 234 235 want := &Team{ID: Int64(1)} 236 if !reflect.DeepEqual(team, want) { 237 t.Errorf("Teams.EditTeamByID returned %+v, want %+v", team, want) 238 } 239 240 if want := `{"name":"n","parent_team_id":null,"privacy":"closed"}` + "\n"; body != want { 241 t.Errorf("Teams.EditTeamByID body = %+v, want %+v", body, want) 242 } 243 } 244 245 func TestTeamsService_EditTeamBySlug(t *testing.T) { 246 client, mux, _, teardown := setup() 247 defer teardown() 248 249 input := NewTeam{Name: "n", Privacy: String("closed")} 250 251 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 252 v := new(NewTeam) 253 json.NewDecoder(r.Body).Decode(v) 254 255 testMethod(t, r, "PATCH") 256 if !reflect.DeepEqual(v, &input) { 257 t.Errorf("Request body = %+v, want %+v", v, input) 258 } 259 260 fmt.Fprint(w, `{"id":1}`) 261 }) 262 263 team, _, err := client.Teams.EditTeamBySlug(context.Background(), "o", "s", input, false) 264 if err != nil { 265 t.Errorf("Teams.EditTeamBySlug returned error: %v", err) 266 } 267 268 want := &Team{ID: Int64(1)} 269 if !reflect.DeepEqual(team, want) { 270 t.Errorf("Teams.EditTeamBySlug returned %+v, want %+v", team, want) 271 } 272 } 273 274 func TestTeamsService_EditTeamBySlug_RemoveParent(t *testing.T) { 275 client, mux, _, teardown := setup() 276 defer teardown() 277 278 input := NewTeam{Name: "n", Privacy: String("closed")} 279 var body string 280 281 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 282 v := new(NewTeam) 283 buf, err := ioutil.ReadAll(r.Body) 284 if err != nil { 285 t.Errorf("Unable to read body: %v", err) 286 } 287 body = string(buf) 288 json.NewDecoder(bytes.NewBuffer(buf)).Decode(v) 289 290 testMethod(t, r, "PATCH") 291 if !reflect.DeepEqual(v, &input) { 292 t.Errorf("Request body = %+v, want %+v", v, input) 293 } 294 295 fmt.Fprint(w, `{"id":1}`) 296 }) 297 298 team, _, err := client.Teams.EditTeamBySlug(context.Background(), "o", "s", input, true) 299 if err != nil { 300 t.Errorf("Teams.EditTeam returned error: %v", err) 301 } 302 303 want := &Team{ID: Int64(1)} 304 if !reflect.DeepEqual(team, want) { 305 t.Errorf("Teams.EditTeam returned %+v, want %+v", team, want) 306 } 307 308 if want := `{"name":"n","parent_team_id":null,"privacy":"closed"}` + "\n"; body != want { 309 t.Errorf("Teams.EditTeam body = %+v, want %+v", body, want) 310 } 311 } 312 313 func TestTeamsService_DeleteTeamByID(t *testing.T) { 314 client, mux, _, teardown := setup() 315 defer teardown() 316 317 mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { 318 testMethod(t, r, "DELETE") 319 }) 320 321 _, err := client.Teams.DeleteTeamByID(context.Background(), 1, 1) 322 if err != nil { 323 t.Errorf("Teams.DeleteTeamByID returned error: %v", err) 324 } 325 } 326 327 func TestTeamsService_DeleteTeamBySlug(t *testing.T) { 328 client, mux, _, teardown := setup() 329 defer teardown() 330 331 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 332 testMethod(t, r, "DELETE") 333 }) 334 335 _, err := client.Teams.DeleteTeamBySlug(context.Background(), "o", "s") 336 if err != nil { 337 t.Errorf("Teams.DeleteTeamBySlug returned error: %v", err) 338 } 339 } 340 341 func TestTeamsService_ListChildTeamsByParentID(t *testing.T) { 342 client, mux, _, teardown := setup() 343 defer teardown() 344 345 mux.HandleFunc("/organizations/1/team/2/teams", func(w http.ResponseWriter, r *http.Request) { 346 testMethod(t, r, "GET") 347 testFormValues(t, r, values{"page": "2"}) 348 fmt.Fprint(w, `[{"id":2}]`) 349 }) 350 351 opt := &ListOptions{Page: 2} 352 teams, _, err := client.Teams.ListChildTeamsByParentID(context.Background(), 1, 2, opt) 353 if err != nil { 354 t.Errorf("Teams.ListChildTeamsByParentID returned error: %v", err) 355 } 356 357 want := []*Team{{ID: Int64(2)}} 358 if !reflect.DeepEqual(teams, want) { 359 t.Errorf("Teams.ListChildTeamsByParentID returned %+v, want %+v", teams, want) 360 } 361 } 362 363 func TestTeamsService_ListChildTeamsByParentSlug(t *testing.T) { 364 client, mux, _, teardown := setup() 365 defer teardown() 366 367 mux.HandleFunc("/orgs/o/teams/s/teams", func(w http.ResponseWriter, r *http.Request) { 368 testMethod(t, r, "GET") 369 testFormValues(t, r, values{"page": "2"}) 370 fmt.Fprint(w, `[{"id":2}]`) 371 }) 372 373 opt := &ListOptions{Page: 2} 374 teams, _, err := client.Teams.ListChildTeamsByParentSlug(context.Background(), "o", "s", opt) 375 if err != nil { 376 t.Errorf("Teams.ListChildTeamsByParentSlug returned error: %v", err) 377 } 378 379 want := []*Team{{ID: Int64(2)}} 380 if !reflect.DeepEqual(teams, want) { 381 t.Errorf("Teams.ListChildTeamsByParentSlug returned %+v, want %+v", teams, want) 382 } 383 } 384 385 func TestTeamsService_ListTeamReposByID(t *testing.T) { 386 client, mux, _, teardown := setup() 387 defer teardown() 388 389 mux.HandleFunc("/organizations/1/team/1/repos", func(w http.ResponseWriter, r *http.Request) { 390 testMethod(t, r, "GET") 391 wantAcceptHeaders := []string{mediaTypeTopicsPreview} 392 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 393 testFormValues(t, r, values{"page": "2"}) 394 fmt.Fprint(w, `[{"id":1}]`) 395 }) 396 397 opt := &ListOptions{Page: 2} 398 members, _, err := client.Teams.ListTeamReposByID(context.Background(), 1, 1, opt) 399 if err != nil { 400 t.Errorf("Teams.ListTeamReposByID returned error: %v", err) 401 } 402 403 want := []*Repository{{ID: Int64(1)}} 404 if !reflect.DeepEqual(members, want) { 405 t.Errorf("Teams.ListTeamReposByID returned %+v, want %+v", members, want) 406 } 407 } 408 409 func TestTeamsService_ListTeamReposBySlug(t *testing.T) { 410 client, mux, _, teardown := setup() 411 defer teardown() 412 413 mux.HandleFunc("/orgs/o/teams/s/repos", func(w http.ResponseWriter, r *http.Request) { 414 testMethod(t, r, "GET") 415 wantAcceptHeaders := []string{mediaTypeTopicsPreview} 416 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 417 testFormValues(t, r, values{"page": "2"}) 418 fmt.Fprint(w, `[{"id":1}]`) 419 }) 420 421 opt := &ListOptions{Page: 2} 422 members, _, err := client.Teams.ListTeamReposBySlug(context.Background(), "o", "s", opt) 423 if err != nil { 424 t.Errorf("Teams.ListTeamReposBySlug returned error: %v", err) 425 } 426 427 want := []*Repository{{ID: Int64(1)}} 428 if !reflect.DeepEqual(members, want) { 429 t.Errorf("Teams.ListTeamReposBySlug returned %+v, want %+v", members, want) 430 } 431 } 432 433 func TestTeamsService_IsTeamRepoByID_true(t *testing.T) { 434 client, mux, _, teardown := setup() 435 defer teardown() 436 437 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 438 testMethod(t, r, "GET") 439 wantAcceptHeaders := []string{mediaTypeOrgPermissionRepo} 440 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 441 fmt.Fprint(w, `{"id":1}`) 442 }) 443 444 repo, _, err := client.Teams.IsTeamRepoByID(context.Background(), 1, 1, "owner", "repo") 445 if err != nil { 446 t.Errorf("Teams.IsTeamRepoByID returned error: %v", err) 447 } 448 449 want := &Repository{ID: Int64(1)} 450 if !reflect.DeepEqual(repo, want) { 451 t.Errorf("Teams.IsTeamRepoByID returned %+v, want %+v", repo, want) 452 } 453 } 454 455 func TestTeamsService_IsTeamRepoBySlug_true(t *testing.T) { 456 client, mux, _, teardown := setup() 457 defer teardown() 458 459 mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 460 testMethod(t, r, "GET") 461 wantAcceptHeaders := []string{mediaTypeOrgPermissionRepo} 462 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 463 fmt.Fprint(w, `{"id":1}`) 464 }) 465 466 repo, _, err := client.Teams.IsTeamRepoBySlug(context.Background(), "org", "slug", "owner", "repo") 467 if err != nil { 468 t.Errorf("Teams.IsTeamRepoBySlug returned error: %v", err) 469 } 470 471 want := &Repository{ID: Int64(1)} 472 if !reflect.DeepEqual(repo, want) { 473 t.Errorf("Teams.IsTeamRepoBySlug returned %+v, want %+v", repo, want) 474 } 475 } 476 477 func TestTeamsService_IsTeamRepoByID_false(t *testing.T) { 478 client, mux, _, teardown := setup() 479 defer teardown() 480 481 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 482 testMethod(t, r, "GET") 483 w.WriteHeader(http.StatusNotFound) 484 }) 485 486 repo, resp, err := client.Teams.IsTeamRepoByID(context.Background(), 1, 1, "owner", "repo") 487 if err == nil { 488 t.Errorf("Expected HTTP 404 response") 489 } 490 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 491 t.Errorf("Teams.IsTeamRepoByID returned status %d, want %d", got, want) 492 } 493 if repo != nil { 494 t.Errorf("Teams.IsTeamRepoByID returned %+v, want nil", repo) 495 } 496 } 497 498 func TestTeamsService_IsTeamRepoBySlug_false(t *testing.T) { 499 client, mux, _, teardown := setup() 500 defer teardown() 501 502 mux.HandleFunc("/orgs/org/teams/slug/repos/o/r", func(w http.ResponseWriter, r *http.Request) { 503 testMethod(t, r, "GET") 504 w.WriteHeader(http.StatusNotFound) 505 }) 506 507 repo, resp, err := client.Teams.IsTeamRepoBySlug(context.Background(), "org", "slug", "owner", "repo") 508 if err == nil { 509 t.Errorf("Expected HTTP 404 response") 510 } 511 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 512 t.Errorf("Teams.IsTeamRepoByID returned status %d, want %d", got, want) 513 } 514 if repo != nil { 515 t.Errorf("Teams.IsTeamRepoByID returned %+v, want nil", repo) 516 } 517 } 518 519 func TestTeamsService_IsTeamRepoByID_error(t *testing.T) { 520 client, mux, _, teardown := setup() 521 defer teardown() 522 523 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 524 testMethod(t, r, "GET") 525 http.Error(w, "BadRequest", http.StatusBadRequest) 526 }) 527 528 repo, resp, err := client.Teams.IsTeamRepoByID(context.Background(), 1, 1, "owner", "repo") 529 if err == nil { 530 t.Errorf("Expected HTTP 400 response") 531 } 532 if got, want := resp.Response.StatusCode, http.StatusBadRequest; got != want { 533 t.Errorf("Teams.IsTeamRepoByID returned status %d, want %d", got, want) 534 } 535 if repo != nil { 536 t.Errorf("Teams.IsTeamRepoByID returned %+v, want nil", repo) 537 } 538 } 539 540 func TestTeamsService_IsTeamRepoBySlug_error(t *testing.T) { 541 client, mux, _, teardown := setup() 542 defer teardown() 543 544 mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 545 testMethod(t, r, "GET") 546 http.Error(w, "BadRequest", http.StatusBadRequest) 547 }) 548 549 repo, resp, err := client.Teams.IsTeamRepoBySlug(context.Background(), "org", "slug", "owner", "repo") 550 if err == nil { 551 t.Errorf("Expected HTTP 400 response") 552 } 553 if got, want := resp.Response.StatusCode, http.StatusBadRequest; got != want { 554 t.Errorf("Teams.IsTeamRepoBySlug returned status %d, want %d", got, want) 555 } 556 if repo != nil { 557 t.Errorf("Teams.IsTeamRepoBySlug returned %+v, want nil", repo) 558 } 559 } 560 561 func TestTeamsService_IsTeamRepoByID_invalidOwner(t *testing.T) { 562 client, _, _, teardown := setup() 563 defer teardown() 564 565 _, _, err := client.Teams.IsTeamRepoByID(context.Background(), 1, 1, "%", "r") 566 testURLParseError(t, err) 567 } 568 569 func TestTeamsService_IsTeamRepoBySlug_invalidOwner(t *testing.T) { 570 client, _, _, teardown := setup() 571 defer teardown() 572 573 _, _, err := client.Teams.IsTeamRepoBySlug(context.Background(), "o", "s", "%", "r") 574 testURLParseError(t, err) 575 } 576 577 func TestTeamsService_AddTeamRepoByID(t *testing.T) { 578 client, mux, _, teardown := setup() 579 defer teardown() 580 581 opt := &TeamAddTeamRepoOptions{Permission: "admin"} 582 583 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 584 v := new(TeamAddTeamRepoOptions) 585 json.NewDecoder(r.Body).Decode(v) 586 587 testMethod(t, r, "PUT") 588 if !reflect.DeepEqual(v, opt) { 589 t.Errorf("Request body = %+v, want %+v", v, opt) 590 } 591 592 w.WriteHeader(http.StatusNoContent) 593 }) 594 595 _, err := client.Teams.AddTeamRepoByID(context.Background(), 1, 1, "owner", "repo", opt) 596 if err != nil { 597 t.Errorf("Teams.AddTeamRepoByID returned error: %v", err) 598 } 599 } 600 601 func TestTeamsService_AddTeamRepoBySlug(t *testing.T) { 602 client, mux, _, teardown := setup() 603 defer teardown() 604 605 opt := &TeamAddTeamRepoOptions{Permission: "admin"} 606 607 mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 608 v := new(TeamAddTeamRepoOptions) 609 json.NewDecoder(r.Body).Decode(v) 610 611 testMethod(t, r, "PUT") 612 if !reflect.DeepEqual(v, opt) { 613 t.Errorf("Request body = %+v, want %+v", v, opt) 614 } 615 616 w.WriteHeader(http.StatusNoContent) 617 }) 618 619 _, err := client.Teams.AddTeamRepoBySlug(context.Background(), "org", "slug", "owner", "repo", opt) 620 if err != nil { 621 t.Errorf("Teams.AddTeamRepoBySlug returned error: %v", err) 622 } 623 } 624 625 func TestTeamsService_AddTeamRepoByID_noAccess(t *testing.T) { 626 client, mux, _, teardown := setup() 627 defer teardown() 628 629 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 630 testMethod(t, r, "PUT") 631 w.WriteHeader(http.StatusUnprocessableEntity) 632 }) 633 634 _, err := client.Teams.AddTeamRepoByID(context.Background(), 1, 1, "owner", "repo", nil) 635 if err == nil { 636 t.Errorf("Expcted error to be returned") 637 } 638 } 639 640 func TestTeamsService_AddTeamRepoBySlug_noAccess(t *testing.T) { 641 client, mux, _, teardown := setup() 642 defer teardown() 643 644 mux.HandleFunc("/orgs/org/teams/slug/repos/o/r", func(w http.ResponseWriter, r *http.Request) { 645 testMethod(t, r, "PUT") 646 w.WriteHeader(http.StatusUnprocessableEntity) 647 }) 648 649 _, err := client.Teams.AddTeamRepoBySlug(context.Background(), "org", "slug", "owner", "repo", nil) 650 if err == nil { 651 t.Errorf("Expcted error to be returned") 652 } 653 } 654 655 func TestTeamsService_AddTeamRepoByID_invalidOwner(t *testing.T) { 656 client, _, _, teardown := setup() 657 defer teardown() 658 659 _, err := client.Teams.AddTeamRepoByID(context.Background(), 1, 1, "%", "r", nil) 660 testURLParseError(t, err) 661 } 662 663 func TestTeamsService_AddTeamRepoBySlug_invalidOwner(t *testing.T) { 664 client, _, _, teardown := setup() 665 defer teardown() 666 667 _, err := client.Teams.AddTeamRepoBySlug(context.Background(), "o", "s", "%", "r", nil) 668 testURLParseError(t, err) 669 } 670 671 func TestTeamsService_RemoveTeamRepoByID(t *testing.T) { 672 client, mux, _, teardown := setup() 673 defer teardown() 674 675 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 676 testMethod(t, r, "DELETE") 677 w.WriteHeader(http.StatusNoContent) 678 }) 679 680 _, err := client.Teams.RemoveTeamRepoByID(context.Background(), 1, 1, "owner", "repo") 681 if err != nil { 682 t.Errorf("Teams.RemoveTeamRepoByID returned error: %v", err) 683 } 684 } 685 686 func TestTeamsService_RemoveTeamRepoBySlug(t *testing.T) { 687 client, mux, _, teardown := setup() 688 defer teardown() 689 690 mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 691 testMethod(t, r, "DELETE") 692 w.WriteHeader(http.StatusNoContent) 693 }) 694 695 _, err := client.Teams.RemoveTeamRepoBySlug(context.Background(), "org", "slug", "owner", "repo") 696 if err != nil { 697 t.Errorf("Teams.RemoveTeamRepoBySlug returned error: %v", err) 698 } 699 } 700 701 func TestTeamsService_RemoveTeamRepoByID_invalidOwner(t *testing.T) { 702 client, _, _, teardown := setup() 703 defer teardown() 704 705 _, err := client.Teams.RemoveTeamRepoByID(context.Background(), 1, 1, "%", "r") 706 testURLParseError(t, err) 707 } 708 709 func TestTeamsService_RemoveTeamRepoBySlug_invalidOwner(t *testing.T) { 710 client, _, _, teardown := setup() 711 defer teardown() 712 713 _, err := client.Teams.RemoveTeamRepoBySlug(context.Background(), "o", "s", "%", "r") 714 testURLParseError(t, err) 715 } 716 717 func TestTeamsService_ListUserTeams(t *testing.T) { 718 client, mux, _, teardown := setup() 719 defer teardown() 720 721 mux.HandleFunc("/user/teams", func(w http.ResponseWriter, r *http.Request) { 722 testMethod(t, r, "GET") 723 testFormValues(t, r, values{"page": "1"}) 724 fmt.Fprint(w, `[{"id":1}]`) 725 }) 726 727 opt := &ListOptions{Page: 1} 728 teams, _, err := client.Teams.ListUserTeams(context.Background(), opt) 729 if err != nil { 730 t.Errorf("Teams.ListUserTeams returned error: %v", err) 731 } 732 733 want := []*Team{{ID: Int64(1)}} 734 if !reflect.DeepEqual(teams, want) { 735 t.Errorf("Teams.ListUserTeams returned %+v, want %+v", teams, want) 736 } 737 } 738 739 func TestTeamsService_ListProjectsByID(t *testing.T) { 740 client, mux, _, teardown := setup() 741 defer teardown() 742 743 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 744 mux.HandleFunc("/organizations/1/team/1/projects", func(w http.ResponseWriter, r *http.Request) { 745 testMethod(t, r, "GET") 746 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 747 fmt.Fprint(w, `[{"id":1}]`) 748 }) 749 750 projects, _, err := client.Teams.ListTeamProjectsByID(context.Background(), 1, 1) 751 if err != nil { 752 t.Errorf("Teams.ListTeamProjectsByID returned error: %v", err) 753 } 754 755 want := []*Project{{ID: Int64(1)}} 756 if !reflect.DeepEqual(projects, want) { 757 t.Errorf("Teams.ListTeamProjectsByID returned %+v, want %+v", projects, want) 758 } 759 } 760 761 func TestTeamsService_ListProjectsBySlug(t *testing.T) { 762 client, mux, _, teardown := setup() 763 defer teardown() 764 765 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 766 mux.HandleFunc("/orgs/o/teams/s/projects", func(w http.ResponseWriter, r *http.Request) { 767 testMethod(t, r, "GET") 768 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 769 fmt.Fprint(w, `[{"id":1}]`) 770 }) 771 772 projects, _, err := client.Teams.ListTeamProjectsBySlug(context.Background(), "o", "s") 773 if err != nil { 774 t.Errorf("Teams.ListTeamProjectsBySlug returned error: %v", err) 775 } 776 777 want := []*Project{{ID: Int64(1)}} 778 if !reflect.DeepEqual(projects, want) { 779 t.Errorf("Teams.ListTeamProjectsBySlug returned %+v, want %+v", projects, want) 780 } 781 } 782 783 func TestTeamsService_ReviewProjectsByID(t *testing.T) { 784 client, mux, _, teardown := setup() 785 defer teardown() 786 787 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 788 mux.HandleFunc("/organizations/1/team/1/projects/1", func(w http.ResponseWriter, r *http.Request) { 789 testMethod(t, r, "GET") 790 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 791 fmt.Fprint(w, `{"id":1}`) 792 }) 793 794 project, _, err := client.Teams.ReviewTeamProjectsByID(context.Background(), 1, 1, 1) 795 if err != nil { 796 t.Errorf("Teams.ReviewTeamProjectsByID returned error: %v", err) 797 } 798 799 want := &Project{ID: Int64(1)} 800 if !reflect.DeepEqual(project, want) { 801 t.Errorf("Teams.ReviewTeamProjectsByID returned %+v, want %+v", project, want) 802 } 803 } 804 805 func TestTeamsService_ReviewProjectsBySlug(t *testing.T) { 806 client, mux, _, teardown := setup() 807 defer teardown() 808 809 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 810 mux.HandleFunc("/orgs/o/teams/s/projects/1", func(w http.ResponseWriter, r *http.Request) { 811 testMethod(t, r, "GET") 812 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 813 fmt.Fprint(w, `{"id":1}`) 814 }) 815 816 project, _, err := client.Teams.ReviewTeamProjectsBySlug(context.Background(), "o", "s", 1) 817 if err != nil { 818 t.Errorf("Teams.ReviewTeamProjectsBySlug returned error: %v", err) 819 } 820 821 want := &Project{ID: Int64(1)} 822 if !reflect.DeepEqual(project, want) { 823 t.Errorf("Teams.ReviewTeamProjectsBySlug returned %+v, want %+v", project, want) 824 } 825 } 826 827 func TestTeamsService_AddTeamProjectByID(t *testing.T) { 828 client, mux, _, teardown := setup() 829 defer teardown() 830 831 opt := &TeamProjectOptions{ 832 Permission: String("admin"), 833 } 834 835 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 836 mux.HandleFunc("/organizations/1/team/1/projects/1", func(w http.ResponseWriter, r *http.Request) { 837 testMethod(t, r, "PUT") 838 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 839 840 v := &TeamProjectOptions{} 841 json.NewDecoder(r.Body).Decode(v) 842 if !reflect.DeepEqual(v, opt) { 843 t.Errorf("Request body = %+v, want %+v", v, opt) 844 } 845 846 w.WriteHeader(http.StatusNoContent) 847 }) 848 849 _, err := client.Teams.AddTeamProjectByID(context.Background(), 1, 1, 1, opt) 850 if err != nil { 851 t.Errorf("Teams.AddTeamProjectByID returned error: %v", err) 852 } 853 } 854 855 func TestTeamsService_AddTeamProjectBySlug(t *testing.T) { 856 client, mux, _, teardown := setup() 857 defer teardown() 858 859 opt := &TeamProjectOptions{ 860 Permission: String("admin"), 861 } 862 863 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 864 mux.HandleFunc("/orgs/o/teams/s/projects/1", func(w http.ResponseWriter, r *http.Request) { 865 testMethod(t, r, "PUT") 866 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 867 868 v := &TeamProjectOptions{} 869 json.NewDecoder(r.Body).Decode(v) 870 if !reflect.DeepEqual(v, opt) { 871 t.Errorf("Request body = %+v, want %+v", v, opt) 872 } 873 874 w.WriteHeader(http.StatusNoContent) 875 }) 876 877 _, err := client.Teams.AddTeamProjectBySlug(context.Background(), "o", "s", 1, opt) 878 if err != nil { 879 t.Errorf("Teams.AddTeamProjectBySlug returned error: %v", err) 880 } 881 } 882 883 func TestTeamsService_RemoveTeamProjectByID(t *testing.T) { 884 client, mux, _, teardown := setup() 885 defer teardown() 886 887 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 888 mux.HandleFunc("/organizations/1/team/1/projects/1", func(w http.ResponseWriter, r *http.Request) { 889 testMethod(t, r, "DELETE") 890 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 891 w.WriteHeader(http.StatusNoContent) 892 }) 893 894 _, err := client.Teams.RemoveTeamProjectByID(context.Background(), 1, 1, 1) 895 if err != nil { 896 t.Errorf("Teams.RemoveTeamProjectByID returned error: %v", err) 897 } 898 } 899 900 func TestTeamsService_RemoveTeamProjectBySlug(t *testing.T) { 901 client, mux, _, teardown := setup() 902 defer teardown() 903 904 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 905 mux.HandleFunc("/orgs/o/teams/s/projects/1", func(w http.ResponseWriter, r *http.Request) { 906 testMethod(t, r, "DELETE") 907 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 908 w.WriteHeader(http.StatusNoContent) 909 }) 910 911 _, err := client.Teams.RemoveTeamProjectBySlug(context.Background(), "o", "s", 1) 912 if err != nil { 913 t.Errorf("Teams.RemoveTeamProjectBySlug returned error: %v", err) 914 } 915 } 916 917 func TestTeamsService_ListIDPGroupsInOrganization(t *testing.T) { 918 client, mux, _, teardown := setup() 919 defer teardown() 920 921 mux.HandleFunc("/orgs/o/team-sync/groups", func(w http.ResponseWriter, r *http.Request) { 922 testMethod(t, r, "GET") 923 testFormValues(t, r, values{ 924 "page": "url-encoded-next-page-token", 925 }) 926 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 927 }) 928 929 opt := &ListCursorOptions{Page: "url-encoded-next-page-token"} 930 groups, _, err := client.Teams.ListIDPGroupsInOrganization(context.Background(), "o", opt) 931 if err != nil { 932 t.Errorf("Teams.ListIDPGroupsInOrganization returned error: %v", err) 933 } 934 935 want := &IDPGroupList{ 936 Groups: []*IDPGroup{ 937 { 938 GroupID: String("1"), 939 GroupName: String("n"), 940 GroupDescription: String("d"), 941 }, 942 }, 943 } 944 if !reflect.DeepEqual(groups, want) { 945 t.Errorf("Teams.ListIDPGroupsInOrganization returned %+v. want %+v", groups, want) 946 } 947 } 948 949 func TestTeamsService_ListIDPGroupsForTeamByID(t *testing.T) { 950 client, mux, _, teardown := setup() 951 defer teardown() 952 953 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 954 testMethod(t, r, "GET") 955 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 956 }) 957 958 groups, _, err := client.Teams.ListIDPGroupsForTeamByID(context.Background(), 1, 1) 959 if err != nil { 960 t.Errorf("Teams.ListIDPGroupsForTeamByID returned error: %v", err) 961 } 962 963 want := &IDPGroupList{ 964 Groups: []*IDPGroup{ 965 { 966 GroupID: String("1"), 967 GroupName: String("n"), 968 GroupDescription: String("d"), 969 }, 970 }, 971 } 972 if !reflect.DeepEqual(groups, want) { 973 t.Errorf("Teams.ListIDPGroupsForTeamByID returned %+v. want %+v", groups, want) 974 } 975 } 976 977 func TestTeamsService_ListIDPGroupsForTeamBySlug(t *testing.T) { 978 client, mux, _, teardown := setup() 979 defer teardown() 980 981 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 982 testMethod(t, r, "GET") 983 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 984 }) 985 986 groups, _, err := client.Teams.ListIDPGroupsForTeamBySlug(context.Background(), "o", "slug") 987 if err != nil { 988 t.Errorf("Teams.ListIDPGroupsForTeamBySlug returned error: %v", err) 989 } 990 991 want := &IDPGroupList{ 992 Groups: []*IDPGroup{ 993 { 994 GroupID: String("1"), 995 GroupName: String("n"), 996 GroupDescription: String("d"), 997 }, 998 }, 999 } 1000 if !reflect.DeepEqual(groups, want) { 1001 t.Errorf("Teams.ListIDPGroupsForTeamBySlug returned %+v. want %+v", groups, want) 1002 } 1003 } 1004 1005 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsByID(t *testing.T) { 1006 client, mux, _, teardown := setup() 1007 defer teardown() 1008 1009 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1010 testMethod(t, r, "PATCH") 1011 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1012 }) 1013 1014 input := IDPGroupList{ 1015 Groups: []*IDPGroup{ 1016 { 1017 GroupID: String("1"), 1018 GroupName: String("n"), 1019 GroupDescription: String("d"), 1020 }, 1021 }, 1022 } 1023 1024 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsByID(context.Background(), 1, 1, input) 1025 if err != nil { 1026 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned error: %v", err) 1027 } 1028 1029 want := &IDPGroupList{ 1030 Groups: []*IDPGroup{ 1031 { 1032 GroupID: String("1"), 1033 GroupName: String("n"), 1034 GroupDescription: String("d"), 1035 }, 1036 }, 1037 } 1038 if !reflect.DeepEqual(groups, want) { 1039 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned %+v. want %+v", groups, want) 1040 } 1041 } 1042 1043 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsBySlug(t *testing.T) { 1044 client, mux, _, teardown := setup() 1045 defer teardown() 1046 1047 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1048 testMethod(t, r, "PATCH") 1049 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1050 }) 1051 1052 input := IDPGroupList{ 1053 Groups: []*IDPGroup{ 1054 { 1055 GroupID: String("1"), 1056 GroupName: String("n"), 1057 GroupDescription: String("d"), 1058 }, 1059 }, 1060 } 1061 1062 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(context.Background(), "o", "slug", input) 1063 if err != nil { 1064 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned error: %v", err) 1065 } 1066 1067 want := &IDPGroupList{ 1068 Groups: []*IDPGroup{ 1069 { 1070 GroupID: String("1"), 1071 GroupName: String("n"), 1072 GroupDescription: String("d"), 1073 }, 1074 }, 1075 } 1076 if !reflect.DeepEqual(groups, want) { 1077 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned %+v. want %+v", groups, want) 1078 } 1079 } 1080 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsByID_empty(t *testing.T) { 1081 client, mux, _, teardown := setup() 1082 defer teardown() 1083 1084 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1085 testMethod(t, r, "PATCH") 1086 fmt.Fprint(w, `{"groups": []}`) 1087 }) 1088 1089 input := IDPGroupList{ 1090 Groups: []*IDPGroup{}, 1091 } 1092 1093 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsByID(context.Background(), 1, 1, input) 1094 if err != nil { 1095 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned error: %v", err) 1096 } 1097 1098 want := &IDPGroupList{ 1099 Groups: []*IDPGroup{}, 1100 } 1101 if !reflect.DeepEqual(groups, want) { 1102 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned %+v. want %+v", groups, want) 1103 } 1104 } 1105 1106 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsBySlug_empty(t *testing.T) { 1107 client, mux, _, teardown := setup() 1108 defer teardown() 1109 1110 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1111 testMethod(t, r, "PATCH") 1112 fmt.Fprint(w, `{"groups": []}`) 1113 }) 1114 1115 input := IDPGroupList{ 1116 Groups: []*IDPGroup{}, 1117 } 1118 1119 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(context.Background(), "o", "slug", input) 1120 if err != nil { 1121 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned error: %v", err) 1122 } 1123 1124 want := &IDPGroupList{ 1125 Groups: []*IDPGroup{}, 1126 } 1127 if !reflect.DeepEqual(groups, want) { 1128 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned %+v. want %+v", groups, want) 1129 } 1130 }