github.com/google/go-github/v66@v66.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 t.Parallel() 23 client, mux, _ := setup(t) 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 t.Parallel() 60 client, _, _ := setup(t) 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 t.Parallel() 69 client, mux, _ := setup(t) 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 t.Parallel() 104 client, mux, _ := setup(t) 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 t.Parallel() 126 client, mux, _ := setup(t) 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 t.Parallel() 161 client, _, _ := setup(t) 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 t.Parallel() 170 client, mux, _ := setup(t) 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 t.Parallel() 192 client, mux, _ := setup(t) 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 t.Parallel() 236 client, _, _ := setup(t) 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 t.Parallel() 245 client, mux, _ := setup(t) 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 t.Parallel() 289 client, mux, _ := setup(t) 290 291 input := NewTeam{Name: "n", NotificationSetting: String("notifications_enabled"), 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,"notification_setting":"notifications_enabled","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 t.Parallel() 329 client, mux, _ := setup(t) 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 t.Parallel() 373 client, mux, _ := setup(t) 374 375 input := NewTeam{Name: "n", NotificationSetting: String("notifications_disabled"), 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,"notification_setting":"notifications_disabled","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 t.Parallel() 413 client, mux, _ := setup(t) 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 t.Parallel() 438 client, mux, _ := setup(t) 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 t.Parallel() 463 client, mux, _ := setup(t) 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 t.Parallel() 500 client, mux, _ := setup(t) 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 t.Parallel() 537 client, mux, _ := setup(t) 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 t.Parallel() 576 client, mux, _ := setup(t) 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 t.Parallel() 615 client, mux, _ := setup(t) 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 t.Parallel() 652 client, mux, _ := setup(t) 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 t.Parallel() 689 client, mux, _ := setup(t) 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 t.Parallel() 711 client, mux, _ := setup(t) 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 t.Parallel() 733 client, mux, _ := setup(t) 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 t.Parallel() 755 client, mux, _ := setup(t) 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 t.Parallel() 777 client, _, _ := setup(t) 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 t.Parallel() 786 client, _, _ := setup(t) 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 t.Parallel() 795 client, mux, _ := setup(t) 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 t.Parallel() 830 client, mux, _ := setup(t) 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 t.Parallel() 865 client, mux, _ := setup(t) 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 t.Parallel() 881 client, mux, _ := setup(t) 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 t.Parallel() 897 client, _, _ := setup(t) 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 t.Parallel() 906 client, _, _ := setup(t) 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 t.Parallel() 915 client, mux, _ := setup(t) 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 t.Parallel() 941 client, mux, _ := setup(t) 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 t.Parallel() 967 client, _, _ := setup(t) 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 t.Parallel() 976 client, _, _ := setup(t) 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 t.Parallel() 985 client, mux, _ := setup(t) 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 t.Parallel() 1017 client, mux, _ := setup(t) 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 t.Parallel() 1054 client, mux, _ := setup(t) 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 t.Parallel() 1091 client, mux, _ := setup(t) 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 t.Parallel() 1128 client, mux, _ := setup(t) 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 t.Parallel() 1165 client, mux, _ := setup(t) 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 t.Parallel() 1204 client, mux, _ := setup(t) 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 t.Parallel() 1243 client, mux, _ := setup(t) 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 t.Parallel() 1271 client, mux, _ := setup(t) 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 t.Parallel() 1299 client, mux, _ := setup(t) 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 t.Parallel() 1350 client, mux, _ := setup(t) 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 t.Parallel() 1393 client, mux, _ := setup(t) 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 t.Parallel() 1436 client, mux, _ := setup(t) 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 t.Parallel() 1489 client, mux, _ := setup(t) 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 t.Parallel() 1541 client, mux, _ := setup(t) 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 t.Parallel() 1568 client, mux, _ := setup(t) 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 t.Parallel() 1595 testJSONMarshal(t, &NewTeam{}, "{}") 1596 1597 u := &NewTeam{ 1598 Name: "n", 1599 Description: String("d"), 1600 Maintainers: []string{"m1", "m2"}, 1601 RepoNames: []string{"repo1", "repo2"}, 1602 NotificationSetting: String("notifications_enabled"), 1603 ParentTeamID: Int64(1), 1604 Permission: String("perm"), 1605 Privacy: String("p"), 1606 LDAPDN: String("l"), 1607 } 1608 1609 want := `{ 1610 "name": "n", 1611 "description": "d", 1612 "maintainers": ["m1", "m2"], 1613 "repo_names": ["repo1", "repo2"], 1614 "parent_team_id": 1, 1615 "notification_setting": "notifications_enabled", 1616 "permission": "perm", 1617 "privacy": "p", 1618 "ldap_dn": "l" 1619 }` 1620 1621 testJSONMarshal(t, u, want) 1622 } 1623 1624 func TestTeams_Marshal(t *testing.T) { 1625 t.Parallel() 1626 testJSONMarshal(t, &Team{}, "{}") 1627 1628 u := &Team{ 1629 ID: Int64(1), 1630 NodeID: String("n"), 1631 Name: String("n"), 1632 Description: String("d"), 1633 URL: String("u"), 1634 Slug: String("s"), 1635 Permission: String("p"), 1636 Privacy: String("p"), 1637 MembersCount: Int(1), 1638 ReposCount: Int(1), 1639 MembersURL: String("m"), 1640 RepositoriesURL: String("r"), 1641 Organization: &Organization{ 1642 Login: String("l"), 1643 ID: Int64(1), 1644 NodeID: String("n"), 1645 AvatarURL: String("a"), 1646 HTMLURL: String("h"), 1647 Name: String("n"), 1648 Company: String("c"), 1649 Blog: String("b"), 1650 Location: String("l"), 1651 Email: String("e"), 1652 }, 1653 Parent: &Team{ 1654 ID: Int64(1), 1655 NodeID: String("n"), 1656 Name: String("n"), 1657 Description: String("d"), 1658 URL: String("u"), 1659 Slug: String("s"), 1660 Permission: String("p"), 1661 Privacy: String("p"), 1662 MembersCount: Int(1), 1663 ReposCount: Int(1), 1664 }, 1665 LDAPDN: String("l"), 1666 } 1667 1668 want := `{ 1669 "id": 1, 1670 "node_id": "n", 1671 "name": "n", 1672 "description": "d", 1673 "url": "u", 1674 "slug": "s", 1675 "permission": "p", 1676 "privacy": "p", 1677 "members_count": 1, 1678 "repos_count": 1, 1679 "members_url": "m", 1680 "repositories_url": "r", 1681 "organization": { 1682 "login": "l", 1683 "id": 1, 1684 "node_id": "n", 1685 "avatar_url": "a", 1686 "html_url": "h", 1687 "name": "n", 1688 "company": "c", 1689 "blog": "b", 1690 "location": "l", 1691 "email": "e" 1692 }, 1693 "parent": { 1694 "id": 1, 1695 "node_id": "n", 1696 "name": "n", 1697 "description": "d", 1698 "url": "u", 1699 "slug": "s", 1700 "permission": "p", 1701 "privacy": "p", 1702 "members_count": 1, 1703 "repos_count": 1 1704 }, 1705 "ldap_dn": "l" 1706 }` 1707 1708 testJSONMarshal(t, u, want) 1709 } 1710 1711 func TestInvitation_Marshal(t *testing.T) { 1712 t.Parallel() 1713 testJSONMarshal(t, &Invitation{}, "{}") 1714 1715 u := &Invitation{ 1716 ID: Int64(1), 1717 NodeID: String("test node"), 1718 Login: String("login123"), 1719 Email: String("go@github.com"), 1720 Role: String("developer"), 1721 CreatedAt: &Timestamp{referenceTime}, 1722 TeamCount: Int(99), 1723 InvitationTeamURL: String("url"), 1724 } 1725 1726 want := `{ 1727 "id": 1, 1728 "node_id": "test node", 1729 "login":"login123", 1730 "email":"go@github.com", 1731 "role":"developer", 1732 "created_at":` + referenceTimeStr + `, 1733 "team_count":99, 1734 "invitation_team_url":"url" 1735 }` 1736 1737 testJSONMarshal(t, u, want) 1738 } 1739 1740 func TestIDPGroup_Marshal(t *testing.T) { 1741 t.Parallel() 1742 testJSONMarshal(t, &IDPGroup{}, "{}") 1743 1744 u := &IDPGroup{ 1745 GroupID: String("abc1"), 1746 GroupName: String("test group"), 1747 GroupDescription: String("test group description"), 1748 } 1749 1750 want := `{ 1751 "group_id": "abc1", 1752 "group_name": "test group", 1753 "group_description":"test group description" 1754 }` 1755 1756 testJSONMarshal(t, u, want) 1757 } 1758 1759 func TestTeamsService_GetExternalGroup(t *testing.T) { 1760 t.Parallel() 1761 client, mux, _ := setup(t) 1762 1763 mux.HandleFunc("/orgs/o/external-group/123", func(w http.ResponseWriter, r *http.Request) { 1764 testMethod(t, r, "GET") 1765 fmt.Fprint(w, `{ 1766 "group_id": 123, 1767 "group_name": "Octocat admins", 1768 "updated_at": "2006-01-02T15:04:05Z", 1769 "teams": [ 1770 { 1771 "team_id": 1, 1772 "team_name": "team-test" 1773 }, 1774 { 1775 "team_id": 2, 1776 "team_name": "team-test2" 1777 } 1778 ], 1779 "members": [ 1780 { 1781 "member_id": 1, 1782 "member_login": "mona-lisa_eocsaxrs", 1783 "member_name": "Mona Lisa", 1784 "member_email": "mona_lisa@github.com" 1785 }, 1786 { 1787 "member_id": 2, 1788 "member_login": "octo-lisa_eocsaxrs", 1789 "member_name": "Octo Lisa", 1790 "member_email": "octo_lisa@github.com" 1791 } 1792 ] 1793 }`) 1794 }) 1795 1796 ctx := context.Background() 1797 externalGroup, _, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1798 if err != nil { 1799 t.Errorf("Teams.GetExternalGroup returned error: %v", err) 1800 } 1801 1802 want := &ExternalGroup{ 1803 GroupID: Int64(123), 1804 GroupName: String("Octocat admins"), 1805 UpdatedAt: &Timestamp{Time: referenceTime}, 1806 Teams: []*ExternalGroupTeam{ 1807 { 1808 TeamID: Int64(1), 1809 TeamName: String("team-test"), 1810 }, 1811 { 1812 TeamID: Int64(2), 1813 TeamName: String("team-test2"), 1814 }, 1815 }, 1816 Members: []*ExternalGroupMember{ 1817 { 1818 MemberID: Int64(1), 1819 MemberLogin: String("mona-lisa_eocsaxrs"), 1820 MemberName: String("Mona Lisa"), 1821 MemberEmail: String("mona_lisa@github.com"), 1822 }, 1823 { 1824 MemberID: Int64(2), 1825 MemberLogin: String("octo-lisa_eocsaxrs"), 1826 MemberName: String("Octo Lisa"), 1827 MemberEmail: String("octo_lisa@github.com"), 1828 }, 1829 }, 1830 } 1831 if !cmp.Equal(externalGroup, want) { 1832 t.Errorf("Teams.GetExternalGroup returned %+v, want %+v", externalGroup, want) 1833 } 1834 1835 const methodName = "GetExternalGroup" 1836 testBadOptions(t, methodName, func() (err error) { 1837 _, _, err = client.Teams.GetExternalGroup(ctx, "\n", -1) 1838 return err 1839 }) 1840 1841 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1842 got, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1843 if got != nil { 1844 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1845 } 1846 return resp, err 1847 }) 1848 } 1849 1850 func TestTeamsService_GetExternalGroup_notFound(t *testing.T) { 1851 t.Parallel() 1852 client, mux, _ := setup(t) 1853 1854 mux.HandleFunc("/orgs/o/external-group/123", func(w http.ResponseWriter, r *http.Request) { 1855 testMethod(t, r, "GET") 1856 w.WriteHeader(http.StatusNotFound) 1857 }) 1858 1859 ctx := context.Background() 1860 eg, resp, err := client.Teams.GetExternalGroup(ctx, "o", 123) 1861 if err == nil { 1862 t.Errorf("Expected HTTP 404 response") 1863 } 1864 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 1865 t.Errorf("Teams.GetExternalGroup returned status %d, want %d", got, want) 1866 } 1867 if eg != nil { 1868 t.Errorf("Teams.GetExternalGroup returned %+v, want nil", eg) 1869 } 1870 } 1871 1872 func TestTeamsService_ListExternalGroups(t *testing.T) { 1873 t.Parallel() 1874 client, mux, _ := setup(t) 1875 1876 mux.HandleFunc("/orgs/o/external-groups", func(w http.ResponseWriter, r *http.Request) { 1877 testMethod(t, r, "GET") 1878 fmt.Fprint(w, `{ 1879 "groups": [ 1880 { 1881 "group_id": 123, 1882 "group_name": "Octocat admins", 1883 "updated_at": "2006-01-02T15:04:05Z" 1884 } 1885 ] 1886 }`) 1887 }) 1888 1889 ctx := context.Background() 1890 opts := &ListExternalGroupsOptions{ 1891 DisplayName: String("Octocat"), 1892 } 1893 list, _, err := client.Teams.ListExternalGroups(ctx, "o", opts) 1894 if err != nil { 1895 t.Errorf("Teams.ListExternalGroups returned error: %v", err) 1896 } 1897 1898 want := &ExternalGroupList{ 1899 Groups: []*ExternalGroup{ 1900 { 1901 GroupID: Int64(123), 1902 GroupName: String("Octocat admins"), 1903 UpdatedAt: &Timestamp{Time: referenceTime}, 1904 }, 1905 }, 1906 } 1907 if !cmp.Equal(list, want) { 1908 t.Errorf("Teams.ListExternalGroups returned %+v, want %+v", list, want) 1909 } 1910 1911 const methodName = "ListExternalGroups" 1912 testBadOptions(t, methodName, func() (err error) { 1913 _, _, err = client.Teams.ListExternalGroups(ctx, "\n", opts) 1914 return err 1915 }) 1916 1917 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1918 got, resp, err := client.Teams.ListExternalGroups(ctx, "o", opts) 1919 if got != nil { 1920 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1921 } 1922 return resp, err 1923 }) 1924 } 1925 1926 func TestTeamsService_ListExternalGroups_notFound(t *testing.T) { 1927 t.Parallel() 1928 client, mux, _ := setup(t) 1929 1930 mux.HandleFunc("/orgs/o/external-groups", func(w http.ResponseWriter, r *http.Request) { 1931 testMethod(t, r, "GET") 1932 w.WriteHeader(http.StatusNotFound) 1933 }) 1934 1935 ctx := context.Background() 1936 eg, resp, err := client.Teams.ListExternalGroups(ctx, "o", nil) 1937 if err == nil { 1938 t.Errorf("Expected HTTP 404 response") 1939 } 1940 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 1941 t.Errorf("Teams.ListExternalGroups returned status %d, want %d", got, want) 1942 } 1943 if eg != nil { 1944 t.Errorf("Teams.ListExternalGroups returned %+v, want nil", eg) 1945 } 1946 } 1947 1948 func TestTeamsService_ListExternalGroupsForTeamBySlug(t *testing.T) { 1949 t.Parallel() 1950 client, mux, _ := setup(t) 1951 1952 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 1953 testMethod(t, r, "GET") 1954 fmt.Fprint(w, `{ 1955 "groups": [ 1956 { 1957 "group_id": 123, 1958 "group_name": "Octocat admins", 1959 "updated_at": "2006-01-02T15:04:05Z" 1960 } 1961 ] 1962 }`) 1963 }) 1964 1965 ctx := context.Background() 1966 list, _, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t") 1967 if err != nil { 1968 t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned error: %v", err) 1969 } 1970 1971 want := &ExternalGroupList{ 1972 Groups: []*ExternalGroup{ 1973 { 1974 GroupID: Int64(123), 1975 GroupName: String("Octocat admins"), 1976 UpdatedAt: &Timestamp{Time: referenceTime}, 1977 }, 1978 }, 1979 } 1980 if !cmp.Equal(list, want) { 1981 t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned %+v, want %+v", list, want) 1982 } 1983 1984 const methodName = "ListExternalGroupsForTeamBySlug" 1985 testBadOptions(t, methodName, func() (err error) { 1986 _, _, err = client.Teams.ListExternalGroupsForTeamBySlug(ctx, "\n", "\n") 1987 return err 1988 }) 1989 1990 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 1991 got, resp, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t") 1992 if got != nil { 1993 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 1994 } 1995 return resp, err 1996 }) 1997 } 1998 1999 func TestTeamsService_ListExternalGroupsForTeamBySlug_notFound(t *testing.T) { 2000 t.Parallel() 2001 client, mux, _ := setup(t) 2002 2003 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2004 testMethod(t, r, "GET") 2005 w.WriteHeader(http.StatusNotFound) 2006 }) 2007 2008 ctx := context.Background() 2009 eg, resp, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, "o", "t") 2010 if err == nil { 2011 t.Errorf("Expected HTTP 404 response") 2012 } 2013 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 2014 t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned status %d, want %d", got, want) 2015 } 2016 if eg != nil { 2017 t.Errorf("Teams.ListExternalGroupsForTeamBySlug returned %+v, want nil", eg) 2018 } 2019 } 2020 2021 func TestTeamsService_UpdateConnectedExternalGroup(t *testing.T) { 2022 t.Parallel() 2023 client, mux, _ := setup(t) 2024 2025 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2026 testMethod(t, r, "PATCH") 2027 fmt.Fprint(w, `{ 2028 "group_id": 123, 2029 "group_name": "Octocat admins", 2030 "updated_at": "2006-01-02T15:04:05Z", 2031 "teams": [ 2032 { 2033 "team_id": 1, 2034 "team_name": "team-test" 2035 }, 2036 { 2037 "team_id": 2, 2038 "team_name": "team-test2" 2039 } 2040 ], 2041 "members": [ 2042 { 2043 "member_id": 1, 2044 "member_login": "mona-lisa_eocsaxrs", 2045 "member_name": "Mona Lisa", 2046 "member_email": "mona_lisa@github.com" 2047 }, 2048 { 2049 "member_id": 2, 2050 "member_login": "octo-lisa_eocsaxrs", 2051 "member_name": "Octo Lisa", 2052 "member_email": "octo_lisa@github.com" 2053 } 2054 ] 2055 }`) 2056 }) 2057 2058 ctx := context.Background() 2059 body := &ExternalGroup{ 2060 GroupID: Int64(123), 2061 } 2062 externalGroup, _, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 2063 if err != nil { 2064 t.Errorf("Teams.UpdateConnectedExternalGroup returned error: %v", err) 2065 } 2066 2067 want := &ExternalGroup{ 2068 GroupID: Int64(123), 2069 GroupName: String("Octocat admins"), 2070 UpdatedAt: &Timestamp{Time: referenceTime}, 2071 Teams: []*ExternalGroupTeam{ 2072 { 2073 TeamID: Int64(1), 2074 TeamName: String("team-test"), 2075 }, 2076 { 2077 TeamID: Int64(2), 2078 TeamName: String("team-test2"), 2079 }, 2080 }, 2081 Members: []*ExternalGroupMember{ 2082 { 2083 MemberID: Int64(1), 2084 MemberLogin: String("mona-lisa_eocsaxrs"), 2085 MemberName: String("Mona Lisa"), 2086 MemberEmail: String("mona_lisa@github.com"), 2087 }, 2088 { 2089 MemberID: Int64(2), 2090 MemberLogin: String("octo-lisa_eocsaxrs"), 2091 MemberName: String("Octo Lisa"), 2092 MemberEmail: String("octo_lisa@github.com"), 2093 }, 2094 }, 2095 } 2096 if !cmp.Equal(externalGroup, want) { 2097 t.Errorf("Teams.GetExternalGroup returned %+v, want %+v", externalGroup, want) 2098 } 2099 2100 const methodName = "UpdateConnectedExternalGroup" 2101 testBadOptions(t, methodName, func() (err error) { 2102 _, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, "\n", "\n", body) 2103 return err 2104 }) 2105 2106 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2107 got, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 2108 if got != nil { 2109 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 2110 } 2111 return resp, err 2112 }) 2113 } 2114 2115 func TestTeamsService_UpdateConnectedExternalGroup_notFound(t *testing.T) { 2116 t.Parallel() 2117 client, mux, _ := setup(t) 2118 2119 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2120 testMethod(t, r, "PATCH") 2121 w.WriteHeader(http.StatusNotFound) 2122 }) 2123 2124 ctx := context.Background() 2125 body := &ExternalGroup{ 2126 GroupID: Int64(123), 2127 } 2128 eg, resp, err := client.Teams.UpdateConnectedExternalGroup(ctx, "o", "t", body) 2129 if err == nil { 2130 t.Errorf("Expected HTTP 404 response") 2131 } 2132 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 2133 t.Errorf("Teams.UpdateConnectedExternalGroup returned status %d, want %d", got, want) 2134 } 2135 if eg != nil { 2136 t.Errorf("Teams.UpdateConnectedExternalGroup returned %+v, want nil", eg) 2137 } 2138 } 2139 2140 func TestTeamsService_RemoveConnectedExternalGroup(t *testing.T) { 2141 t.Parallel() 2142 client, mux, _ := setup(t) 2143 2144 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2145 testMethod(t, r, "DELETE") 2146 w.WriteHeader(http.StatusNoContent) 2147 }) 2148 2149 ctx := context.Background() 2150 _, err := client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2151 if err != nil { 2152 t.Errorf("Teams.RemoveConnectedExternalGroup returned error: %v", err) 2153 } 2154 2155 const methodName = "RemoveConnectedExternalGroup" 2156 testBadOptions(t, methodName, func() (err error) { 2157 _, err = client.Teams.RemoveConnectedExternalGroup(ctx, "\n", "\n") 2158 return err 2159 }) 2160 2161 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 2162 return client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2163 }) 2164 } 2165 2166 func TestTeamsService_RemoveConnectedExternalGroup_notFound(t *testing.T) { 2167 t.Parallel() 2168 client, mux, _ := setup(t) 2169 2170 mux.HandleFunc("/orgs/o/teams/t/external-groups", func(w http.ResponseWriter, r *http.Request) { 2171 testMethod(t, r, "DELETE") 2172 w.WriteHeader(http.StatusNotFound) 2173 }) 2174 2175 ctx := context.Background() 2176 resp, err := client.Teams.RemoveConnectedExternalGroup(ctx, "o", "t") 2177 if err == nil { 2178 t.Errorf("Expected HTTP 404 response") 2179 } 2180 if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { 2181 t.Errorf("Teams.GetExternalGroup returned status %d, want %d", got, want) 2182 } 2183 } 2184 2185 func TestIDPGroupList_Marshal(t *testing.T) { 2186 t.Parallel() 2187 testJSONMarshal(t, &IDPGroupList{}, "{}") 2188 2189 u := &IDPGroupList{ 2190 Groups: []*IDPGroup{ 2191 { 2192 GroupID: String("abc1"), 2193 GroupName: String("test group"), 2194 GroupDescription: String("test group description"), 2195 }, 2196 { 2197 GroupID: String("abc2"), 2198 GroupName: String("test group2"), 2199 GroupDescription: String("test group description2"), 2200 }, 2201 }, 2202 } 2203 2204 want := `{ 2205 "groups": [ 2206 { 2207 "group_id": "abc1", 2208 "group_name": "test group", 2209 "group_description": "test group description" 2210 }, 2211 { 2212 "group_id": "abc2", 2213 "group_name": "test group2", 2214 "group_description": "test group description2" 2215 } 2216 ] 2217 }` 2218 2219 testJSONMarshal(t, u, want) 2220 } 2221 2222 func TestExternalGroupMember_Marshal(t *testing.T) { 2223 t.Parallel() 2224 testJSONMarshal(t, &ExternalGroupMember{}, "{}") 2225 2226 u := &ExternalGroupMember{ 2227 MemberID: Int64(1), 2228 MemberLogin: String("test member"), 2229 MemberName: String("test member name"), 2230 MemberEmail: String("test member email"), 2231 } 2232 2233 want := `{ 2234 "member_id": 1, 2235 "member_login": "test member", 2236 "member_name":"test member name", 2237 "member_email":"test member email" 2238 }` 2239 2240 testJSONMarshal(t, u, want) 2241 } 2242 2243 func TestExternalGroup_Marshal(t *testing.T) { 2244 t.Parallel() 2245 testJSONMarshal(t, &ExternalGroup{}, "{}") 2246 2247 u := &ExternalGroup{ 2248 GroupID: Int64(123), 2249 GroupName: String("group1"), 2250 UpdatedAt: &Timestamp{referenceTime}, 2251 Teams: []*ExternalGroupTeam{ 2252 { 2253 TeamID: Int64(1), 2254 TeamName: String("team-test"), 2255 }, 2256 { 2257 TeamID: Int64(2), 2258 TeamName: String("team-test2"), 2259 }, 2260 }, 2261 Members: []*ExternalGroupMember{ 2262 { 2263 MemberID: Int64(1), 2264 MemberLogin: String("test"), 2265 MemberName: String("test"), 2266 MemberEmail: String("test@github.com"), 2267 }, 2268 }, 2269 } 2270 2271 want := `{ 2272 "group_id": 123, 2273 "group_name": "group1", 2274 "updated_at": ` + referenceTimeStr + `, 2275 "teams": [ 2276 { 2277 "team_id": 1, 2278 "team_name": "team-test" 2279 }, 2280 { 2281 "team_id": 2, 2282 "team_name": "team-test2" 2283 } 2284 ], 2285 "members": [ 2286 { 2287 "member_id": 1, 2288 "member_login": "test", 2289 "member_name": "test", 2290 "member_email": "test@github.com" 2291 } 2292 ] 2293 }` 2294 2295 testJSONMarshal(t, u, want) 2296 } 2297 2298 func TestExternalGroupTeam_Marshal(t *testing.T) { 2299 t.Parallel() 2300 testJSONMarshal(t, &ExternalGroupTeam{}, "{}") 2301 2302 u := &ExternalGroupTeam{ 2303 TeamID: Int64(123), 2304 TeamName: String("test"), 2305 } 2306 2307 want := `{ 2308 "team_id": 123, 2309 "team_name": "test" 2310 }` 2311 2312 testJSONMarshal(t, u, want) 2313 } 2314 2315 func TestListExternalGroupsOptions_Marshal(t *testing.T) { 2316 t.Parallel() 2317 testJSONMarshal(t, &ListExternalGroupsOptions{}, "{}") 2318 2319 u := &ListExternalGroupsOptions{ 2320 DisplayName: String("test"), 2321 ListOptions: ListOptions{ 2322 Page: 1, 2323 PerPage: 2, 2324 }, 2325 } 2326 2327 want := `{ 2328 "DisplayName": "test", 2329 "page": 1, 2330 "PerPage": 2 2331 }` 2332 2333 testJSONMarshal(t, u, want) 2334 } 2335 2336 func TestTeamAddTeamRepoOptions_Marshal(t *testing.T) { 2337 t.Parallel() 2338 testJSONMarshal(t, &TeamAddTeamRepoOptions{}, "{}") 2339 2340 u := &TeamAddTeamRepoOptions{ 2341 Permission: "a", 2342 } 2343 2344 want := `{ 2345 "permission": "a" 2346 }` 2347 2348 testJSONMarshal(t, u, want) 2349 }