github.com/google/go-github/v42@v42.0.0/github/apps_test.go (about) 1 // Copyright 2016 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 "time" 15 16 "github.com/google/go-cmp/cmp" 17 ) 18 19 func TestAppsService_Get_authenticatedApp(t *testing.T) { 20 client, mux, _, teardown := setup() 21 defer teardown() 22 23 mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) { 24 testMethod(t, r, "GET") 25 fmt.Fprint(w, `{"id":1}`) 26 }) 27 28 ctx := context.Background() 29 app, _, err := client.Apps.Get(ctx, "") 30 if err != nil { 31 t.Errorf("Apps.Get returned error: %v", err) 32 } 33 34 want := &App{ID: Int64(1)} 35 if !cmp.Equal(app, want) { 36 t.Errorf("Apps.Get returned %+v, want %+v", app, want) 37 } 38 39 const methodName = "Get" 40 testBadOptions(t, methodName, func() (err error) { 41 _, _, err = client.Apps.Get(ctx, "\n") 42 return err 43 }) 44 45 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 46 got, resp, err := client.Apps.Get(ctx, "") 47 if got != nil { 48 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 49 } 50 return resp, err 51 }) 52 } 53 54 func TestAppsService_Get_specifiedApp(t *testing.T) { 55 client, mux, _, teardown := setup() 56 defer teardown() 57 58 mux.HandleFunc("/apps/a", func(w http.ResponseWriter, r *http.Request) { 59 testMethod(t, r, "GET") 60 fmt.Fprint(w, `{"html_url":"https://github.com/apps/a"}`) 61 }) 62 63 ctx := context.Background() 64 app, _, err := client.Apps.Get(ctx, "a") 65 if err != nil { 66 t.Errorf("Apps.Get returned error: %v", err) 67 } 68 69 want := &App{HTMLURL: String("https://github.com/apps/a")} 70 if !cmp.Equal(app, want) { 71 t.Errorf("Apps.Get returned %+v, want %+v", *app.HTMLURL, *want.HTMLURL) 72 } 73 } 74 75 func TestAppsService_ListInstallations(t *testing.T) { 76 client, mux, _, teardown := setup() 77 defer teardown() 78 79 mux.HandleFunc("/app/installations", func(w http.ResponseWriter, r *http.Request) { 80 testMethod(t, r, "GET") 81 testFormValues(t, r, values{ 82 "page": "1", 83 "per_page": "2", 84 }) 85 fmt.Fprint(w, `[{ 86 "id":1, 87 "app_id":1, 88 "target_id":1, 89 "target_type": "Organization", 90 "permissions": { 91 "actions": "read", 92 "administration": "read", 93 "checks": "read", 94 "contents": "read", 95 "content_references": "read", 96 "deployments": "read", 97 "environments": "read", 98 "issues": "write", 99 "metadata": "read", 100 "members": "read", 101 "organization_administration": "write", 102 "organization_hooks": "write", 103 "organization_plan": "read", 104 "organization_pre_receive_hooks": "write", 105 "organization_projects": "read", 106 "organization_secrets": "read", 107 "organization_self_hosted_runners": "read", 108 "organization_user_blocking": "write", 109 "packages": "read", 110 "pages": "read", 111 "pull_requests": "write", 112 "repository_hooks": "write", 113 "repository_projects": "read", 114 "repository_pre_receive_hooks": "read", 115 "secrets": "read", 116 "secret_scanning_alerts": "read", 117 "security_events": "read", 118 "single_file": "write", 119 "statuses": "write", 120 "team_discussions": "read", 121 "vulnerability_alerts": "read", 122 "workflows": "write" 123 }, 124 "events": [ 125 "push", 126 "pull_request" 127 ], 128 "single_file_name": "config.yml", 129 "repository_selection": "selected", 130 "created_at": "2018-01-01T00:00:00Z", 131 "updated_at": "2018-01-01T00:00:00Z"}]`, 132 ) 133 }) 134 135 opt := &ListOptions{Page: 1, PerPage: 2} 136 ctx := context.Background() 137 installations, _, err := client.Apps.ListInstallations(ctx, opt) 138 if err != nil { 139 t.Errorf("Apps.ListInstallations returned error: %v", err) 140 } 141 142 date := Timestamp{Time: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)} 143 want := []*Installation{{ 144 ID: Int64(1), 145 AppID: Int64(1), 146 TargetID: Int64(1), 147 TargetType: String("Organization"), 148 SingleFileName: String("config.yml"), 149 RepositorySelection: String("selected"), 150 Permissions: &InstallationPermissions{ 151 Actions: String("read"), 152 Administration: String("read"), 153 Checks: String("read"), 154 Contents: String("read"), 155 ContentReferences: String("read"), 156 Deployments: String("read"), 157 Environments: String("read"), 158 Issues: String("write"), 159 Metadata: String("read"), 160 Members: String("read"), 161 OrganizationAdministration: String("write"), 162 OrganizationHooks: String("write"), 163 OrganizationPlan: String("read"), 164 OrganizationPreReceiveHooks: String("write"), 165 OrganizationProjects: String("read"), 166 OrganizationSecrets: String("read"), 167 OrganizationSelfHostedRunners: String("read"), 168 OrganizationUserBlocking: String("write"), 169 Packages: String("read"), 170 Pages: String("read"), 171 PullRequests: String("write"), 172 RepositoryHooks: String("write"), 173 RepositoryProjects: String("read"), 174 RepositoryPreReceiveHooks: String("read"), 175 Secrets: String("read"), 176 SecretScanningAlerts: String("read"), 177 SecurityEvents: String("read"), 178 SingleFile: String("write"), 179 Statuses: String("write"), 180 TeamDiscussions: String("read"), 181 VulnerabilityAlerts: String("read"), 182 Workflows: String("write")}, 183 Events: []string{"push", "pull_request"}, 184 CreatedAt: &date, 185 UpdatedAt: &date, 186 }} 187 if !cmp.Equal(installations, want) { 188 t.Errorf("Apps.ListInstallations returned %+v, want %+v", installations, want) 189 } 190 191 const methodName = "ListInstallations" 192 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 193 got, resp, err := client.Apps.ListInstallations(ctx, opt) 194 if got != nil { 195 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 196 } 197 return resp, err 198 }) 199 } 200 201 func TestAppsService_GetInstallation(t *testing.T) { 202 client, mux, _, teardown := setup() 203 defer teardown() 204 205 mux.HandleFunc("/app/installations/1", func(w http.ResponseWriter, r *http.Request) { 206 testMethod(t, r, "GET") 207 fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`) 208 }) 209 210 ctx := context.Background() 211 installation, _, err := client.Apps.GetInstallation(ctx, 1) 212 if err != nil { 213 t.Errorf("Apps.GetInstallation returned error: %v", err) 214 } 215 216 want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")} 217 if !cmp.Equal(installation, want) { 218 t.Errorf("Apps.GetInstallation returned %+v, want %+v", installation, want) 219 } 220 221 const methodName = "GetInstallation" 222 testBadOptions(t, methodName, func() (err error) { 223 _, _, err = client.Apps.GetInstallation(ctx, -1) 224 return err 225 }) 226 227 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 228 got, resp, err := client.Apps.GetInstallation(ctx, 1) 229 if got != nil { 230 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 231 } 232 return resp, err 233 }) 234 } 235 236 func TestAppsService_ListUserInstallations(t *testing.T) { 237 client, mux, _, teardown := setup() 238 defer teardown() 239 240 mux.HandleFunc("/user/installations", func(w http.ResponseWriter, r *http.Request) { 241 testMethod(t, r, "GET") 242 testFormValues(t, r, values{ 243 "page": "1", 244 "per_page": "2", 245 }) 246 fmt.Fprint(w, `{"installations":[{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}]}`) 247 }) 248 249 opt := &ListOptions{Page: 1, PerPage: 2} 250 ctx := context.Background() 251 installations, _, err := client.Apps.ListUserInstallations(ctx, opt) 252 if err != nil { 253 t.Errorf("Apps.ListUserInstallations returned error: %v", err) 254 } 255 256 want := []*Installation{{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}} 257 if !cmp.Equal(installations, want) { 258 t.Errorf("Apps.ListUserInstallations returned %+v, want %+v", installations, want) 259 } 260 261 const methodName = "ListUserInstallations" 262 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 263 got, resp, err := client.Apps.ListUserInstallations(ctx, opt) 264 if got != nil { 265 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 266 } 267 return resp, err 268 }) 269 } 270 271 func TestAppsService_SuspendInstallation(t *testing.T) { 272 client, mux, _, teardown := setup() 273 defer teardown() 274 275 mux.HandleFunc("/app/installations/1/suspended", func(w http.ResponseWriter, r *http.Request) { 276 testMethod(t, r, "PUT") 277 278 w.WriteHeader(http.StatusNoContent) 279 }) 280 281 ctx := context.Background() 282 if _, err := client.Apps.SuspendInstallation(ctx, 1); err != nil { 283 t.Errorf("Apps.SuspendInstallation returned error: %v", err) 284 } 285 286 const methodName = "SuspendInstallation" 287 testBadOptions(t, methodName, func() (err error) { 288 _, err = client.Apps.SuspendInstallation(ctx, -1) 289 return err 290 }) 291 292 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 293 return client.Apps.SuspendInstallation(ctx, 1) 294 }) 295 } 296 297 func TestAppsService_UnsuspendInstallation(t *testing.T) { 298 client, mux, _, teardown := setup() 299 defer teardown() 300 301 mux.HandleFunc("/app/installations/1/suspended", func(w http.ResponseWriter, r *http.Request) { 302 testMethod(t, r, "DELETE") 303 304 w.WriteHeader(http.StatusNoContent) 305 }) 306 307 ctx := context.Background() 308 if _, err := client.Apps.UnsuspendInstallation(ctx, 1); err != nil { 309 t.Errorf("Apps.UnsuspendInstallation returned error: %v", err) 310 } 311 312 const methodName = "UnsuspendInstallation" 313 testBadOptions(t, methodName, func() (err error) { 314 _, err = client.Apps.UnsuspendInstallation(ctx, -1) 315 return err 316 }) 317 318 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 319 return client.Apps.UnsuspendInstallation(ctx, 1) 320 }) 321 } 322 323 func TestAppsService_DeleteInstallation(t *testing.T) { 324 client, mux, _, teardown := setup() 325 defer teardown() 326 327 mux.HandleFunc("/app/installations/1", func(w http.ResponseWriter, r *http.Request) { 328 testMethod(t, r, "DELETE") 329 w.WriteHeader(http.StatusNoContent) 330 }) 331 332 ctx := context.Background() 333 _, err := client.Apps.DeleteInstallation(ctx, 1) 334 if err != nil { 335 t.Errorf("Apps.DeleteInstallation returned error: %v", err) 336 } 337 338 const methodName = "DeleteInstallation" 339 testBadOptions(t, methodName, func() (err error) { 340 _, err = client.Apps.DeleteInstallation(ctx, -1) 341 return err 342 }) 343 344 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 345 return client.Apps.DeleteInstallation(ctx, 1) 346 }) 347 } 348 349 func TestAppsService_CreateInstallationToken(t *testing.T) { 350 client, mux, _, teardown := setup() 351 defer teardown() 352 353 mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { 354 testMethod(t, r, "POST") 355 fmt.Fprint(w, `{"token":"t"}`) 356 }) 357 358 ctx := context.Background() 359 token, _, err := client.Apps.CreateInstallationToken(ctx, 1, nil) 360 if err != nil { 361 t.Errorf("Apps.CreateInstallationToken returned error: %v", err) 362 } 363 364 want := &InstallationToken{Token: String("t")} 365 if !cmp.Equal(token, want) { 366 t.Errorf("Apps.CreateInstallationToken returned %+v, want %+v", token, want) 367 } 368 369 const methodName = "CreateInstallationToken" 370 testBadOptions(t, methodName, func() (err error) { 371 _, _, err = client.Apps.CreateInstallationToken(ctx, -1, nil) 372 return err 373 }) 374 375 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 376 got, resp, err := client.Apps.CreateInstallationToken(ctx, 1, nil) 377 if got != nil { 378 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 379 } 380 return resp, err 381 }) 382 } 383 384 func TestAppsService_CreateInstallationTokenWithOptions(t *testing.T) { 385 client, mux, _, teardown := setup() 386 defer teardown() 387 388 installationTokenOptions := &InstallationTokenOptions{ 389 RepositoryIDs: []int64{1234}, 390 Repositories: []string{"foo"}, 391 Permissions: &InstallationPermissions{ 392 Contents: String("write"), 393 Issues: String("read"), 394 }, 395 } 396 397 mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { 398 v := new(InstallationTokenOptions) 399 json.NewDecoder(r.Body).Decode(v) 400 401 if !cmp.Equal(v, installationTokenOptions) { 402 t.Errorf("request sent %+v, want %+v", v, installationTokenOptions) 403 } 404 405 testMethod(t, r, "POST") 406 fmt.Fprint(w, `{"token":"t"}`) 407 }) 408 409 ctx := context.Background() 410 token, _, err := client.Apps.CreateInstallationToken(ctx, 1, installationTokenOptions) 411 if err != nil { 412 t.Errorf("Apps.CreateInstallationToken returned error: %v", err) 413 } 414 415 want := &InstallationToken{Token: String("t")} 416 if !cmp.Equal(token, want) { 417 t.Errorf("Apps.CreateInstallationToken returned %+v, want %+v", token, want) 418 } 419 } 420 421 func TestAppsService_CreateAttachement(t *testing.T) { 422 client, mux, _, teardown := setup() 423 defer teardown() 424 425 mux.HandleFunc("/content_references/11/attachments", func(w http.ResponseWriter, r *http.Request) { 426 testMethod(t, r, "POST") 427 testHeader(t, r, "Accept", mediaTypeContentAttachmentsPreview) 428 429 w.WriteHeader(http.StatusOK) 430 w.Write([]byte(`{"id":1,"title":"title1","body":"body1"}`)) 431 }) 432 433 ctx := context.Background() 434 got, _, err := client.Apps.CreateAttachment(ctx, 11, "title1", "body1") 435 if err != nil { 436 t.Errorf("CreateAttachment returned error: %v", err) 437 } 438 439 want := &Attachment{ID: Int64(1), Title: String("title1"), Body: String("body1")} 440 if !cmp.Equal(got, want) { 441 t.Errorf("CreateAttachment = %+v, want %+v", got, want) 442 } 443 444 const methodName = "CreateAttachment" 445 testBadOptions(t, methodName, func() (err error) { 446 _, _, err = client.Apps.CreateAttachment(ctx, -11, "\n", "\n") 447 return err 448 }) 449 450 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 451 got, resp, err := client.Apps.CreateAttachment(ctx, 11, "title1", "body1") 452 if got != nil { 453 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 454 } 455 return resp, err 456 }) 457 } 458 459 func TestAppsService_FindOrganizationInstallation(t *testing.T) { 460 client, mux, _, teardown := setup() 461 defer teardown() 462 463 mux.HandleFunc("/orgs/o/installation", func(w http.ResponseWriter, r *http.Request) { 464 testMethod(t, r, "GET") 465 fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`) 466 }) 467 468 ctx := context.Background() 469 installation, _, err := client.Apps.FindOrganizationInstallation(ctx, "o") 470 if err != nil { 471 t.Errorf("Apps.FindOrganizationInstallation returned error: %v", err) 472 } 473 474 want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")} 475 if !cmp.Equal(installation, want) { 476 t.Errorf("Apps.FindOrganizationInstallation returned %+v, want %+v", installation, want) 477 } 478 479 const methodName = "FindOrganizationInstallation" 480 testBadOptions(t, methodName, func() (err error) { 481 _, _, err = client.Apps.FindOrganizationInstallation(ctx, "\n") 482 return err 483 }) 484 485 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 486 got, resp, err := client.Apps.FindOrganizationInstallation(ctx, "o") 487 if got != nil { 488 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 489 } 490 return resp, err 491 }) 492 } 493 494 func TestAppsService_FindRepositoryInstallation(t *testing.T) { 495 client, mux, _, teardown := setup() 496 defer teardown() 497 498 mux.HandleFunc("/repos/o/r/installation", func(w http.ResponseWriter, r *http.Request) { 499 testMethod(t, r, "GET") 500 fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`) 501 }) 502 503 ctx := context.Background() 504 installation, _, err := client.Apps.FindRepositoryInstallation(ctx, "o", "r") 505 if err != nil { 506 t.Errorf("Apps.FindRepositoryInstallation returned error: %v", err) 507 } 508 509 want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")} 510 if !cmp.Equal(installation, want) { 511 t.Errorf("Apps.FindRepositoryInstallation returned %+v, want %+v", installation, want) 512 } 513 514 const methodName = "FindRepositoryInstallation" 515 testBadOptions(t, methodName, func() (err error) { 516 _, _, err = client.Apps.FindRepositoryInstallation(ctx, "\n", "\n") 517 return err 518 }) 519 520 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 521 got, resp, err := client.Apps.FindRepositoryInstallation(ctx, "o", "r") 522 if got != nil { 523 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 524 } 525 return resp, err 526 }) 527 } 528 529 func TestAppsService_FindRepositoryInstallationByID(t *testing.T) { 530 client, mux, _, teardown := setup() 531 defer teardown() 532 533 mux.HandleFunc("/repositories/1/installation", func(w http.ResponseWriter, r *http.Request) { 534 testMethod(t, r, "GET") 535 fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`) 536 }) 537 538 ctx := context.Background() 539 installation, _, err := client.Apps.FindRepositoryInstallationByID(ctx, 1) 540 if err != nil { 541 t.Errorf("Apps.FindRepositoryInstallationByID returned error: %v", err) 542 } 543 544 want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")} 545 if !cmp.Equal(installation, want) { 546 t.Errorf("Apps.FindRepositoryInstallationByID returned %+v, want %+v", installation, want) 547 } 548 549 const methodName = "FindRepositoryInstallationByID" 550 testBadOptions(t, methodName, func() (err error) { 551 _, _, err = client.Apps.FindRepositoryInstallationByID(ctx, -1) 552 return err 553 }) 554 555 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 556 got, resp, err := client.Apps.FindRepositoryInstallationByID(ctx, 1) 557 if got != nil { 558 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 559 } 560 return resp, err 561 }) 562 } 563 564 func TestAppsService_FindUserInstallation(t *testing.T) { 565 client, mux, _, teardown := setup() 566 defer teardown() 567 568 mux.HandleFunc("/users/u/installation", func(w http.ResponseWriter, r *http.Request) { 569 testMethod(t, r, "GET") 570 fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "User"}`) 571 }) 572 573 ctx := context.Background() 574 installation, _, err := client.Apps.FindUserInstallation(ctx, "u") 575 if err != nil { 576 t.Errorf("Apps.FindUserInstallation returned error: %v", err) 577 } 578 579 want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("User")} 580 if !cmp.Equal(installation, want) { 581 t.Errorf("Apps.FindUserInstallation returned %+v, want %+v", installation, want) 582 } 583 584 const methodName = "FindUserInstallation" 585 testBadOptions(t, methodName, func() (err error) { 586 _, _, err = client.Apps.FindUserInstallation(ctx, "\n") 587 return err 588 }) 589 590 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 591 got, resp, err := client.Apps.FindUserInstallation(ctx, "u") 592 if got != nil { 593 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 594 } 595 return resp, err 596 }) 597 } 598 599 func TestContentReference_Marshal(t *testing.T) { 600 testJSONMarshal(t, &ContentReference{}, "{}") 601 602 u := &ContentReference{ 603 ID: Int64(1), 604 NodeID: String("nid"), 605 Reference: String("r"), 606 } 607 608 want := `{ 609 "id": 1, 610 "node_id": "nid", 611 "reference": "r" 612 }` 613 614 testJSONMarshal(t, u, want) 615 } 616 617 func TestAttachment_Marshal(t *testing.T) { 618 testJSONMarshal(t, &Attachment{}, "{}") 619 620 u := &Attachment{ 621 ID: Int64(1), 622 Title: String("t"), 623 Body: String("b"), 624 } 625 626 want := `{ 627 "id": 1, 628 "title": "t", 629 "body": "b" 630 }` 631 632 testJSONMarshal(t, u, want) 633 } 634 635 func TestInstallationPermissions_Marshal(t *testing.T) { 636 testJSONMarshal(t, &InstallationPermissions{}, "{}") 637 638 u := &InstallationPermissions{ 639 Actions: String("a"), 640 Administration: String("ad"), 641 Checks: String("c"), 642 Contents: String("co"), 643 ContentReferences: String("cr"), 644 Deployments: String("d"), 645 Environments: String("e"), 646 Issues: String("i"), 647 Metadata: String("md"), 648 Members: String("m"), 649 OrganizationAdministration: String("oa"), 650 OrganizationHooks: String("oh"), 651 OrganizationPlan: String("op"), 652 OrganizationPreReceiveHooks: String("opr"), 653 OrganizationProjects: String("op"), 654 OrganizationSecrets: String("os"), 655 OrganizationSelfHostedRunners: String("osh"), 656 OrganizationUserBlocking: String("oub"), 657 Packages: String("pkg"), 658 Pages: String("pg"), 659 PullRequests: String("pr"), 660 RepositoryHooks: String("rh"), 661 RepositoryProjects: String("rp"), 662 RepositoryPreReceiveHooks: String("rprh"), 663 Secrets: String("s"), 664 SecretScanningAlerts: String("ssa"), 665 SecurityEvents: String("se"), 666 SingleFile: String("sf"), 667 Statuses: String("s"), 668 TeamDiscussions: String("td"), 669 VulnerabilityAlerts: String("va"), 670 Workflows: String("w"), 671 } 672 673 want := `{ 674 "actions": "a", 675 "administration": "ad", 676 "checks": "c", 677 "contents": "co", 678 "content_references": "cr", 679 "deployments": "d", 680 "environments": "e", 681 "issues": "i", 682 "metadata": "md", 683 "members": "m", 684 "organization_administration": "oa", 685 "organization_hooks": "oh", 686 "organization_plan": "op", 687 "organization_pre_receive_hooks": "opr", 688 "organization_projects": "op", 689 "organization_secrets": "os", 690 "organization_self_hosted_runners": "osh", 691 "organization_user_blocking": "oub", 692 "packages": "pkg", 693 "pages": "pg", 694 "pull_requests": "pr", 695 "repository_hooks": "rh", 696 "repository_projects": "rp", 697 "repository_pre_receive_hooks": "rprh", 698 "secrets": "s", 699 "secret_scanning_alerts": "ssa", 700 "security_events": "se", 701 "single_file": "sf", 702 "statuses": "s", 703 "team_discussions": "td", 704 "vulnerability_alerts":"va", 705 "workflows": "w" 706 }` 707 708 testJSONMarshal(t, u, want) 709 } 710 711 func TestInstallation_Marshal(t *testing.T) { 712 testJSONMarshal(t, &Installation{}, "{}") 713 714 u := &Installation{ 715 ID: Int64(1), 716 NodeID: String("nid"), 717 AppID: Int64(1), 718 AppSlug: String("as"), 719 TargetID: Int64(1), 720 Account: &User{ 721 Login: String("l"), 722 ID: Int64(1), 723 URL: String("u"), 724 AvatarURL: String("a"), 725 GravatarID: String("g"), 726 Name: String("n"), 727 Company: String("c"), 728 Blog: String("b"), 729 Location: String("l"), 730 Email: String("e"), 731 Hireable: Bool(true), 732 Bio: String("b"), 733 TwitterUsername: String("t"), 734 PublicRepos: Int(1), 735 Followers: Int(1), 736 Following: Int(1), 737 CreatedAt: &Timestamp{referenceTime}, 738 SuspendedAt: &Timestamp{referenceTime}, 739 }, 740 AccessTokensURL: String("atu"), 741 RepositoriesURL: String("ru"), 742 HTMLURL: String("hu"), 743 TargetType: String("tt"), 744 SingleFileName: String("sfn"), 745 RepositorySelection: String("rs"), 746 Events: []string{"e"}, 747 SingleFilePaths: []string{"s"}, 748 Permissions: &InstallationPermissions{ 749 Actions: String("a"), 750 Administration: String("ad"), 751 Checks: String("c"), 752 Contents: String("co"), 753 ContentReferences: String("cr"), 754 Deployments: String("d"), 755 Environments: String("e"), 756 Issues: String("i"), 757 Metadata: String("md"), 758 Members: String("m"), 759 OrganizationAdministration: String("oa"), 760 OrganizationHooks: String("oh"), 761 OrganizationPlan: String("op"), 762 OrganizationPreReceiveHooks: String("opr"), 763 OrganizationProjects: String("op"), 764 OrganizationSecrets: String("os"), 765 OrganizationSelfHostedRunners: String("osh"), 766 OrganizationUserBlocking: String("oub"), 767 Packages: String("pkg"), 768 Pages: String("pg"), 769 PullRequests: String("pr"), 770 RepositoryHooks: String("rh"), 771 RepositoryProjects: String("rp"), 772 RepositoryPreReceiveHooks: String("rprh"), 773 Secrets: String("s"), 774 SecretScanningAlerts: String("ssa"), 775 SecurityEvents: String("se"), 776 SingleFile: String("sf"), 777 Statuses: String("s"), 778 TeamDiscussions: String("td"), 779 VulnerabilityAlerts: String("va"), 780 Workflows: String("w"), 781 }, 782 CreatedAt: &Timestamp{referenceTime}, 783 UpdatedAt: &Timestamp{referenceTime}, 784 HasMultipleSingleFiles: Bool(false), 785 SuspendedBy: &User{ 786 Login: String("l"), 787 ID: Int64(1), 788 URL: String("u"), 789 AvatarURL: String("a"), 790 GravatarID: String("g"), 791 Name: String("n"), 792 Company: String("c"), 793 Blog: String("b"), 794 Location: String("l"), 795 Email: String("e"), 796 Hireable: Bool(true), 797 Bio: String("b"), 798 TwitterUsername: String("t"), 799 PublicRepos: Int(1), 800 Followers: Int(1), 801 Following: Int(1), 802 CreatedAt: &Timestamp{referenceTime}, 803 SuspendedAt: &Timestamp{referenceTime}, 804 }, 805 SuspendedAt: &Timestamp{referenceTime}, 806 } 807 808 want := `{ 809 "id": 1, 810 "node_id": "nid", 811 "app_id": 1, 812 "app_slug": "as", 813 "target_id": 1, 814 "account": { 815 "login": "l", 816 "id": 1, 817 "avatar_url": "a", 818 "gravatar_id": "g", 819 "name": "n", 820 "company": "c", 821 "blog": "b", 822 "location": "l", 823 "email": "e", 824 "hireable": true, 825 "bio": "b", 826 "twitter_username": "t", 827 "public_repos": 1, 828 "followers": 1, 829 "following": 1, 830 "created_at": ` + referenceTimeStr + `, 831 "suspended_at": ` + referenceTimeStr + `, 832 "url": "u" 833 }, 834 "access_tokens_url": "atu", 835 "repositories_url": "ru", 836 "html_url": "hu", 837 "target_type": "tt", 838 "single_file_name": "sfn", 839 "repository_selection": "rs", 840 "events": [ 841 "e" 842 ], 843 "single_file_paths": [ 844 "s" 845 ], 846 "permissions": { 847 "actions": "a", 848 "administration": "ad", 849 "checks": "c", 850 "contents": "co", 851 "content_references": "cr", 852 "deployments": "d", 853 "environments": "e", 854 "issues": "i", 855 "metadata": "md", 856 "members": "m", 857 "organization_administration": "oa", 858 "organization_hooks": "oh", 859 "organization_plan": "op", 860 "organization_pre_receive_hooks": "opr", 861 "organization_projects": "op", 862 "organization_secrets": "os", 863 "organization_self_hosted_runners": "osh", 864 "organization_user_blocking": "oub", 865 "packages": "pkg", 866 "pages": "pg", 867 "pull_requests": "pr", 868 "repository_hooks": "rh", 869 "repository_projects": "rp", 870 "repository_pre_receive_hooks": "rprh", 871 "secrets": "s", 872 "secret_scanning_alerts": "ssa", 873 "security_events": "se", 874 "single_file": "sf", 875 "statuses": "s", 876 "team_discussions": "td", 877 "vulnerability_alerts": "va", 878 "workflows": "w" 879 }, 880 "created_at": ` + referenceTimeStr + `, 881 "updated_at": ` + referenceTimeStr + `, 882 "has_multiple_single_files": false, 883 "suspended_by": { 884 "login": "l", 885 "id": 1, 886 "avatar_url": "a", 887 "gravatar_id": "g", 888 "name": "n", 889 "company": "c", 890 "blog": "b", 891 "location": "l", 892 "email": "e", 893 "hireable": true, 894 "bio": "b", 895 "twitter_username": "t", 896 "public_repos": 1, 897 "followers": 1, 898 "following": 1, 899 "created_at": ` + referenceTimeStr + `, 900 "suspended_at": ` + referenceTimeStr + `, 901 "url": "u" 902 }, 903 "suspended_at": ` + referenceTimeStr + ` 904 }` 905 906 testJSONMarshal(t, u, want) 907 } 908 909 func TestInstallationTokenOptions_Marshal(t *testing.T) { 910 testJSONMarshal(t, &InstallationTokenOptions{}, "{}") 911 912 u := &InstallationTokenOptions{ 913 RepositoryIDs: []int64{1}, 914 Permissions: &InstallationPermissions{ 915 Actions: String("a"), 916 Administration: String("ad"), 917 Checks: String("c"), 918 Contents: String("co"), 919 ContentReferences: String("cr"), 920 Deployments: String("d"), 921 Environments: String("e"), 922 Issues: String("i"), 923 Metadata: String("md"), 924 Members: String("m"), 925 OrganizationAdministration: String("oa"), 926 OrganizationHooks: String("oh"), 927 OrganizationPlan: String("op"), 928 OrganizationPreReceiveHooks: String("opr"), 929 OrganizationProjects: String("op"), 930 OrganizationSecrets: String("os"), 931 OrganizationSelfHostedRunners: String("osh"), 932 OrganizationUserBlocking: String("oub"), 933 Packages: String("pkg"), 934 Pages: String("pg"), 935 PullRequests: String("pr"), 936 RepositoryHooks: String("rh"), 937 RepositoryProjects: String("rp"), 938 RepositoryPreReceiveHooks: String("rprh"), 939 Secrets: String("s"), 940 SecretScanningAlerts: String("ssa"), 941 SecurityEvents: String("se"), 942 SingleFile: String("sf"), 943 Statuses: String("s"), 944 TeamDiscussions: String("td"), 945 VulnerabilityAlerts: String("va"), 946 Workflows: String("w"), 947 }, 948 } 949 950 want := `{ 951 "repository_ids": [1], 952 "permissions": { 953 "actions": "a", 954 "administration": "ad", 955 "checks": "c", 956 "contents": "co", 957 "content_references": "cr", 958 "deployments": "d", 959 "environments": "e", 960 "issues": "i", 961 "metadata": "md", 962 "members": "m", 963 "organization_administration": "oa", 964 "organization_hooks": "oh", 965 "organization_plan": "op", 966 "organization_pre_receive_hooks": "opr", 967 "organization_projects": "op", 968 "organization_secrets": "os", 969 "organization_self_hosted_runners": "osh", 970 "organization_user_blocking": "oub", 971 "packages": "pkg", 972 "pages": "pg", 973 "pull_requests": "pr", 974 "repository_hooks": "rh", 975 "repository_projects": "rp", 976 "repository_pre_receive_hooks": "rprh", 977 "secrets": "s", 978 "secret_scanning_alerts": "ssa", 979 "security_events": "se", 980 "single_file": "sf", 981 "statuses": "s", 982 "team_discussions": "td", 983 "vulnerability_alerts": "va", 984 "workflows": "w" 985 } 986 }` 987 988 testJSONMarshal(t, u, want) 989 } 990 991 func TestInstallationToken_Marshal(t *testing.T) { 992 testJSONMarshal(t, &InstallationToken{}, "{}") 993 994 u := &InstallationToken{ 995 Token: String("t"), 996 ExpiresAt: &referenceTime, 997 Permissions: &InstallationPermissions{ 998 Actions: String("a"), 999 Administration: String("ad"), 1000 Checks: String("c"), 1001 Contents: String("co"), 1002 ContentReferences: String("cr"), 1003 Deployments: String("d"), 1004 Environments: String("e"), 1005 Issues: String("i"), 1006 Metadata: String("md"), 1007 Members: String("m"), 1008 OrganizationAdministration: String("oa"), 1009 OrganizationHooks: String("oh"), 1010 OrganizationPlan: String("op"), 1011 OrganizationPreReceiveHooks: String("opr"), 1012 OrganizationProjects: String("op"), 1013 OrganizationSecrets: String("os"), 1014 OrganizationSelfHostedRunners: String("osh"), 1015 OrganizationUserBlocking: String("oub"), 1016 Packages: String("pkg"), 1017 Pages: String("pg"), 1018 PullRequests: String("pr"), 1019 RepositoryHooks: String("rh"), 1020 RepositoryProjects: String("rp"), 1021 RepositoryPreReceiveHooks: String("rprh"), 1022 Secrets: String("s"), 1023 SecretScanningAlerts: String("ssa"), 1024 SecurityEvents: String("se"), 1025 SingleFile: String("sf"), 1026 Statuses: String("s"), 1027 TeamDiscussions: String("td"), 1028 VulnerabilityAlerts: String("va"), 1029 Workflows: String("w"), 1030 }, 1031 Repositories: []*Repository{ 1032 { 1033 ID: Int64(1), 1034 URL: String("u"), 1035 Name: String("n"), 1036 }, 1037 }, 1038 } 1039 1040 want := `{ 1041 "token": "t", 1042 "expires_at": ` + referenceTimeStr + `, 1043 "permissions": { 1044 "actions": "a", 1045 "administration": "ad", 1046 "checks": "c", 1047 "contents": "co", 1048 "content_references": "cr", 1049 "deployments": "d", 1050 "environments": "e", 1051 "issues": "i", 1052 "metadata": "md", 1053 "members": "m", 1054 "organization_administration": "oa", 1055 "organization_hooks": "oh", 1056 "organization_plan": "op", 1057 "organization_pre_receive_hooks": "opr", 1058 "organization_projects": "op", 1059 "organization_secrets": "os", 1060 "organization_self_hosted_runners": "osh", 1061 "organization_user_blocking": "oub", 1062 "packages": "pkg", 1063 "pages": "pg", 1064 "pull_requests": "pr", 1065 "repository_hooks": "rh", 1066 "repository_projects": "rp", 1067 "repository_pre_receive_hooks": "rprh", 1068 "secrets": "s", 1069 "secret_scanning_alerts": "ssa", 1070 "security_events": "se", 1071 "single_file": "sf", 1072 "statuses": "s", 1073 "team_discussions": "td", 1074 "vulnerability_alerts": "va", 1075 "workflows": "w" 1076 }, 1077 "repositories": [ 1078 { 1079 "id": 1, 1080 "url": "u", 1081 "name": "n" 1082 } 1083 ] 1084 }` 1085 1086 testJSONMarshal(t, u, want) 1087 } 1088 1089 func TestApp_Marshal(t *testing.T) { 1090 testJSONMarshal(t, &App{}, "{}") 1091 1092 u := &App{ 1093 ID: Int64(1), 1094 Slug: String("s"), 1095 NodeID: String("nid"), 1096 Owner: &User{ 1097 Login: String("l"), 1098 ID: Int64(1), 1099 URL: String("u"), 1100 AvatarURL: String("a"), 1101 GravatarID: String("g"), 1102 Name: String("n"), 1103 Company: String("c"), 1104 Blog: String("b"), 1105 Location: String("l"), 1106 Email: String("e"), 1107 Hireable: Bool(true), 1108 Bio: String("b"), 1109 TwitterUsername: String("t"), 1110 PublicRepos: Int(1), 1111 Followers: Int(1), 1112 Following: Int(1), 1113 CreatedAt: &Timestamp{referenceTime}, 1114 SuspendedAt: &Timestamp{referenceTime}, 1115 }, 1116 Name: String("n"), 1117 Description: String("d"), 1118 ExternalURL: String("eu"), 1119 HTMLURL: String("hu"), 1120 CreatedAt: &Timestamp{referenceTime}, 1121 UpdatedAt: &Timestamp{referenceTime}, 1122 Permissions: &InstallationPermissions{ 1123 Actions: String("a"), 1124 Administration: String("ad"), 1125 Checks: String("c"), 1126 Contents: String("co"), 1127 ContentReferences: String("cr"), 1128 Deployments: String("d"), 1129 Environments: String("e"), 1130 Issues: String("i"), 1131 Metadata: String("md"), 1132 Members: String("m"), 1133 OrganizationAdministration: String("oa"), 1134 OrganizationHooks: String("oh"), 1135 OrganizationPlan: String("op"), 1136 OrganizationPreReceiveHooks: String("opr"), 1137 OrganizationProjects: String("op"), 1138 OrganizationSecrets: String("os"), 1139 OrganizationSelfHostedRunners: String("osh"), 1140 OrganizationUserBlocking: String("oub"), 1141 Packages: String("pkg"), 1142 Pages: String("pg"), 1143 PullRequests: String("pr"), 1144 RepositoryHooks: String("rh"), 1145 RepositoryProjects: String("rp"), 1146 RepositoryPreReceiveHooks: String("rprh"), 1147 Secrets: String("s"), 1148 SecretScanningAlerts: String("ssa"), 1149 SecurityEvents: String("se"), 1150 SingleFile: String("sf"), 1151 Statuses: String("s"), 1152 TeamDiscussions: String("td"), 1153 VulnerabilityAlerts: String("va"), 1154 Workflows: String("w"), 1155 }, 1156 Events: []string{"s"}, 1157 } 1158 1159 want := `{ 1160 "id": 1, 1161 "slug": "s", 1162 "node_id": "nid", 1163 "owner": { 1164 "login": "l", 1165 "id": 1, 1166 "avatar_url": "a", 1167 "gravatar_id": "g", 1168 "name": "n", 1169 "company": "c", 1170 "blog": "b", 1171 "location": "l", 1172 "email": "e", 1173 "hireable": true, 1174 "bio": "b", 1175 "twitter_username": "t", 1176 "public_repos": 1, 1177 "followers": 1, 1178 "following": 1, 1179 "created_at": ` + referenceTimeStr + `, 1180 "suspended_at": ` + referenceTimeStr + `, 1181 "url": "u" 1182 }, 1183 "name": "n", 1184 "description": "d", 1185 "external_url": "eu", 1186 "html_url": "hu", 1187 "created_at": ` + referenceTimeStr + `, 1188 "updated_at": ` + referenceTimeStr + `, 1189 "permissions": { 1190 "actions": "a", 1191 "administration": "ad", 1192 "checks": "c", 1193 "contents": "co", 1194 "content_references": "cr", 1195 "deployments": "d", 1196 "environments": "e", 1197 "issues": "i", 1198 "metadata": "md", 1199 "members": "m", 1200 "organization_administration": "oa", 1201 "organization_hooks": "oh", 1202 "organization_plan": "op", 1203 "organization_pre_receive_hooks": "opr", 1204 "organization_projects": "op", 1205 "organization_secrets": "os", 1206 "organization_self_hosted_runners": "osh", 1207 "organization_user_blocking": "oub", 1208 "packages": "pkg", 1209 "pages": "pg", 1210 "pull_requests": "pr", 1211 "repository_hooks": "rh", 1212 "repository_projects": "rp", 1213 "repository_pre_receive_hooks": "rprh", 1214 "secrets": "s", 1215 "secret_scanning_alerts": "ssa", 1216 "security_events": "se", 1217 "single_file": "sf", 1218 "statuses": "s", 1219 "team_discussions": "td", 1220 "vulnerability_alerts": "va", 1221 "workflows": "w" 1222 }, 1223 "events": ["s"] 1224 }` 1225 1226 testJSONMarshal(t, u, want) 1227 }