github.com/google/go-github/v74@v74.0.0/github/orgs_packages_test.go (about) 1 // Copyright 2021 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 "io" 12 "net/http" 13 "testing" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestOrganizationsService_ListPackages(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 mux.HandleFunc("/orgs/o/packages", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 _, err := io.WriteString(w, `[{ 25 "id": 197, 26 "name": "hello_docker", 27 "package_type": "container", 28 "owner": { 29 "login": "github", 30 "id": 9919, 31 "node_id": "MDEyOk9yZ2FuaXphdGlvbjk5MTk=", 32 "avatar_url": "https://avatars.githubusercontent.com/u/9919?v=4", 33 "gravatar_id": "", 34 "url": "https://api.github.com/users/github", 35 "html_url": "https://github.com/github", 36 "followers_url": "https://api.github.com/users/github/followers", 37 "following_url": "https://api.github.com/users/github/following{/other_user}", 38 "gists_url": "https://api.github.com/users/github/gists{/gist_id}", 39 "starred_url": "https://api.github.com/users/github/starred{/owner}{/repo}", 40 "subscriptions_url": "https://api.github.com/users/github/subscriptions", 41 "organizations_url": "https://api.github.com/users/github/orgs", 42 "repos_url": "https://api.github.com/users/github/repos", 43 "events_url": "https://api.github.com/users/github/events{/privacy}", 44 "received_events_url": "https://api.github.com/users/github/received_events", 45 "type": "Organization", 46 "site_admin": false 47 }, 48 "version_count": 1, 49 "visibility": "private", 50 "url": "https://api.github.com/orgs/github/packages/container/hello_docker", 51 "created_at": `+referenceTimeStr+`, 52 "updated_at": `+referenceTimeStr+`, 53 "html_url": "https://github.com/orgs/github/packages/container/package/hello_docker" 54 } 55 ]`) 56 if err != nil { 57 t.Fatal("Failed to write test response: ", err) 58 } 59 }) 60 61 ctx := context.Background() 62 packages, _, err := client.Organizations.ListPackages(ctx, "o", &PackageListOptions{}) 63 if err != nil { 64 t.Errorf("Organizations.ListPackages returned error: %v", err) 65 } 66 67 want := []*Package{{ 68 ID: Ptr(int64(197)), 69 Name: Ptr("hello_docker"), 70 PackageType: Ptr("container"), 71 VersionCount: Ptr(int64(1)), 72 Visibility: Ptr("private"), 73 URL: Ptr("https://api.github.com/orgs/github/packages/container/hello_docker"), 74 HTMLURL: Ptr("https://github.com/orgs/github/packages/container/package/hello_docker"), 75 CreatedAt: &Timestamp{referenceTime}, 76 UpdatedAt: &Timestamp{referenceTime}, 77 Owner: &User{ 78 Login: Ptr("github"), 79 ID: Ptr(int64(9919)), 80 NodeID: Ptr("MDEyOk9yZ2FuaXphdGlvbjk5MTk="), 81 AvatarURL: Ptr("https://avatars.githubusercontent.com/u/9919?v=4"), 82 GravatarID: Ptr(""), 83 URL: Ptr("https://api.github.com/users/github"), 84 HTMLURL: Ptr("https://github.com/github"), 85 FollowersURL: Ptr("https://api.github.com/users/github/followers"), 86 FollowingURL: Ptr("https://api.github.com/users/github/following{/other_user}"), 87 GistsURL: Ptr("https://api.github.com/users/github/gists{/gist_id}"), 88 StarredURL: Ptr("https://api.github.com/users/github/starred{/owner}{/repo}"), 89 SubscriptionsURL: Ptr("https://api.github.com/users/github/subscriptions"), 90 OrganizationsURL: Ptr("https://api.github.com/users/github/orgs"), 91 ReposURL: Ptr("https://api.github.com/users/github/repos"), 92 EventsURL: Ptr("https://api.github.com/users/github/events{/privacy}"), 93 ReceivedEventsURL: Ptr("https://api.github.com/users/github/received_events"), 94 Type: Ptr("Organization"), 95 SiteAdmin: Ptr(false), 96 }, 97 }} 98 if !cmp.Equal(packages, want) { 99 t.Errorf("Organizations.ListPackages returned %+v, want %+v", packages, want) 100 } 101 102 const methodName = "ListPackages" 103 testBadOptions(t, methodName, func() (err error) { 104 _, _, err = client.Organizations.ListPackages(ctx, "\n", &PackageListOptions{}) 105 return err 106 }) 107 108 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 109 got, resp, err := client.Organizations.ListPackages(ctx, "o", &PackageListOptions{}) 110 if got != nil { 111 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 112 } 113 return resp, err 114 }) 115 } 116 117 func TestOrganizationsService_GetPackage(t *testing.T) { 118 t.Parallel() 119 client, mux, _ := setup(t) 120 121 // don't url escape the package name here since mux will convert it to a slash automatically 122 mux.HandleFunc("/orgs/o/packages/container/hello%2fhello_docker", func(w http.ResponseWriter, r *http.Request) { 123 testMethod(t, r, "GET") 124 _, err := io.WriteString(w, `{ 125 "id": 197, 126 "name": "hello/hello_docker", 127 "package_type": "container", 128 "version_count": 1, 129 "visibility": "private", 130 "url": "https://api.github.com/orgs/github/packages/container/hello%2Fhello_docker", 131 "created_at": `+referenceTimeStr+`, 132 "updated_at": `+referenceTimeStr+`, 133 "html_url": "https://github.com/orgs/github/packages/container/package/hello%2Fhello_docker" 134 }`) 135 if err != nil { 136 t.Fatal("Failed to write test response: ", err) 137 } 138 }) 139 140 ctx := context.Background() 141 packages, _, err := client.Organizations.GetPackage(ctx, "o", "container", "hello/hello_docker") 142 if err != nil { 143 t.Errorf("Organizations.GetPackage returned error: %v", err) 144 } 145 146 want := &Package{ 147 ID: Ptr(int64(197)), 148 Name: Ptr("hello/hello_docker"), 149 PackageType: Ptr("container"), 150 VersionCount: Ptr(int64(1)), 151 Visibility: Ptr("private"), 152 URL: Ptr("https://api.github.com/orgs/github/packages/container/hello%2Fhello_docker"), 153 HTMLURL: Ptr("https://github.com/orgs/github/packages/container/package/hello%2Fhello_docker"), 154 CreatedAt: &Timestamp{referenceTime}, 155 UpdatedAt: &Timestamp{referenceTime}, 156 } 157 if !cmp.Equal(packages, want) { 158 t.Errorf("Organizations.GetPackage returned %+v, want %+v", packages, want) 159 } 160 161 const methodName = "GetPackage" 162 testBadOptions(t, methodName, func() (err error) { 163 _, _, err = client.Organizations.GetPackage(ctx, "\n", "", "") 164 return err 165 }) 166 167 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 168 got, resp, err := client.Organizations.GetPackage(ctx, "", "", "") 169 if got != nil { 170 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 171 } 172 return resp, err 173 }) 174 } 175 176 func TestOrganizationsService_DeletePackage(t *testing.T) { 177 t.Parallel() 178 client, mux, _ := setup(t) 179 180 // don't url escape the package name here since mux will convert it to a slash automatically 181 mux.HandleFunc("/orgs/o/packages/container/hello%2fhello_docker", func(_ http.ResponseWriter, r *http.Request) { 182 testMethod(t, r, "DELETE") 183 }) 184 185 ctx := context.Background() 186 _, err := client.Organizations.DeletePackage(ctx, "o", "container", "hello/hello_docker") 187 if err != nil { 188 t.Errorf("Organizations.DeletePackage returned error: %v", err) 189 } 190 191 const methodName = "DeletePackage" 192 testBadOptions(t, methodName, func() (err error) { 193 _, _, err = client.Organizations.GetPackage(ctx, "\n", "\n", "\n") 194 return err 195 }) 196 197 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 198 got, resp, err := client.Organizations.GetPackage(ctx, "", "", "") 199 if got != nil { 200 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 201 } 202 return resp, err 203 }) 204 } 205 206 func TestOrganizationsService_RestorePackage(t *testing.T) { 207 t.Parallel() 208 client, mux, _ := setup(t) 209 210 // don't url escape the package name here since mux will convert it to a slash automatically 211 mux.HandleFunc("/orgs/o/packages/container/hello%2Fhello_docker/restore", func(_ http.ResponseWriter, r *http.Request) { 212 testMethod(t, r, "POST") 213 }) 214 215 ctx := context.Background() 216 _, err := client.Organizations.RestorePackage(ctx, "o", "container", "hello/hello_docker") 217 if err != nil { 218 t.Errorf("Organizations.RestorePackage returned error: %v", err) 219 } 220 221 const methodName = "RestorePackage" 222 testBadOptions(t, methodName, func() (err error) { 223 _, err = client.Organizations.RestorePackage(ctx, "\n", "", "") 224 return err 225 }) 226 227 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 228 return client.Organizations.RestorePackage(ctx, "", "container", "hello/hello_docker") 229 }) 230 } 231 232 func TestOrganizationsService_ListPackagesVersions(t *testing.T) { 233 t.Parallel() 234 client, mux, _ := setup(t) 235 236 m := `{ 237 "package_type": "container", 238 "container": { 239 "tags": [ 240 "latest" 241 ] 242 } 243 }` 244 245 // don't url escape the package name here since mux will convert it to a slash automatically 246 mux.HandleFunc("/orgs/o/packages/container/hello%2Fhello_docker/versions", func(w http.ResponseWriter, r *http.Request) { 247 testMethod(t, r, "GET") 248 testFormValues(t, r, values{"per_page": "2", "page": "1", "state": "deleted", "visibility": "internal", "package_type": "container"}) 249 _, err := io.WriteString(w, `[ 250 { 251 "id": 45763, 252 "name": "sha256:08a44bab0bddaddd8837a8b381aebc2e4b933768b981685a9e088360af0d3dd9", 253 "url": "https://api.github.com/users/octocat/packages/container/hello%2Fhello_docker/versions/45763", 254 "package_html_url": "https://github.com/users/octocat/packages/container/package/hello%2Fhello_docker", 255 "created_at": `+referenceTimeStr+`, 256 "updated_at": `+referenceTimeStr+`, 257 "html_url": "https://github.com/users/octocat/packages/container/hello%2Fhello_docker/45763", 258 "metadata": `+m+` 259 }]`) 260 if err != nil { 261 t.Fatal("Failed to write test response: ", err) 262 } 263 }) 264 265 ctx := context.Background() 266 opts := &PackageListOptions{ 267 Ptr("internal"), Ptr("container"), Ptr("deleted"), ListOptions{Page: 1, PerPage: 2}, 268 } 269 packages, _, err := client.Organizations.PackageGetAllVersions(ctx, "o", "container", "hello/hello_docker", opts) 270 if err != nil { 271 t.Errorf("Organizations.PackageGetAllVersions returned error: %v", err) 272 } 273 274 want := []*PackageVersion{{ 275 ID: Ptr(int64(45763)), 276 Name: Ptr("sha256:08a44bab0bddaddd8837a8b381aebc2e4b933768b981685a9e088360af0d3dd9"), 277 URL: Ptr("https://api.github.com/users/octocat/packages/container/hello%2Fhello_docker/versions/45763"), 278 PackageHTMLURL: Ptr("https://github.com/users/octocat/packages/container/package/hello%2Fhello_docker"), 279 CreatedAt: &Timestamp{referenceTime}, 280 UpdatedAt: &Timestamp{referenceTime}, 281 HTMLURL: Ptr("https://github.com/users/octocat/packages/container/hello%2Fhello_docker/45763"), 282 Metadata: json.RawMessage(m), 283 }} 284 if !cmp.Equal(packages, want) { 285 t.Errorf("Organizations.PackageGetAllVersions returned %+v, want %+v", packages, want) 286 } 287 288 const methodName = "PackageGetAllVersions" 289 testBadOptions(t, methodName, func() (err error) { 290 _, _, err = client.Organizations.PackageGetAllVersions(ctx, "\n", "", "", &PackageListOptions{}) 291 return err 292 }) 293 294 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 295 got, resp, err := client.Organizations.PackageGetAllVersions(ctx, "", "", "", &PackageListOptions{}) 296 if got != nil { 297 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 298 } 299 return resp, err 300 }) 301 } 302 303 func TestOrganizationsService_PackageGetVersion(t *testing.T) { 304 t.Parallel() 305 client, mux, _ := setup(t) 306 307 m := `{ 308 "package_type": "container", 309 "container": { 310 "tags": [ 311 "latest" 312 ] 313 } 314 }` 315 316 // don't url escape the package name here since mux will convert it to a slash automatically 317 mux.HandleFunc("/orgs/o/packages/container/hello%2Fhello_docker/versions/45763", func(w http.ResponseWriter, r *http.Request) { 318 testMethod(t, r, "GET") 319 _, err := io.WriteString(w, ` 320 { 321 "id": 45763, 322 "name": "sha256:08a44bab0bddaddd8837a8b381aebc2e4b933768b981685a9e088360af0d3dd9", 323 "url": "https://api.github.com/users/octocat/packages/container/hello%2Fhello_docker/versions/45763", 324 "package_html_url": "https://github.com/users/octocat/packages/container/package/hello%2Fhello_docker", 325 "created_at": `+referenceTimeStr+`, 326 "updated_at": `+referenceTimeStr+`, 327 "html_url": "https://github.com/users/octocat/packages/container/hello%2Fhello_docker/45763", 328 "metadata": `+m+` 329 }`) 330 if err != nil { 331 t.Fatal("Failed to write test response: ", err) 332 } 333 }) 334 335 ctx := context.Background() 336 packages, _, err := client.Organizations.PackageGetVersion(ctx, "o", "container", "hello/hello_docker", 45763) 337 if err != nil { 338 t.Errorf("Organizations.PackageGetVersion returned error: %v", err) 339 } 340 341 want := &PackageVersion{ 342 ID: Ptr(int64(45763)), 343 Name: Ptr("sha256:08a44bab0bddaddd8837a8b381aebc2e4b933768b981685a9e088360af0d3dd9"), 344 URL: Ptr("https://api.github.com/users/octocat/packages/container/hello%2Fhello_docker/versions/45763"), 345 PackageHTMLURL: Ptr("https://github.com/users/octocat/packages/container/package/hello%2Fhello_docker"), 346 CreatedAt: &Timestamp{referenceTime}, 347 UpdatedAt: &Timestamp{referenceTime}, 348 HTMLURL: Ptr("https://github.com/users/octocat/packages/container/hello%2Fhello_docker/45763"), 349 Metadata: json.RawMessage(m), 350 } 351 if !cmp.Equal(packages, want) { 352 t.Errorf("Organizations.PackageGetVersion returned %+v, want %+v", packages, want) 353 } 354 355 const methodName = "PackageGetVersion" 356 testBadOptions(t, methodName, func() (err error) { 357 _, _, err = client.Organizations.PackageGetVersion(ctx, "\n", "", "", 0) 358 return err 359 }) 360 361 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 362 got, resp, err := client.Organizations.PackageGetVersion(ctx, "", "", "", 45763) 363 if got != nil { 364 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 365 } 366 return resp, err 367 }) 368 } 369 370 func TestOrganizationsService_PackageDeleteVersion(t *testing.T) { 371 t.Parallel() 372 client, mux, _ := setup(t) 373 374 // don't url escape the package name here since mux will convert it to a slash automatically 375 mux.HandleFunc("/orgs/o/packages/container/hello%2Fhello_docker/versions/45763", func(_ http.ResponseWriter, r *http.Request) { 376 testMethod(t, r, "DELETE") 377 }) 378 379 ctx := context.Background() 380 _, err := client.Organizations.PackageDeleteVersion(ctx, "o", "container", "hello/hello_docker", 45763) 381 if err != nil { 382 t.Errorf("Organizations.PackageDeleteVersion returned error: %v", err) 383 } 384 385 const methodName = "PackageDeleteVersion" 386 testBadOptions(t, methodName, func() (err error) { 387 _, err = client.Organizations.PackageDeleteVersion(ctx, "\n", "", "", 0) 388 return err 389 }) 390 391 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 392 return client.Organizations.PackageDeleteVersion(ctx, "", "", "", 45763) 393 }) 394 } 395 396 func TestOrganizationsService_PackageRestoreVersion(t *testing.T) { 397 t.Parallel() 398 client, mux, _ := setup(t) 399 400 // don't url escape the package name here since mux will convert it to a slash automatically 401 mux.HandleFunc("/orgs/o/packages/container/hello%2Fhello_docker/versions/45763/restore", func(_ http.ResponseWriter, r *http.Request) { 402 testMethod(t, r, "POST") 403 }) 404 405 ctx := context.Background() 406 _, err := client.Organizations.PackageRestoreVersion(ctx, "o", "container", "hello/hello_docker", 45763) 407 if err != nil { 408 t.Errorf("Organizations.PackageRestoreVersion returned error: %v", err) 409 } 410 411 const methodName = "PackageRestoreVersion" 412 testBadOptions(t, methodName, func() (err error) { 413 _, err = client.Organizations.PackageRestoreVersion(ctx, "\n", "", "", 0) 414 return err 415 }) 416 417 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 418 return client.Organizations.PackageRestoreVersion(ctx, "", "", "", 45763) 419 }) 420 }