github.com/google/go-github/v74@v74.0.0/github/codespaces_secrets_test.go (about) 1 // Copyright 2023 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 "fmt" 11 "net/http" 12 "testing" 13 "time" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestCodespacesService_ListSecrets(t *testing.T) { 19 t.Parallel() 20 type test struct { 21 name string 22 handleFunc func(*http.ServeMux) 23 call func(context.Context, *Client) (*Secrets, *Response, error) 24 badCall func(context.Context, *Client) (*Secrets, *Response, error) 25 methodName string 26 } 27 opts := &ListOptions{Page: 2, PerPage: 2} 28 tests := []*test{ 29 { 30 name: "User", 31 handleFunc: func(mux *http.ServeMux) { 32 mux.HandleFunc("/user/codespaces/secrets", func(w http.ResponseWriter, r *http.Request) { 33 testMethod(t, r, "GET") 34 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 35 fmt.Fprint(w, `{"total_count":4,"secrets":[{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 36 }) 37 }, 38 call: func(ctx context.Context, client *Client) (*Secrets, *Response, error) { 39 return client.Codespaces.ListUserSecrets(ctx, opts) 40 }, 41 methodName: "ListUserSecrets", 42 }, 43 { 44 name: "Org", 45 handleFunc: func(mux *http.ServeMux) { 46 mux.HandleFunc("/orgs/o/codespaces/secrets", func(w http.ResponseWriter, r *http.Request) { 47 testMethod(t, r, "GET") 48 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 49 fmt.Fprint(w, `{"total_count":4,"secrets":[{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 50 }) 51 }, 52 call: func(ctx context.Context, client *Client) (*Secrets, *Response, error) { 53 return client.Codespaces.ListOrgSecrets(ctx, "o", opts) 54 }, 55 badCall: func(ctx context.Context, client *Client) (*Secrets, *Response, error) { 56 return client.Codespaces.ListOrgSecrets(ctx, "\n", opts) 57 }, 58 methodName: "ListOrgSecrets", 59 }, 60 { 61 name: "Repo", 62 handleFunc: func(mux *http.ServeMux) { 63 mux.HandleFunc("/repos/o/r/codespaces/secrets", func(w http.ResponseWriter, r *http.Request) { 64 testMethod(t, r, "GET") 65 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 66 fmt.Fprint(w, `{"total_count":4,"secrets":[{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 67 }) 68 }, 69 call: func(ctx context.Context, client *Client) (*Secrets, *Response, error) { 70 return client.Codespaces.ListRepoSecrets(ctx, "o", "r", opts) 71 }, 72 badCall: func(ctx context.Context, client *Client) (*Secrets, *Response, error) { 73 return client.Codespaces.ListRepoSecrets(ctx, "\n", "\n", opts) 74 }, 75 methodName: "ListRepoSecrets", 76 }, 77 } 78 79 for _, tt := range tests { 80 t.Run(tt.name, func(t *testing.T) { 81 t.Parallel() 82 client, mux, _ := setup(t) 83 84 tt.handleFunc(mux) 85 86 ctx := context.Background() 87 secrets, _, err := tt.call(ctx, client) 88 if err != nil { 89 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 90 } 91 92 want := &Secrets{ 93 TotalCount: 4, 94 Secrets: []*Secret{ 95 {Name: "A", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 96 {Name: "B", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 97 }, 98 } 99 if !cmp.Equal(secrets, want) { 100 t.Errorf("Codespaces.%v returned %+v, want %+v", tt.methodName, secrets, want) 101 } 102 103 if tt.badCall != nil { 104 testBadOptions(t, tt.methodName, func() (err error) { 105 _, _, err = tt.badCall(ctx, client) 106 return err 107 }) 108 } 109 110 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 111 got, resp, err := tt.call(ctx, client) 112 if got != nil { 113 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", tt.methodName, got) 114 } 115 return resp, err 116 }) 117 }) 118 } 119 } 120 121 func TestCodespacesService_GetSecret(t *testing.T) { 122 t.Parallel() 123 type test struct { 124 name string 125 handleFunc func(*http.ServeMux) 126 call func(context.Context, *Client) (*Secret, *Response, error) 127 badCall func(context.Context, *Client) (*Secret, *Response, error) 128 methodName string 129 } 130 tests := []*test{ 131 { 132 name: "User", 133 handleFunc: func(mux *http.ServeMux) { 134 mux.HandleFunc("/user/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { 135 testMethod(t, r, "GET") 136 fmt.Fprint(w, `{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) 137 }) 138 }, 139 call: func(ctx context.Context, client *Client) (*Secret, *Response, error) { 140 return client.Codespaces.GetUserSecret(ctx, "NAME") 141 }, 142 methodName: "GetUserSecret", 143 }, 144 { 145 name: "Org", 146 handleFunc: func(mux *http.ServeMux) { 147 mux.HandleFunc("/orgs/o/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { 148 testMethod(t, r, "GET") 149 fmt.Fprint(w, `{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) 150 }) 151 }, 152 call: func(ctx context.Context, client *Client) (*Secret, *Response, error) { 153 return client.Codespaces.GetOrgSecret(ctx, "o", "NAME") 154 }, 155 badCall: func(ctx context.Context, client *Client) (*Secret, *Response, error) { 156 return client.Codespaces.GetOrgSecret(ctx, "\n", "\n") 157 }, 158 methodName: "GetOrgSecret", 159 }, 160 { 161 name: "Repo", 162 handleFunc: func(mux *http.ServeMux) { 163 mux.HandleFunc("/repos/o/r/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { 164 testMethod(t, r, "GET") 165 fmt.Fprint(w, `{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) 166 }) 167 }, 168 call: func(ctx context.Context, client *Client) (*Secret, *Response, error) { 169 return client.Codespaces.GetRepoSecret(ctx, "o", "r", "NAME") 170 }, 171 badCall: func(ctx context.Context, client *Client) (*Secret, *Response, error) { 172 return client.Codespaces.GetRepoSecret(ctx, "\n", "\n", "\n") 173 }, 174 methodName: "GetRepoSecret", 175 }, 176 } 177 178 for _, tt := range tests { 179 t.Run(tt.name, func(t *testing.T) { 180 t.Parallel() 181 client, mux, _ := setup(t) 182 183 tt.handleFunc(mux) 184 185 ctx := context.Background() 186 secret, _, err := tt.call(ctx, client) 187 if err != nil { 188 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 189 } 190 191 want := &Secret{Name: "A", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}} 192 if !cmp.Equal(secret, want) { 193 t.Errorf("Codespaces.%v returned %+v, want %+v", tt.methodName, secret, want) 194 } 195 196 if tt.badCall != nil { 197 testBadOptions(t, tt.methodName, func() (err error) { 198 _, _, err = tt.badCall(ctx, client) 199 return err 200 }) 201 } 202 203 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 204 got, resp, err := tt.call(ctx, client) 205 if got != nil { 206 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", tt.methodName, got) 207 } 208 return resp, err 209 }) 210 }) 211 } 212 } 213 214 func TestCodespacesService_CreateOrUpdateSecret(t *testing.T) { 215 t.Parallel() 216 type test struct { 217 name string 218 handleFunc func(*http.ServeMux) 219 call func(context.Context, *Client, *EncryptedSecret) (*Response, error) 220 badCall func(context.Context, *Client, *EncryptedSecret) (*Response, error) 221 methodName string 222 } 223 tests := []*test{ 224 { 225 name: "User", 226 handleFunc: func(mux *http.ServeMux) { 227 mux.HandleFunc("/user/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { 228 testMethod(t, r, "PUT") 229 testHeader(t, r, "Content-Type", "application/json") 230 testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") 231 w.WriteHeader(http.StatusCreated) 232 }) 233 }, 234 call: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) { 235 return client.Codespaces.CreateOrUpdateUserSecret(ctx, e) 236 }, 237 methodName: "CreateOrUpdateUserSecret", 238 }, 239 { 240 name: "Org", 241 handleFunc: func(mux *http.ServeMux) { 242 mux.HandleFunc("/orgs/o/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { 243 testMethod(t, r, "PUT") 244 testHeader(t, r, "Content-Type", "application/json") 245 testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") 246 w.WriteHeader(http.StatusCreated) 247 }) 248 }, 249 call: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) { 250 return client.Codespaces.CreateOrUpdateOrgSecret(ctx, "o", e) 251 }, 252 badCall: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) { 253 return client.Codespaces.CreateOrUpdateOrgSecret(ctx, "\n", e) 254 }, 255 methodName: "CreateOrUpdateOrgSecret", 256 }, 257 { 258 name: "Repo", 259 handleFunc: func(mux *http.ServeMux) { 260 mux.HandleFunc("/repos/o/r/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { 261 testMethod(t, r, "PUT") 262 testHeader(t, r, "Content-Type", "application/json") 263 testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") 264 w.WriteHeader(http.StatusCreated) 265 }) 266 }, 267 call: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) { 268 return client.Codespaces.CreateOrUpdateRepoSecret(ctx, "o", "r", e) 269 }, 270 badCall: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) { 271 return client.Codespaces.CreateOrUpdateRepoSecret(ctx, "\n", "\n", e) 272 }, 273 methodName: "CreateOrUpdateRepoSecret", 274 }, 275 } 276 277 for _, tt := range tests { 278 t.Run(tt.name, func(t *testing.T) { 279 t.Parallel() 280 client, mux, _ := setup(t) 281 282 tt.handleFunc(mux) 283 284 input := &EncryptedSecret{ 285 Name: "NAME", 286 EncryptedValue: "QIv=", 287 KeyID: "1234", 288 } 289 ctx := context.Background() 290 _, err := tt.call(ctx, client, input) 291 if err != nil { 292 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 293 } 294 295 if tt.badCall != nil { 296 testBadOptions(t, tt.methodName, func() (err error) { 297 _, err = tt.badCall(ctx, client, input) 298 return err 299 }) 300 } 301 302 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 303 return tt.call(ctx, client, input) 304 }) 305 }) 306 } 307 } 308 309 func TestCodespacesService_DeleteSecret(t *testing.T) { 310 t.Parallel() 311 type test struct { 312 name string 313 handleFunc func(*http.ServeMux) 314 call func(context.Context, *Client) (*Response, error) 315 badCall func(context.Context, *Client) (*Response, error) 316 methodName string 317 } 318 tests := []*test{ 319 { 320 name: "User", 321 handleFunc: func(mux *http.ServeMux) { 322 mux.HandleFunc("/user/codespaces/secrets/NAME", func(_ http.ResponseWriter, r *http.Request) { 323 testMethod(t, r, "DELETE") 324 }) 325 }, 326 call: func(ctx context.Context, client *Client) (*Response, error) { 327 return client.Codespaces.DeleteUserSecret(ctx, "NAME") 328 }, 329 methodName: "DeleteUserSecret", 330 }, 331 { 332 name: "Org", 333 handleFunc: func(mux *http.ServeMux) { 334 mux.HandleFunc("/orgs/o/codespaces/secrets/NAME", func(_ http.ResponseWriter, r *http.Request) { 335 testMethod(t, r, "DELETE") 336 }) 337 }, 338 call: func(ctx context.Context, client *Client) (*Response, error) { 339 return client.Codespaces.DeleteOrgSecret(ctx, "o", "NAME") 340 }, 341 badCall: func(ctx context.Context, client *Client) (*Response, error) { 342 return client.Codespaces.DeleteOrgSecret(ctx, "\n", "\n") 343 }, 344 methodName: "DeleteOrgSecret", 345 }, 346 { 347 name: "Repo", 348 handleFunc: func(mux *http.ServeMux) { 349 mux.HandleFunc("/repos/o/r/codespaces/secrets/NAME", func(_ http.ResponseWriter, r *http.Request) { 350 testMethod(t, r, "DELETE") 351 }) 352 }, 353 call: func(ctx context.Context, client *Client) (*Response, error) { 354 return client.Codespaces.DeleteRepoSecret(ctx, "o", "r", "NAME") 355 }, 356 badCall: func(ctx context.Context, client *Client) (*Response, error) { 357 return client.Codespaces.DeleteRepoSecret(ctx, "\n", "\n", "\n") 358 }, 359 methodName: "DeleteRepoSecret", 360 }, 361 } 362 363 for _, tt := range tests { 364 t.Run(tt.name, func(t *testing.T) { 365 t.Parallel() 366 client, mux, _ := setup(t) 367 368 tt.handleFunc(mux) 369 370 ctx := context.Background() 371 _, err := tt.call(ctx, client) 372 if err != nil { 373 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 374 } 375 376 if tt.badCall != nil { 377 testBadOptions(t, tt.methodName, func() (err error) { 378 _, err = tt.badCall(ctx, client) 379 return err 380 }) 381 } 382 383 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 384 return tt.call(ctx, client) 385 }) 386 }) 387 } 388 } 389 390 func TestCodespacesService_GetPublicKey(t *testing.T) { 391 t.Parallel() 392 type test struct { 393 name string 394 handleFunc func(*http.ServeMux) 395 call func(context.Context, *Client) (*PublicKey, *Response, error) 396 badCall func(context.Context, *Client) (*PublicKey, *Response, error) 397 methodName string 398 } 399 400 tests := []*test{ 401 { 402 name: "User", 403 handleFunc: func(mux *http.ServeMux) { 404 mux.HandleFunc("/user/codespaces/secrets/public-key", func(w http.ResponseWriter, r *http.Request) { 405 testMethod(t, r, "GET") 406 fmt.Fprint(w, `{"key_id":"1234","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`) 407 }) 408 }, 409 call: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) { 410 return client.Codespaces.GetUserPublicKey(ctx) 411 }, 412 methodName: "GetUserPublicKey", 413 }, 414 { 415 name: "Org", 416 handleFunc: func(mux *http.ServeMux) { 417 mux.HandleFunc("/orgs/o/codespaces/secrets/public-key", func(w http.ResponseWriter, r *http.Request) { 418 testMethod(t, r, "GET") 419 fmt.Fprint(w, `{"key_id":"1234","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`) 420 }) 421 }, 422 call: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) { 423 return client.Codespaces.GetOrgPublicKey(ctx, "o") 424 }, 425 badCall: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) { 426 return client.Codespaces.GetOrgPublicKey(ctx, "\n") 427 }, 428 methodName: "GetOrgPublicKey", 429 }, 430 { 431 name: "Repo", 432 handleFunc: func(mux *http.ServeMux) { 433 mux.HandleFunc("/repos/o/r/codespaces/secrets/public-key", func(w http.ResponseWriter, r *http.Request) { 434 testMethod(t, r, "GET") 435 fmt.Fprint(w, `{"key_id":"1234","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`) 436 }) 437 }, 438 call: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) { 439 return client.Codespaces.GetRepoPublicKey(ctx, "o", "r") 440 }, 441 badCall: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) { 442 return client.Codespaces.GetRepoPublicKey(ctx, "\n", "\n") 443 }, 444 methodName: "GetRepoPublicKey", 445 }, 446 } 447 448 for _, tt := range tests { 449 t.Run(tt.name, func(t *testing.T) { 450 t.Parallel() 451 client, mux, _ := setup(t) 452 453 tt.handleFunc(mux) 454 455 ctx := context.Background() 456 key, _, err := tt.call(ctx, client) 457 if err != nil { 458 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 459 } 460 461 want := &PublicKey{KeyID: Ptr("1234"), Key: Ptr("2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234")} 462 if !cmp.Equal(key, want) { 463 t.Errorf("Codespaces.%v returned %+v, want %+v", tt.methodName, key, want) 464 } 465 466 if tt.badCall != nil { 467 testBadOptions(t, tt.methodName, func() (err error) { 468 _, _, err = tt.badCall(ctx, client) 469 return err 470 }) 471 } 472 473 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 474 got, resp, err := tt.call(ctx, client) 475 if got != nil { 476 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", tt.methodName, got) 477 } 478 return resp, err 479 }) 480 }) 481 } 482 } 483 484 func TestCodespacesService_ListSelectedReposForSecret(t *testing.T) { 485 t.Parallel() 486 type test struct { 487 name string 488 handleFunc func(*http.ServeMux) 489 call func(context.Context, *Client) (*SelectedReposList, *Response, error) 490 badCall func(context.Context, *Client) (*SelectedReposList, *Response, error) 491 methodName string 492 } 493 opts := &ListOptions{Page: 2, PerPage: 2} 494 tests := []*test{ 495 { 496 name: "User", 497 handleFunc: func(mux *http.ServeMux) { 498 mux.HandleFunc("/user/codespaces/secrets/NAME/repositories", func(w http.ResponseWriter, r *http.Request) { 499 testMethod(t, r, "GET") 500 fmt.Fprint(w, `{"total_count":1,"repositories":[{"id":1}]}`) 501 }) 502 }, 503 call: func(ctx context.Context, client *Client) (*SelectedReposList, *Response, error) { 504 return client.Codespaces.ListSelectedReposForUserSecret(ctx, "NAME", opts) 505 }, 506 methodName: "ListSelectedReposForUserSecret", 507 }, 508 { 509 name: "Org", 510 handleFunc: func(mux *http.ServeMux) { 511 mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories", func(w http.ResponseWriter, r *http.Request) { 512 testMethod(t, r, "GET") 513 fmt.Fprint(w, `{"total_count":1,"repositories":[{"id":1}]}`) 514 }) 515 }, 516 call: func(ctx context.Context, client *Client) (*SelectedReposList, *Response, error) { 517 return client.Codespaces.ListSelectedReposForOrgSecret(ctx, "o", "NAME", opts) 518 }, 519 badCall: func(ctx context.Context, client *Client) (*SelectedReposList, *Response, error) { 520 return client.Codespaces.ListSelectedReposForOrgSecret(ctx, "\n", "\n", opts) 521 }, 522 methodName: "ListSelectedReposForOrgSecret", 523 }, 524 } 525 526 for _, tt := range tests { 527 t.Run(tt.name, func(t *testing.T) { 528 t.Parallel() 529 client, mux, _ := setup(t) 530 531 tt.handleFunc(mux) 532 533 ctx := context.Background() 534 repos, _, err := tt.call(ctx, client) 535 if err != nil { 536 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 537 } 538 539 want := &SelectedReposList{ 540 TotalCount: Ptr(1), 541 Repositories: []*Repository{ 542 {ID: Ptr(int64(1))}, 543 }, 544 } 545 546 if !cmp.Equal(repos, want) { 547 t.Errorf("Codespaces.%v returned %+v, want %+v", tt.methodName, repos, want) 548 } 549 550 if tt.badCall != nil { 551 testBadOptions(t, tt.methodName, func() (err error) { 552 _, _, err = tt.badCall(ctx, client) 553 return err 554 }) 555 } 556 557 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 558 got, resp, err := tt.call(ctx, client) 559 if got != nil { 560 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", tt.methodName, got) 561 } 562 return resp, err 563 }) 564 }) 565 } 566 } 567 568 func TestCodespacesService_SetSelectedReposForSecret(t *testing.T) { 569 t.Parallel() 570 type test struct { 571 name string 572 handleFunc func(*http.ServeMux) 573 call func(context.Context, *Client) (*Response, error) 574 badCall func(context.Context, *Client) (*Response, error) 575 methodName string 576 } 577 ids := SelectedRepoIDs{64780797} 578 tests := []*test{ 579 { 580 name: "User", 581 handleFunc: func(mux *http.ServeMux) { 582 mux.HandleFunc("/user/codespaces/secrets/NAME/repositories", func(_ http.ResponseWriter, r *http.Request) { 583 testMethod(t, r, "PUT") 584 testHeader(t, r, "Content-Type", "application/json") 585 testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") 586 }) 587 }, 588 call: func(ctx context.Context, client *Client) (*Response, error) { 589 return client.Codespaces.SetSelectedReposForUserSecret(ctx, "NAME", ids) 590 }, 591 methodName: "SetSelectedReposForUserSecret", 592 }, 593 { 594 name: "Org", 595 handleFunc: func(mux *http.ServeMux) { 596 mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories", func(_ http.ResponseWriter, r *http.Request) { 597 testMethod(t, r, "PUT") 598 testHeader(t, r, "Content-Type", "application/json") 599 testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") 600 }) 601 }, 602 call: func(ctx context.Context, client *Client) (*Response, error) { 603 return client.Codespaces.SetSelectedReposForOrgSecret(ctx, "o", "NAME", ids) 604 }, 605 badCall: func(ctx context.Context, client *Client) (*Response, error) { 606 return client.Codespaces.SetSelectedReposForOrgSecret(ctx, "\n", "\n", ids) 607 }, 608 methodName: "SetSelectedReposForOrgSecret", 609 }, 610 } 611 612 for _, tt := range tests { 613 t.Run(tt.name, func(t *testing.T) { 614 t.Parallel() 615 client, mux, _ := setup(t) 616 617 tt.handleFunc(mux) 618 619 ctx := context.Background() 620 _, err := tt.call(ctx, client) 621 if err != nil { 622 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 623 } 624 625 if tt.badCall != nil { 626 testBadOptions(t, tt.methodName, func() (err error) { 627 _, err = tt.badCall(ctx, client) 628 return err 629 }) 630 } 631 632 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 633 return tt.call(ctx, client) 634 }) 635 }) 636 } 637 } 638 639 func TestCodespacesService_AddSelectedReposForSecret(t *testing.T) { 640 t.Parallel() 641 type test struct { 642 name string 643 handleFunc func(*http.ServeMux) 644 call func(context.Context, *Client) (*Response, error) 645 badCall func(context.Context, *Client) (*Response, error) 646 methodName string 647 } 648 repo := &Repository{ID: Ptr(int64(1234))} 649 tests := []*test{ 650 { 651 name: "User", 652 handleFunc: func(mux *http.ServeMux) { 653 mux.HandleFunc("/user/codespaces/secrets/NAME/repositories/1234", func(_ http.ResponseWriter, r *http.Request) { 654 testMethod(t, r, "PUT") 655 }) 656 }, 657 call: func(ctx context.Context, client *Client) (*Response, error) { 658 return client.Codespaces.AddSelectedRepoToUserSecret(ctx, "NAME", repo) 659 }, 660 methodName: "AddSelectedRepoToUserSecret", 661 }, 662 { 663 name: "Org", 664 handleFunc: func(mux *http.ServeMux) { 665 mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories/1234", func(_ http.ResponseWriter, r *http.Request) { 666 testMethod(t, r, "PUT") 667 }) 668 }, 669 call: func(ctx context.Context, client *Client) (*Response, error) { 670 return client.Codespaces.AddSelectedRepoToOrgSecret(ctx, "o", "NAME", repo) 671 }, 672 badCall: func(ctx context.Context, client *Client) (*Response, error) { 673 return client.Codespaces.AddSelectedRepoToOrgSecret(ctx, "\n", "\n", repo) 674 }, 675 methodName: "AddSelectedRepoToOrgSecret", 676 }, 677 } 678 679 for _, tt := range tests { 680 t.Run(tt.name, func(t *testing.T) { 681 t.Parallel() 682 client, mux, _ := setup(t) 683 684 tt.handleFunc(mux) 685 686 ctx := context.Background() 687 _, err := tt.call(ctx, client) 688 if err != nil { 689 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 690 } 691 692 if tt.badCall != nil { 693 testBadOptions(t, tt.methodName, func() (err error) { 694 _, err = tt.badCall(ctx, client) 695 return err 696 }) 697 } 698 699 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 700 return tt.call(ctx, client) 701 }) 702 }) 703 } 704 } 705 706 func TestCodespacesService_RemoveSelectedReposFromSecret(t *testing.T) { 707 t.Parallel() 708 type test struct { 709 name string 710 handleFunc func(*http.ServeMux) 711 call func(context.Context, *Client) (*Response, error) 712 badCall func(context.Context, *Client) (*Response, error) 713 methodName string 714 } 715 repo := &Repository{ID: Ptr(int64(1234))} 716 tests := []*test{ 717 { 718 name: "User", 719 handleFunc: func(mux *http.ServeMux) { 720 mux.HandleFunc("/user/codespaces/secrets/NAME/repositories/1234", func(_ http.ResponseWriter, r *http.Request) { 721 testMethod(t, r, "DELETE") 722 }) 723 }, 724 call: func(ctx context.Context, client *Client) (*Response, error) { 725 return client.Codespaces.RemoveSelectedRepoFromUserSecret(ctx, "NAME", repo) 726 }, 727 methodName: "RemoveSelectedRepoFromUserSecret", 728 }, 729 { 730 name: "Org", 731 handleFunc: func(mux *http.ServeMux) { 732 mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories/1234", func(_ http.ResponseWriter, r *http.Request) { 733 testMethod(t, r, "DELETE") 734 }) 735 }, 736 call: func(ctx context.Context, client *Client) (*Response, error) { 737 return client.Codespaces.RemoveSelectedRepoFromOrgSecret(ctx, "o", "NAME", repo) 738 }, 739 badCall: func(ctx context.Context, client *Client) (*Response, error) { 740 return client.Codespaces.RemoveSelectedRepoFromOrgSecret(ctx, "\n", "\n", repo) 741 }, 742 methodName: "RemoveSelectedRepoFromOrgSecret", 743 }, 744 } 745 746 for _, tt := range tests { 747 t.Run(tt.name, func(t *testing.T) { 748 t.Parallel() 749 client, mux, _ := setup(t) 750 751 tt.handleFunc(mux) 752 753 ctx := context.Background() 754 _, err := tt.call(ctx, client) 755 if err != nil { 756 t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err) 757 } 758 759 if tt.badCall != nil { 760 testBadOptions(t, tt.methodName, func() (err error) { 761 _, err = tt.badCall(ctx, client) 762 return err 763 }) 764 } 765 766 testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) { 767 return tt.call(ctx, client) 768 }) 769 }) 770 } 771 }