github.com/google/go-github/v50@v50.2.0/github/actions_variables_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 TestActionsService_ListRepoVariables(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/o/r/actions/variables", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 25 fmt.Fprint(w, `{"total_count":4,"variables":[{"name":"A","value":"AA","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","value":"BB","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 26 }) 27 28 opts := &ListOptions{Page: 2, PerPage: 2} 29 ctx := context.Background() 30 variables, _, err := client.Actions.ListRepoVariables(ctx, "o", "r", opts) 31 if err != nil { 32 t.Errorf("Actions.ListRepoVariables returned error: %v", err) 33 } 34 35 want := &ActionsVariables{ 36 TotalCount: 4, 37 Variables: []*ActionsVariable{ 38 {Name: "A", Value: "AA", 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)}}, 39 {Name: "B", Value: "BB", 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)}}, 40 }, 41 } 42 if !cmp.Equal(variables, want) { 43 t.Errorf("Actions.ListRepoVariables returned %+v, want %+v", variables, want) 44 } 45 46 const methodName = "ListRepoVariables" 47 testBadOptions(t, methodName, func() (err error) { 48 _, _, err = client.Actions.ListRepoVariables(ctx, "\n", "\n", opts) 49 return err 50 }) 51 52 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 53 got, resp, err := client.Actions.ListRepoVariables(ctx, "o", "r", opts) 54 if got != nil { 55 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 56 } 57 return resp, err 58 }) 59 } 60 61 func TestActionsService_GetRepoVariable(t *testing.T) { 62 client, mux, _, teardown := setup() 63 defer teardown() 64 65 mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 66 testMethod(t, r, "GET") 67 fmt.Fprint(w, `{"name":"NAME","value":"VALUE","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) 68 }) 69 70 ctx := context.Background() 71 variable, _, err := client.Actions.GetRepoVariable(ctx, "o", "r", "NAME") 72 if err != nil { 73 t.Errorf("Actions.GetRepoVariable returned error: %v", err) 74 } 75 76 want := &ActionsVariable{ 77 Name: "NAME", 78 Value: "VALUE", 79 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 80 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 81 } 82 if !cmp.Equal(variable, want) { 83 t.Errorf("Actions.GetRepoVariable returned %+v, want %+v", variable, want) 84 } 85 86 const methodName = "GetRepoVariable" 87 testBadOptions(t, methodName, func() (err error) { 88 _, _, err = client.Actions.GetRepoVariable(ctx, "\n", "\n", "\n") 89 return err 90 }) 91 92 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 93 got, resp, err := client.Actions.GetRepoVariable(ctx, "o", "r", "NAME") 94 if got != nil { 95 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 96 } 97 return resp, err 98 }) 99 } 100 101 func TestActionsService_CreateRepoVariable(t *testing.T) { 102 client, mux, _, teardown := setup() 103 defer teardown() 104 105 mux.HandleFunc("/repos/o/r/actions/variables", func(w http.ResponseWriter, r *http.Request) { 106 testMethod(t, r, "POST") 107 testHeader(t, r, "Content-Type", "application/json") 108 testBody(t, r, `{"name":"NAME","value":"VALUE"}`+"\n") 109 w.WriteHeader(http.StatusCreated) 110 }) 111 112 input := &ActionsVariable{ 113 Name: "NAME", 114 Value: "VALUE", 115 } 116 ctx := context.Background() 117 _, err := client.Actions.CreateRepoVariable(ctx, "o", "r", input) 118 if err != nil { 119 t.Errorf("Actions.CreateRepoVariable returned error: %v", err) 120 } 121 122 const methodName = "CreateRepoVariable" 123 testBadOptions(t, methodName, func() (err error) { 124 _, err = client.Actions.CreateRepoVariable(ctx, "\n", "\n", input) 125 return err 126 }) 127 128 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 129 return client.Actions.CreateRepoVariable(ctx, "o", "r", input) 130 }) 131 } 132 133 func TestActionsService_UpdateRepoVariable(t *testing.T) { 134 client, mux, _, teardown := setup() 135 defer teardown() 136 137 mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 138 testMethod(t, r, "PATCH") 139 testHeader(t, r, "Content-Type", "application/json") 140 testBody(t, r, `{"name":"NAME","value":"VALUE"}`+"\n") 141 w.WriteHeader(http.StatusNoContent) 142 }) 143 144 input := &ActionsVariable{ 145 Name: "NAME", 146 Value: "VALUE", 147 } 148 ctx := context.Background() 149 _, err := client.Actions.UpdateRepoVariable(ctx, "o", "r", input) 150 if err != nil { 151 t.Errorf("Actions.UpdateRepoVariable returned error: %v", err) 152 } 153 154 const methodName = "UpdateRepoVariable" 155 testBadOptions(t, methodName, func() (err error) { 156 _, err = client.Actions.UpdateRepoVariable(ctx, "\n", "\n", input) 157 return err 158 }) 159 160 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 161 return client.Actions.UpdateRepoVariable(ctx, "o", "r", input) 162 }) 163 } 164 165 func TestActionsService_DeleteRepoVariable(t *testing.T) { 166 client, mux, _, teardown := setup() 167 defer teardown() 168 169 mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 170 testMethod(t, r, "DELETE") 171 }) 172 173 ctx := context.Background() 174 _, err := client.Actions.DeleteRepoVariable(ctx, "o", "r", "NAME") 175 if err != nil { 176 t.Errorf("Actions.( returned error: %v", err) 177 } 178 179 const methodName = "DeleteRepoVariable" 180 testBadOptions(t, methodName, func() (err error) { 181 _, err = client.Actions.DeleteRepoVariable(ctx, "\n", "\n", "\n") 182 return err 183 }) 184 185 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 186 return client.Actions.DeleteRepoVariable(ctx, "o", "r", "NAME") 187 }) 188 } 189 190 func TestActionsService_ListOrgVariables(t *testing.T) { 191 client, mux, _, teardown := setup() 192 defer teardown() 193 194 mux.HandleFunc("/orgs/o/actions/variables", func(w http.ResponseWriter, r *http.Request) { 195 testMethod(t, r, "GET") 196 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 197 fmt.Fprint(w, `{"total_count":3,"variables":[{"name":"A","value":"AA","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"private"},{"name":"B","value":"BB","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"all"},{"name":"C","value":"CC","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"selected","selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}]}`) 198 }) 199 200 opts := &ListOptions{Page: 2, PerPage: 2} 201 ctx := context.Background() 202 variables, _, err := client.Actions.ListOrgVariables(ctx, "o", opts) 203 if err != nil { 204 t.Errorf("Actions.ListOrgVariables returned error: %v", err) 205 } 206 207 want := &ActionsVariables{ 208 TotalCount: 3, 209 Variables: []*ActionsVariable{ 210 {Name: "A", Value: "AA", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("private")}, 211 {Name: "B", Value: "BB", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("all")}, 212 {Name: "C", Value: "CC", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("selected"), SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories")}, 213 }, 214 } 215 if !cmp.Equal(variables, want) { 216 t.Errorf("Actions.ListOrgVariables returned %+v, want %+v", variables, want) 217 } 218 219 const methodName = "ListOrgVariables" 220 testBadOptions(t, methodName, func() (err error) { 221 _, _, err = client.Actions.ListOrgVariables(ctx, "\n", opts) 222 return err 223 }) 224 225 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 226 got, resp, err := client.Actions.ListOrgVariables(ctx, "o", opts) 227 if got != nil { 228 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 229 } 230 return resp, err 231 }) 232 } 233 234 func TestActionsService_GetOrgVariable(t *testing.T) { 235 client, mux, _, teardown := setup() 236 defer teardown() 237 238 mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 239 testMethod(t, r, "GET") 240 fmt.Fprint(w, `{"name":"NAME","value":"VALUE","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z","visibility":"selected","selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}`) 241 }) 242 243 ctx := context.Background() 244 variable, _, err := client.Actions.GetOrgVariable(ctx, "o", "NAME") 245 if err != nil { 246 t.Errorf("Actions.GetOrgVariable returned error: %v", err) 247 } 248 249 want := &ActionsVariable{ 250 Name: "NAME", 251 Value: "VALUE", 252 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 253 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 254 Visibility: String("selected"), 255 SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"), 256 } 257 if !cmp.Equal(variable, want) { 258 t.Errorf("Actions.GetOrgVariable returned %+v, want %+v", variable, want) 259 } 260 261 const methodName = "GetOrgVariable" 262 testBadOptions(t, methodName, func() (err error) { 263 _, _, err = client.Actions.GetOrgVariable(ctx, "\n", "\n") 264 return err 265 }) 266 267 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 268 got, resp, err := client.Actions.GetOrgVariable(ctx, "o", "NAME") 269 if got != nil { 270 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 271 } 272 return resp, err 273 }) 274 } 275 276 func TestActionsService_CreateOrgVariable(t *testing.T) { 277 client, mux, _, teardown := setup() 278 defer teardown() 279 280 mux.HandleFunc("/orgs/o/actions/variables", func(w http.ResponseWriter, r *http.Request) { 281 testMethod(t, r, "POST") 282 testHeader(t, r, "Content-Type", "application/json") 283 testBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") 284 w.WriteHeader(http.StatusCreated) 285 }) 286 287 input := &ActionsVariable{ 288 Name: "NAME", 289 Value: "VALUE", 290 Visibility: String("selected"), 291 SelectedRepositoryIDs: &SelectedRepoIDs{1296269, 1269280}, 292 } 293 ctx := context.Background() 294 _, err := client.Actions.CreateOrgVariable(ctx, "o", input) 295 if err != nil { 296 t.Errorf("Actions.CreateOrgVariable returned error: %v", err) 297 } 298 299 const methodName = "CreateOrgVariable" 300 testBadOptions(t, methodName, func() (err error) { 301 _, err = client.Actions.CreateOrgVariable(ctx, "\n", input) 302 return err 303 }) 304 305 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 306 return client.Actions.CreateOrgVariable(ctx, "o", input) 307 }) 308 } 309 310 func TestActionsService_UpdateOrgVariable(t *testing.T) { 311 client, mux, _, teardown := setup() 312 defer teardown() 313 314 mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 315 testMethod(t, r, "PATCH") 316 testHeader(t, r, "Content-Type", "application/json") 317 testBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") 318 w.WriteHeader(http.StatusNoContent) 319 }) 320 321 input := &ActionsVariable{ 322 Name: "NAME", 323 Value: "VALUE", 324 Visibility: String("selected"), 325 SelectedRepositoryIDs: &SelectedRepoIDs{1296269, 1269280}, 326 } 327 ctx := context.Background() 328 _, err := client.Actions.UpdateOrgVariable(ctx, "o", input) 329 if err != nil { 330 t.Errorf("Actions.UpdateOrgVariable returned error: %v", err) 331 } 332 333 const methodName = "UpdateOrgVariable" 334 testBadOptions(t, methodName, func() (err error) { 335 _, err = client.Actions.UpdateOrgVariable(ctx, "\n", input) 336 return err 337 }) 338 339 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 340 return client.Actions.UpdateOrgVariable(ctx, "o", input) 341 }) 342 } 343 344 func TestActionsService_ListSelectedReposForOrgVariable(t *testing.T) { 345 client, mux, _, teardown := setup() 346 defer teardown() 347 348 mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories", func(w http.ResponseWriter, r *http.Request) { 349 testMethod(t, r, "GET") 350 fmt.Fprintf(w, `{"total_count":1,"repositories":[{"id":1}]}`) 351 }) 352 353 opts := &ListOptions{Page: 2, PerPage: 2} 354 ctx := context.Background() 355 repos, _, err := client.Actions.ListSelectedReposForOrgVariable(ctx, "o", "NAME", opts) 356 if err != nil { 357 t.Errorf("Actions.( returned error: %v", err) 358 } 359 360 want := &SelectedReposList{ 361 TotalCount: Int(1), 362 Repositories: []*Repository{ 363 {ID: Int64(1)}, 364 }, 365 } 366 if !cmp.Equal(repos, want) { 367 t.Errorf("Actions.( returned %+v, want %+v", repos, want) 368 } 369 370 const methodName = "ListSelectedReposForOrgVariable" 371 testBadOptions(t, methodName, func() (err error) { 372 _, _, err = client.Actions.ListSelectedReposForOrgVariable(ctx, "\n", "\n", opts) 373 return err 374 }) 375 376 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 377 got, resp, err := client.Actions.ListSelectedReposForOrgVariable(ctx, "o", "NAME", opts) 378 if got != nil { 379 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 380 } 381 return resp, err 382 }) 383 } 384 385 func TestActionsService_SetSelectedReposForOrgSVariable(t *testing.T) { 386 client, mux, _, teardown := setup() 387 defer teardown() 388 389 mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories", func(w http.ResponseWriter, r *http.Request) { 390 testMethod(t, r, "PUT") 391 testHeader(t, r, "Content-Type", "application/json") 392 testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") 393 }) 394 395 ctx := context.Background() 396 _, err := client.Actions.SetSelectedReposForOrgVariable(ctx, "o", "NAME", SelectedRepoIDs{64780797}) 397 if err != nil { 398 t.Errorf("Actions.( returned error: %v", err) 399 } 400 401 const methodName = "SetSelectedReposForOrgVariable" 402 testBadOptions(t, methodName, func() (err error) { 403 _, err = client.Actions.SetSelectedReposForOrgVariable(ctx, "\n", "\n", SelectedRepoIDs{64780797}) 404 return err 405 }) 406 407 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 408 return client.Actions.SetSelectedReposForOrgVariable(ctx, "o", "NAME", SelectedRepoIDs{64780797}) 409 }) 410 } 411 412 func TestActionsService_AddSelectedRepoToOrgVariable(t *testing.T) { 413 client, mux, _, teardown := setup() 414 defer teardown() 415 416 mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) { 417 testMethod(t, r, "PUT") 418 }) 419 420 repo := &Repository{ID: Int64(1234)} 421 ctx := context.Background() 422 _, err := client.Actions.AddSelectedRepoToOrgVariable(ctx, "o", "NAME", repo) 423 if err != nil { 424 t.Errorf("Actions.AddSelectedRepoToOrgVariable returned error: %v", err) 425 } 426 427 const methodName = "AddSelectedRepoToOrgVariable" 428 testBadOptions(t, methodName, func() (err error) { 429 _, err = client.Actions.AddSelectedRepoToOrgVariable(ctx, "\n", "\n", repo) 430 return err 431 }) 432 433 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 434 return client.Actions.AddSelectedRepoToOrgVariable(ctx, "o", "NAME", repo) 435 }) 436 } 437 438 func TestActionsService_RemoveSelectedRepoFromOrgVariable(t *testing.T) { 439 client, mux, _, teardown := setup() 440 defer teardown() 441 442 mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) { 443 testMethod(t, r, "DELETE") 444 }) 445 446 repo := &Repository{ID: Int64(1234)} 447 ctx := context.Background() 448 _, err := client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "o", "NAME", repo) 449 if err != nil { 450 t.Errorf("Actions.RemoveSelectedRepoFromOrgVariable returned error: %v", err) 451 } 452 453 const methodName = "RemoveSelectedRepoFromOrgVariable" 454 testBadOptions(t, methodName, func() (err error) { 455 _, err = client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "\n", "\n", repo) 456 return err 457 }) 458 459 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 460 return client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "o", "NAME", repo) 461 }) 462 } 463 464 func TestActionsService_DeleteOrgVariable(t *testing.T) { 465 client, mux, _, teardown := setup() 466 defer teardown() 467 468 mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 469 testMethod(t, r, "DELETE") 470 }) 471 472 ctx := context.Background() 473 _, err := client.Actions.DeleteOrgVariable(ctx, "o", "NAME") 474 if err != nil { 475 t.Errorf("Actions.DeleteOrgVariable returned error: %v", err) 476 } 477 478 const methodName = "DeleteOrgVariable" 479 testBadOptions(t, methodName, func() (err error) { 480 _, err = client.Actions.DeleteOrgVariable(ctx, "\n", "\n") 481 return err 482 }) 483 484 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 485 return client.Actions.DeleteOrgVariable(ctx, "o", "NAME") 486 }) 487 } 488 489 func TestActionsService_ListEnvVariables(t *testing.T) { 490 client, mux, _, teardown := setup() 491 defer teardown() 492 493 mux.HandleFunc("/repositories/1/environments/e/variables", func(w http.ResponseWriter, r *http.Request) { 494 testMethod(t, r, "GET") 495 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 496 fmt.Fprint(w, `{"total_count":4,"variables":[{"name":"A","value":"AA","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","value":"BB","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 497 }) 498 499 opts := &ListOptions{Page: 2, PerPage: 2} 500 ctx := context.Background() 501 variables, _, err := client.Actions.ListEnvVariables(ctx, 1, "e", opts) 502 if err != nil { 503 t.Errorf("Actions.ListEnvVariables returned error: %v", err) 504 } 505 506 want := &ActionsVariables{ 507 TotalCount: 4, 508 Variables: []*ActionsVariable{ 509 {Name: "A", Value: "AA", 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)}}, 510 {Name: "B", Value: "BB", 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)}}, 511 }, 512 } 513 if !cmp.Equal(variables, want) { 514 t.Errorf("Actions.ListEnvVariables returned %+v, want %+v", variables, want) 515 } 516 517 const methodName = "ListEnvVariables" 518 testBadOptions(t, methodName, func() (err error) { 519 _, _, err = client.Actions.ListEnvVariables(ctx, 0.0, "\n", opts) 520 return err 521 }) 522 523 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 524 got, resp, err := client.Actions.ListEnvVariables(ctx, 1, "e", opts) 525 if got != nil { 526 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 527 } 528 return resp, err 529 }) 530 } 531 532 func TestActionsService_GetEnvVariable(t *testing.T) { 533 client, mux, _, teardown := setup() 534 defer teardown() 535 536 mux.HandleFunc("/repositories/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { 537 testMethod(t, r, "GET") 538 fmt.Fprint(w, `{"name":"variable","value":"VAR","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) 539 }) 540 541 ctx := context.Background() 542 variable, _, err := client.Actions.GetEnvVariable(ctx, 1, "e", "variable") 543 if err != nil { 544 t.Errorf("Actions.GetEnvVariable returned error: %v", err) 545 } 546 547 want := &ActionsVariable{ 548 Name: "variable", 549 Value: "VAR", 550 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 551 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 552 } 553 if !cmp.Equal(variable, want) { 554 t.Errorf("Actions.GetEnvVariable returned %+v, want %+v", variable, want) 555 } 556 557 const methodName = "GetEnvVariable" 558 testBadOptions(t, methodName, func() (err error) { 559 _, _, err = client.Actions.GetEnvVariable(ctx, 0.0, "\n", "\n") 560 return err 561 }) 562 563 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 564 got, resp, err := client.Actions.GetEnvVariable(ctx, 1, "e", "variable") 565 if got != nil { 566 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 567 } 568 return resp, err 569 }) 570 } 571 572 func TestActionsService_CreateEnvVariable(t *testing.T) { 573 client, mux, _, teardown := setup() 574 defer teardown() 575 576 mux.HandleFunc("/repositories/1/environments/e/variables", func(w http.ResponseWriter, r *http.Request) { 577 testMethod(t, r, "POST") 578 testHeader(t, r, "Content-Type", "application/json") 579 testBody(t, r, `{"name":"variable","value":"VAR"}`+"\n") 580 w.WriteHeader(http.StatusCreated) 581 }) 582 583 input := &ActionsVariable{ 584 Name: "variable", 585 Value: "VAR", 586 } 587 ctx := context.Background() 588 _, err := client.Actions.CreateEnvVariable(ctx, 1, "e", input) 589 if err != nil { 590 t.Errorf("Actions.CreateEnvVariable returned error: %v", err) 591 } 592 593 const methodName = "CreateEnvVariable" 594 testBadOptions(t, methodName, func() (err error) { 595 _, err = client.Actions.CreateEnvVariable(ctx, 0.0, "\n", input) 596 return err 597 }) 598 599 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 600 return client.Actions.CreateEnvVariable(ctx, 1, "e", input) 601 }) 602 } 603 604 func TestActionsService_UpdateEnvVariable(t *testing.T) { 605 client, mux, _, teardown := setup() 606 defer teardown() 607 608 mux.HandleFunc("/repositories/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { 609 testMethod(t, r, "PATCH") 610 testHeader(t, r, "Content-Type", "application/json") 611 testBody(t, r, `{"name":"variable","value":"VAR"}`+"\n") 612 w.WriteHeader(http.StatusNoContent) 613 }) 614 615 input := &ActionsVariable{ 616 Name: "variable", 617 Value: "VAR", 618 } 619 ctx := context.Background() 620 _, err := client.Actions.UpdateEnvVariable(ctx, 1, "e", input) 621 if err != nil { 622 t.Errorf("Actions.UpdateEnvVariable returned error: %v", err) 623 } 624 625 const methodName = "UpdateEnvVariable" 626 testBadOptions(t, methodName, func() (err error) { 627 _, err = client.Actions.UpdateEnvVariable(ctx, 0.0, "\n", input) 628 return err 629 }) 630 631 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 632 return client.Actions.UpdateEnvVariable(ctx, 1, "e", input) 633 }) 634 } 635 636 func TestActionsService_DeleteEnvVariable(t *testing.T) { 637 client, mux, _, teardown := setup() 638 defer teardown() 639 640 mux.HandleFunc("/repositories/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { 641 testMethod(t, r, "DELETE") 642 }) 643 644 ctx := context.Background() 645 _, err := client.Actions.DeleteEnvVariable(ctx, 1, "e", "variable") 646 if err != nil { 647 t.Errorf("Actions.DeleteEnvVariable returned error: %v", err) 648 } 649 650 const methodName = "DeleteEnvVariable" 651 testBadOptions(t, methodName, func() (err error) { 652 _, err = client.Actions.DeleteEnvVariable(ctx, 0.0, "\n", "\n") 653 return err 654 }) 655 656 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 657 return client.Actions.DeleteEnvVariable(ctx, 1, "r", "variable") 658 }) 659 }