github.com/google/go-github/v33@v33.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  	"reflect"
    14  	"testing"
    15  )
    16  
    17  func TestOrganization_marshal(t *testing.T) {
    18  	testJSONMarshal(t, &Organization{}, "{}")
    19  
    20  	o := &Organization{
    21  		BillingEmail:                         String("support@github.com"),
    22  		Blog:                                 String("https://github.com/blog"),
    23  		Company:                              String("GitHub"),
    24  		Email:                                String("support@github.com"),
    25  		TwitterUsername:                      String("github"),
    26  		Location:                             String("San Francisco"),
    27  		Name:                                 String("github"),
    28  		Description:                          String("GitHub, the company."),
    29  		IsVerified:                           Bool(true),
    30  		HasOrganizationProjects:              Bool(true),
    31  		HasRepositoryProjects:                Bool(true),
    32  		DefaultRepoPermission:                String("read"),
    33  		MembersCanCreateRepos:                Bool(true),
    34  		MembersCanCreateInternalRepos:        Bool(true),
    35  		MembersCanCreatePrivateRepos:         Bool(true),
    36  		MembersCanCreatePublicRepos:          Bool(false),
    37  		MembersAllowedRepositoryCreationType: String("all"),
    38  	}
    39  	want := `
    40  		{
    41  			"billing_email": "support@github.com",
    42  			"blog": "https://github.com/blog",
    43  			"company": "GitHub",
    44  			"email": "support@github.com",
    45  			"twitter_username": "github",
    46  			"location": "San Francisco",
    47  			"name": "github",
    48  			"description": "GitHub, the company.",
    49  			"is_verified": true,
    50  			"has_organization_projects": true,
    51  			"has_repository_projects": true,
    52  			"default_repository_permission": "read",
    53  			"members_can_create_repositories": true,
    54  			"members_can_create_public_repositories": false,
    55  			"members_can_create_private_repositories": true,
    56  			"members_can_create_internal_repositories": true,
    57  			"members_allowed_repository_creation_type": "all"
    58  		}
    59  	`
    60  	testJSONMarshal(t, o, want)
    61  }
    62  
    63  func TestOrganizationsService_ListAll(t *testing.T) {
    64  	client, mux, _, teardown := setup()
    65  	defer teardown()
    66  
    67  	since := int64(1342004)
    68  	mux.HandleFunc("/organizations", func(w http.ResponseWriter, r *http.Request) {
    69  		testMethod(t, r, "GET")
    70  		testFormValues(t, r, values{"since": "1342004"})
    71  		fmt.Fprint(w, `[{"id":4314092}]`)
    72  	})
    73  
    74  	opt := &OrganizationsListOptions{Since: since}
    75  	orgs, _, err := client.Organizations.ListAll(context.Background(), opt)
    76  	if err != nil {
    77  		t.Errorf("Organizations.ListAll returned error: %v", err)
    78  	}
    79  
    80  	want := []*Organization{{ID: Int64(4314092)}}
    81  	if !reflect.DeepEqual(orgs, want) {
    82  		t.Errorf("Organizations.ListAll returned %+v, want %+v", orgs, want)
    83  	}
    84  }
    85  
    86  func TestOrganizationsService_List_authenticatedUser(t *testing.T) {
    87  	client, mux, _, teardown := setup()
    88  	defer teardown()
    89  
    90  	mux.HandleFunc("/user/orgs", func(w http.ResponseWriter, r *http.Request) {
    91  		testMethod(t, r, "GET")
    92  		fmt.Fprint(w, `[{"id":1},{"id":2}]`)
    93  	})
    94  
    95  	orgs, _, err := client.Organizations.List(context.Background(), "", nil)
    96  	if err != nil {
    97  		t.Errorf("Organizations.List returned error: %v", err)
    98  	}
    99  
   100  	want := []*Organization{{ID: Int64(1)}, {ID: Int64(2)}}
   101  	if !reflect.DeepEqual(orgs, want) {
   102  		t.Errorf("Organizations.List returned %+v, want %+v", orgs, want)
   103  	}
   104  }
   105  
   106  func TestOrganizationsService_List_specifiedUser(t *testing.T) {
   107  	client, mux, _, teardown := setup()
   108  	defer teardown()
   109  
   110  	mux.HandleFunc("/users/u/orgs", func(w http.ResponseWriter, r *http.Request) {
   111  		testMethod(t, r, "GET")
   112  		testFormValues(t, r, values{"page": "2"})
   113  		fmt.Fprint(w, `[{"id":1},{"id":2}]`)
   114  	})
   115  
   116  	opt := &ListOptions{Page: 2}
   117  	orgs, _, err := client.Organizations.List(context.Background(), "u", opt)
   118  	if err != nil {
   119  		t.Errorf("Organizations.List returned error: %v", err)
   120  	}
   121  
   122  	want := []*Organization{{ID: Int64(1)}, {ID: Int64(2)}}
   123  	if !reflect.DeepEqual(orgs, want) {
   124  		t.Errorf("Organizations.List returned %+v, want %+v", orgs, want)
   125  	}
   126  }
   127  
   128  func TestOrganizationsService_List_invalidUser(t *testing.T) {
   129  	client, _, _, teardown := setup()
   130  	defer teardown()
   131  
   132  	_, _, err := client.Organizations.List(context.Background(), "%", nil)
   133  	testURLParseError(t, err)
   134  }
   135  
   136  func TestOrganizationsService_Get(t *testing.T) {
   137  	client, mux, _, teardown := setup()
   138  	defer teardown()
   139  
   140  	mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
   141  		testMethod(t, r, "GET")
   142  		testHeader(t, r, "Accept", mediaTypeMemberAllowedRepoCreationTypePreview)
   143  		fmt.Fprint(w, `{"id":1, "login":"l", "url":"u", "avatar_url": "a", "location":"l"}`)
   144  	})
   145  
   146  	org, _, err := client.Organizations.Get(context.Background(), "o")
   147  	if err != nil {
   148  		t.Errorf("Organizations.Get returned error: %v", err)
   149  	}
   150  
   151  	want := &Organization{ID: Int64(1), Login: String("l"), URL: String("u"), AvatarURL: String("a"), Location: String("l")}
   152  	if !reflect.DeepEqual(org, want) {
   153  		t.Errorf("Organizations.Get returned %+v, want %+v", org, want)
   154  	}
   155  }
   156  
   157  func TestOrganizationsService_Get_invalidOrg(t *testing.T) {
   158  	client, _, _, teardown := setup()
   159  	defer teardown()
   160  
   161  	_, _, err := client.Organizations.Get(context.Background(), "%")
   162  	testURLParseError(t, err)
   163  }
   164  
   165  func TestOrganizationsService_GetByID(t *testing.T) {
   166  	client, mux, _, teardown := setup()
   167  	defer teardown()
   168  
   169  	mux.HandleFunc("/organizations/1", func(w http.ResponseWriter, r *http.Request) {
   170  		testMethod(t, r, "GET")
   171  		fmt.Fprint(w, `{"id":1, "login":"l", "url":"u", "avatar_url": "a", "location":"l"}`)
   172  	})
   173  
   174  	org, _, err := client.Organizations.GetByID(context.Background(), 1)
   175  	if err != nil {
   176  		t.Fatalf("Organizations.GetByID returned error: %v", err)
   177  	}
   178  
   179  	want := &Organization{ID: Int64(1), Login: String("l"), URL: String("u"), AvatarURL: String("a"), Location: String("l")}
   180  	if !reflect.DeepEqual(org, want) {
   181  		t.Errorf("Organizations.GetByID returned %+v, want %+v", org, want)
   182  	}
   183  }
   184  
   185  func TestOrganizationsService_Edit(t *testing.T) {
   186  	client, mux, _, teardown := setup()
   187  	defer teardown()
   188  
   189  	input := &Organization{Login: String("l")}
   190  
   191  	mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
   192  		v := new(Organization)
   193  		json.NewDecoder(r.Body).Decode(v)
   194  
   195  		testHeader(t, r, "Accept", mediaTypeMemberAllowedRepoCreationTypePreview)
   196  		testMethod(t, r, "PATCH")
   197  		if !reflect.DeepEqual(v, input) {
   198  			t.Errorf("Request body = %+v, want %+v", v, input)
   199  		}
   200  
   201  		fmt.Fprint(w, `{"id":1}`)
   202  	})
   203  
   204  	org, _, err := client.Organizations.Edit(context.Background(), "o", input)
   205  	if err != nil {
   206  		t.Errorf("Organizations.Edit returned error: %v", err)
   207  	}
   208  
   209  	want := &Organization{ID: Int64(1)}
   210  	if !reflect.DeepEqual(org, want) {
   211  		t.Errorf("Organizations.Edit returned %+v, want %+v", org, want)
   212  	}
   213  }
   214  
   215  func TestOrganizationsService_Edit_invalidOrg(t *testing.T) {
   216  	client, _, _, teardown := setup()
   217  	defer teardown()
   218  
   219  	_, _, err := client.Organizations.Edit(context.Background(), "%", nil)
   220  	testURLParseError(t, err)
   221  }
   222  
   223  func TestOrganizationsService_ListInstallations(t *testing.T) {
   224  	client, mux, _, teardown := setup()
   225  	defer teardown()
   226  
   227  	mux.HandleFunc("/orgs/o/installations", func(w http.ResponseWriter, r *http.Request) {
   228  		testMethod(t, r, "GET")
   229  		fmt.Fprint(w, `{"total_count": 1, "installations": [{ "id": 1, "app_id": 5}]}`)
   230  	})
   231  
   232  	apps, _, err := client.Organizations.ListInstallations(context.Background(), "o", nil)
   233  	if err != nil {
   234  		t.Errorf("Organizations.ListInstallations returned error: %v", err)
   235  	}
   236  
   237  	want := &OrganizationInstallations{TotalCount: Int(1), Installations: []*Installation{{ID: Int64(1), AppID: Int64(5)}}}
   238  	if !reflect.DeepEqual(apps, want) {
   239  		t.Errorf("Organizations.ListInstallations returned %+v, want %+v", apps, want)
   240  	}
   241  }
   242  
   243  func TestOrganizationsService_ListInstallations_invalidOrg(t *testing.T) {
   244  	client, _, _, teardown := setup()
   245  	defer teardown()
   246  
   247  	_, _, err := client.Organizations.ListInstallations(context.Background(), "%", nil)
   248  	testURLParseError(t, err)
   249  
   250  }
   251  
   252  func TestOrganizationsService_ListInstallations_withListOptions(t *testing.T) {
   253  	client, mux, _, teardown := setup()
   254  	defer teardown()
   255  
   256  	mux.HandleFunc("/orgs/o/installations", func(w http.ResponseWriter, r *http.Request) {
   257  		testMethod(t, r, "GET")
   258  		testFormValues(t, r, values{"page": "2"})
   259  		fmt.Fprint(w, `{"total_count": 2, "installations": [{ "id": 2, "app_id": 10}]}`)
   260  	})
   261  
   262  	apps, _, err := client.Organizations.ListInstallations(context.Background(), "o", &ListOptions{Page: 2})
   263  	if err != nil {
   264  		t.Errorf("Organizations.ListInstallations returned error: %v", err)
   265  	}
   266  
   267  	want := &OrganizationInstallations{TotalCount: Int(2), Installations: []*Installation{{ID: Int64(2), AppID: Int64(10)}}}
   268  	if !reflect.DeepEqual(apps, want) {
   269  		t.Errorf("Organizations.ListInstallations returned %+v, want %+v", apps, want)
   270  	}
   271  
   272  	// Test ListOptions failure
   273  	_, _, err = client.Organizations.ListInstallations(context.Background(), "%", &ListOptions{})
   274  	if err == nil {
   275  		t.Error("Organizations.ListInstallations returned error: nil")
   276  	}
   277  }