github.com/google/go-github/v49@v49.1.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 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 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 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 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 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 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 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("Expcted 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("Expcted 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 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 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 }) 1306 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1307 }) 1308 1309 opt := &ListCursorOptions{Page: "url-encoded-next-page-token"} 1310 ctx := context.Background() 1311 groups, _, err := client.Teams.ListIDPGroupsInOrganization(ctx, "o", opt) 1312 if err != nil { 1313 t.Errorf("Teams.ListIDPGroupsInOrganization returned error: %v", err) 1314 } 1315 1316 want := &IDPGroupList{ 1317 Groups: []*IDPGroup{ 1318 { 1319 GroupID: String("1"), 1320 GroupName: String("n"), 1321 GroupDescription: String("d"), 1322 }, 1323 }, 1324 } 1325 if !cmp.Equal(groups, want) { 1326 t.Errorf("Teams.ListIDPGroupsInOrganization returned %+v. want %+v", groups, want) 1327 } 1328 1329 const methodName = "ListIDPGroupsInOrganization" 1330 testBadOptions(t, methodName, func() (err error) { 1331 _, _, err = client.Teams.ListIDPGroupsInOrganization(ctx, "\n", opt) 1332 return err 1333 }) 1334 1335 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1336 got, resp, err := client.Teams.ListIDPGroupsInOrganization(ctx, "o", opt) 1337 if got != nil { 1338 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1339 } 1340 return resp, err 1341 }) 1342 } 1343 1344 func TestTeamsService_ListIDPGroupsForTeamByID(t *testing.T) { 1345 client, mux, _, teardown := setup() 1346 defer teardown() 1347 1348 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1349 testMethod(t, r, "GET") 1350 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1351 }) 1352 1353 ctx := context.Background() 1354 groups, _, err := client.Teams.ListIDPGroupsForTeamByID(ctx, 1, 1) 1355 if err != nil { 1356 t.Errorf("Teams.ListIDPGroupsForTeamByID returned error: %v", err) 1357 } 1358 1359 want := &IDPGroupList{ 1360 Groups: []*IDPGroup{ 1361 { 1362 GroupID: String("1"), 1363 GroupName: String("n"), 1364 GroupDescription: String("d"), 1365 }, 1366 }, 1367 } 1368 if !cmp.Equal(groups, want) { 1369 t.Errorf("Teams.ListIDPGroupsForTeamByID returned %+v. want %+v", groups, want) 1370 } 1371 1372 const methodName = "ListIDPGroupsForTeamByID" 1373 testBadOptions(t, methodName, func() (err error) { 1374 _, _, err = client.Teams.ListIDPGroupsForTeamByID(ctx, -1, -1) 1375 return err 1376 }) 1377 1378 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1379 got, resp, err := client.Teams.ListIDPGroupsForTeamByID(ctx, 1, 1) 1380 if got != nil { 1381 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1382 } 1383 return resp, err 1384 }) 1385 } 1386 1387 func TestTeamsService_ListIDPGroupsForTeamBySlug(t *testing.T) { 1388 client, mux, _, teardown := setup() 1389 defer teardown() 1390 1391 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1392 testMethod(t, r, "GET") 1393 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1394 }) 1395 1396 ctx := context.Background() 1397 groups, _, err := client.Teams.ListIDPGroupsForTeamBySlug(ctx, "o", "slug") 1398 if err != nil { 1399 t.Errorf("Teams.ListIDPGroupsForTeamBySlug returned error: %v", err) 1400 } 1401 1402 want := &IDPGroupList{ 1403 Groups: []*IDPGroup{ 1404 { 1405 GroupID: String("1"), 1406 GroupName: String("n"), 1407 GroupDescription: String("d"), 1408 }, 1409 }, 1410 } 1411 if !cmp.Equal(groups, want) { 1412 t.Errorf("Teams.ListIDPGroupsForTeamBySlug returned %+v. want %+v", groups, want) 1413 } 1414 1415 const methodName = "ListIDPGroupsForTeamBySlug" 1416 testBadOptions(t, methodName, func() (err error) { 1417 _, _, err = client.Teams.ListIDPGroupsForTeamBySlug(ctx, "\n", "\n") 1418 return err 1419 }) 1420 1421 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1422 got, resp, err := client.Teams.ListIDPGroupsForTeamBySlug(ctx, "o", "slug") 1423 if got != nil { 1424 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1425 } 1426 return resp, err 1427 }) 1428 } 1429 1430 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsByID(t *testing.T) { 1431 client, mux, _, teardown := setup() 1432 defer teardown() 1433 1434 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1435 testMethod(t, r, "PATCH") 1436 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1437 }) 1438 1439 input := IDPGroupList{ 1440 Groups: []*IDPGroup{ 1441 { 1442 GroupID: String("1"), 1443 GroupName: String("n"), 1444 GroupDescription: String("d"), 1445 }, 1446 }, 1447 } 1448 1449 ctx := context.Background() 1450 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsByID(ctx, 1, 1, input) 1451 if err != nil { 1452 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned error: %v", err) 1453 } 1454 1455 want := &IDPGroupList{ 1456 Groups: []*IDPGroup{ 1457 { 1458 GroupID: String("1"), 1459 GroupName: String("n"), 1460 GroupDescription: String("d"), 1461 }, 1462 }, 1463 } 1464 if !cmp.Equal(groups, want) { 1465 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned %+v. want %+v", groups, want) 1466 } 1467 1468 const methodName = "CreateOrUpdateIDPGroupConnectionsByID" 1469 testBadOptions(t, methodName, func() (err error) { 1470 _, _, err = client.Teams.CreateOrUpdateIDPGroupConnectionsByID(ctx, -1, -1, input) 1471 return err 1472 }) 1473 1474 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1475 got, resp, err := client.Teams.CreateOrUpdateIDPGroupConnectionsByID(ctx, 1, 1, input) 1476 if got != nil { 1477 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1478 } 1479 return resp, err 1480 }) 1481 } 1482 1483 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsBySlug(t *testing.T) { 1484 client, mux, _, teardown := setup() 1485 defer teardown() 1486 1487 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1488 testMethod(t, r, "PATCH") 1489 fmt.Fprint(w, `{"groups": [{"group_id": "1", "group_name": "n", "group_description": "d"}]}`) 1490 }) 1491 1492 input := IDPGroupList{ 1493 Groups: []*IDPGroup{ 1494 { 1495 GroupID: String("1"), 1496 GroupName: String("n"), 1497 GroupDescription: String("d"), 1498 }, 1499 }, 1500 } 1501 1502 ctx := context.Background() 1503 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(ctx, "o", "slug", input) 1504 if err != nil { 1505 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned error: %v", err) 1506 } 1507 1508 want := &IDPGroupList{ 1509 Groups: []*IDPGroup{ 1510 { 1511 GroupID: String("1"), 1512 GroupName: String("n"), 1513 GroupDescription: String("d"), 1514 }, 1515 }, 1516 } 1517 if !cmp.Equal(groups, want) { 1518 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned %+v. want %+v", groups, want) 1519 } 1520 1521 const methodName = "CreateOrUpdateIDPGroupConnectionsBySlug" 1522 testBadOptions(t, methodName, func() (err error) { 1523 _, _, err = client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(ctx, "\n", "\n", input) 1524 return err 1525 }) 1526 1527 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1528 got, resp, err := client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(ctx, "o", "slug", input) 1529 if got != nil { 1530 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1531 } 1532 return resp, err 1533 }) 1534 } 1535 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsByID_empty(t *testing.T) { 1536 client, mux, _, teardown := setup() 1537 defer teardown() 1538 1539 mux.HandleFunc("/organizations/1/team/1/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1540 testMethod(t, r, "PATCH") 1541 fmt.Fprint(w, `{"groups": []}`) 1542 }) 1543 1544 input := IDPGroupList{ 1545 Groups: []*IDPGroup{}, 1546 } 1547 1548 ctx := context.Background() 1549 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsByID(ctx, 1, 1, input) 1550 if err != nil { 1551 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned error: %v", err) 1552 } 1553 1554 want := &IDPGroupList{ 1555 Groups: []*IDPGroup{}, 1556 } 1557 if !cmp.Equal(groups, want) { 1558 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsByID returned %+v. want %+v", groups, want) 1559 } 1560 } 1561 1562 func TestTeamsService_CreateOrUpdateIDPGroupConnectionsBySlug_empty(t *testing.T) { 1563 client, mux, _, teardown := setup() 1564 defer teardown() 1565 1566 mux.HandleFunc("/orgs/o/teams/slug/team-sync/group-mappings", func(w http.ResponseWriter, r *http.Request) { 1567 testMethod(t, r, "PATCH") 1568 fmt.Fprint(w, `{"groups": []}`) 1569 }) 1570 1571 input := IDPGroupList{ 1572 Groups: []*IDPGroup{}, 1573 } 1574 1575 ctx := context.Background() 1576 groups, _, err := client.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(ctx, "o", "slug", input) 1577 if err != nil { 1578 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned error: %v", err) 1579 } 1580 1581 want := &IDPGroupList{ 1582 Groups: []*IDPGroup{}, 1583 } 1584 if !cmp.Equal(groups, want) { 1585 t.Errorf("Teams.CreateOrUpdateIDPGroupConnectionsBySlug returned %+v. want %+v", groups, want) 1586 } 1587 } 1588 1589 func TestNewTeam_Marshal(t *testing.T) { 1590 testJSONMarshal(t, &NewTeam{}, "{}") 1591 1592 u := &NewTeam{ 1593 Name: "n", 1594 Description: String("d"), 1595 Maintainers: []string{"m1", "m2"}, 1596 RepoNames: []string{"repo1", "repo2"}, 1597 ParentTeamID: Int64(1), 1598 Permission: String("perm"), 1599 Privacy: String("p"), 1600 LDAPDN: String("l"), 1601 } 1602 1603 want := `{ 1604 "name": "n", 1605 "description": "d", 1606 "maintainers": ["m1", "m2"], 1607 "repo_names": ["repo1", "repo2"], 1608 "parent_team_id": 1, 1609 "permission": "perm", 1610 "privacy": "p", 1611 "ldap_dn": "l" 1612 }` 1613 1614 testJSONMarshal(t, u, want) 1615 } 1616 1617 func TestTeams_Marshal(t *testing.T) { 1618 testJSONMarshal(t, &Team{}, "{}") 1619 1620 u := &Team{ 1621 ID: Int64(1), 1622 NodeID: String("n"), 1623 Name: String("n"), 1624 Description: String("d"), 1625 URL: String("u"), 1626 Slug: String("s"), 1627 Permission: String("p"), 1628 Privacy: String("p"), 1629 MembersCount: Int(1), 1630 ReposCount: Int(1), 1631 MembersURL: String("m"), 1632 RepositoriesURL: String("r"), 1633 Organization: &Organization{ 1634 Login: String("l"), 1635 ID: Int64(1), 1636 NodeID: String("n"), 1637 AvatarURL: String("a"), 1638 HTMLURL: String("h"), 1639 Name: String("n"), 1640 Company: String("c"), 1641 Blog: String("b"), 1642 Location: String("l"), 1643 Email: String("e"), 1644 }, 1645 Parent: &Team{ 1646 ID: Int64(1), 1647 NodeID: String("n"), 1648 Name: String("n"), 1649 Description: String("d"), 1650 URL: String("u"), 1651 Slug: String("s"), 1652 Permission: String("p"), 1653 Privacy: String("p"), 1654 MembersCount: Int(1), 1655 ReposCount: Int(1), 1656 }, 1657 LDAPDN: String("l"), 1658 } 1659 1660 want := `{ 1661 "id": 1, 1662 "node_id": "n", 1663 "name": "n", 1664 "description": "d", 1665 "url": "u", 1666 "slug": "s", 1667 "permission": "p", 1668 "privacy": "p", 1669 "members_count": 1, 1670 "repos_count": 1, 1671 "members_url": "m", 1672 "repositories_url": "r", 1673 "organization": { 1674 "login": "l", 1675 "id": 1, 1676 "node_id": "n", 1677 "avatar_url": "a", 1678 "html_url": "h", 1679 "name": "n", 1680 "company": "c", 1681 "blog": "b", 1682 "location": "l", 1683 "email": "e" 1684 }, 1685 "parent": { 1686 "id": 1, 1687 "node_id": "n", 1688 "name": "n", 1689 "description": "d", 1690 "url": "u", 1691 "slug": "s", 1692 "permission": "p", 1693 "privacy": "p", 1694 "members_count": 1, 1695 "repos_count": 1 1696 }, 1697 "ldap_dn": "l" 1698 }` 1699 1700 testJSONMarshal(t, u, want) 1701 } 1702 1703 func TestInvitation_Marshal(t *testing.T) { 1704 testJSONMarshal(t, &Invitation{}, "{}") 1705 1706 u := &Invitation{ 1707 ID: Int64(1), 1708 NodeID: String("test node"), 1709 Login: String("login123"), 1710 Email: String("go@github.com"), 1711 Role: String("developer"), 1712 CreatedAt: &referenceTime, 1713 TeamCount: Int(99), 1714 InvitationTeamURL: String("url"), 1715 } 1716 1717 want := `{ 1718 "id": 1, 1719 "node_id": "test node", 1720 "login":"login123", 1721 "email":"go@github.com", 1722 "role":"developer", 1723 "created_at":` + referenceTimeStr + `, 1724 "team_count":99, 1725 "invitation_team_url":"url" 1726 }` 1727 1728 testJSONMarshal(t, u, want) 1729 } 1730 1731 func TestIDPGroup_Marshal(t *testing.T) { 1732 testJSONMarshal(t, &IDPGroup{}, "{}") 1733 1734 u := &IDPGroup{ 1735 GroupID: String("abc1"), 1736 GroupName: String("test group"), 1737 GroupDescription: String("test group descripation"), 1738 } 1739 1740 want := `{ 1741 "group_id": "abc1", 1742 "group_name": "test group", 1743 "group_description":"test group descripation" 1744 }` 1745 1746 testJSONMarshal(t, u, want) 1747 } 1748 1749 func TestTeamsService_GetExternalGroup(t *testing.T) { 1750 client, mux, _, teardown := setup() 1751 defer teardown() 1752 1753 mux.HandleFunc("/orgs/o/external-group/123", func(w http.ResponseWriter, r *http.Request) { 1754 testMethod(t, r, "GET") 1755 fmt.Fprint(w, `{ 1756 "group_id": 123, 1757 "group_name": "Octocat admins", 1758 "updated_at": "2006-01-02T15:04:05Z", 1759 "teams": [ 1760 { 1761 "team_id": 1, 1762 "team_name": "team-test" 1763 }, 1764 { 1765 "team_id": 2, 1766 "team_name": "team-test2" 1767 } 1768 ], 1769 "members": [ 1770 { 1771 "member_id": 1, 1772 "member_login": "mona-lisa_eocsaxrs", 1773 "member_name": "Mona Lisa", 1774 "member_email": "mona_lisa@github.com" 1775 }, 1776 { 1777 "member_id": 2, 1778 "member_login": "octo-lisa_eocsaxrs", 1779 "member_name": "Octo Lisa", 1780 "member_email": "octo_lisa@github.com" 1781 } 1782 ] 1783 }`) 1784 }) 1785 1786 ctx := context.Background() 1787 externalGroup, _, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1788 if err != nil { 1789 t.Errorf("Teams.GetExternalGroup returned error: %v", err) 1790 } 1791 1792 want := &ExternalGroup{ 1793 GroupID: Int64(123), 1794 GroupName: String("Octocat admins"), 1795 UpdatedAt: &Timestamp{Time: referenceTime}, 1796 Teams: []*ExternalGroupTeam{ 1797 { 1798 TeamID: Int64(1), 1799 TeamName: String("team-test"), 1800 }, 1801 { 1802 TeamID: Int64(2), 1803 TeamName: String("team-test2"), 1804 }, 1805 }, 1806 Members: []*ExternalGroupMember{ 1807 { 1808 MemberID: Int64(1), 1809 MemberLogin: String("mona-lisa_eocsaxrs"), 1810 MemberName: String("Mona Lisa"), 1811 MemberEmail: String("mona_lisa@github.com"), 1812 }, 1813 { 1814 MemberID: Int64(2), 1815 MemberLogin: String("octo-lisa_eocsaxrs"), 1816 MemberName: String("Octo Lisa"), 1817 MemberEmail: String("octo_lisa@github.com"), 1818 }, 1819 }, 1820 } 1821 if !cmp.Equal(externalGroup, want) { 1822 t.Errorf("Teams.GetExternalGroup returned %+v, want %+v", externalGroup, want) 1823 } 1824 1825 const methodName = "GetExternalGroup" 1826 testBadOptions(t, methodName, func() (err error) { 1827 _, _, err = client.Teams.GetExternalGroup(ctx, "\n", -1) 1828 return err 1829 }) 1830 1831 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1832 got, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1833 if got != nil { 1834 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1835 } 1836 return resp, err 1837 }) 1838 } 1839 1840 func TestTeamsService_GetExternalGroup_notFound(t *testing.T) { 1841 client, mux, _, teardown := setup() 1842 defer teardown() 1843 1844 mux.HandleFunc("/orgs/o/external-group/123", func(w http.ResponseWriter, r *http.Request) { 1845 testMethod(t, r, "GET") 1846 w.WriteHeader(http.StatusNotFound) 1847 }) 1848 1849 ctx := context.Background() 1850 eg, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1851 if err == nil { 1852 t.Errorf("Expected HTTP 404 response") 1853 } 1854 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 1855 t.Errorf("Teams.GetExternalGroup returned status %d, want %d", got, want) 1856 } 1857 if eg != nil { 1858 t.Errorf("Teams.GetExternalGroup returned %+v, want nil", eg) 1859 } 1860 } 1861 1862 func TestTeamsService_ListExternalGroups(t *testing.T) { 1863 client, mux, _, teardown := setup() 1864 defer teardown() 1865 1866 mux.HandleFunc("/orgs/o/external-groups", func(w http.ResponseWriter, r *http.Request) { 1867 testMethod(t, r, "GET") 1868 fmt.Fprint(w, `{ 1869 "groups": [ 1870 { 1871 "group_id": 123, 1872 "group_name": "Octocat admins", 1873 "updated_at": "2006-01-02T15:04:05Z" 1874 } 1875 ] 1876 }`) 1877 }) 1878 1879 ctx := context.Background() 1880 opts := &ListExternalGroupsOptions{ 1881 DisplayName: String("Octocat"), 1882 } 1883 list, _, err := client.Teams.ListExternalGroups(ctx, "o", opts) 1884 if err != nil { 1885 t.Errorf("Teams.ListExternalGroups returned error: %v", err) 1886 } 1887 1888 want := &ExternalGroupList{ 1889 Groups: []*ExternalGroup{ 1890 { 1891 GroupID: Int64(123), 1892 GroupName: String("Octocat admins"), 1893 UpdatedAt: &Timestamp{Time: referenceTime}, 1894 }, 1895 }, 1896 } 1897 if !cmp.Equal(list, want) { 1898 t.Errorf("Teams.ListExternalGroups returned %+v, want %+v", list, want) 1899 } 1900 1901 const methodName = "ListExternalGroups" 1902 testBadOptions(t, methodName, func() (err error) { 1903 _, _, err = client.Teams.ListExternalGroups(ctx, "\n", opts) 1904 return err 1905 }) 1906 1907 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1908 got, resp, err := client.Teams.ListExternalGroups(ctx, "o", opts) 1909 if got != nil { 1910 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1911 } 1912 return resp, err 1913 }) 1914 } 1915 1916 func TestTeamsService_ListExternalGroups_notFound(t *testing.T) { 1917 client, mux, _, teardown := setup() 1918 defer teardown() 1919 1920 mux.HandleFunc("/orgs/o/external-groups", func(w http.ResponseWriter, r *http.Request) { 1921 testMethod(t, r, "GET") 1922 w.WriteHeader(http.StatusNotFound) 1923 }) 1924 1925 ctx := context.Background() 1926 eg, resp, err := client.Teams.ListExternalGroups(ctx, "o", nil) 1927 if err == nil { 1928 t.Errorf("Expected HTTP 404 response") 1929 } 1930 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 1931 t.Errorf("Teams.ListExternalGroups returned status %d, want %d", got, want) 1932 } 1933 if eg != nil { 1934 t.Errorf("Teams.ListExternalGroups returned %+v, want nil", eg) 1935 } 1936 } 1937 1938 func TestTeamsService_UpdateConnectedExternalGroup(t *testing.T) { 1939 client, mux, _, teardown := setup() 1940 defer teardown() 1941 1942 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 1943 testMethod(t, r, "PATCH") 1944 fmt.Fprint(w, `{ 1945 "group_id": 123, 1946 "group_name": "Octocat admins", 1947 "updated_at": "2006-01-02T15:04:05Z", 1948 "teams": [ 1949 { 1950 "team_id": 1, 1951 "team_name": "team-test" 1952 }, 1953 { 1954 "team_id": 2, 1955 "team_name": "team-test2" 1956 } 1957 ], 1958 "members": [ 1959 { 1960 "member_id": 1, 1961 "member_login": "mona-lisa_eocsaxrs", 1962 "member_name": "Mona Lisa", 1963 "member_email": "mona_lisa@github.com" 1964 }, 1965 { 1966 "member_id": 2, 1967 "member_login": "octo-lisa_eocsaxrs", 1968 "member_name": "Octo Lisa", 1969 "member_email": "octo_lisa@github.com" 1970 } 1971 ] 1972 }`) 1973 }) 1974 1975 ctx := context.Background() 1976 body := &ExternalGroup{ 1977 GroupID: Int64(123), 1978 } 1979 externalGroup, _, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 1980 if err != nil { 1981 t.Errorf("Teams.UpdateConnectedExternalGroup returned error: %v", err) 1982 } 1983 1984 want := &ExternalGroup{ 1985 GroupID: Int64(123), 1986 GroupName: String("Octocat admins"), 1987 UpdatedAt: &Timestamp{Time: referenceTime}, 1988 Teams: []*ExternalGroupTeam{ 1989 { 1990 TeamID: Int64(1), 1991 TeamName: String("team-test"), 1992 }, 1993 { 1994 TeamID: Int64(2), 1995 TeamName: String("team-test2"), 1996 }, 1997 }, 1998 Members: []*ExternalGroupMember{ 1999 { 2000 MemberID: Int64(1), 2001 MemberLogin: String("mona-lisa_eocsaxrs"), 2002 MemberName: String("Mona Lisa"), 2003 MemberEmail: String("mona_lisa@github.com"), 2004 }, 2005 { 2006 MemberID: Int64(2), 2007 MemberLogin: String("octo-lisa_eocsaxrs"), 2008 MemberName: String("Octo Lisa"), 2009 MemberEmail: String("octo_lisa@github.com"), 2010 }, 2011 }, 2012 } 2013 if !cmp.Equal(externalGroup, want) { 2014 t.Errorf("Teams.GetExternalGroup returned %+v, want %+v", externalGroup, want) 2015 } 2016 2017 const methodName = "UpdateConnectedExternalGroup" 2018 testBadOptions(t, methodName, func() (err error) { 2019 _, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, "\n", "\n", body) 2020 return err 2021 }) 2022 2023 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2024 got, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 2025 if got != nil { 2026 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2027 } 2028 return resp, err 2029 }) 2030 } 2031 2032 func TestTeamsService_UpdateConnectedExternalGroup_notFound(t *testing.T) { 2033 client, mux, _, teardown := setup() 2034 defer teardown() 2035 2036 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2037 testMethod(t, r, "PATCH") 2038 w.WriteHeader(http.StatusNotFound) 2039 }) 2040 2041 ctx := context.Background() 2042 body := &ExternalGroup{ 2043 GroupID: Int64(123), 2044 } 2045 eg, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 2046 if err == nil { 2047 t.Errorf("Expected HTTP 404 response") 2048 } 2049 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 2050 t.Errorf("Teams.UpdateConnectedExternalGroup returned status %d, want %d", got, want) 2051 } 2052 if eg != nil { 2053 t.Errorf("Teams.UpdateConnectedExternalGroup returned %+v, want nil", eg) 2054 } 2055 } 2056 2057 func TestTeamsService_RemoveConnectedExternalGroup(t *testing.T) { 2058 client, mux, _, teardown := setup() 2059 defer teardown() 2060 2061 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2062 testMethod(t, r, "DELETE") 2063 w.WriteHeader(http.StatusNoContent) 2064 }) 2065 2066 ctx := context.Background() 2067 _, err := client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2068 if err != nil { 2069 t.Errorf("Teams.RemoveConnectedExternalGroup returned error: %v", err) 2070 } 2071 2072 const methodName = "RemoveConnectedExternalGroup" 2073 testBadOptions(t, methodName, func() (err error) { 2074 _, err = client.Teams.RemoveConnectedExternalGroup(ctx, "\n", "\n") 2075 return err 2076 }) 2077 2078 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2079 return client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2080 }) 2081 } 2082 2083 func TestTeamsService_RemoveConnectedExternalGroup_notFound(t *testing.T) { 2084 client, mux, _, teardown := setup() 2085 defer teardown() 2086 2087 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2088 testMethod(t, r, "DELETE") 2089 w.WriteHeader(http.StatusNotFound) 2090 }) 2091 2092 ctx := context.Background() 2093 resp, err := client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2094 if err == nil { 2095 t.Errorf("Expected HTTP 404 response") 2096 } 2097 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 2098 t.Errorf("Teams.GetExternalGroup returned status %d, want %d", got, want) 2099 } 2100 } 2101 2102 func TestIDPGroupList_Marshal(t *testing.T) { 2103 testJSONMarshal(t, &IDPGroupList{}, "{}") 2104 2105 u := &IDPGroupList{ 2106 Groups: []*IDPGroup{ 2107 { 2108 GroupID: String("abc1"), 2109 GroupName: String("test group"), 2110 GroupDescription: String("test group descripation"), 2111 }, 2112 { 2113 GroupID: String("abc2"), 2114 GroupName: String("test group2"), 2115 GroupDescription: String("test group descripation2"), 2116 }, 2117 }, 2118 } 2119 2120 want := `{ 2121 "groups": [ 2122 { 2123 "group_id": "abc1", 2124 "group_name": "test group", 2125 "group_description": "test group descripation" 2126 }, 2127 { 2128 "group_id": "abc2", 2129 "group_name": "test group2", 2130 "group_description": "test group descripation2" 2131 } 2132 ] 2133 }` 2134 2135 testJSONMarshal(t, u, want) 2136 } 2137 2138 func TestExternalGroupMember_Marshal(t *testing.T) { 2139 testJSONMarshal(t, &ExternalGroupMember{}, "{}") 2140 2141 u := &ExternalGroupMember{ 2142 MemberID: Int64(1), 2143 MemberLogin: String("test member"), 2144 MemberName: String("test member name"), 2145 MemberEmail: String("test member email"), 2146 } 2147 2148 want := `{ 2149 "member_id": 1, 2150 "member_login": "test member", 2151 "member_name":"test member name", 2152 "member_email":"test member email" 2153 }` 2154 2155 testJSONMarshal(t, u, want) 2156 } 2157 2158 func TestExternalGroup_Marshal(t *testing.T) { 2159 testJSONMarshal(t, &ExternalGroup{}, "{}") 2160 2161 u := &ExternalGroup{ 2162 GroupID: Int64(123), 2163 GroupName: String("group1"), 2164 UpdatedAt: &Timestamp{referenceTime}, 2165 Teams: []*ExternalGroupTeam{ 2166 { 2167 TeamID: Int64(1), 2168 TeamName: String("team-test"), 2169 }, 2170 { 2171 TeamID: Int64(2), 2172 TeamName: String("team-test2"), 2173 }, 2174 }, 2175 Members: []*ExternalGroupMember{ 2176 { 2177 MemberID: Int64(1), 2178 MemberLogin: String("test"), 2179 MemberName: String("test"), 2180 MemberEmail: String("test@github.com"), 2181 }, 2182 }, 2183 } 2184 2185 want := `{ 2186 "group_id": 123, 2187 "group_name": "group1", 2188 "updated_at": ` + referenceTimeStr + `, 2189 "teams": [ 2190 { 2191 "team_id": 1, 2192 "team_name": "team-test" 2193 }, 2194 { 2195 "team_id": 2, 2196 "team_name": "team-test2" 2197 } 2198 ], 2199 "members": [ 2200 { 2201 "member_id": 1, 2202 "member_login": "test", 2203 "member_name": "test", 2204 "member_email": "test@github.com" 2205 } 2206 ] 2207 }` 2208 2209 testJSONMarshal(t, u, want) 2210 } 2211 2212 func TestExternalGroupTeam_Marshal(t *testing.T) { 2213 testJSONMarshal(t, &ExternalGroupTeam{}, "{}") 2214 2215 u := &ExternalGroupTeam{ 2216 TeamID: Int64(123), 2217 TeamName: String("test"), 2218 } 2219 2220 want := `{ 2221 "team_id": 123, 2222 "team_name": "test" 2223 }` 2224 2225 testJSONMarshal(t, u, want) 2226 } 2227 2228 func TestListExternalGroupsOptions_Marshal(t *testing.T) { 2229 testJSONMarshal(t, &ListExternalGroupsOptions{}, "{}") 2230 2231 u := &ListExternalGroupsOptions{ 2232 DisplayName: String("test"), 2233 ListOptions: ListOptions{ 2234 Page: 1, 2235 PerPage: 2, 2236 }, 2237 } 2238 2239 want := `{ 2240 "DisplayName": "test", 2241 "page": 1, 2242 "PerPage": 2 2243 }` 2244 2245 testJSONMarshal(t, u, want) 2246 } 2247 2248 func TestTeamAddTeamRepoOptions_Marshal(t *testing.T) { 2249 testJSONMarshal(t, &TeamAddTeamRepoOptions{}, "{}") 2250 2251 u := &TeamAddTeamRepoOptions{ 2252 Permission: "a", 2253 } 2254 2255 want := `{ 2256 "permission": "a" 2257 }` 2258 2259 testJSONMarshal(t, u, want) 2260 }