github.com/google/go-github/v66@v66.0.0/github/repos_deployments_test.go (about) 1 // Copyright 2014 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 "strings" 14 "testing" 15 16 "github.com/google/go-cmp/cmp" 17 ) 18 19 func TestRepositoriesService_ListDeployments(t *testing.T) { 20 t.Parallel() 21 client, mux, _ := setup(t) 22 23 mux.HandleFunc("/repos/o/r/deployments", func(w http.ResponseWriter, r *http.Request) { 24 testMethod(t, r, "GET") 25 testFormValues(t, r, values{"environment": "test"}) 26 fmt.Fprint(w, `[{"id":1}, {"id":2}]`) 27 }) 28 29 opt := &DeploymentsListOptions{Environment: "test"} 30 ctx := context.Background() 31 deployments, _, err := client.Repositories.ListDeployments(ctx, "o", "r", opt) 32 if err != nil { 33 t.Errorf("Repositories.ListDeployments returned error: %v", err) 34 } 35 36 want := []*Deployment{{ID: Int64(1)}, {ID: Int64(2)}} 37 if !cmp.Equal(deployments, want) { 38 t.Errorf("Repositories.ListDeployments returned %+v, want %+v", deployments, want) 39 } 40 41 const methodName = "ListDeployments" 42 testBadOptions(t, methodName, func() (err error) { 43 _, _, err = client.Repositories.ListDeployments(ctx, "\n", "\n", opt) 44 return err 45 }) 46 47 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 48 got, resp, err := client.Repositories.ListDeployments(ctx, "o", "r", opt) 49 if got != nil { 50 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 51 } 52 return resp, err 53 }) 54 } 55 56 func TestRepositoriesService_GetDeployment(t *testing.T) { 57 t.Parallel() 58 client, mux, _ := setup(t) 59 60 mux.HandleFunc("/repos/o/r/deployments/3", func(w http.ResponseWriter, r *http.Request) { 61 testMethod(t, r, "GET") 62 fmt.Fprint(w, `{"id":3}`) 63 }) 64 65 ctx := context.Background() 66 deployment, _, err := client.Repositories.GetDeployment(ctx, "o", "r", 3) 67 if err != nil { 68 t.Errorf("Repositories.GetDeployment returned error: %v", err) 69 } 70 71 want := &Deployment{ID: Int64(3)} 72 73 if !cmp.Equal(deployment, want) { 74 t.Errorf("Repositories.GetDeployment returned %+v, want %+v", deployment, want) 75 } 76 77 const methodName = "GetDeployment" 78 testBadOptions(t, methodName, func() (err error) { 79 _, _, err = client.Repositories.GetDeployment(ctx, "\n", "\n", 3) 80 return err 81 }) 82 83 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 84 got, resp, err := client.Repositories.GetDeployment(ctx, "o", "r", 3) 85 if got != nil { 86 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 87 } 88 return resp, err 89 }) 90 } 91 92 func TestRepositoriesService_CreateDeployment(t *testing.T) { 93 t.Parallel() 94 client, mux, _ := setup(t) 95 96 input := &DeploymentRequest{Ref: String("1111"), Task: String("deploy"), TransientEnvironment: Bool(true)} 97 98 mux.HandleFunc("/repos/o/r/deployments", func(w http.ResponseWriter, r *http.Request) { 99 v := new(DeploymentRequest) 100 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 101 102 testMethod(t, r, "POST") 103 wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview} 104 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 105 if !cmp.Equal(v, input) { 106 t.Errorf("Request body = %+v, want %+v", v, input) 107 } 108 109 fmt.Fprint(w, `{"ref": "1111", "task": "deploy"}`) 110 }) 111 112 ctx := context.Background() 113 deployment, _, err := client.Repositories.CreateDeployment(ctx, "o", "r", input) 114 if err != nil { 115 t.Errorf("Repositories.CreateDeployment returned error: %v", err) 116 } 117 118 want := &Deployment{Ref: String("1111"), Task: String("deploy")} 119 if !cmp.Equal(deployment, want) { 120 t.Errorf("Repositories.CreateDeployment returned %+v, want %+v", deployment, want) 121 } 122 123 const methodName = "CreateDeployment" 124 testBadOptions(t, methodName, func() (err error) { 125 _, _, err = client.Repositories.CreateDeployment(ctx, "\n", "\n", input) 126 return err 127 }) 128 129 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 130 got, resp, err := client.Repositories.CreateDeployment(ctx, "o", "r", input) 131 if got != nil { 132 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 133 } 134 return resp, err 135 }) 136 } 137 138 func TestRepositoriesService_DeleteDeployment(t *testing.T) { 139 t.Parallel() 140 client, mux, _ := setup(t) 141 142 mux.HandleFunc("/repos/o/r/deployments/1", func(w http.ResponseWriter, r *http.Request) { 143 testMethod(t, r, "DELETE") 144 w.WriteHeader(http.StatusNoContent) 145 }) 146 147 ctx := context.Background() 148 resp, err := client.Repositories.DeleteDeployment(ctx, "o", "r", 1) 149 if err != nil { 150 t.Errorf("Repositories.DeleteDeployment returned error: %v", err) 151 } 152 if resp.StatusCode != http.StatusNoContent { 153 t.Error("Repositories.DeleteDeployment should return a 204 status") 154 } 155 156 resp, err = client.Repositories.DeleteDeployment(ctx, "o", "r", 2) 157 if err == nil { 158 t.Error("Repositories.DeleteDeployment should return an error") 159 } 160 if resp.StatusCode != http.StatusNotFound { 161 t.Error("Repositories.DeleteDeployment should return a 404 status") 162 } 163 164 const methodName = "DeleteDeployment" 165 testBadOptions(t, methodName, func() (err error) { 166 _, err = client.Repositories.DeleteDeployment(ctx, "\n", "\n", 1) 167 return err 168 }) 169 170 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 171 return client.Repositories.DeleteDeployment(ctx, "o", "r", 1) 172 }) 173 } 174 175 func TestRepositoriesService_ListDeploymentStatuses(t *testing.T) { 176 t.Parallel() 177 client, mux, _ := setup(t) 178 179 wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview} 180 mux.HandleFunc("/repos/o/r/deployments/1/statuses", func(w http.ResponseWriter, r *http.Request) { 181 testMethod(t, r, "GET") 182 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 183 testFormValues(t, r, values{"page": "2"}) 184 fmt.Fprint(w, `[{"id":1}, {"id":2}]`) 185 }) 186 187 opt := &ListOptions{Page: 2} 188 ctx := context.Background() 189 statutses, _, err := client.Repositories.ListDeploymentStatuses(ctx, "o", "r", 1, opt) 190 if err != nil { 191 t.Errorf("Repositories.ListDeploymentStatuses returned error: %v", err) 192 } 193 194 want := []*DeploymentStatus{{ID: Int64(1)}, {ID: Int64(2)}} 195 if !cmp.Equal(statutses, want) { 196 t.Errorf("Repositories.ListDeploymentStatuses returned %+v, want %+v", statutses, want) 197 } 198 199 const methodName = "ListDeploymentStatuses" 200 testBadOptions(t, methodName, func() (err error) { 201 _, _, err = client.Repositories.ListDeploymentStatuses(ctx, "\n", "\n", 1, opt) 202 return err 203 }) 204 205 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 206 got, resp, err := client.Repositories.ListDeploymentStatuses(ctx, "o", "r", 1, opt) 207 if got != nil { 208 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 209 } 210 return resp, err 211 }) 212 } 213 214 func TestRepositoriesService_GetDeploymentStatus(t *testing.T) { 215 t.Parallel() 216 client, mux, _ := setup(t) 217 218 wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview} 219 mux.HandleFunc("/repos/o/r/deployments/3/statuses/4", func(w http.ResponseWriter, r *http.Request) { 220 testMethod(t, r, "GET") 221 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 222 fmt.Fprint(w, `{"id":4}`) 223 }) 224 225 ctx := context.Background() 226 deploymentStatus, _, err := client.Repositories.GetDeploymentStatus(ctx, "o", "r", 3, 4) 227 if err != nil { 228 t.Errorf("Repositories.GetDeploymentStatus returned error: %v", err) 229 } 230 231 want := &DeploymentStatus{ID: Int64(4)} 232 if !cmp.Equal(deploymentStatus, want) { 233 t.Errorf("Repositories.GetDeploymentStatus returned %+v, want %+v", deploymentStatus, want) 234 } 235 236 const methodName = "GetDeploymentStatus" 237 testBadOptions(t, methodName, func() (err error) { 238 _, _, err = client.Repositories.GetDeploymentStatus(ctx, "\n", "\n", 3, 4) 239 return err 240 }) 241 242 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 243 got, resp, err := client.Repositories.GetDeploymentStatus(ctx, "o", "r", 3, 4) 244 if got != nil { 245 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 246 } 247 return resp, err 248 }) 249 } 250 251 func TestRepositoriesService_CreateDeploymentStatus(t *testing.T) { 252 t.Parallel() 253 client, mux, _ := setup(t) 254 255 input := &DeploymentStatusRequest{State: String("inactive"), Description: String("deploy"), AutoInactive: Bool(false)} 256 257 mux.HandleFunc("/repos/o/r/deployments/1/statuses", func(w http.ResponseWriter, r *http.Request) { 258 v := new(DeploymentStatusRequest) 259 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 260 261 testMethod(t, r, "POST") 262 wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview} 263 testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) 264 if !cmp.Equal(v, input) { 265 t.Errorf("Request body = %+v, want %+v", v, input) 266 } 267 268 fmt.Fprint(w, `{"state": "inactive", "description": "deploy"}`) 269 }) 270 271 ctx := context.Background() 272 deploymentStatus, _, err := client.Repositories.CreateDeploymentStatus(ctx, "o", "r", 1, input) 273 if err != nil { 274 t.Errorf("Repositories.CreateDeploymentStatus returned error: %v", err) 275 } 276 277 want := &DeploymentStatus{State: String("inactive"), Description: String("deploy")} 278 if !cmp.Equal(deploymentStatus, want) { 279 t.Errorf("Repositories.CreateDeploymentStatus returned %+v, want %+v", deploymentStatus, want) 280 } 281 282 const methodName = "CreateDeploymentStatus" 283 testBadOptions(t, methodName, func() (err error) { 284 _, _, err = client.Repositories.CreateDeploymentStatus(ctx, "\n", "\n", 1, input) 285 return err 286 }) 287 288 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 289 got, resp, err := client.Repositories.CreateDeploymentStatus(ctx, "o", "r", 1, input) 290 if got != nil { 291 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 292 } 293 return resp, err 294 }) 295 } 296 297 func TestDeploymentStatusRequest_Marshal(t *testing.T) { 298 t.Parallel() 299 testJSONMarshal(t, &DeploymentStatusRequest{}, "{}") 300 301 r := &DeploymentStatusRequest{ 302 State: String("state"), 303 LogURL: String("logurl"), 304 Description: String("desc"), 305 Environment: String("env"), 306 EnvironmentURL: String("eurl"), 307 AutoInactive: Bool(false), 308 } 309 310 want := `{ 311 "state": "state", 312 "log_url": "logurl", 313 "description": "desc", 314 "environment": "env", 315 "environment_url": "eurl", 316 "auto_inactive": false 317 }` 318 319 testJSONMarshal(t, r, want) 320 } 321 322 func TestDeploymentStatus_Marshal(t *testing.T) { 323 t.Parallel() 324 testJSONMarshal(t, &DeploymentStatus{}, "{}") 325 326 r := &DeploymentStatus{ 327 ID: Int64(1), 328 State: String("state"), 329 Creator: &User{ 330 Login: String("l"), 331 ID: Int64(1), 332 URL: String("u"), 333 AvatarURL: String("a"), 334 GravatarID: String("g"), 335 Name: String("n"), 336 Company: String("c"), 337 Blog: String("b"), 338 Location: String("l"), 339 Email: String("e"), 340 Hireable: Bool(true), 341 Bio: String("b"), 342 TwitterUsername: String("t"), 343 PublicRepos: Int(1), 344 Followers: Int(1), 345 Following: Int(1), 346 CreatedAt: &Timestamp{referenceTime}, 347 SuspendedAt: &Timestamp{referenceTime}, 348 }, 349 Description: String("desc"), 350 Environment: String("env"), 351 NodeID: String("nid"), 352 CreatedAt: &Timestamp{referenceTime}, 353 UpdatedAt: &Timestamp{referenceTime}, 354 TargetURL: String("turl"), 355 DeploymentURL: String("durl"), 356 RepositoryURL: String("rurl"), 357 EnvironmentURL: String("eurl"), 358 LogURL: String("lurl"), 359 URL: String("url"), 360 } 361 362 want := `{ 363 "id": 1, 364 "state": "state", 365 "creator": { 366 "login": "l", 367 "id": 1, 368 "avatar_url": "a", 369 "gravatar_id": "g", 370 "name": "n", 371 "company": "c", 372 "blog": "b", 373 "location": "l", 374 "email": "e", 375 "hireable": true, 376 "bio": "b", 377 "twitter_username": "t", 378 "public_repos": 1, 379 "followers": 1, 380 "following": 1, 381 "created_at": ` + referenceTimeStr + `, 382 "suspended_at": ` + referenceTimeStr + `, 383 "url": "u" 384 }, 385 "description": "desc", 386 "environment": "env", 387 "node_id": "nid", 388 "created_at": ` + referenceTimeStr + `, 389 "updated_at": ` + referenceTimeStr + `, 390 "target_url": "turl", 391 "deployment_url": "durl", 392 "repository_url": "rurl", 393 "environment_url": "eurl", 394 "log_url": "lurl", 395 "url": "url" 396 }` 397 398 testJSONMarshal(t, r, want) 399 } 400 401 func TestDeploymentRequest_Marshal(t *testing.T) { 402 t.Parallel() 403 testJSONMarshal(t, &DeploymentRequest{}, "{}") 404 405 r := &DeploymentRequest{ 406 Ref: String("ref"), 407 Task: String("task"), 408 AutoMerge: Bool(false), 409 RequiredContexts: &[]string{"s"}, 410 Payload: "payload", 411 Environment: String("environment"), 412 Description: String("description"), 413 TransientEnvironment: Bool(false), 414 ProductionEnvironment: Bool(false), 415 } 416 417 want := `{ 418 "ref": "ref", 419 "task": "task", 420 "auto_merge": false, 421 "required_contexts": ["s"], 422 "payload": "payload", 423 "environment": "environment", 424 "description": "description", 425 "transient_environment": false, 426 "production_environment": false 427 }` 428 429 testJSONMarshal(t, r, want) 430 } 431 432 func TestDeployment_Marshal(t *testing.T) { 433 t.Parallel() 434 testJSONMarshal(t, &Deployment{}, "{}") 435 436 str := "s" 437 jsonMsg, _ := json.Marshal(str) 438 439 r := &Deployment{ 440 URL: String("url"), 441 ID: Int64(1), 442 SHA: String("sha"), 443 Ref: String("ref"), 444 Task: String("task"), 445 Payload: jsonMsg, 446 Environment: String("env"), 447 Description: String("desc"), 448 Creator: &User{ 449 Login: String("l"), 450 ID: Int64(1), 451 URL: String("u"), 452 AvatarURL: String("a"), 453 GravatarID: String("g"), 454 Name: String("n"), 455 Company: String("c"), 456 Blog: String("b"), 457 Location: String("l"), 458 Email: String("e"), 459 Hireable: Bool(true), 460 Bio: String("b"), 461 TwitterUsername: String("t"), 462 PublicRepos: Int(1), 463 Followers: Int(1), 464 Following: Int(1), 465 CreatedAt: &Timestamp{referenceTime}, 466 SuspendedAt: &Timestamp{referenceTime}, 467 }, 468 CreatedAt: &Timestamp{referenceTime}, 469 UpdatedAt: &Timestamp{referenceTime}, 470 StatusesURL: String("surl"), 471 RepositoryURL: String("rurl"), 472 NodeID: String("nid"), 473 } 474 475 want := `{ 476 "url": "url", 477 "id": 1, 478 "sha": "sha", 479 "ref": "ref", 480 "task": "task", 481 "payload": "s", 482 "environment": "env", 483 "description": "desc", 484 "creator": { 485 "login": "l", 486 "id": 1, 487 "avatar_url": "a", 488 "gravatar_id": "g", 489 "name": "n", 490 "company": "c", 491 "blog": "b", 492 "location": "l", 493 "email": "e", 494 "hireable": true, 495 "bio": "b", 496 "twitter_username": "t", 497 "public_repos": 1, 498 "followers": 1, 499 "following": 1, 500 "created_at": ` + referenceTimeStr + `, 501 "suspended_at": ` + referenceTimeStr + `, 502 "url": "u" 503 }, 504 "created_at": ` + referenceTimeStr + `, 505 "updated_at": ` + referenceTimeStr + `, 506 "statuses_url": "surl", 507 "repository_url": "rurl", 508 "node_id": "nid" 509 }` 510 511 testJSONMarshal(t, r, want) 512 }