github.com/google/go-github/v53@v53.2.0/github/repos_collaborators_test.go (about) 1 // Copyright 2013 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "encoding/json" 11 "fmt" 12 "net/http" 13 "testing" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestRepositoriesService_ListCollaborators(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{"page": "2"}) 25 fmt.Fprintf(w, `[{"id":1}, {"id":2}]`) 26 }) 27 28 opt := &ListCollaboratorsOptions{ 29 ListOptions: ListOptions{Page: 2}, 30 } 31 ctx := context.Background() 32 users, _, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt) 33 if err != nil { 34 t.Errorf("Repositories.ListCollaborators returned error: %v", err) 35 } 36 37 want := []*User{{ID: Int64(1)}, {ID: Int64(2)}} 38 if !cmp.Equal(users, want) { 39 t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want) 40 } 41 42 const methodName = "ListCollaborators" 43 testBadOptions(t, methodName, func() (err error) { 44 _, _, err = client.Repositories.ListCollaborators(ctx, "\n", "\n", opt) 45 return err 46 }) 47 48 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 49 got, resp, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt) 50 if got != nil { 51 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 52 } 53 return resp, err 54 }) 55 } 56 57 func TestRepositoriesService_ListCollaborators_withAffiliation(t *testing.T) { 58 client, mux, _, teardown := setup() 59 defer teardown() 60 61 mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) { 62 testMethod(t, r, "GET") 63 testFormValues(t, r, values{"affiliation": "all", "page": "2"}) 64 fmt.Fprintf(w, `[{"id":1}, {"id":2}]`) 65 }) 66 67 opt := &ListCollaboratorsOptions{ 68 ListOptions: ListOptions{Page: 2}, 69 Affiliation: "all", 70 } 71 ctx := context.Background() 72 users, _, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt) 73 if err != nil { 74 t.Errorf("Repositories.ListCollaborators returned error: %v", err) 75 } 76 77 want := []*User{{ID: Int64(1)}, {ID: Int64(2)}} 78 if !cmp.Equal(users, want) { 79 t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want) 80 } 81 82 const methodName = "ListCollaborators" 83 testBadOptions(t, methodName, func() (err error) { 84 _, _, err = client.Repositories.ListCollaborators(ctx, "\n", "\n", opt) 85 return err 86 }) 87 88 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 89 got, resp, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt) 90 if got != nil { 91 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 92 } 93 return resp, err 94 }) 95 } 96 97 func TestRepositoriesService_ListCollaborators_withPermission(t *testing.T) { 98 client, mux, _, teardown := setup() 99 defer teardown() 100 101 mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) { 102 testMethod(t, r, "GET") 103 testFormValues(t, r, values{"permission": "pull", "page": "2"}) 104 fmt.Fprintf(w, `[{"id":1}, {"id":2}]`) 105 }) 106 107 opt := &ListCollaboratorsOptions{ 108 ListOptions: ListOptions{Page: 2}, 109 Permission: "pull", 110 } 111 ctx := context.Background() 112 users, _, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt) 113 if err != nil { 114 t.Errorf("Repositories.ListCollaborators returned error: %v", err) 115 } 116 117 want := []*User{{ID: Int64(1)}, {ID: Int64(2)}} 118 if !cmp.Equal(users, want) { 119 t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want) 120 } 121 122 const methodName = "ListCollaborators" 123 testBadOptions(t, methodName, func() (err error) { 124 _, _, err = client.Repositories.ListCollaborators(ctx, "\n", "\n", opt) 125 return err 126 }) 127 128 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 129 got, resp, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt) 130 if got != nil { 131 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 132 } 133 return resp, err 134 }) 135 } 136 137 func TestRepositoriesService_ListCollaborators_invalidOwner(t *testing.T) { 138 client, _, _, teardown := setup() 139 defer teardown() 140 141 ctx := context.Background() 142 _, _, err := client.Repositories.ListCollaborators(ctx, "%", "%", nil) 143 testURLParseError(t, err) 144 } 145 146 func TestRepositoriesService_IsCollaborator_True(t *testing.T) { 147 client, mux, _, teardown := setup() 148 defer teardown() 149 150 mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { 151 testMethod(t, r, "GET") 152 w.WriteHeader(http.StatusNoContent) 153 }) 154 155 ctx := context.Background() 156 isCollab, _, err := client.Repositories.IsCollaborator(ctx, "o", "r", "u") 157 if err != nil { 158 t.Errorf("Repositories.IsCollaborator returned error: %v", err) 159 } 160 161 if !isCollab { 162 t.Errorf("Repositories.IsCollaborator returned false, want true") 163 } 164 165 const methodName = "IsCollaborator" 166 testBadOptions(t, methodName, func() (err error) { 167 _, _, err = client.Repositories.IsCollaborator(ctx, "\n", "\n", "\n") 168 return err 169 }) 170 171 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 172 got, resp, err := client.Repositories.IsCollaborator(ctx, "o", "r", "u") 173 if got { 174 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got) 175 } 176 return resp, err 177 }) 178 } 179 180 func TestRepositoriesService_IsCollaborator_False(t *testing.T) { 181 client, mux, _, teardown := setup() 182 defer teardown() 183 184 mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { 185 testMethod(t, r, "GET") 186 w.WriteHeader(http.StatusNotFound) 187 }) 188 189 ctx := context.Background() 190 isCollab, _, err := client.Repositories.IsCollaborator(ctx, "o", "r", "u") 191 if err != nil { 192 t.Errorf("Repositories.IsCollaborator returned error: %v", err) 193 } 194 195 if isCollab { 196 t.Errorf("Repositories.IsCollaborator returned true, want false") 197 } 198 199 const methodName = "IsCollaborator" 200 testBadOptions(t, methodName, func() (err error) { 201 _, _, err = client.Repositories.IsCollaborator(ctx, "\n", "\n", "\n") 202 return err 203 }) 204 205 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 206 got, resp, err := client.Repositories.IsCollaborator(ctx, "o", "r", "u") 207 if got { 208 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got) 209 } 210 return resp, err 211 }) 212 } 213 214 func TestRepositoriesService_IsCollaborator_invalidUser(t *testing.T) { 215 client, _, _, teardown := setup() 216 defer teardown() 217 218 ctx := context.Background() 219 _, _, err := client.Repositories.IsCollaborator(ctx, "%", "%", "%") 220 testURLParseError(t, err) 221 } 222 223 func TestRepositoryService_GetPermissionLevel(t *testing.T) { 224 client, mux, _, teardown := setup() 225 defer teardown() 226 227 mux.HandleFunc("/repos/o/r/collaborators/u/permission", func(w http.ResponseWriter, r *http.Request) { 228 testMethod(t, r, "GET") 229 fmt.Fprintf(w, `{"permission":"admin","user":{"login":"u"}}`) 230 }) 231 232 ctx := context.Background() 233 rpl, _, err := client.Repositories.GetPermissionLevel(ctx, "o", "r", "u") 234 if err != nil { 235 t.Errorf("Repositories.GetPermissionLevel returned error: %v", err) 236 } 237 238 want := &RepositoryPermissionLevel{ 239 Permission: String("admin"), 240 User: &User{ 241 Login: String("u"), 242 }, 243 } 244 245 if !cmp.Equal(rpl, want) { 246 t.Errorf("Repositories.GetPermissionLevel returned %+v, want %+v", rpl, want) 247 } 248 249 const methodName = "GetPermissionLevel" 250 testBadOptions(t, methodName, func() (err error) { 251 _, _, err = client.Repositories.GetPermissionLevel(ctx, "\n", "\n", "\n") 252 return err 253 }) 254 255 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 256 got, resp, err := client.Repositories.GetPermissionLevel(ctx, "o", "r", "u") 257 if got != nil { 258 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 259 } 260 return resp, err 261 }) 262 } 263 264 func TestRepositoriesService_AddCollaborator(t *testing.T) { 265 client, mux, _, teardown := setup() 266 defer teardown() 267 268 opt := &RepositoryAddCollaboratorOptions{Permission: "admin"} 269 mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { 270 v := new(RepositoryAddCollaboratorOptions) 271 json.NewDecoder(r.Body).Decode(v) 272 testMethod(t, r, "PUT") 273 if !cmp.Equal(v, opt) { 274 t.Errorf("Request body = %+v, want %+v", v, opt) 275 } 276 w.WriteHeader(http.StatusOK) 277 w.Write([]byte(`{"permissions": "write","url": "https://api.github.com/user/repository_invitations/1296269","html_url": "https://github.com/octocat/Hello-World/invitations","id":1,"permissions":"write","repository":{"url":"s","name":"r","id":1},"invitee":{"login":"u"},"inviter":{"login":"o"}}`)) 278 }) 279 ctx := context.Background() 280 collaboratorInvitation, _, err := client.Repositories.AddCollaborator(ctx, "o", "r", "u", opt) 281 if err != nil { 282 t.Errorf("Repositories.AddCollaborator returned error: %v", err) 283 } 284 want := &CollaboratorInvitation{ 285 ID: Int64(1), 286 Repo: &Repository{ 287 ID: Int64(1), 288 URL: String("s"), 289 Name: String("r"), 290 }, 291 Invitee: &User{ 292 Login: String("u"), 293 }, 294 Inviter: &User{ 295 Login: String("o"), 296 }, 297 Permissions: String("write"), 298 URL: String("https://api.github.com/user/repository_invitations/1296269"), 299 HTMLURL: String("https://github.com/octocat/Hello-World/invitations"), 300 } 301 302 if !cmp.Equal(collaboratorInvitation, want) { 303 t.Errorf("AddCollaborator returned %+v, want %+v", collaboratorInvitation, want) 304 } 305 306 const methodName = "AddCollaborator" 307 testBadOptions(t, methodName, func() (err error) { 308 _, _, err = client.Repositories.AddCollaborator(ctx, "\n", "\n", "\n", opt) 309 return err 310 }) 311 312 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 313 got, resp, err := client.Repositories.AddCollaborator(ctx, "o", "r", "u", opt) 314 if got != nil { 315 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 316 } 317 return resp, err 318 }) 319 } 320 321 func TestRepositoriesService_AddCollaborator_invalidUser(t *testing.T) { 322 client, _, _, teardown := setup() 323 defer teardown() 324 325 ctx := context.Background() 326 _, _, err := client.Repositories.AddCollaborator(ctx, "%", "%", "%", nil) 327 testURLParseError(t, err) 328 } 329 330 func TestRepositoriesService_RemoveCollaborator(t *testing.T) { 331 client, mux, _, teardown := setup() 332 defer teardown() 333 334 mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { 335 testMethod(t, r, "DELETE") 336 w.WriteHeader(http.StatusNoContent) 337 }) 338 339 ctx := context.Background() 340 _, err := client.Repositories.RemoveCollaborator(ctx, "o", "r", "u") 341 if err != nil { 342 t.Errorf("Repositories.RemoveCollaborator returned error: %v", err) 343 } 344 345 const methodName = "RemoveCollaborator" 346 testBadOptions(t, methodName, func() (err error) { 347 _, err = client.Repositories.RemoveCollaborator(ctx, "\n", "\n", "\n") 348 return err 349 }) 350 351 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 352 return client.Repositories.RemoveCollaborator(ctx, "o", "r", "u") 353 }) 354 } 355 356 func TestRepositoriesService_RemoveCollaborator_invalidUser(t *testing.T) { 357 client, _, _, teardown := setup() 358 defer teardown() 359 360 ctx := context.Background() 361 _, err := client.Repositories.RemoveCollaborator(ctx, "%", "%", "%") 362 testURLParseError(t, err) 363 } 364 365 func TestRepositoryAddCollaboratorOptions_Marshal(t *testing.T) { 366 testJSONMarshal(t, &RepositoryAddCollaboratorOptions{}, "{}") 367 368 r := &RepositoryAddCollaboratorOptions{ 369 Permission: "permission", 370 } 371 372 want := `{ 373 "permission": "permission" 374 }` 375 376 testJSONMarshal(t, r, want) 377 } 378 379 func TestRepositoryPermissionLevel_Marshal(t *testing.T) { 380 testJSONMarshal(t, &RepositoryPermissionLevel{}, "{}") 381 382 r := &RepositoryPermissionLevel{ 383 Permission: String("permission"), 384 User: &User{ 385 Login: String("l"), 386 ID: Int64(1), 387 URL: String("u"), 388 AvatarURL: String("a"), 389 GravatarID: String("g"), 390 Name: String("n"), 391 Company: String("c"), 392 Blog: String("b"), 393 Location: String("l"), 394 Email: String("e"), 395 Hireable: Bool(true), 396 Bio: String("b"), 397 TwitterUsername: String("t"), 398 PublicRepos: Int(1), 399 Followers: Int(1), 400 Following: Int(1), 401 CreatedAt: &Timestamp{referenceTime}, 402 SuspendedAt: &Timestamp{referenceTime}, 403 }, 404 } 405 406 want := `{ 407 "permission": "permission", 408 "user": { 409 "login": "l", 410 "id": 1, 411 "avatar_url": "a", 412 "gravatar_id": "g", 413 "name": "n", 414 "company": "c", 415 "blog": "b", 416 "location": "l", 417 "email": "e", 418 "hireable": true, 419 "bio": "b", 420 "twitter_username": "t", 421 "public_repos": 1, 422 "followers": 1, 423 "following": 1, 424 "created_at": ` + referenceTimeStr + `, 425 "suspended_at": ` + referenceTimeStr + `, 426 "url": "u" 427 } 428 }` 429 430 testJSONMarshal(t, r, want) 431 } 432 433 func TestCollaboratorInvitation_Marshal(t *testing.T) { 434 testJSONMarshal(t, &CollaboratorInvitation{}, "{}") 435 436 r := &CollaboratorInvitation{ 437 ID: Int64(1), 438 Repo: &Repository{ 439 440 ID: Int64(1), 441 URL: String("url"), 442 Name: String("n"), 443 }, 444 Invitee: &User{ 445 Login: String("l"), 446 ID: Int64(1), 447 URL: String("u"), 448 AvatarURL: String("a"), 449 GravatarID: String("g"), 450 Name: String("n"), 451 Company: String("c"), 452 Blog: String("b"), 453 Location: String("l"), 454 Email: String("e"), 455 Hireable: Bool(true), 456 Bio: String("b"), 457 TwitterUsername: String("t"), 458 PublicRepos: Int(1), 459 Followers: Int(1), 460 Following: Int(1), 461 CreatedAt: &Timestamp{referenceTime}, 462 SuspendedAt: &Timestamp{referenceTime}, 463 }, 464 Inviter: &User{ 465 Login: String("l"), 466 ID: Int64(1), 467 URL: String("u"), 468 AvatarURL: String("a"), 469 GravatarID: String("g"), 470 Name: String("n"), 471 Company: String("c"), 472 Blog: String("b"), 473 Location: String("l"), 474 Email: String("e"), 475 Hireable: Bool(true), 476 Bio: String("b"), 477 TwitterUsername: String("t"), 478 PublicRepos: Int(1), 479 Followers: Int(1), 480 Following: Int(1), 481 CreatedAt: &Timestamp{referenceTime}, 482 SuspendedAt: &Timestamp{referenceTime}, 483 }, 484 Permissions: String("per"), 485 CreatedAt: &Timestamp{referenceTime}, 486 URL: String("url"), 487 HTMLURL: String("hurl"), 488 } 489 490 want := `{ 491 "id": 1, 492 "repository": { 493 "id": 1, 494 "name": "n", 495 "url": "url" 496 }, 497 "invitee": { 498 "login": "l", 499 "id": 1, 500 "avatar_url": "a", 501 "gravatar_id": "g", 502 "name": "n", 503 "company": "c", 504 "blog": "b", 505 "location": "l", 506 "email": "e", 507 "hireable": true, 508 "bio": "b", 509 "twitter_username": "t", 510 "public_repos": 1, 511 "followers": 1, 512 "following": 1, 513 "created_at": ` + referenceTimeStr + `, 514 "suspended_at": ` + referenceTimeStr + `, 515 "url": "u" 516 }, 517 "inviter": { 518 "login": "l", 519 "id": 1, 520 "avatar_url": "a", 521 "gravatar_id": "g", 522 "name": "n", 523 "company": "c", 524 "blog": "b", 525 "location": "l", 526 "email": "e", 527 "hireable": true, 528 "bio": "b", 529 "twitter_username": "t", 530 "public_repos": 1, 531 "followers": 1, 532 "following": 1, 533 "created_at": ` + referenceTimeStr + `, 534 "suspended_at": ` + referenceTimeStr + `, 535 "url": "u" 536 }, 537 "permissions": "per", 538 "created_at": ` + referenceTimeStr + `, 539 "url": "url", 540 "html_url": "hurl" 541 }` 542 543 testJSONMarshal(t, r, want) 544 }