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