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