github.com/google/go-github/v64@v64.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" 14 "net/http" 15 "strings" 16 "testing" 17 18 "github.com/google/go-cmp/cmp" 19 ) 20 21 func TestTeamsService_ListTeams(t *testing.T) { 22 client, mux, _, teardown := setup() 23 defer teardown() 24 25 mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) { 26 testMethod(t, r, "GET") 27 testFormValues(t, r, values{"page": "2"}) 28 fmt.Fprint(w, `[{"id":1}]`) 29 }) 30 31 opt := &ListOptions{Page: 2} 32 ctx := context.Background() 33 teams, _, err := client.Teams.ListTeams(ctx, "o", opt) 34 if err != nil { 35 t.Errorf("Teams.ListTeams returned error: %v", err) 36 } 37 38 want := []*Team{{ID: Int64(1)}} 39 if !cmp.Equal(teams, want) { 40 t.Errorf("Teams.ListTeams returned %+v, want %+v", teams, want) 41 } 42 43 const methodName = "ListTeams" 44 testBadOptions(t, methodName, func() (err error) { 45 _, _, err = client.Teams.ListTeams(ctx, "\n", opt) 46 return err 47 }) 48 49 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 50 got, resp, err := client.Teams.ListTeams(ctx, "o", opt) 51 if got != nil { 52 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 53 } 54 return resp, err 55 }) 56 } 57 58 func TestTeamsService_ListTeams_invalidOrg(t *testing.T) { 59 client, _, _, teardown := setup() 60 defer teardown() 61 62 ctx := context.Background() 63 _, _, err := client.Teams.ListTeams(ctx, "%", nil) 64 testURLParseError(t, err) 65 } 66 67 func TestTeamsService_GetTeamByID(t *testing.T) { 68 client, mux, _, teardown := setup() 69 defer teardown() 70 71 mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { 72 testMethod(t, r, "GET") 73 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}`) 74 }) 75 76 ctx := context.Background() 77 team, _, err := client.Teams.GetTeamByID(ctx, 1, 1) 78 if err != nil { 79 t.Errorf("Teams.GetTeamByID returned error: %v", err) 80 } 81 82 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")} 83 if !cmp.Equal(team, want) { 84 t.Errorf("Teams.GetTeamByID returned %+v, want %+v", team, want) 85 } 86 87 const methodName = "GetTeamByID" 88 testBadOptions(t, methodName, func() (err error) { 89 _, _, err = client.Teams.GetTeamByID(ctx, -1, -1) 90 return err 91 }) 92 93 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 94 got, resp, err := client.Teams.GetTeamByID(ctx, 1, 1) 95 if got != nil { 96 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 97 } 98 return resp, err 99 }) 100 } 101 102 func TestTeamsService_GetTeamByID_notFound(t *testing.T) { 103 client, mux, _, teardown := setup() 104 defer teardown() 105 106 mux.HandleFunc("/organizations/1/team/2", func(w http.ResponseWriter, r *http.Request) { 107 testMethod(t, r, "GET") 108 w.WriteHeader(http.StatusNotFound) 109 }) 110 111 ctx := context.Background() 112 team, resp, err := client.Teams.GetTeamByID(ctx, 1, 2) 113 if err == nil { 114 t.Errorf("Expected HTTP 404 response") 115 } 116 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 117 t.Errorf("Teams.GetTeamByID returned status %d, want %d", got, want) 118 } 119 if team != nil { 120 t.Errorf("Teams.GetTeamByID returned %+v, want nil", team) 121 } 122 } 123 124 func TestTeamsService_GetTeamBySlug(t *testing.T) { 125 client, mux, _, teardown := setup() 126 defer teardown() 127 128 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 129 testMethod(t, r, "GET") 130 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}`) 131 }) 132 133 ctx := context.Background() 134 team, _, err := client.Teams.GetTeamBySlug(ctx, "o", "s") 135 if err != nil { 136 t.Errorf("Teams.GetTeamBySlug returned error: %v", err) 137 } 138 139 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")} 140 if !cmp.Equal(team, want) { 141 t.Errorf("Teams.GetTeamBySlug returned %+v, want %+v", team, want) 142 } 143 144 const methodName = "GetTeamBySlug" 145 testBadOptions(t, methodName, func() (err error) { 146 _, _, err = client.Teams.GetTeamBySlug(ctx, "\n", "\n") 147 return err 148 }) 149 150 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 151 got, resp, err := client.Teams.GetTeamBySlug(ctx, "o", "s") 152 if got != nil { 153 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 154 } 155 return resp, err 156 }) 157 } 158 159 func TestTeamsService_GetTeamBySlug_invalidOrg(t *testing.T) { 160 client, _, _, teardown := setup() 161 defer teardown() 162 163 ctx := context.Background() 164 _, _, err := client.Teams.GetTeamBySlug(ctx, "%", "s") 165 testURLParseError(t, err) 166 } 167 168 func TestTeamsService_GetTeamBySlug_notFound(t *testing.T) { 169 client, mux, _, teardown := setup() 170 defer teardown() 171 172 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 173 testMethod(t, r, "GET") 174 w.WriteHeader(http.StatusNotFound) 175 }) 176 177 ctx := context.Background() 178 team, resp, err := client.Teams.GetTeamBySlug(ctx, "o", "s") 179 if err == nil { 180 t.Errorf("Expected HTTP 404 response") 181 } 182 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 183 t.Errorf("Teams.GetTeamBySlug returned status %d, want %d", got, want) 184 } 185 if team != nil { 186 t.Errorf("Teams.GetTeamBySlug returned %+v, want nil", team) 187 } 188 } 189 190 func TestTeamsService_CreateTeam(t *testing.T) { 191 client, mux, _, teardown := setup() 192 defer teardown() 193 194 input := NewTeam{Name: "n", Privacy: String("closed"), RepoNames: []string{"r"}} 195 196 mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) { 197 v := new(NewTeam) 198 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 199 200 testMethod(t, r, "POST") 201 if !cmp.Equal(v, &input) { 202 t.Errorf("Request body = %+v, want %+v", v, input) 203 } 204 205 fmt.Fprint(w, `{"id":1}`) 206 }) 207 208 ctx := context.Background() 209 team, _, err := client.Teams.CreateTeam(ctx, "o", input) 210 if err != nil { 211 t.Errorf("Teams.CreateTeam returned error: %v", err) 212 } 213 214 want := &Team{ID: Int64(1)} 215 if !cmp.Equal(team, want) { 216 t.Errorf("Teams.CreateTeam returned %+v, want %+v", team, want) 217 } 218 219 const methodName = "CreateTeam" 220 testBadOptions(t, methodName, func() (err error) { 221 _, _, err = client.Teams.CreateTeam(ctx, "\n", input) 222 return err 223 }) 224 225 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 226 got, resp, err := client.Teams.CreateTeam(ctx, "o", input) 227 if got != nil { 228 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 229 } 230 return resp, err 231 }) 232 } 233 234 func TestTeamsService_CreateTeam_invalidOrg(t *testing.T) { 235 client, _, _, teardown := setup() 236 defer teardown() 237 238 ctx := context.Background() 239 _, _, err := client.Teams.CreateTeam(ctx, "%", NewTeam{}) 240 testURLParseError(t, err) 241 } 242 243 func TestTeamsService_EditTeamByID(t *testing.T) { 244 client, mux, _, teardown := setup() 245 defer teardown() 246 247 input := NewTeam{Name: "n", Privacy: String("closed")} 248 249 mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { 250 v := new(NewTeam) 251 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 252 253 testMethod(t, r, "PATCH") 254 if !cmp.Equal(v, &input) { 255 t.Errorf("Request body = %+v, want %+v", v, input) 256 } 257 258 fmt.Fprint(w, `{"id":1}`) 259 }) 260 261 ctx := context.Background() 262 team, _, err := client.Teams.EditTeamByID(ctx, 1, 1, input, false) 263 if err != nil { 264 t.Errorf("Teams.EditTeamByID returned error: %v", err) 265 } 266 267 want := &Team{ID: Int64(1)} 268 if !cmp.Equal(team, want) { 269 t.Errorf("Teams.EditTeamByID returned %+v, want %+v", team, want) 270 } 271 272 const methodName = "EditTeamByID" 273 testBadOptions(t, methodName, func() (err error) { 274 _, _, err = client.Teams.EditTeamByID(ctx, -1, -1, input, false) 275 return err 276 }) 277 278 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 279 got, resp, err := client.Teams.EditTeamByID(ctx, 1, 1, input, false) 280 if got != nil { 281 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 282 } 283 return resp, err 284 }) 285 } 286 287 func TestTeamsService_EditTeamByID_RemoveParent(t *testing.T) { 288 client, mux, _, teardown := setup() 289 defer teardown() 290 291 input := NewTeam{Name: "n", Privacy: String("closed")} 292 var body string 293 294 mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { 295 v := new(NewTeam) 296 buf, err := io.ReadAll(r.Body) 297 if err != nil { 298 t.Errorf("Unable to read body: %v", err) 299 } 300 body = string(buf) 301 assertNilError(t, json.NewDecoder(bytes.NewBuffer(buf)).Decode(v)) 302 303 testMethod(t, r, "PATCH") 304 if !cmp.Equal(v, &input) { 305 t.Errorf("Request body = %+v, want %+v", v, input) 306 } 307 308 fmt.Fprint(w, `{"id":1}`) 309 }) 310 311 ctx := context.Background() 312 team, _, err := client.Teams.EditTeamByID(ctx, 1, 1, input, true) 313 if err != nil { 314 t.Errorf("Teams.EditTeamByID returned error: %v", err) 315 } 316 317 want := &Team{ID: Int64(1)} 318 if !cmp.Equal(team, want) { 319 t.Errorf("Teams.EditTeamByID returned %+v, want %+v", team, want) 320 } 321 322 if want := `{"name":"n","parent_team_id":null,"privacy":"closed"}` + "\n"; body != want { 323 t.Errorf("Teams.EditTeamByID body = %+v, want %+v", body, want) 324 } 325 } 326 327 func TestTeamsService_EditTeamBySlug(t *testing.T) { 328 client, mux, _, teardown := setup() 329 defer teardown() 330 331 input := NewTeam{Name: "n", Privacy: String("closed")} 332 333 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 334 v := new(NewTeam) 335 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 336 337 testMethod(t, r, "PATCH") 338 if !cmp.Equal(v, &input) { 339 t.Errorf("Request body = %+v, want %+v", v, input) 340 } 341 342 fmt.Fprint(w, `{"id":1}`) 343 }) 344 345 ctx := context.Background() 346 team, _, err := client.Teams.EditTeamBySlug(ctx, "o", "s", input, false) 347 if err != nil { 348 t.Errorf("Teams.EditTeamBySlug returned error: %v", err) 349 } 350 351 want := &Team{ID: Int64(1)} 352 if !cmp.Equal(team, want) { 353 t.Errorf("Teams.EditTeamBySlug returned %+v, want %+v", team, want) 354 } 355 356 const methodName = "EditTeamBySlug" 357 testBadOptions(t, methodName, func() (err error) { 358 _, _, err = client.Teams.EditTeamBySlug(ctx, "\n", "\n", input, false) 359 return err 360 }) 361 362 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 363 got, resp, err := client.Teams.EditTeamBySlug(ctx, "o", "s", input, false) 364 if got != nil { 365 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 366 } 367 return resp, err 368 }) 369 } 370 371 func TestTeamsService_EditTeamBySlug_RemoveParent(t *testing.T) { 372 client, mux, _, teardown := setup() 373 defer teardown() 374 375 input := NewTeam{Name: "n", Privacy: String("closed")} 376 var body string 377 378 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 379 v := new(NewTeam) 380 buf, err := io.ReadAll(r.Body) 381 if err != nil { 382 t.Errorf("Unable to read body: %v", err) 383 } 384 body = string(buf) 385 assertNilError(t, json.NewDecoder(bytes.NewBuffer(buf)).Decode(v)) 386 387 testMethod(t, r, "PATCH") 388 if !cmp.Equal(v, &input) { 389 t.Errorf("Request body = %+v, want %+v", v, input) 390 } 391 392 fmt.Fprint(w, `{"id":1}`) 393 }) 394 395 ctx := context.Background() 396 team, _, err := client.Teams.EditTeamBySlug(ctx, "o", "s", input, true) 397 if err != nil { 398 t.Errorf("Teams.EditTeam returned error: %v", err) 399 } 400 401 want := &Team{ID: Int64(1)} 402 if !cmp.Equal(team, want) { 403 t.Errorf("Teams.EditTeam returned %+v, want %+v", team, want) 404 } 405 406 if want := `{"name":"n","parent_team_id":null,"privacy":"closed"}` + "\n"; body != want { 407 t.Errorf("Teams.EditTeam body = %+v, want %+v", body, want) 408 } 409 } 410 411 func TestTeamsService_DeleteTeamByID(t *testing.T) { 412 client, mux, _, teardown := setup() 413 defer teardown() 414 415 mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { 416 testMethod(t, r, "DELETE") 417 }) 418 419 ctx := context.Background() 420 _, err := client.Teams.DeleteTeamByID(ctx, 1, 1) 421 if err != nil { 422 t.Errorf("Teams.DeleteTeamByID returned error: %v", err) 423 } 424 425 const methodName = "DeleteTeamByID" 426 testBadOptions(t, methodName, func() (err error) { 427 _, err = client.Teams.DeleteTeamByID(ctx, -1, -1) 428 return err 429 }) 430 431 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 432 return client.Teams.DeleteTeamByID(ctx, 1, 1) 433 }) 434 } 435 436 func TestTeamsService_DeleteTeamBySlug(t *testing.T) { 437 client, mux, _, teardown := setup() 438 defer teardown() 439 440 mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { 441 testMethod(t, r, "DELETE") 442 }) 443 444 ctx := context.Background() 445 _, err := client.Teams.DeleteTeamBySlug(ctx, "o", "s") 446 if err != nil { 447 t.Errorf("Teams.DeleteTeamBySlug returned error: %v", err) 448 } 449 450 const methodName = "DeleteTeamBySlug" 451 testBadOptions(t, methodName, func() (err error) { 452 _, err = client.Teams.DeleteTeamBySlug(ctx, "\n", "\n") 453 return err 454 }) 455 456 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 457 return client.Teams.DeleteTeamBySlug(ctx, "o", "s") 458 }) 459 } 460 461 func TestTeamsService_ListChildTeamsByParentID(t *testing.T) { 462 client, mux, _, teardown := setup() 463 defer teardown() 464 465 mux.HandleFunc("/organizations/1/team/2/teams", func(w http.ResponseWriter, r *http.Request) { 466 testMethod(t, r, "GET") 467 testFormValues(t, r, values{"page": "2"}) 468 fmt.Fprint(w, `[{"id":2}]`) 469 }) 470 471 opt := &ListOptions{Page: 2} 472 ctx := context.Background() 473 teams, _, err := client.Teams.ListChildTeamsByParentID(ctx, 1, 2, opt) 474 if err != nil { 475 t.Errorf("Teams.ListChildTeamsByParentID returned error: %v", err) 476 } 477 478 want := []*Team{{ID: Int64(2)}} 479 if !cmp.Equal(teams, want) { 480 t.Errorf("Teams.ListChildTeamsByParentID returned %+v, want %+v", teams, want) 481 } 482 483 const methodName = "ListChildTeamsByParentID" 484 testBadOptions(t, methodName, func() (err error) { 485 _, _, err = client.Teams.ListChildTeamsByParentID(ctx, -1, -2, opt) 486 return err 487 }) 488 489 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 490 got, resp, err := client.Teams.ListChildTeamsByParentID(ctx, 1, 2, opt) 491 if got != nil { 492 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 493 } 494 return resp, err 495 }) 496 } 497 498 func TestTeamsService_ListChildTeamsByParentSlug(t *testing.T) { 499 client, mux, _, teardown := setup() 500 defer teardown() 501 502 mux.HandleFunc("/orgs/o/teams/s/teams", func(w http.ResponseWriter, r *http.Request) { 503 testMethod(t, r, "GET") 504 testFormValues(t, r, values{"page": "2"}) 505 fmt.Fprint(w, `[{"id":2}]`) 506 }) 507 508 opt := &ListOptions{Page: 2} 509 ctx := context.Background() 510 teams, _, err := client.Teams.ListChildTeamsByParentSlug(ctx, "o", "s", opt) 511 if err != nil { 512 t.Errorf("Teams.ListChildTeamsByParentSlug returned error: %v", err) 513 } 514 515 want := []*Team{{ID: Int64(2)}} 516 if !cmp.Equal(teams, want) { 517 t.Errorf("Teams.ListChildTeamsByParentSlug returned %+v, want %+v", teams, want) 518 } 519 520 const methodName = "ListChildTeamsByParentSlug" 521 testBadOptions(t, methodName, func() (err error) { 522 _, _, err = client.Teams.ListChildTeamsByParentSlug(ctx, "\n", "\n", opt) 523 return err 524 }) 525 526 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 527 got, resp, err := client.Teams.ListChildTeamsByParentSlug(ctx, "o", "s", opt) 528 if got != nil { 529 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 530 } 531 return resp, err 532 }) 533 } 534 535 func TestTeamsService_ListTeamReposByID(t *testing.T) { 536 client, mux, _, teardown := setup() 537 defer teardown() 538 539 mux.HandleFunc("/organizations/1/team/1/repos", func(w http.ResponseWriter, r *http.Request) { 540 testMethod(t, r, "GET") 541 wantAcceptHeaders := []string{mediaTypeTopicsPreview} 542 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 543 testFormValues(t, r, values{"page": "2"}) 544 fmt.Fprint(w, `[{"id":1}]`) 545 }) 546 547 opt := &ListOptions{Page: 2} 548 ctx := context.Background() 549 members, _, err := client.Teams.ListTeamReposByID(ctx, 1, 1, opt) 550 if err != nil { 551 t.Errorf("Teams.ListTeamReposByID returned error: %v", err) 552 } 553 554 want := []*Repository{{ID: Int64(1)}} 555 if !cmp.Equal(members, want) { 556 t.Errorf("Teams.ListTeamReposByID returned %+v, want %+v", members, want) 557 } 558 559 const methodName = "ListTeamReposByID" 560 testBadOptions(t, methodName, func() (err error) { 561 _, _, err = client.Teams.ListTeamReposByID(ctx, -1, -1, opt) 562 return err 563 }) 564 565 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 566 got, resp, err := client.Teams.ListTeamReposByID(ctx, 1, 1, opt) 567 if got != nil { 568 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 569 } 570 return resp, err 571 }) 572 } 573 574 func TestTeamsService_ListTeamReposBySlug(t *testing.T) { 575 client, mux, _, teardown := setup() 576 defer teardown() 577 578 mux.HandleFunc("/orgs/o/teams/s/repos", func(w http.ResponseWriter, r *http.Request) { 579 testMethod(t, r, "GET") 580 wantAcceptHeaders := []string{mediaTypeTopicsPreview} 581 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 582 testFormValues(t, r, values{"page": "2"}) 583 fmt.Fprint(w, `[{"id":1}]`) 584 }) 585 586 opt := &ListOptions{Page: 2} 587 ctx := context.Background() 588 members, _, err := client.Teams.ListTeamReposBySlug(ctx, "o", "s", opt) 589 if err != nil { 590 t.Errorf("Teams.ListTeamReposBySlug returned error: %v", err) 591 } 592 593 want := []*Repository{{ID: Int64(1)}} 594 if !cmp.Equal(members, want) { 595 t.Errorf("Teams.ListTeamReposBySlug returned %+v, want %+v", members, want) 596 } 597 598 const methodName = "ListTeamReposBySlug" 599 testBadOptions(t, methodName, func() (err error) { 600 _, _, err = client.Teams.ListTeamReposBySlug(ctx, "\n", "\n", opt) 601 return err 602 }) 603 604 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 605 got, resp, err := client.Teams.ListTeamReposBySlug(ctx, "o", "s", opt) 606 if got != nil { 607 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 608 } 609 return resp, err 610 }) 611 } 612 613 func TestTeamsService_IsTeamRepoByID_true(t *testing.T) { 614 client, mux, _, teardown := setup() 615 defer teardown() 616 617 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 618 testMethod(t, r, "GET") 619 wantAcceptHeaders := []string{mediaTypeOrgPermissionRepo} 620 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 621 fmt.Fprint(w, `{"id":1}`) 622 }) 623 624 ctx := context.Background() 625 repo, _, err := client.Teams.IsTeamRepoByID(ctx, 1, 1, "owner", "repo") 626 if err != nil { 627 t.Errorf("Teams.IsTeamRepoByID returned error: %v", err) 628 } 629 630 want := &Repository{ID: Int64(1)} 631 if !cmp.Equal(repo, want) { 632 t.Errorf("Teams.IsTeamRepoByID returned %+v, want %+v", repo, want) 633 } 634 635 const methodName = "IsTeamRepoByID" 636 testBadOptions(t, methodName, func() (err error) { 637 _, _, err = client.Teams.IsTeamRepoByID(ctx, -1, -1, "\n", "\n") 638 return err 639 }) 640 641 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 642 got, resp, err := client.Teams.IsTeamRepoByID(ctx, 1, 1, "owner", "repo") 643 if got != nil { 644 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 645 } 646 return resp, err 647 }) 648 } 649 650 func TestTeamsService_IsTeamRepoBySlug_true(t *testing.T) { 651 client, mux, _, teardown := setup() 652 defer teardown() 653 654 mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 655 testMethod(t, r, "GET") 656 wantAcceptHeaders := []string{mediaTypeOrgPermissionRepo} 657 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 658 fmt.Fprint(w, `{"id":1}`) 659 }) 660 661 ctx := context.Background() 662 repo, _, err := client.Teams.IsTeamRepoBySlug(ctx, "org", "slug", "owner", "repo") 663 if err != nil { 664 t.Errorf("Teams.IsTeamRepoBySlug returned error: %v", err) 665 } 666 667 want := &Repository{ID: Int64(1)} 668 if !cmp.Equal(repo, want) { 669 t.Errorf("Teams.IsTeamRepoBySlug returned %+v, want %+v", repo, want) 670 } 671 672 const methodName = "IsTeamRepoBySlug" 673 testBadOptions(t, methodName, func() (err error) { 674 _, _, err = client.Teams.IsTeamRepoBySlug(ctx, "\n", "\n", "\n", "\n") 675 return err 676 }) 677 678 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 679 got, resp, err := client.Teams.IsTeamRepoBySlug(ctx, "org", "slug", "owner", "repo") 680 if got != nil { 681 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 682 } 683 return resp, err 684 }) 685 } 686 687 func TestTeamsService_IsTeamRepoByID_false(t *testing.T) { 688 client, mux, _, teardown := setup() 689 defer teardown() 690 691 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 692 testMethod(t, r, "GET") 693 w.WriteHeader(http.StatusNotFound) 694 }) 695 696 ctx := context.Background() 697 repo, resp, err := client.Teams.IsTeamRepoByID(ctx, 1, 1, "owner", "repo") 698 if err == nil { 699 t.Errorf("Expected HTTP 404 response") 700 } 701 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 702 t.Errorf("Teams.IsTeamRepoByID returned status %d, want %d", got, want) 703 } 704 if repo != nil { 705 t.Errorf("Teams.IsTeamRepoByID returned %+v, want nil", repo) 706 } 707 } 708 709 func TestTeamsService_IsTeamRepoBySlug_false(t *testing.T) { 710 client, mux, _, teardown := setup() 711 defer teardown() 712 713 mux.HandleFunc("/orgs/org/teams/slug/repos/o/r", func(w http.ResponseWriter, r *http.Request) { 714 testMethod(t, r, "GET") 715 w.WriteHeader(http.StatusNotFound) 716 }) 717 718 ctx := context.Background() 719 repo, resp, err := client.Teams.IsTeamRepoBySlug(ctx, "org", "slug", "owner", "repo") 720 if err == nil { 721 t.Errorf("Expected HTTP 404 response") 722 } 723 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 724 t.Errorf("Teams.IsTeamRepoByID returned status %d, want %d", got, want) 725 } 726 if repo != nil { 727 t.Errorf("Teams.IsTeamRepoByID returned %+v, want nil", repo) 728 } 729 } 730 731 func TestTeamsService_IsTeamRepoByID_error(t *testing.T) { 732 client, mux, _, teardown := setup() 733 defer teardown() 734 735 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 736 testMethod(t, r, "GET") 737 http.Error(w, "BadRequest", http.StatusBadRequest) 738 }) 739 740 ctx := context.Background() 741 repo, resp, err := client.Teams.IsTeamRepoByID(ctx, 1, 1, "owner", "repo") 742 if err == nil { 743 t.Errorf("Expected HTTP 400 response") 744 } 745 if got, want := resp.Response.StatusCode, http.StatusBadRequest; got != want { 746 t.Errorf("Teams.IsTeamRepoByID returned status %d, want %d", got, want) 747 } 748 if repo != nil { 749 t.Errorf("Teams.IsTeamRepoByID returned %+v, want nil", repo) 750 } 751 } 752 753 func TestTeamsService_IsTeamRepoBySlug_error(t *testing.T) { 754 client, mux, _, teardown := setup() 755 defer teardown() 756 757 mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 758 testMethod(t, r, "GET") 759 http.Error(w, "BadRequest", http.StatusBadRequest) 760 }) 761 762 ctx := context.Background() 763 repo, resp, err := client.Teams.IsTeamRepoBySlug(ctx, "org", "slug", "owner", "repo") 764 if err == nil { 765 t.Errorf("Expected HTTP 400 response") 766 } 767 if got, want := resp.Response.StatusCode, http.StatusBadRequest; got != want { 768 t.Errorf("Teams.IsTeamRepoBySlug returned status %d, want %d", got, want) 769 } 770 if repo != nil { 771 t.Errorf("Teams.IsTeamRepoBySlug returned %+v, want nil", repo) 772 } 773 } 774 775 func TestTeamsService_IsTeamRepoByID_invalidOwner(t *testing.T) { 776 client, _, _, teardown := setup() 777 defer teardown() 778 779 ctx := context.Background() 780 _, _, err := client.Teams.IsTeamRepoByID(ctx, 1, 1, "%", "r") 781 testURLParseError(t, err) 782 } 783 784 func TestTeamsService_IsTeamRepoBySlug_invalidOwner(t *testing.T) { 785 client, _, _, teardown := setup() 786 defer teardown() 787 788 ctx := context.Background() 789 _, _, err := client.Teams.IsTeamRepoBySlug(ctx, "o", "s", "%", "r") 790 testURLParseError(t, err) 791 } 792 793 func TestTeamsService_AddTeamRepoByID(t *testing.T) { 794 client, mux, _, teardown := setup() 795 defer teardown() 796 797 opt := &TeamAddTeamRepoOptions{Permission: "admin"} 798 799 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 800 v := new(TeamAddTeamRepoOptions) 801 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 802 803 testMethod(t, r, "PUT") 804 if !cmp.Equal(v, opt) { 805 t.Errorf("Request body = %+v, want %+v", v, opt) 806 } 807 808 w.WriteHeader(http.StatusNoContent) 809 }) 810 811 ctx := context.Background() 812 _, err := client.Teams.AddTeamRepoByID(ctx, 1, 1, "owner", "repo", opt) 813 if err != nil { 814 t.Errorf("Teams.AddTeamRepoByID returned error: %v", err) 815 } 816 817 const methodName = "AddTeamRepoByID" 818 testBadOptions(t, methodName, func() (err error) { 819 _, err = client.Teams.AddTeamRepoByID(ctx, 1, 1, "\n", "\n", opt) 820 return err 821 }) 822 823 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 824 return client.Teams.AddTeamRepoByID(ctx, 1, 1, "owner", "repo", opt) 825 }) 826 } 827 828 func TestTeamsService_AddTeamRepoBySlug(t *testing.T) { 829 client, mux, _, teardown := setup() 830 defer teardown() 831 832 opt := &TeamAddTeamRepoOptions{Permission: "admin"} 833 834 mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 835 v := new(TeamAddTeamRepoOptions) 836 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 837 838 testMethod(t, r, "PUT") 839 if !cmp.Equal(v, opt) { 840 t.Errorf("Request body = %+v, want %+v", v, opt) 841 } 842 843 w.WriteHeader(http.StatusNoContent) 844 }) 845 846 ctx := context.Background() 847 _, err := client.Teams.AddTeamRepoBySlug(ctx, "org", "slug", "owner", "repo", opt) 848 if err != nil { 849 t.Errorf("Teams.AddTeamRepoBySlug returned error: %v", err) 850 } 851 852 const methodName = "AddTeamRepoBySlug" 853 testBadOptions(t, methodName, func() (err error) { 854 _, err = client.Teams.AddTeamRepoBySlug(ctx, "\n", "\n", "\n", "\n", opt) 855 return err 856 }) 857 858 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 859 return client.Teams.AddTeamRepoBySlug(ctx, "org", "slug", "owner", "repo", opt) 860 }) 861 } 862 863 func TestTeamsService_AddTeamRepoByID_noAccess(t *testing.T) { 864 client, mux, _, teardown := setup() 865 defer teardown() 866 867 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 868 testMethod(t, r, "PUT") 869 w.WriteHeader(http.StatusUnprocessableEntity) 870 }) 871 872 ctx := context.Background() 873 _, err := client.Teams.AddTeamRepoByID(ctx, 1, 1, "owner", "repo", nil) 874 if err == nil { 875 t.Errorf("Expected error to be returned") 876 } 877 } 878 879 func TestTeamsService_AddTeamRepoBySlug_noAccess(t *testing.T) { 880 client, mux, _, teardown := setup() 881 defer teardown() 882 883 mux.HandleFunc("/orgs/org/teams/slug/repos/o/r", func(w http.ResponseWriter, r *http.Request) { 884 testMethod(t, r, "PUT") 885 w.WriteHeader(http.StatusUnprocessableEntity) 886 }) 887 888 ctx := context.Background() 889 _, err := client.Teams.AddTeamRepoBySlug(ctx, "org", "slug", "owner", "repo", nil) 890 if err == nil { 891 t.Errorf("Expected error to be returned") 892 } 893 } 894 895 func TestTeamsService_AddTeamRepoByID_invalidOwner(t *testing.T) { 896 client, _, _, teardown := setup() 897 defer teardown() 898 899 ctx := context.Background() 900 _, err := client.Teams.AddTeamRepoByID(ctx, 1, 1, "%", "r", nil) 901 testURLParseError(t, err) 902 } 903 904 func TestTeamsService_AddTeamRepoBySlug_invalidOwner(t *testing.T) { 905 client, _, _, teardown := setup() 906 defer teardown() 907 908 ctx := context.Background() 909 _, err := client.Teams.AddTeamRepoBySlug(ctx, "o", "s", "%", "r", nil) 910 testURLParseError(t, err) 911 } 912 913 func TestTeamsService_RemoveTeamRepoByID(t *testing.T) { 914 client, mux, _, teardown := setup() 915 defer teardown() 916 917 mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 918 testMethod(t, r, "DELETE") 919 w.WriteHeader(http.StatusNoContent) 920 }) 921 922 ctx := context.Background() 923 _, err := client.Teams.RemoveTeamRepoByID(ctx, 1, 1, "owner", "repo") 924 if err != nil { 925 t.Errorf("Teams.RemoveTeamRepoByID returned error: %v", err) 926 } 927 928 const methodName = "RemoveTeamRepoByID" 929 testBadOptions(t, methodName, func() (err error) { 930 _, err = client.Teams.RemoveTeamRepoByID(ctx, -1, -1, "\n", "\n") 931 return err 932 }) 933 934 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 935 return client.Teams.RemoveTeamRepoByID(ctx, 1, 1, "owner", "repo") 936 }) 937 } 938 939 func TestTeamsService_RemoveTeamRepoBySlug(t *testing.T) { 940 client, mux, _, teardown := setup() 941 defer teardown() 942 943 mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { 944 testMethod(t, r, "DELETE") 945 w.WriteHeader(http.StatusNoContent) 946 }) 947 948 ctx := context.Background() 949 _, err := client.Teams.RemoveTeamRepoBySlug(ctx, "org", "slug", "owner", "repo") 950 if err != nil { 951 t.Errorf("Teams.RemoveTeamRepoBySlug returned error: %v", err) 952 } 953 954 const methodName = "RemoveTeamRepoBySlug" 955 testBadOptions(t, methodName, func() (err error) { 956 _, err = client.Teams.RemoveTeamRepoBySlug(ctx, "\n", "\n", "\n", "\n") 957 return err 958 }) 959 960 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 961 return client.Teams.RemoveTeamRepoBySlug(ctx, "org", "slug", "owner", "repo") 962 }) 963 } 964 965 func TestTeamsService_RemoveTeamRepoByID_invalidOwner(t *testing.T) { 966 client, _, _, teardown := setup() 967 defer teardown() 968 969 ctx := context.Background() 970 _, err := client.Teams.RemoveTeamRepoByID(ctx, 1, 1, "%", "r") 971 testURLParseError(t, err) 972 } 973 974 func TestTeamsService_RemoveTeamRepoBySlug_invalidOwner(t *testing.T) { 975 client, _, _, teardown := setup() 976 defer teardown() 977 978 ctx := context.Background() 979 _, err := client.Teams.RemoveTeamRepoBySlug(ctx, "o", "s", "%", "r") 980 testURLParseError(t, err) 981 } 982 983 func TestTeamsService_ListUserTeams(t *testing.T) { 984 client, mux, _, teardown := setup() 985 defer teardown() 986 987 mux.HandleFunc("/user/teams", func(w http.ResponseWriter, r *http.Request) { 988 testMethod(t, r, "GET") 989 testFormValues(t, r, values{"page": "1"}) 990 fmt.Fprint(w, `[{"id":1}]`) 991 }) 992 993 opt := &ListOptions{Page: 1} 994 ctx := context.Background() 995 teams, _, err := client.Teams.ListUserTeams(ctx, opt) 996 if err != nil { 997 t.Errorf("Teams.ListUserTeams returned error: %v", err) 998 } 999 1000 want := []*Team{{ID: Int64(1)}} 1001 if !cmp.Equal(teams, want) { 1002 t.Errorf("Teams.ListUserTeams returned %+v, want %+v", teams, want) 1003 } 1004 1005 const methodName = "ListUserTeams" 1006 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1007 got, resp, err := client.Teams.ListUserTeams(ctx, opt) 1008 if got != nil { 1009 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1010 } 1011 return resp, err 1012 }) 1013 } 1014 1015 func TestTeamsService_ListProjectsByID(t *testing.T) { 1016 client, mux, _, teardown := setup() 1017 defer teardown() 1018 1019 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 1020 mux.HandleFunc("/organizations/1/team/1/projects", func(w http.ResponseWriter, r *http.Request) { 1021 testMethod(t, r, "GET") 1022 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 1023 fmt.Fprint(w, `[{"id":1}]`) 1024 }) 1025 1026 ctx := context.Background() 1027 projects, _, err := client.Teams.ListTeamProjectsByID(ctx, 1, 1) 1028 if err != nil { 1029 t.Errorf("Teams.ListTeamProjectsByID returned error: %v", err) 1030 } 1031 1032 want := []*Project{{ID: Int64(1)}} 1033 if !cmp.Equal(projects, want) { 1034 t.Errorf("Teams.ListTeamProjectsByID returned %+v, want %+v", projects, want) 1035 } 1036 1037 const methodName = "ListTeamProjectsByID" 1038 testBadOptions(t, methodName, func() (err error) { 1039 _, _, err = client.Teams.ListTeamProjectsByID(ctx, -1, -1) 1040 return err 1041 }) 1042 1043 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1044 got, resp, err := client.Teams.ListTeamProjectsByID(ctx, 1, 1) 1045 if got != nil { 1046 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1047 } 1048 return resp, err 1049 }) 1050 } 1051 1052 func TestTeamsService_ListProjectsBySlug(t *testing.T) { 1053 client, mux, _, teardown := setup() 1054 defer teardown() 1055 1056 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 1057 mux.HandleFunc("/orgs/o/teams/s/projects", func(w http.ResponseWriter, r *http.Request) { 1058 testMethod(t, r, "GET") 1059 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 1060 fmt.Fprint(w, `[{"id":1}]`) 1061 }) 1062 1063 ctx := context.Background() 1064 projects, _, err := client.Teams.ListTeamProjectsBySlug(ctx, "o", "s") 1065 if err != nil { 1066 t.Errorf("Teams.ListTeamProjectsBySlug returned error: %v", err) 1067 } 1068 1069 want := []*Project{{ID: Int64(1)}} 1070 if !cmp.Equal(projects, want) { 1071 t.Errorf("Teams.ListTeamProjectsBySlug returned %+v, want %+v", projects, want) 1072 } 1073 1074 const methodName = "ListTeamProjectsBySlug" 1075 testBadOptions(t, methodName, func() (err error) { 1076 _, _, err = client.Teams.ListTeamProjectsBySlug(ctx, "\n", "\n") 1077 return err 1078 }) 1079 1080 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1081 got, resp, err := client.Teams.ListTeamProjectsBySlug(ctx, "o", "s") 1082 if got != nil { 1083 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1084 } 1085 return resp, err 1086 }) 1087 } 1088 1089 func TestTeamsService_ReviewProjectsByID(t *testing.T) { 1090 client, mux, _, teardown := setup() 1091 defer teardown() 1092 1093 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 1094 mux.HandleFunc("/organizations/1/team/1/projects/1", func(w http.ResponseWriter, r *http.Request) { 1095 testMethod(t, r, "GET") 1096 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 1097 fmt.Fprint(w, `{"id":1}`) 1098 }) 1099 1100 ctx := context.Background() 1101 project, _, err := client.Teams.ReviewTeamProjectsByID(ctx, 1, 1, 1) 1102 if err != nil { 1103 t.Errorf("Teams.ReviewTeamProjectsByID returned error: %v", err) 1104 } 1105 1106 want := &Project{ID: Int64(1)} 1107 if !cmp.Equal(project, want) { 1108 t.Errorf("Teams.ReviewTeamProjectsByID returned %+v, want %+v", project, want) 1109 } 1110 1111 const methodName = "ReviewTeamProjectsByID" 1112 testBadOptions(t, methodName, func() (err error) { 1113 _, _, err = client.Teams.ReviewTeamProjectsByID(ctx, -1, -1, -1) 1114 return err 1115 }) 1116 1117 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1118 got, resp, err := client.Teams.ReviewTeamProjectsByID(ctx, 1, 1, 1) 1119 if got != nil { 1120 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1121 } 1122 return resp, err 1123 }) 1124 } 1125 1126 func TestTeamsService_ReviewProjectsBySlug(t *testing.T) { 1127 client, mux, _, teardown := setup() 1128 defer teardown() 1129 1130 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 1131 mux.HandleFunc("/orgs/o/teams/s/projects/1", func(w http.ResponseWriter, r *http.Request) { 1132 testMethod(t, r, "GET") 1133 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 1134 fmt.Fprint(w, `{"id":1}`) 1135 }) 1136 1137 ctx := context.Background() 1138 project, _, err := client.Teams.ReviewTeamProjectsBySlug(ctx, "o", "s", 1) 1139 if err != nil { 1140 t.Errorf("Teams.ReviewTeamProjectsBySlug returned error: %v", err) 1141 } 1142 1143 want := &Project{ID: Int64(1)} 1144 if !cmp.Equal(project, want) { 1145 t.Errorf("Teams.ReviewTeamProjectsBySlug returned %+v, want %+v", project, want) 1146 } 1147 1148 const methodName = "ReviewTeamProjectsBySlug" 1149 testBadOptions(t, methodName, func() (err error) { 1150 _, _, err = client.Teams.ReviewTeamProjectsBySlug(ctx, "\n", "\n", -1) 1151 return err 1152 }) 1153 1154 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1155 got, resp, err := client.Teams.ReviewTeamProjectsBySlug(ctx, "o", "s", 1) 1156 if got != nil { 1157 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1158 } 1159 return resp, err 1160 }) 1161 } 1162 1163 func TestTeamsService_AddTeamProjectByID(t *testing.T) { 1164 client, mux, _, teardown := setup() 1165 defer teardown() 1166 1167 opt := &TeamProjectOptions{ 1168 Permission: String("admin"), 1169 } 1170 1171 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 1172 mux.HandleFunc("/organizations/1/team/1/projects/1", func(w http.ResponseWriter, r *http.Request) { 1173 testMethod(t, r, "PUT") 1174 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 1175 1176 v := &TeamProjectOptions{} 1177 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 1178 if !cmp.Equal(v, opt) { 1179 t.Errorf("Request body = %+v, want %+v", v, opt) 1180 } 1181 1182 w.WriteHeader(http.StatusNoContent) 1183 }) 1184 1185 ctx := context.Background() 1186 _, err := client.Teams.AddTeamProjectByID(ctx, 1, 1, 1, opt) 1187 if err != nil { 1188 t.Errorf("Teams.AddTeamProjectByID returned error: %v", err) 1189 } 1190 1191 const methodName = "AddTeamProjectByID" 1192 testBadOptions(t, methodName, func() (err error) { 1193 _, err = client.Teams.AddTeamProjectByID(ctx, -1, -1, -1, opt) 1194 return err 1195 }) 1196 1197 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1198 return client.Teams.AddTeamProjectByID(ctx, 1, 1, 1, opt) 1199 }) 1200 } 1201 1202 func TestTeamsService_AddTeamProjectBySlug(t *testing.T) { 1203 client, mux, _, teardown := setup() 1204 defer teardown() 1205 1206 opt := &TeamProjectOptions{ 1207 Permission: String("admin"), 1208 } 1209 1210 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 1211 mux.HandleFunc("/orgs/o/teams/s/projects/1", func(w http.ResponseWriter, r *http.Request) { 1212 testMethod(t, r, "PUT") 1213 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 1214 1215 v := &TeamProjectOptions{} 1216 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 1217 if !cmp.Equal(v, opt) { 1218 t.Errorf("Request body = %+v, want %+v", v, opt) 1219 } 1220 1221 w.WriteHeader(http.StatusNoContent) 1222 }) 1223 1224 ctx := context.Background() 1225 _, err := client.Teams.AddTeamProjectBySlug(ctx, "o", "s", 1, opt) 1226 if err != nil { 1227 t.Errorf("Teams.AddTeamProjectBySlug returned error: %v", err) 1228 } 1229 1230 const methodName = "AddTeamProjectBySlug" 1231 testBadOptions(t, methodName, func() (err error) { 1232 _, err = client.Teams.AddTeamProjectBySlug(ctx, "\n", "\n", -1, opt) 1233 return err 1234 }) 1235 1236 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1237 return client.Teams.AddTeamProjectBySlug(ctx, "o", "s", 1, opt) 1238 }) 1239 } 1240 1241 func TestTeamsService_RemoveTeamProjectByID(t *testing.T) { 1242 client, mux, _, teardown := setup() 1243 defer teardown() 1244 1245 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 1246 mux.HandleFunc("/organizations/1/team/1/projects/1", func(w http.ResponseWriter, r *http.Request) { 1247 testMethod(t, r, "DELETE") 1248 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 1249 w.WriteHeader(http.StatusNoContent) 1250 }) 1251 1252 ctx := context.Background() 1253 _, err := client.Teams.RemoveTeamProjectByID(ctx, 1, 1, 1) 1254 if err != nil { 1255 t.Errorf("Teams.RemoveTeamProjectByID returned error: %v", err) 1256 } 1257 1258 const methodName = "RemoveTeamProjectByID" 1259 testBadOptions(t, methodName, func() (err error) { 1260 _, err = client.Teams.RemoveTeamProjectByID(ctx, -1, -1, -1) 1261 return err 1262 }) 1263 1264 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1265 return client.Teams.RemoveTeamProjectByID(ctx, 1, 1, 1) 1266 }) 1267 } 1268 1269 func TestTeamsService_RemoveTeamProjectBySlug(t *testing.T) { 1270 client, mux, _, teardown := setup() 1271 defer teardown() 1272 1273 wantAcceptHeaders := []string{mediaTypeProjectsPreview} 1274 mux.HandleFunc("/orgs/o/teams/s/projects/1", func(w http.ResponseWriter, r *http.Request) { 1275 testMethod(t, r, "DELETE") 1276 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 1277 w.WriteHeader(http.StatusNoContent) 1278 }) 1279 1280 ctx := context.Background() 1281 _, err := client.Teams.RemoveTeamProjectBySlug(ctx, "o", "s", 1) 1282 if err != nil { 1283 t.Errorf("Teams.RemoveTeamProjectBySlug returned error: %v", err) 1284 } 1285 1286 const methodName = "RemoveTeamProjectBySlug" 1287 testBadOptions(t, methodName, func() (err error) { 1288 _, err = client.Teams.RemoveTeamProjectBySlug(ctx, "\n", "\n", -1) 1289 return err 1290 }) 1291 1292 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1293 return client.Teams.RemoveTeamProjectBySlug(ctx, "o", "s", 1) 1294 }) 1295 } 1296 1297 func TestTeamsService_ListIDPGroupsInOrganization(t *testing.T) { 1298 client, mux, _, teardown := setup() 1299 defer teardown() 1300 1301 mux.HandleFunc("/orgs/o/team-sync/groups", func(w http.ResponseWriter, r *http.Request) { 1302 testMethod(t, r, "GET") 1303 testFormValues(t, r, values{ 1304 "page": "url-encoded-next-page-token", 1305 "q": "n", 1306 }) 1307 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1308 }) 1309 1310 opt := &ListIDPGroupsOptions{ 1311 Query: "n", 1312 ListCursorOptions: ListCursorOptions{Page: "url-encoded-next-page-token"}, 1313 } 1314 ctx := context.Background() 1315 groups, _, err := client.Teams.ListIDPGroupsInOrganization(ctx, "o", opt) 1316 if err != nil { 1317 t.Errorf("Teams.ListIDPGroupsInOrganization returned error: %v", err) 1318 } 1319 1320 want := &IDPGroupList{ 1321 Groups: []*IDPGroup{ 1322 { 1323 GroupID: String("1"), 1324 GroupName: String("n"), 1325 GroupDescription: String("d"), 1326 }, 1327 }, 1328 } 1329 if !cmp.Equal(groups, want) { 1330 t.Errorf("Teams.ListIDPGroupsInOrganization returned %+v. want %+v", groups, want) 1331 } 1332 1333 const methodName = "ListIDPGroupsInOrganization" 1334 testBadOptions(t, methodName, func() (err error) { 1335 _, _, err = client.Teams.ListIDPGroupsInOrganization(ctx, "\n", opt) 1336 return err 1337 }) 1338 1339 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1340 got, resp, err := client.Teams.ListIDPGroupsInOrganization(ctx, "o", opt) 1341 if got != nil { 1342 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1343 } 1344 return resp, err 1345 }) 1346 } 1347 1348 func TestTeamsService_ListIDPGroupsForTeamByID(t *testing.T) { 1349 client, mux, _, teardown := setup() 1350 defer teardown() 1351 1352 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1353 testMethod(t, r, "GET") 1354 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1355 }) 1356 1357 ctx := context.Background() 1358 groups, _, err := client.Teams.ListIDPGroupsForTeamByID(ctx, 1, 1) 1359 if err != nil { 1360 t.Errorf("Teams.ListIDPGroupsForTeamByID returned error: %v", err) 1361 } 1362 1363 want := &IDPGroupList{ 1364 Groups: []*IDPGroup{ 1365 { 1366 GroupID: String("1"), 1367 GroupName: String("n"), 1368 GroupDescription: String("d"), 1369 }, 1370 }, 1371 } 1372 if !cmp.Equal(groups, want) { 1373 t.Errorf("Teams.ListIDPGroupsForTeamByID returned %+v. want %+v", groups, want) 1374 } 1375 1376 const methodName = "ListIDPGroupsForTeamByID" 1377 testBadOptions(t, methodName, func() (err error) { 1378 _, _, err = client.Teams.ListIDPGroupsForTeamByID(ctx, -1, -1) 1379 return err 1380 }) 1381 1382 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1383 got, resp, err := client.Teams.ListIDPGroupsForTeamByID(ctx, 1, 1) 1384 if got != nil { 1385 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1386 } 1387 return resp, err 1388 }) 1389 } 1390 1391 func TestTeamsService_ListIDPGroupsForTeamBySlug(t *testing.T) { 1392 client, mux, _, teardown := setup() 1393 defer teardown() 1394 1395 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1396 testMethod(t, r, "GET") 1397 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1398 }) 1399 1400 ctx := context.Background() 1401 groups, _, err := client.Teams.ListIDPGroupsForTeamBySlug(ctx, "o", "slug") 1402 if err != nil { 1403 t.Errorf("Teams.ListIDPGroupsForTeamBySlug returned error: %v", err) 1404 } 1405 1406 want := &IDPGroupList{ 1407 Groups: []*IDPGroup{ 1408 { 1409 GroupID: String("1"), 1410 GroupName: String("n"), 1411 GroupDescription: String("d"), 1412 }, 1413 }, 1414 } 1415 if !cmp.Equal(groups, want) { 1416 t.Errorf("Teams.ListIDPGroupsForTeamBySlug returned %+v. want %+v", groups, want) 1417 } 1418 1419 const methodName = "ListIDPGroupsForTeamBySlug" 1420 testBadOptions(t, methodName, func() (err error) { 1421 _, _, err = client.Teams.ListIDPGroupsForTeamBySlug(ctx, "\n", "\n") 1422 return err 1423 }) 1424 1425 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1426 got, resp, err := client.Teams.ListIDPGroupsForTeamBySlug(ctx, "o", "slug") 1427 if got != nil { 1428 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1429 } 1430 return resp, err 1431 }) 1432 } 1433 1434 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsByID(t *testing.T) { 1435 client, mux, _, teardown := setup() 1436 defer teardown() 1437 1438 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1439 testMethod(t, r, "PATCH") 1440 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1441 }) 1442 1443 input := IDPGroupList{ 1444 Groups: []*IDPGroup{ 1445 { 1446 GroupID: String("1"), 1447 GroupName: String("n"), 1448 GroupDescription: String("d"), 1449 }, 1450 }, 1451 } 1452 1453 ctx := context.Background() 1454 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsByID(ctx, 1, 1, input) 1455 if err != nil { 1456 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned error: %v", err) 1457 } 1458 1459 want := &IDPGroupList{ 1460 Groups: []*IDPGroup{ 1461 { 1462 GroupID: String("1"), 1463 GroupName: String("n"), 1464 GroupDescription: String("d"), 1465 }, 1466 }, 1467 } 1468 if !cmp.Equal(groups, want) { 1469 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned %+v. want %+v", groups, want) 1470 } 1471 1472 const methodName = "CreateOrUpdateIDPGroupConnectionsByID" 1473 testBadOptions(t, methodName, func() (err error) { 1474 _, _, err = client.Teams.CreateOrUpdateIDPGroupConnectionsByID(ctx, -1, -1, input) 1475 return err 1476 }) 1477 1478 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1479 got, resp, err := client.Teams.CreateOrUpdateIDPGroupConnectionsByID(ctx, 1, 1, input) 1480 if got != nil { 1481 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1482 } 1483 return resp, err 1484 }) 1485 } 1486 1487 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsBySlug(t *testing.T) { 1488 client, mux, _, teardown := setup() 1489 defer teardown() 1490 1491 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1492 testMethod(t, r, "PATCH") 1493 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1494 }) 1495 1496 input := IDPGroupList{ 1497 Groups: []*IDPGroup{ 1498 { 1499 GroupID: String("1"), 1500 GroupName: String("n"), 1501 GroupDescription: String("d"), 1502 }, 1503 }, 1504 } 1505 1506 ctx := context.Background() 1507 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(ctx, "o", "slug", input) 1508 if err != nil { 1509 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned error: %v", err) 1510 } 1511 1512 want := &IDPGroupList{ 1513 Groups: []*IDPGroup{ 1514 { 1515 GroupID: String("1"), 1516 GroupName: String("n"), 1517 GroupDescription: String("d"), 1518 }, 1519 }, 1520 } 1521 if !cmp.Equal(groups, want) { 1522 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned %+v. want %+v", groups, want) 1523 } 1524 1525 const methodName = "CreateOrUpdateIDPGroupConnectionsBySlug" 1526 testBadOptions(t, methodName, func() (err error) { 1527 _, _, err = client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(ctx, "\n", "\n", input) 1528 return err 1529 }) 1530 1531 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1532 got, resp, err := client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(ctx, "o", "slug", input) 1533 if got != nil { 1534 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1535 } 1536 return resp, err 1537 }) 1538 } 1539 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsByID_empty(t *testing.T) { 1540 client, mux, _, teardown := setup() 1541 defer teardown() 1542 1543 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1544 testMethod(t, r, "PATCH") 1545 fmt.Fprint(w, `{"groups": []}`) 1546 }) 1547 1548 input := IDPGroupList{ 1549 Groups: []*IDPGroup{}, 1550 } 1551 1552 ctx := context.Background() 1553 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsByID(ctx, 1, 1, input) 1554 if err != nil { 1555 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned error: %v", err) 1556 } 1557 1558 want := &IDPGroupList{ 1559 Groups: []*IDPGroup{}, 1560 } 1561 if !cmp.Equal(groups, want) { 1562 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned %+v. want %+v", groups, want) 1563 } 1564 } 1565 1566 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsBySlug_empty(t *testing.T) { 1567 client, mux, _, teardown := setup() 1568 defer teardown() 1569 1570 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1571 testMethod(t, r, "PATCH") 1572 fmt.Fprint(w, `{"groups": []}`) 1573 }) 1574 1575 input := IDPGroupList{ 1576 Groups: []*IDPGroup{}, 1577 } 1578 1579 ctx := context.Background() 1580 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(ctx, "o", "slug", input) 1581 if err != nil { 1582 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned error: %v", err) 1583 } 1584 1585 want := &IDPGroupList{ 1586 Groups: []*IDPGroup{}, 1587 } 1588 if !cmp.Equal(groups, want) { 1589 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned %+v. want %+v", groups, want) 1590 } 1591 } 1592 1593 func TestNewTeam_Marshal(t *testing.T) { 1594 testJSONMarshal(t, &NewTeam{}, "{}") 1595 1596 u := &NewTeam{ 1597 Name: "n", 1598 Description: String("d"), 1599 Maintainers: []string{"m1", "m2"}, 1600 RepoNames: []string{"repo1", "repo2"}, 1601 NotificationSetting: String("notifications_enabled"), 1602 ParentTeamID: Int64(1), 1603 Permission: String("perm"), 1604 Privacy: String("p"), 1605 LDAPDN: String("l"), 1606 } 1607 1608 want := `{ 1609 "name": "n", 1610 "description": "d", 1611 "maintainers": ["m1", "m2"], 1612 "repo_names": ["repo1", "repo2"], 1613 "parent_team_id": 1, 1614 "notification_setting": "notifications_enabled", 1615 "permission": "perm", 1616 "privacy": "p", 1617 "ldap_dn": "l" 1618 }` 1619 1620 testJSONMarshal(t, u, want) 1621 } 1622 1623 func TestTeams_Marshal(t *testing.T) { 1624 testJSONMarshal(t, &Team{}, "{}") 1625 1626 u := &Team{ 1627 ID: Int64(1), 1628 NodeID: String("n"), 1629 Name: String("n"), 1630 Description: String("d"), 1631 URL: String("u"), 1632 Slug: String("s"), 1633 Permission: String("p"), 1634 Privacy: String("p"), 1635 MembersCount: Int(1), 1636 ReposCount: Int(1), 1637 MembersURL: String("m"), 1638 RepositoriesURL: String("r"), 1639 Organization: &Organization{ 1640 Login: String("l"), 1641 ID: Int64(1), 1642 NodeID: String("n"), 1643 AvatarURL: String("a"), 1644 HTMLURL: String("h"), 1645 Name: String("n"), 1646 Company: String("c"), 1647 Blog: String("b"), 1648 Location: String("l"), 1649 Email: String("e"), 1650 }, 1651 Parent: &Team{ 1652 ID: Int64(1), 1653 NodeID: String("n"), 1654 Name: String("n"), 1655 Description: String("d"), 1656 URL: String("u"), 1657 Slug: String("s"), 1658 Permission: String("p"), 1659 Privacy: String("p"), 1660 MembersCount: Int(1), 1661 ReposCount: Int(1), 1662 }, 1663 LDAPDN: String("l"), 1664 } 1665 1666 want := `{ 1667 "id": 1, 1668 "node_id": "n", 1669 "name": "n", 1670 "description": "d", 1671 "url": "u", 1672 "slug": "s", 1673 "permission": "p", 1674 "privacy": "p", 1675 "members_count": 1, 1676 "repos_count": 1, 1677 "members_url": "m", 1678 "repositories_url": "r", 1679 "organization": { 1680 "login": "l", 1681 "id": 1, 1682 "node_id": "n", 1683 "avatar_url": "a", 1684 "html_url": "h", 1685 "name": "n", 1686 "company": "c", 1687 "blog": "b", 1688 "location": "l", 1689 "email": "e" 1690 }, 1691 "parent": { 1692 "id": 1, 1693 "node_id": "n", 1694 "name": "n", 1695 "description": "d", 1696 "url": "u", 1697 "slug": "s", 1698 "permission": "p", 1699 "privacy": "p", 1700 "members_count": 1, 1701 "repos_count": 1 1702 }, 1703 "ldap_dn": "l" 1704 }` 1705 1706 testJSONMarshal(t, u, want) 1707 } 1708 1709 func TestInvitation_Marshal(t *testing.T) { 1710 testJSONMarshal(t, &Invitation{}, "{}") 1711 1712 u := &Invitation{ 1713 ID: Int64(1), 1714 NodeID: String("test node"), 1715 Login: String("login123"), 1716 Email: String("go@github.com"), 1717 Role: String("developer"), 1718 CreatedAt: &Timestamp{referenceTime}, 1719 TeamCount: Int(99), 1720 InvitationTeamURL: String("url"), 1721 } 1722 1723 want := `{ 1724 "id": 1, 1725 "node_id": "test node", 1726 "login":"login123", 1727 "email":"go@github.com", 1728 "role":"developer", 1729 "created_at":` + referenceTimeStr + `, 1730 "team_count":99, 1731 "invitation_team_url":"url" 1732 }` 1733 1734 testJSONMarshal(t, u, want) 1735 } 1736 1737 func TestIDPGroup_Marshal(t *testing.T) { 1738 testJSONMarshal(t, &IDPGroup{}, "{}") 1739 1740 u := &IDPGroup{ 1741 GroupID: String("abc1"), 1742 GroupName: String("test group"), 1743 GroupDescription: String("test group descripation"), 1744 } 1745 1746 want := `{ 1747 "group_id": "abc1", 1748 "group_name": "test group", 1749 "group_description":"test group descripation" 1750 }` 1751 1752 testJSONMarshal(t, u, want) 1753 } 1754 1755 func TestTeamsService_GetExternalGroup(t *testing.T) { 1756 client, mux, _, teardown := setup() 1757 defer teardown() 1758 1759 mux.HandleFunc("/orgs/o/external-group/123", func(w http.ResponseWriter, r *http.Request) { 1760 testMethod(t, r, "GET") 1761 fmt.Fprint(w, `{ 1762 "group_id": 123, 1763 "group_name": "Octocat admins", 1764 "updated_at": "2006-01-02T15:04:05Z", 1765 "teams": [ 1766 { 1767 "team_id": 1, 1768 "team_name": "team-test" 1769 }, 1770 { 1771 "team_id": 2, 1772 "team_name": "team-test2" 1773 } 1774 ], 1775 "members": [ 1776 { 1777 "member_id": 1, 1778 "member_login": "mona-lisa_eocsaxrs", 1779 "member_name": "Mona Lisa", 1780 "member_email": "mona_lisa@github.com" 1781 }, 1782 { 1783 "member_id": 2, 1784 "member_login": "octo-lisa_eocsaxrs", 1785 "member_name": "Octo Lisa", 1786 "member_email": "octo_lisa@github.com" 1787 } 1788 ] 1789 }`) 1790 }) 1791 1792 ctx := context.Background() 1793 externalGroup, _, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1794 if err != nil { 1795 t.Errorf("Teams.GetExternalGroup returned error: %v", err) 1796 } 1797 1798 want := &ExternalGroup{ 1799 GroupID: Int64(123), 1800 GroupName: String("Octocat admins"), 1801 UpdatedAt: &Timestamp{Time: referenceTime}, 1802 Teams: []*ExternalGroupTeam{ 1803 { 1804 TeamID: Int64(1), 1805 TeamName: String("team-test"), 1806 }, 1807 { 1808 TeamID: Int64(2), 1809 TeamName: String("team-test2"), 1810 }, 1811 }, 1812 Members: []*ExternalGroupMember{ 1813 { 1814 MemberID: Int64(1), 1815 MemberLogin: String("mona-lisa_eocsaxrs"), 1816 MemberName: String("Mona Lisa"), 1817 MemberEmail: String("mona_lisa@github.com"), 1818 }, 1819 { 1820 MemberID: Int64(2), 1821 MemberLogin: String("octo-lisa_eocsaxrs"), 1822 MemberName: String("Octo Lisa"), 1823 MemberEmail: String("octo_lisa@github.com"), 1824 }, 1825 }, 1826 } 1827 if !cmp.Equal(externalGroup, want) { 1828 t.Errorf("Teams.GetExternalGroup returned %+v, want %+v", externalGroup, want) 1829 } 1830 1831 const methodName = "GetExternalGroup" 1832 testBadOptions(t, methodName, func() (err error) { 1833 _, _, err = client.Teams.GetExternalGroup(ctx, "\n", -1) 1834 return err 1835 }) 1836 1837 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1838 got, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1839 if got != nil { 1840 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1841 } 1842 return resp, err 1843 }) 1844 } 1845 1846 func TestTeamsService_GetExternalGroup_notFound(t *testing.T) { 1847 client, mux, _, teardown := setup() 1848 defer teardown() 1849 1850 mux.HandleFunc("/orgs/o/external-group/123", func(w http.ResponseWriter, r *http.Request) { 1851 testMethod(t, r, "GET") 1852 w.WriteHeader(http.StatusNotFound) 1853 }) 1854 1855 ctx := context.Background() 1856 eg, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1857 if err == nil { 1858 t.Errorf("Expected HTTP 404 response") 1859 } 1860 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 1861 t.Errorf("Teams.GetExternalGroup returned status %d, want %d", got, want) 1862 } 1863 if eg != nil { 1864 t.Errorf("Teams.GetExternalGroup returned %+v, want nil", eg) 1865 } 1866 } 1867 1868 func TestTeamsService_ListExternalGroups(t *testing.T) { 1869 client, mux, _, teardown := setup() 1870 defer teardown() 1871 1872 mux.HandleFunc("/orgs/o/external-groups", func(w http.ResponseWriter, r *http.Request) { 1873 testMethod(t, r, "GET") 1874 fmt.Fprint(w, `{ 1875 "groups": [ 1876 { 1877 "group_id": 123, 1878 "group_name": "Octocat admins", 1879 "updated_at": "2006-01-02T15:04:05Z" 1880 } 1881 ] 1882 }`) 1883 }) 1884 1885 ctx := context.Background() 1886 opts := &ListExternalGroupsOptions{ 1887 DisplayName: String("Octocat"), 1888 } 1889 list, _, err := client.Teams.ListExternalGroups(ctx, "o", opts) 1890 if err != nil { 1891 t.Errorf("Teams.ListExternalGroups returned error: %v", err) 1892 } 1893 1894 want := &ExternalGroupList{ 1895 Groups: []*ExternalGroup{ 1896 { 1897 GroupID: Int64(123), 1898 GroupName: String("Octocat admins"), 1899 UpdatedAt: &Timestamp{Time: referenceTime}, 1900 }, 1901 }, 1902 } 1903 if !cmp.Equal(list, want) { 1904 t.Errorf("Teams.ListExternalGroups returned %+v, want %+v", list, want) 1905 } 1906 1907 const methodName = "ListExternalGroups" 1908 testBadOptions(t, methodName, func() (err error) { 1909 _, _, err = client.Teams.ListExternalGroups(ctx, "\n", opts) 1910 return err 1911 }) 1912 1913 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1914 got, resp, err := client.Teams.ListExternalGroups(ctx, "o", opts) 1915 if got != nil { 1916 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1917 } 1918 return resp, err 1919 }) 1920 } 1921 1922 func TestTeamsService_ListExternalGroups_notFound(t *testing.T) { 1923 client, mux, _, teardown := setup() 1924 defer teardown() 1925 1926 mux.HandleFunc("/orgs/o/external-groups", func(w http.ResponseWriter, r *http.Request) { 1927 testMethod(t, r, "GET") 1928 w.WriteHeader(http.StatusNotFound) 1929 }) 1930 1931 ctx := context.Background() 1932 eg, resp, err := client.Teams.ListExternalGroups(ctx, "o", nil) 1933 if err == nil { 1934 t.Errorf("Expected HTTP 404 response") 1935 } 1936 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 1937 t.Errorf("Teams.ListExternalGroups returned status %d, want %d", got, want) 1938 } 1939 if eg != nil { 1940 t.Errorf("Teams.ListExternalGroups returned %+v, want nil", eg) 1941 } 1942 } 1943 1944 func TestTeamsService_ListExternalGroupsForTeamBySlug(t *testing.T) { 1945 client, mux, _, teardown := setup() 1946 defer teardown() 1947 1948 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 1949 testMethod(t, r, "GET") 1950 fmt.Fprint(w, `{ 1951 "groups": [ 1952 { 1953 "group_id": 123, 1954 "group_name": "Octocat admins", 1955 "updated_at": "2006-01-02T15:04:05Z" 1956 } 1957 ] 1958 }`) 1959 }) 1960 1961 ctx := context.Background() 1962 list, _, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t") 1963 if err != nil { 1964 t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned error: %v", err) 1965 } 1966 1967 want := &ExternalGroupList{ 1968 Groups: []*ExternalGroup{ 1969 { 1970 GroupID: Int64(123), 1971 GroupName: String("Octocat admins"), 1972 UpdatedAt: &Timestamp{Time: referenceTime}, 1973 }, 1974 }, 1975 } 1976 if !cmp.Equal(list, want) { 1977 t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned %+v, want %+v", list, want) 1978 } 1979 1980 const methodName = "ListExternalGroupsForTeamBySlug" 1981 testBadOptions(t, methodName, func() (err error) { 1982 _, _, err = client.Teams.ListExternalGroupsForTeamBySlug(ctx, "\n", "\n") 1983 return err 1984 }) 1985 1986 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1987 got, resp, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t") 1988 if got != nil { 1989 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1990 } 1991 return resp, err 1992 }) 1993 } 1994 1995 func TestTeamsService_ListExternalGroupsForTeamBySlug_notFound(t *testing.T) { 1996 client, mux, _, teardown := setup() 1997 defer teardown() 1998 1999 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2000 testMethod(t, r, "GET") 2001 w.WriteHeader(http.StatusNotFound) 2002 }) 2003 2004 ctx := context.Background() 2005 eg, resp, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t") 2006 if err == nil { 2007 t.Errorf("Expected HTTP 404 response") 2008 } 2009 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 2010 t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned status %d, want %d", got, want) 2011 } 2012 if eg != nil { 2013 t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned %+v, want nil", eg) 2014 } 2015 } 2016 2017 func TestTeamsService_UpdateConnectedExternalGroup(t *testing.T) { 2018 client, mux, _, teardown := setup() 2019 defer teardown() 2020 2021 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2022 testMethod(t, r, "PATCH") 2023 fmt.Fprint(w, `{ 2024 "group_id": 123, 2025 "group_name": "Octocat admins", 2026 "updated_at": "2006-01-02T15:04:05Z", 2027 "teams": [ 2028 { 2029 "team_id": 1, 2030 "team_name": "team-test" 2031 }, 2032 { 2033 "team_id": 2, 2034 "team_name": "team-test2" 2035 } 2036 ], 2037 "members": [ 2038 { 2039 "member_id": 1, 2040 "member_login": "mona-lisa_eocsaxrs", 2041 "member_name": "Mona Lisa", 2042 "member_email": "mona_lisa@github.com" 2043 }, 2044 { 2045 "member_id": 2, 2046 "member_login": "octo-lisa_eocsaxrs", 2047 "member_name": "Octo Lisa", 2048 "member_email": "octo_lisa@github.com" 2049 } 2050 ] 2051 }`) 2052 }) 2053 2054 ctx := context.Background() 2055 body := &ExternalGroup{ 2056 GroupID: Int64(123), 2057 } 2058 externalGroup, _, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 2059 if err != nil { 2060 t.Errorf("Teams.UpdateConnectedExternalGroup returned error: %v", err) 2061 } 2062 2063 want := &ExternalGroup{ 2064 GroupID: Int64(123), 2065 GroupName: String("Octocat admins"), 2066 UpdatedAt: &Timestamp{Time: referenceTime}, 2067 Teams: []*ExternalGroupTeam{ 2068 { 2069 TeamID: Int64(1), 2070 TeamName: String("team-test"), 2071 }, 2072 { 2073 TeamID: Int64(2), 2074 TeamName: String("team-test2"), 2075 }, 2076 }, 2077 Members: []*ExternalGroupMember{ 2078 { 2079 MemberID: Int64(1), 2080 MemberLogin: String("mona-lisa_eocsaxrs"), 2081 MemberName: String("Mona Lisa"), 2082 MemberEmail: String("mona_lisa@github.com"), 2083 }, 2084 { 2085 MemberID: Int64(2), 2086 MemberLogin: String("octo-lisa_eocsaxrs"), 2087 MemberName: String("Octo Lisa"), 2088 MemberEmail: String("octo_lisa@github.com"), 2089 }, 2090 }, 2091 } 2092 if !cmp.Equal(externalGroup, want) { 2093 t.Errorf("Teams.GetExternalGroup returned %+v, want %+v", externalGroup, want) 2094 } 2095 2096 const methodName = "UpdateConnectedExternalGroup" 2097 testBadOptions(t, methodName, func() (err error) { 2098 _, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, "\n", "\n", body) 2099 return err 2100 }) 2101 2102 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2103 got, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 2104 if got != nil { 2105 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2106 } 2107 return resp, err 2108 }) 2109 } 2110 2111 func TestTeamsService_UpdateConnectedExternalGroup_notFound(t *testing.T) { 2112 client, mux, _, teardown := setup() 2113 defer teardown() 2114 2115 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2116 testMethod(t, r, "PATCH") 2117 w.WriteHeader(http.StatusNotFound) 2118 }) 2119 2120 ctx := context.Background() 2121 body := &ExternalGroup{ 2122 GroupID: Int64(123), 2123 } 2124 eg, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 2125 if err == nil { 2126 t.Errorf("Expected HTTP 404 response") 2127 } 2128 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 2129 t.Errorf("Teams.UpdateConnectedExternalGroup returned status %d, want %d", got, want) 2130 } 2131 if eg != nil { 2132 t.Errorf("Teams.UpdateConnectedExternalGroup returned %+v, want nil", eg) 2133 } 2134 } 2135 2136 func TestTeamsService_RemoveConnectedExternalGroup(t *testing.T) { 2137 client, mux, _, teardown := setup() 2138 defer teardown() 2139 2140 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2141 testMethod(t, r, "DELETE") 2142 w.WriteHeader(http.StatusNoContent) 2143 }) 2144 2145 ctx := context.Background() 2146 _, err := client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2147 if err != nil { 2148 t.Errorf("Teams.RemoveConnectedExternalGroup returned error: %v", err) 2149 } 2150 2151 const methodName = "RemoveConnectedExternalGroup" 2152 testBadOptions(t, methodName, func() (err error) { 2153 _, err = client.Teams.RemoveConnectedExternalGroup(ctx, "\n", "\n") 2154 return err 2155 }) 2156 2157 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2158 return client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2159 }) 2160 } 2161 2162 func TestTeamsService_RemoveConnectedExternalGroup_notFound(t *testing.T) { 2163 client, mux, _, teardown := setup() 2164 defer teardown() 2165 2166 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2167 testMethod(t, r, "DELETE") 2168 w.WriteHeader(http.StatusNotFound) 2169 }) 2170 2171 ctx := context.Background() 2172 resp, err := client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2173 if err == nil { 2174 t.Errorf("Expected HTTP 404 response") 2175 } 2176 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 2177 t.Errorf("Teams.GetExternalGroup returned status %d, want %d", got, want) 2178 } 2179 } 2180 2181 func TestIDPGroupList_Marshal(t *testing.T) { 2182 testJSONMarshal(t, &IDPGroupList{}, "{}") 2183 2184 u := &IDPGroupList{ 2185 Groups: []*IDPGroup{ 2186 { 2187 GroupID: String("abc1"), 2188 GroupName: String("test group"), 2189 GroupDescription: String("test group descripation"), 2190 }, 2191 { 2192 GroupID: String("abc2"), 2193 GroupName: String("test group2"), 2194 GroupDescription: String("test group descripation2"), 2195 }, 2196 }, 2197 } 2198 2199 want := `{ 2200 "groups": [ 2201 { 2202 "group_id": "abc1", 2203 "group_name": "test group", 2204 "group_description": "test group descripation" 2205 }, 2206 { 2207 "group_id": "abc2", 2208 "group_name": "test group2", 2209 "group_description": "test group descripation2" 2210 } 2211 ] 2212 }` 2213 2214 testJSONMarshal(t, u, want) 2215 } 2216 2217 func TestExternalGroupMember_Marshal(t *testing.T) { 2218 testJSONMarshal(t, &ExternalGroupMember{}, "{}") 2219 2220 u := &ExternalGroupMember{ 2221 MemberID: Int64(1), 2222 MemberLogin: String("test member"), 2223 MemberName: String("test member name"), 2224 MemberEmail: String("test member email"), 2225 } 2226 2227 want := `{ 2228 "member_id": 1, 2229 "member_login": "test member", 2230 "member_name":"test member name", 2231 "member_email":"test member email" 2232 }` 2233 2234 testJSONMarshal(t, u, want) 2235 } 2236 2237 func TestExternalGroup_Marshal(t *testing.T) { 2238 testJSONMarshal(t, &ExternalGroup{}, "{}") 2239 2240 u := &ExternalGroup{ 2241 GroupID: Int64(123), 2242 GroupName: String("group1"), 2243 UpdatedAt: &Timestamp{referenceTime}, 2244 Teams: []*ExternalGroupTeam{ 2245 { 2246 TeamID: Int64(1), 2247 TeamName: String("team-test"), 2248 }, 2249 { 2250 TeamID: Int64(2), 2251 TeamName: String("team-test2"), 2252 }, 2253 }, 2254 Members: []*ExternalGroupMember{ 2255 { 2256 MemberID: Int64(1), 2257 MemberLogin: String("test"), 2258 MemberName: String("test"), 2259 MemberEmail: String("test@github.com"), 2260 }, 2261 }, 2262 } 2263 2264 want := `{ 2265 "group_id": 123, 2266 "group_name": "group1", 2267 "updated_at": ` + referenceTimeStr + `, 2268 "teams": [ 2269 { 2270 "team_id": 1, 2271 "team_name": "team-test" 2272 }, 2273 { 2274 "team_id": 2, 2275 "team_name": "team-test2" 2276 } 2277 ], 2278 "members": [ 2279 { 2280 "member_id": 1, 2281 "member_login": "test", 2282 "member_name": "test", 2283 "member_email": "test@github.com" 2284 } 2285 ] 2286 }` 2287 2288 testJSONMarshal(t, u, want) 2289 } 2290 2291 func TestExternalGroupTeam_Marshal(t *testing.T) { 2292 testJSONMarshal(t, &ExternalGroupTeam{}, "{}") 2293 2294 u := &ExternalGroupTeam{ 2295 TeamID: Int64(123), 2296 TeamName: String("test"), 2297 } 2298 2299 want := `{ 2300 "team_id": 123, 2301 "team_name": "test" 2302 }` 2303 2304 testJSONMarshal(t, u, want) 2305 } 2306 2307 func TestListExternalGroupsOptions_Marshal(t *testing.T) { 2308 testJSONMarshal(t, &ListExternalGroupsOptions{}, "{}") 2309 2310 u := &ListExternalGroupsOptions{ 2311 DisplayName: String("test"), 2312 ListOptions: ListOptions{ 2313 Page: 1, 2314 PerPage: 2, 2315 }, 2316 } 2317 2318 want := `{ 2319 "DisplayName": "test", 2320 "page": 1, 2321 "PerPage": 2 2322 }` 2323 2324 testJSONMarshal(t, u, want) 2325 } 2326 2327 func TestTeamAddTeamRepoOptions_Marshal(t *testing.T) { 2328 testJSONMarshal(t, &TeamAddTeamRepoOptions{}, "{}") 2329 2330 u := &TeamAddTeamRepoOptions{ 2331 Permission: "a", 2332 } 2333 2334 want := `{ 2335 "permission": "a" 2336 }` 2337 2338 testJSONMarshal(t, u, want) 2339 }