...

Source file src/github.com/google/go-github/v55/github/orgs_test.go

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

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

View as plain text