|
| 1 | +// Copyright 2025 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 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/google/go-cmp/cmp" |
| 14 | +) |
| 15 | + |
| 16 | +func TestEnterpriseService_ListRepositoriesForOrgAppInstallation(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + client, mux, _ := setup(t) |
| 19 | + |
| 20 | + mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories", func(w http.ResponseWriter, r *http.Request) { |
| 21 | + testMethod(t, r, "GET") |
| 22 | + testFormValues(t, r, values{"page": "1"}) |
| 23 | + fmt.Fprint(w, `[{"id":1}]`) |
| 24 | + }) |
| 25 | + |
| 26 | + ctx := t.Context() |
| 27 | + repos, _, err := client.Enterprise.ListRepositoriesForOrgAppInstallation(ctx, "e", "o", 1, &ListOptions{Page: 1}) |
| 28 | + if err != nil { |
| 29 | + t.Errorf("Enterprise.ListRepositoriesForOrgAppInstallation returned error: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + want := []*AccessibleRepository{{ID: 1}} |
| 33 | + if diff := cmp.Diff(repos, want); diff != "" { |
| 34 | + t.Errorf("Enterprise.ListRepositoriesForOrgAppInstallation returned diff (-want +got):\n%v", diff) |
| 35 | + } |
| 36 | + |
| 37 | + const methodName = "ListRepositoriesForOrgAppInstallation" |
| 38 | + testBadOptions(t, methodName, func() (err error) { |
| 39 | + _, _, err = client.Enterprise.ListRepositoriesForOrgAppInstallation(ctx, "\n", "\n", -1, &ListOptions{}) |
| 40 | + return err |
| 41 | + }) |
| 42 | + |
| 43 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 44 | + _, resp, err := client.Enterprise.ListRepositoriesForOrgAppInstallation(ctx, "e", "o", 1, &ListOptions{}) |
| 45 | + return resp, err |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func TestEnterpriseService_UpdateAppInstallationRepositories(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + client, mux, _ := setup(t) |
| 52 | + |
| 53 | + input := UpdateAppInstallationRepositoriesOptions{ |
| 54 | + RepositorySelection: String("selected"), |
| 55 | + SelectedRepositoryIDs: []int64{1, 2}, |
| 56 | + } |
| 57 | + |
| 58 | + mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories", func(w http.ResponseWriter, r *http.Request) { |
| 59 | + testMethod(t, r, "PATCH") |
| 60 | + testBody(t, r, `{"repository_selection":"selected","selected_repository_ids":[1,2]}`+"\n") |
| 61 | + fmt.Fprint(w, `{"id":1, "repository_selection":"selected"}`) |
| 62 | + }) |
| 63 | + |
| 64 | + ctx := t.Context() |
| 65 | + inst, _, err := client.Enterprise.UpdateAppInstallationRepositories(ctx, "e", "o", 1, input) |
| 66 | + if err != nil { |
| 67 | + t.Errorf("Enterprise.UpdateAppInstallationRepositories returned error: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + want := &Installation{ID: Ptr(int64(1)), RepositorySelection: Ptr("selected")} |
| 71 | + if diff := cmp.Diff(inst, want); diff != "" { |
| 72 | + t.Errorf("Enterprise.UpdateAppInstallationRepositories returned diff (-want +got):\n%v", diff) |
| 73 | + } |
| 74 | + |
| 75 | + const methodName = "UpdateAppInstallationRepositories" |
| 76 | + testBadOptions(t, methodName, func() (err error) { |
| 77 | + _, _, err = client.Enterprise.UpdateAppInstallationRepositories(ctx, "\n", "\n", -1, input) |
| 78 | + return err |
| 79 | + }) |
| 80 | + |
| 81 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 82 | + _, resp, err := client.Enterprise.UpdateAppInstallationRepositories(ctx, "e", "o", 1, input) |
| 83 | + return resp, err |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +func TestEnterpriseService_AddRepositoriesToAppInstallation(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + client, mux, _ := setup(t) |
| 90 | + |
| 91 | + input := AppInstallationRepositoriesOptions{SelectedRepositoryIDs: []int64{1, 2}} |
| 92 | + |
| 93 | + mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories/add", func(w http.ResponseWriter, r *http.Request) { |
| 94 | + testMethod(t, r, "PATCH") |
| 95 | + testBody(t, r, `{"selected_repository_ids":[1,2]}`+"\n") |
| 96 | + fmt.Fprint(w, `[{"id":1},{"id":2}]`) |
| 97 | + }) |
| 98 | + |
| 99 | + ctx := t.Context() |
| 100 | + repos, _, err := client.Enterprise.AddRepositoriesToAppInstallation(ctx, "e", "o", 1, input) |
| 101 | + if err != nil { |
| 102 | + t.Errorf("Enterprise.AddRepositoriesToAppInstallation returned error: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + want := []*AccessibleRepository{{ID: 1}, {ID: 2}} |
| 106 | + if diff := cmp.Diff(repos, want); diff != "" { |
| 107 | + t.Errorf("Enterprise.AddRepositoriesToAppInstallation returned diff (-want +got):\n%v", diff) |
| 108 | + } |
| 109 | + |
| 110 | + const methodName = "AddRepositoriesToAppInstallation" |
| 111 | + testBadOptions(t, methodName, func() (err error) { |
| 112 | + _, _, err = client.Enterprise.AddRepositoriesToAppInstallation(ctx, "\n", "\n", -1, input) |
| 113 | + return err |
| 114 | + }) |
| 115 | + |
| 116 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 117 | + _, resp, err := client.Enterprise.AddRepositoriesToAppInstallation(ctx, "e", "o", 1, input) |
| 118 | + return resp, err |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func TestEnterpriseService_RemoveRepositoriesFromAppInstallation(t *testing.T) { |
| 123 | + t.Parallel() |
| 124 | + client, mux, _ := setup(t) |
| 125 | + |
| 126 | + input := AppInstallationRepositoriesOptions{SelectedRepositoryIDs: []int64{1, 2}} |
| 127 | + |
| 128 | + mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories/remove", func(w http.ResponseWriter, r *http.Request) { |
| 129 | + testMethod(t, r, "PATCH") |
| 130 | + testBody(t, r, `{"selected_repository_ids":[1,2]}`+"\n") |
| 131 | + fmt.Fprint(w, `[{"id":1},{"id":2}]`) |
| 132 | + }) |
| 133 | + |
| 134 | + ctx := t.Context() |
| 135 | + repos, _, err := client.Enterprise.RemoveRepositoriesFromAppInstallation(ctx, "e", "o", 1, input) |
| 136 | + if err != nil { |
| 137 | + t.Errorf("Enterprise.RemoveRepositoriesFromAppInstallation returned error: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + want := []*AccessibleRepository{{ID: 1}, {ID: 2}} |
| 141 | + if diff := cmp.Diff(repos, want); diff != "" { |
| 142 | + t.Errorf("Enterprise.RemoveRepositoriesFromAppInstallation returned diff (-want +got):\n%v", diff) |
| 143 | + } |
| 144 | + |
| 145 | + const methodName = "RemoveRepositoriesFromAppInstallation" |
| 146 | + testBadOptions(t, methodName, func() (err error) { |
| 147 | + _, _, err = client.Enterprise.RemoveRepositoriesFromAppInstallation(ctx, "\n", "\n", -1, input) |
| 148 | + return err |
| 149 | + }) |
| 150 | + |
| 151 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 152 | + _, resp, err := client.Enterprise.RemoveRepositoriesFromAppInstallation(ctx, "e", "o", 1, input) |
| 153 | + return resp, err |
| 154 | + }) |
| 155 | +} |
0 commit comments