|
| 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_ListAppInstallableOrganizations(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + client, mux, _ := setup(t) |
| 19 | + |
| 20 | + mux.HandleFunc("/enterprises/e/apps/installable_organizations", func(w http.ResponseWriter, r *http.Request) { |
| 21 | + testMethod(t, r, "GET") |
| 22 | + fmt.Fprint(w, `[{"id":1, "login":"org1"}]`) |
| 23 | + }) |
| 24 | + |
| 25 | + ctx := t.Context() |
| 26 | + opts := &ListOptions{Page: 1, PerPage: 10} |
| 27 | + got, _, err := client.Enterprise.ListAppInstallableOrganizations(ctx, "e", opts) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Enterprise.ListAppInstallableOrganizations returned error: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + want := []*InstallableOrganization{ |
| 33 | + {ID: int64(1), Login: "org1"}, |
| 34 | + } |
| 35 | + |
| 36 | + if !cmp.Equal(got, want) { |
| 37 | + t.Errorf("Enterprise.ListAppInstallableOrganizations = %+v, want %+v", got, want) |
| 38 | + } |
| 39 | + |
| 40 | + const methodName = "ListAppInstallableOrganizations" |
| 41 | + testBadOptions(t, methodName, func() error { |
| 42 | + _, _, err := client.Enterprise.ListAppInstallableOrganizations(ctx, "\n", opts) |
| 43 | + return err |
| 44 | + }) |
| 45 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 46 | + got, resp, err := client.Enterprise.ListAppInstallableOrganizations(ctx, "e", nil) |
| 47 | + if got != nil { |
| 48 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 49 | + } |
| 50 | + return resp, err |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func TestEnterpriseService_ListAppAccessibleOrganizationRepositories(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + client, mux, _ := setup(t) |
| 57 | + |
| 58 | + mux.HandleFunc("/enterprises/e/apps/installable_organizations/org1/accessible_repositories", func(w http.ResponseWriter, r *http.Request) { |
| 59 | + testMethod(t, r, "GET") |
| 60 | + fmt.Fprint(w, `[{"id":10, "name":"repo1", "full_name":"org1/repo1"}]`) |
| 61 | + }) |
| 62 | + |
| 63 | + opts := &ListOptions{Page: 2, PerPage: 2} |
| 64 | + ctx := t.Context() |
| 65 | + repos, _, err := client.Enterprise.ListAppAccessibleOrganizationRepositories(ctx, "e", "org1", opts) |
| 66 | + if err != nil { |
| 67 | + t.Errorf("Enterprise.ListAppAccessibleOrganizationRepositories returned error: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + want := []*AccessibleRepository{ |
| 71 | + {ID: int64(10), Name: "repo1", FullName: "org1/repo1"}, |
| 72 | + } |
| 73 | + |
| 74 | + if !cmp.Equal(repos, want) { |
| 75 | + t.Errorf("Enterprise.ListAppAccessibleOrganizationRepositories returned %+v, want %+v", repos, want) |
| 76 | + } |
| 77 | + |
| 78 | + const methodName = "ListAppAccessibleOrganizationRepositories" |
| 79 | + |
| 80 | + testBadOptions(t, methodName, func() (err error) { |
| 81 | + _, _, err = client.Enterprise.ListAppAccessibleOrganizationRepositories(ctx, "\n", "org1", opts) |
| 82 | + return err |
| 83 | + }) |
| 84 | + |
| 85 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 86 | + got, resp, err := client.Enterprise.ListAppAccessibleOrganizationRepositories(ctx, "e", "org1", nil) |
| 87 | + if got != nil { |
| 88 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 89 | + } |
| 90 | + return resp, err |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +func TestEnterpriseService_ListAppInstallations(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + client, mux, _ := setup(t) |
| 97 | + |
| 98 | + mux.HandleFunc("/enterprises/e/apps/organizations/org1/installations", func(w http.ResponseWriter, r *http.Request) { |
| 99 | + testMethod(t, r, "GET") |
| 100 | + testFormValues(t, r, values{"per_page": "2", "page": "2"}) |
| 101 | + fmt.Fprint(w, `[{"id":99}]`) |
| 102 | + }) |
| 103 | + |
| 104 | + opts := &ListOptions{Page: 2, PerPage: 2} |
| 105 | + ctx := t.Context() |
| 106 | + installations, _, err := client.Enterprise.ListAppInstallations(ctx, "e", "org1", opts) |
| 107 | + if err != nil { |
| 108 | + t.Errorf("ListAppInstallations returned error: %v", err) |
| 109 | + } |
| 110 | + want := []*Installation{ |
| 111 | + {ID: Ptr(int64(99))}, |
| 112 | + } |
| 113 | + |
| 114 | + if !cmp.Equal(installations, want) { |
| 115 | + t.Errorf("ListAppInstallations returned %+v, want %+v", installations, want) |
| 116 | + } |
| 117 | + |
| 118 | + const methodName = "ListAppInstallations" |
| 119 | + |
| 120 | + testBadOptions(t, methodName, func() (err error) { |
| 121 | + _, _, err = client.Enterprise.ListAppInstallations(ctx, "\n", "org1", &ListOptions{}) |
| 122 | + return err |
| 123 | + }) |
| 124 | + |
| 125 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 126 | + got, resp, err := client.Enterprise.ListAppInstallations(ctx, "e", "org1", nil) |
| 127 | + if got != nil { |
| 128 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 129 | + } |
| 130 | + return resp, err |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +func TestEnterpriseService_InstallApp(t *testing.T) { |
| 135 | + t.Parallel() |
| 136 | + client, mux, _ := setup(t) |
| 137 | + |
| 138 | + mux.HandleFunc("/enterprises/e/apps/organizations/org1/installations", func(w http.ResponseWriter, r *http.Request) { |
| 139 | + testMethod(t, r, "POST") |
| 140 | + testBody(t, r, `{"client_id":"cid","repository_selection":"selected","repositories":["r1","r2"]}`+"\n") |
| 141 | + fmt.Fprint(w, `{"id":555}`) |
| 142 | + }) |
| 143 | + |
| 144 | + req := InstallAppRequest{ |
| 145 | + ClientID: "cid", |
| 146 | + RepositorySelection: "selected", |
| 147 | + Repositories: []string{"r1", "r2"}, |
| 148 | + } |
| 149 | + |
| 150 | + ctx := t.Context() |
| 151 | + installation, _, err := client.Enterprise.InstallApp(ctx, "e", "org1", req) |
| 152 | + if err != nil { |
| 153 | + t.Errorf("InstallApp returned error: %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + want := &Installation{ID: Ptr(int64(555))} |
| 157 | + |
| 158 | + if !cmp.Equal(installation, want) { |
| 159 | + t.Errorf("InstallApp returned %+v, want %+v", installation, want) |
| 160 | + } |
| 161 | + |
| 162 | + const methodName = "InstallApp" |
| 163 | + |
| 164 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 165 | + got, resp, err := client.Enterprise.InstallApp(ctx, "e", "org1", req) |
| 166 | + if got != nil { |
| 167 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 168 | + } |
| 169 | + return resp, err |
| 170 | + }) |
| 171 | +} |
| 172 | + |
| 173 | +func TestEnterpriseService_UninstallApp(t *testing.T) { |
| 174 | + t.Parallel() |
| 175 | + client, mux, _ := setup(t) |
| 176 | + |
| 177 | + mux.HandleFunc("/enterprises/e/apps/organizations/org1/installations/123", func(w http.ResponseWriter, r *http.Request) { |
| 178 | + testMethod(t, r, "DELETE") |
| 179 | + w.WriteHeader(http.StatusNoContent) |
| 180 | + }) |
| 181 | + |
| 182 | + ctx := t.Context() |
| 183 | + resp, err := client.Enterprise.UninstallApp(ctx, "e", "org1", 123) |
| 184 | + if err != nil { |
| 185 | + t.Errorf("UninstallApp returned error: %v", err) |
| 186 | + } |
| 187 | + |
| 188 | + if resp.StatusCode != http.StatusNoContent { |
| 189 | + t.Errorf("UninstallApp returned status %v, want %v", resp.StatusCode, http.StatusNoContent) |
| 190 | + } |
| 191 | + |
| 192 | + const methodName = "UninstallApp" |
| 193 | + |
| 194 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 195 | + return client.Enterprise.UninstallApp(ctx, "e", "org1", 123) |
| 196 | + }) |
| 197 | +} |
0 commit comments