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