...

Source file src/github.com/google/go-github/v45/github/orgs_packages_test.go

Documentation: github.com/google/go-github/v45/github

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

View as plain text