github.com/google/go-github/v68@v68.0.0/github/orgs_test.go (about)

     1  // Copyright 2013 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  	"fmt"
    12  	"net/http"
    13  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestOrganization_Marshal(t *testing.T) {
    19  	t.Parallel()
    20  	testJSONMarshal(t, &Organization{}, "{}")
    21  
    22  	o := &Organization{
    23  		BillingEmail:                         Ptr("support@github.com"),
    24  		Blog:                                 Ptr("https://github.com/blog"),
    25  		Company:                              Ptr("GitHub"),
    26  		Email:                                Ptr("support@github.com"),
    27  		TwitterUsername:                      Ptr("github"),
    28  		Location:                             Ptr("San Francisco"),
    29  		Name:                                 Ptr("github"),
    30  		Description:                          Ptr("GitHub, the company."),
    31  		IsVerified:                           Ptr(true),
    32  		HasOrganizationProjects:              Ptr(true),
    33  		HasRepositoryProjects:                Ptr(true),
    34  		DefaultRepoPermission:                Ptr("read"),
    35  		MembersCanCreateRepos:                Ptr(true),
    36  		MembersCanCreateInternalRepos:        Ptr(true),
    37  		MembersCanCreatePrivateRepos:         Ptr(true),
    38  		MembersCanCreatePublicRepos:          Ptr(false),
    39  		MembersAllowedRepositoryCreationType: Ptr("all"),
    40  		MembersCanCreatePages:                Ptr(true),
    41  		MembersCanCreatePublicPages:          Ptr(false),
    42  		MembersCanCreatePrivatePages:         Ptr(true),
    43  	}
    44  	want := `
    45  		{
    46  			"billing_email": "support@github.com",
    47  			"blog": "https://github.com/blog",
    48  			"company": "GitHub",
    49  			"email": "support@github.com",
    50  			"twitter_username": "github",
    51  			"location": "San Francisco",
    52  			"name": "github",
    53  			"description": "GitHub, the company.",
    54  			"is_verified": true,
    55  			"has_organization_projects": true,
    56  			"has_repository_projects": true,
    57  			"default_repository_permission": "read",
    58  			"members_can_create_repositories": true,
    59  			"members_can_create_public_repositories": false,
    60  			"members_can_create_private_repositories": true,
    61  			"members_can_create_internal_repositories": true,
    62  			"members_allowed_repository_creation_type": "all",
    63  			"members_can_create_pages": true,
    64  			"members_can_create_public_pages": false,
    65  			"members_can_create_private_pages": true
    66  		}
    67  	`
    68  	testJSONMarshal(t, o, want)
    69  }
    70  
    71  func TestOrganizationsService_ListAll(t *testing.T) {
    72  	t.Parallel()
    73  	client, mux, _ := setup(t)
    74  
    75  	since := int64(1342004)
    76  	mux.HandleFunc("/organizations", func(w http.ResponseWriter, r *http.Request) {
    77  		testMethod(t, r, "GET")
    78  		testFormValues(t, r, values{"since": "1342004"})
    79  		fmt.Fprint(w, `[{"id":4314092}]`)
    80  	})
    81  
    82  	opt := &OrganizationsListOptions{Since: since}
    83  	ctx := context.Background()
    84  	orgs, _, err := client.Organizations.ListAll(ctx, opt)
    85  	if err != nil {
    86  		t.Errorf("Organizations.ListAll returned error: %v", err)
    87  	}
    88  
    89  	want := []*Organization{{ID: Ptr(int64(4314092))}}
    90  	if !cmp.Equal(orgs, want) {
    91  		t.Errorf("Organizations.ListAll returned %+v, want %+v", orgs, want)
    92  	}
    93  
    94  	const methodName = "ListAll"
    95  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    96  		got, resp, err := client.Organizations.ListAll(ctx, opt)
    97  		if got != nil {
    98  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    99  		}
   100  		return resp, err
   101  	})
   102  }
   103  
   104  func TestOrganizationsService_List_authenticatedUser(t *testing.T) {
   105  	t.Parallel()
   106  	client, mux, _ := setup(t)
   107  
   108  	mux.HandleFunc("/user/orgs", func(w http.ResponseWriter, r *http.Request) {
   109  		testMethod(t, r, "GET")
   110  		fmt.Fprint(w, `[{"id":1},{"id":2}]`)
   111  	})
   112  
   113  	ctx := context.Background()
   114  	orgs, _, err := client.Organizations.List(ctx, "", nil)
   115  	if err != nil {
   116  		t.Errorf("Organizations.List returned error: %v", err)
   117  	}
   118  
   119  	want := []*Organization{{ID: Ptr(int64(1))}, {ID: Ptr(int64(2))}}
   120  	if !cmp.Equal(orgs, want) {
   121  		t.Errorf("Organizations.List returned %+v, want %+v", orgs, want)
   122  	}
   123  
   124  	const methodName = "List"
   125  	testBadOptions(t, methodName, func() (err error) {
   126  		_, _, err = client.Organizations.List(ctx, "\n", nil)
   127  		return err
   128  	})
   129  
   130  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   131  		got, resp, err := client.Organizations.List(ctx, "", nil)
   132  		if got != nil {
   133  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   134  		}
   135  		return resp, err
   136  	})
   137  }
   138  
   139  func TestOrganizationsService_List_specifiedUser(t *testing.T) {
   140  	t.Parallel()
   141  	client, mux, _ := setup(t)
   142  
   143  	mux.HandleFunc("/users/u/orgs", func(w http.ResponseWriter, r *http.Request) {
   144  		testMethod(t, r, "GET")
   145  		testFormValues(t, r, values{"page": "2"})
   146  		fmt.Fprint(w, `[{"id":1},{"id":2}]`)
   147  	})
   148  
   149  	opt := &ListOptions{Page: 2}
   150  	ctx := context.Background()
   151  	orgs, _, err := client.Organizations.List(ctx, "u", opt)
   152  	if err != nil {
   153  		t.Errorf("Organizations.List returned error: %v", err)
   154  	}
   155  
   156  	want := []*Organization{{ID: Ptr(int64(1))}, {ID: Ptr(int64(2))}}
   157  	if !cmp.Equal(orgs, want) {
   158  		t.Errorf("Organizations.List returned %+v, want %+v", orgs, want)
   159  	}
   160  
   161  	const methodName = "List"
   162  	testBadOptions(t, methodName, func() (err error) {
   163  		_, _, err = client.Organizations.List(ctx, "\n", opt)
   164  		return err
   165  	})
   166  
   167  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   168  		got, resp, err := client.Organizations.List(ctx, "u", opt)
   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_List_invalidUser(t *testing.T) {
   177  	t.Parallel()
   178  	client, _, _ := setup(t)
   179  
   180  	ctx := context.Background()
   181  	_, _, err := client.Organizations.List(ctx, "%", nil)
   182  	testURLParseError(t, err)
   183  }
   184  
   185  func TestOrganizationsService_Get(t *testing.T) {
   186  	t.Parallel()
   187  	client, mux, _ := setup(t)
   188  
   189  	mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
   190  		testMethod(t, r, "GET")
   191  		testHeader(t, r, "Accept", mediaTypeMemberAllowedRepoCreationTypePreview)
   192  		fmt.Fprint(w, `{"id":1, "login":"l", "url":"u", "avatar_url": "a", "location":"l"}`)
   193  	})
   194  
   195  	ctx := context.Background()
   196  	org, _, err := client.Organizations.Get(ctx, "o")
   197  	if err != nil {
   198  		t.Errorf("Organizations.Get returned error: %v", err)
   199  	}
   200  
   201  	want := &Organization{ID: Ptr(int64(1)), Login: Ptr("l"), URL: Ptr("u"), AvatarURL: Ptr("a"), Location: Ptr("l")}
   202  	if !cmp.Equal(org, want) {
   203  		t.Errorf("Organizations.Get returned %+v, want %+v", org, want)
   204  	}
   205  
   206  	const methodName = "Get"
   207  	testBadOptions(t, methodName, func() (err error) {
   208  		_, _, err = client.Organizations.Get(ctx, "\n")
   209  		return err
   210  	})
   211  
   212  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   213  		got, resp, err := client.Organizations.Get(ctx, "o")
   214  		if got != nil {
   215  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   216  		}
   217  		return resp, err
   218  	})
   219  }
   220  
   221  func TestOrganizationsService_Get_invalidOrg(t *testing.T) {
   222  	t.Parallel()
   223  	client, _, _ := setup(t)
   224  
   225  	ctx := context.Background()
   226  	_, _, err := client.Organizations.Get(ctx, "%")
   227  	testURLParseError(t, err)
   228  }
   229  
   230  func TestOrganizationsService_GetByID(t *testing.T) {
   231  	t.Parallel()
   232  	client, mux, _ := setup(t)
   233  
   234  	mux.HandleFunc("/organizations/1", func(w http.ResponseWriter, r *http.Request) {
   235  		testMethod(t, r, "GET")
   236  		fmt.Fprint(w, `{"id":1, "login":"l", "url":"u", "avatar_url": "a", "location":"l"}`)
   237  	})
   238  
   239  	ctx := context.Background()
   240  	org, _, err := client.Organizations.GetByID(ctx, 1)
   241  	if err != nil {
   242  		t.Fatalf("Organizations.GetByID returned error: %v", err)
   243  	}
   244  
   245  	want := &Organization{ID: Ptr(int64(1)), Login: Ptr("l"), URL: Ptr("u"), AvatarURL: Ptr("a"), Location: Ptr("l")}
   246  	if !cmp.Equal(org, want) {
   247  		t.Errorf("Organizations.GetByID returned %+v, want %+v", org, want)
   248  	}
   249  
   250  	const methodName = "GetByID"
   251  	testBadOptions(t, methodName, func() (err error) {
   252  		_, _, err = client.Organizations.GetByID(ctx, -1)
   253  		return err
   254  	})
   255  
   256  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   257  		got, resp, err := client.Organizations.GetByID(ctx, 1)
   258  		if got != nil {
   259  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   260  		}
   261  		return resp, err
   262  	})
   263  }
   264  
   265  func TestOrganizationsService_Edit(t *testing.T) {
   266  	t.Parallel()
   267  	client, mux, _ := setup(t)
   268  
   269  	input := &Organization{Login: Ptr("l")}
   270  
   271  	mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
   272  		v := new(Organization)
   273  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
   274  
   275  		testHeader(t, r, "Accept", mediaTypeMemberAllowedRepoCreationTypePreview)
   276  		testMethod(t, r, "PATCH")
   277  		if !cmp.Equal(v, input) {
   278  			t.Errorf("Request body = %+v, want %+v", v, input)
   279  		}
   280  
   281  		fmt.Fprint(w, `{"id":1}`)
   282  	})
   283  
   284  	ctx := context.Background()
   285  	org, _, err := client.Organizations.Edit(ctx, "o", input)
   286  	if err != nil {
   287  		t.Errorf("Organizations.Edit returned error: %v", err)
   288  	}
   289  
   290  	want := &Organization{ID: Ptr(int64(1))}
   291  	if !cmp.Equal(org, want) {
   292  		t.Errorf("Organizations.Edit returned %+v, want %+v", org, want)
   293  	}
   294  
   295  	const methodName = "Edit"
   296  	testBadOptions(t, methodName, func() (err error) {
   297  		_, _, err = client.Organizations.Edit(ctx, "\n", input)
   298  		return err
   299  	})
   300  
   301  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   302  		got, resp, err := client.Organizations.Edit(ctx, "o", input)
   303  		if got != nil {
   304  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   305  		}
   306  		return resp, err
   307  	})
   308  }
   309  
   310  func TestOrganizationsService_Edit_invalidOrg(t *testing.T) {
   311  	t.Parallel()
   312  	client, _, _ := setup(t)
   313  
   314  	ctx := context.Background()
   315  	_, _, err := client.Organizations.Edit(ctx, "%", nil)
   316  	testURLParseError(t, err)
   317  }
   318  
   319  func TestOrganizationsService_Delete(t *testing.T) {
   320  	t.Parallel()
   321  	client, mux, _ := setup(t)
   322  
   323  	mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
   324  		testMethod(t, r, "DELETE")
   325  	})
   326  
   327  	ctx := context.Background()
   328  	_, err := client.Organizations.Delete(ctx, "o")
   329  	if err != nil {
   330  		t.Errorf("Organizations.Delete returned error: %v", err)
   331  	}
   332  
   333  	const methodName = "Delete"
   334  	testBadOptions(t, methodName, func() (err error) {
   335  		_, err = client.Organizations.Delete(ctx, "\n")
   336  		return err
   337  	})
   338  
   339  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   340  		return client.Organizations.Delete(ctx, "o")
   341  	})
   342  }
   343  
   344  func TestOrganizationsService_ListInstallations(t *testing.T) {
   345  	t.Parallel()
   346  	client, mux, _ := setup(t)
   347  
   348  	mux.HandleFunc("/orgs/o/installations", func(w http.ResponseWriter, r *http.Request) {
   349  		testMethod(t, r, "GET")
   350  		fmt.Fprint(w, `{"total_count": 1, "installations": [{ "id": 1, "app_id": 5}]}`)
   351  	})
   352  
   353  	ctx := context.Background()
   354  	apps, _, err := client.Organizations.ListInstallations(ctx, "o", nil)
   355  	if err != nil {
   356  		t.Errorf("Organizations.ListInstallations returned error: %v", err)
   357  	}
   358  
   359  	want := &OrganizationInstallations{TotalCount: Ptr(1), Installations: []*Installation{{ID: Ptr(int64(1)), AppID: Ptr(int64(5))}}}
   360  	if !cmp.Equal(apps, want) {
   361  		t.Errorf("Organizations.ListInstallations returned %+v, want %+v", apps, want)
   362  	}
   363  
   364  	const methodName = "ListInstallations"
   365  	testBadOptions(t, methodName, func() (err error) {
   366  		_, _, err = client.Organizations.ListInstallations(ctx, "\no", nil)
   367  		return err
   368  	})
   369  
   370  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   371  		got, resp, err := client.Organizations.ListInstallations(ctx, "o", nil)
   372  		if got != nil {
   373  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   374  		}
   375  		return resp, err
   376  	})
   377  }
   378  
   379  func TestOrganizationsService_ListInstallations_invalidOrg(t *testing.T) {
   380  	t.Parallel()
   381  	client, _, _ := setup(t)
   382  
   383  	ctx := context.Background()
   384  	_, _, err := client.Organizations.ListInstallations(ctx, "%", nil)
   385  	testURLParseError(t, err)
   386  }
   387  
   388  func TestOrganizationsService_ListInstallations_withListOptions(t *testing.T) {
   389  	t.Parallel()
   390  	client, mux, _ := setup(t)
   391  
   392  	mux.HandleFunc("/orgs/o/installations", func(w http.ResponseWriter, r *http.Request) {
   393  		testMethod(t, r, "GET")
   394  		testFormValues(t, r, values{"page": "2"})
   395  		fmt.Fprint(w, `{"total_count": 2, "installations": [{ "id": 2, "app_id": 10}]}`)
   396  	})
   397  
   398  	ctx := context.Background()
   399  	apps, _, err := client.Organizations.ListInstallations(ctx, "o", &ListOptions{Page: 2})
   400  	if err != nil {
   401  		t.Errorf("Organizations.ListInstallations returned error: %v", err)
   402  	}
   403  
   404  	want := &OrganizationInstallations{TotalCount: Ptr(2), Installations: []*Installation{{ID: Ptr(int64(2)), AppID: Ptr(int64(10))}}}
   405  	if !cmp.Equal(apps, want) {
   406  		t.Errorf("Organizations.ListInstallations returned %+v, want %+v", apps, want)
   407  	}
   408  
   409  	// Test ListOptions failure
   410  	_, _, err = client.Organizations.ListInstallations(ctx, "%", &ListOptions{})
   411  	if err == nil {
   412  		t.Error("Organizations.ListInstallations returned error: nil")
   413  	}
   414  
   415  	const methodName = "ListInstallations"
   416  	testBadOptions(t, methodName, func() (err error) {
   417  		_, _, err = client.Organizations.ListInstallations(ctx, "\n", &ListOptions{Page: 2})
   418  		return err
   419  	})
   420  
   421  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   422  		got, resp, err := client.Organizations.ListInstallations(ctx, "o", &ListOptions{Page: 2})
   423  		if got != nil {
   424  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   425  		}
   426  		return resp, err
   427  	})
   428  }
   429  
   430  func TestOrganizationInstallations_Marshal(t *testing.T) {
   431  	t.Parallel()
   432  	testJSONMarshal(t, &OrganizationInstallations{}, "{}")
   433  
   434  	o := &OrganizationInstallations{
   435  		TotalCount:    Ptr(1),
   436  		Installations: []*Installation{{ID: Ptr(int64(1))}},
   437  	}
   438  	want := `{
   439  		"total_count": 1,
   440  		"installations": [
   441  			{
   442  				"id": 1
   443  			}
   444  		]
   445  	}`
   446  
   447  	testJSONMarshal(t, o, want)
   448  }
   449  
   450  func TestPlan_Marshal(t *testing.T) {
   451  	t.Parallel()
   452  	testJSONMarshal(t, &Plan{}, "{}")
   453  
   454  	o := &Plan{
   455  		Name:          Ptr("name"),
   456  		Space:         Ptr(1),
   457  		Collaborators: Ptr(1),
   458  		PrivateRepos:  Ptr(int64(1)),
   459  		FilledSeats:   Ptr(1),
   460  		Seats:         Ptr(1),
   461  	}
   462  	want := `{
   463  		"name": "name",
   464  		"space": 1,
   465  		"collaborators": 1,
   466  		"private_repos": 1,
   467  		"filled_seats": 1,
   468  		"seats": 1
   469  	}`
   470  
   471  	testJSONMarshal(t, o, want)
   472  }