github.com/google/go-github/v49@v49.1.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 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_ListInstallations(t *testing.T) { 319 client, mux, _, teardown := setup() 320 defer teardown() 321 322 mux.HandleFunc("/orgs/o/installations", func(w http.ResponseWriter, r *http.Request) { 323 testMethod(t, r, "GET") 324 fmt.Fprint(w, `{"total_count": 1, "installations": [{ "id": 1, "app_id": 5}]}`) 325 }) 326 327 ctx := context.Background() 328 apps, _, err := client.Organizations.ListInstallations(ctx, "o", nil) 329 if err != nil { 330 t.Errorf("Organizations.ListInstallations returned error: %v", err) 331 } 332 333 want := &OrganizationInstallations{TotalCount: Int(1), Installations: []*Installation{{ID: Int64(1), AppID: Int64(5)}}} 334 if !cmp.Equal(apps, want) { 335 t.Errorf("Organizations.ListInstallations returned %+v, want %+v", apps, want) 336 } 337 338 const methodName = "ListInstallations" 339 testBadOptions(t, methodName, func() (err error) { 340 _, _, err = client.Organizations.ListInstallations(ctx, "\no", nil) 341 return err 342 }) 343 344 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 345 got, resp, err := client.Organizations.ListInstallations(ctx, "o", nil) 346 if got != nil { 347 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 348 } 349 return resp, err 350 }) 351 } 352 353 func TestOrganizationsService_ListInstallations_invalidOrg(t *testing.T) { 354 client, _, _, teardown := setup() 355 defer teardown() 356 357 ctx := context.Background() 358 _, _, err := client.Organizations.ListInstallations(ctx, "%", nil) 359 testURLParseError(t, err) 360 } 361 362 func TestOrganizationsService_ListInstallations_withListOptions(t *testing.T) { 363 client, mux, _, teardown := setup() 364 defer teardown() 365 366 mux.HandleFunc("/orgs/o/installations", func(w http.ResponseWriter, r *http.Request) { 367 testMethod(t, r, "GET") 368 testFormValues(t, r, values{"page": "2"}) 369 fmt.Fprint(w, `{"total_count": 2, "installations": [{ "id": 2, "app_id": 10}]}`) 370 }) 371 372 ctx := context.Background() 373 apps, _, err := client.Organizations.ListInstallations(ctx, "o", &ListOptions{Page: 2}) 374 if err != nil { 375 t.Errorf("Organizations.ListInstallations returned error: %v", err) 376 } 377 378 want := &OrganizationInstallations{TotalCount: Int(2), Installations: []*Installation{{ID: Int64(2), AppID: Int64(10)}}} 379 if !cmp.Equal(apps, want) { 380 t.Errorf("Organizations.ListInstallations returned %+v, want %+v", apps, want) 381 } 382 383 // Test ListOptions failure 384 _, _, err = client.Organizations.ListInstallations(ctx, "%", &ListOptions{}) 385 if err == nil { 386 t.Error("Organizations.ListInstallations returned error: nil") 387 } 388 389 const methodName = "ListInstallations" 390 testBadOptions(t, methodName, func() (err error) { 391 _, _, err = client.Organizations.ListInstallations(ctx, "\n", &ListOptions{Page: 2}) 392 return err 393 }) 394 395 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 396 got, resp, err := client.Organizations.ListInstallations(ctx, "o", &ListOptions{Page: 2}) 397 if got != nil { 398 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 399 } 400 return resp, err 401 }) 402 } 403 404 func TestOrganizationInstallations_Marshal(t *testing.T) { 405 testJSONMarshal(t, &OrganizationInstallations{}, "{}") 406 407 o := &OrganizationInstallations{ 408 TotalCount: Int(1), 409 Installations: []*Installation{{ID: Int64(1)}}, 410 } 411 want := `{ 412 "total_count": 1, 413 "installations": [ 414 { 415 "id": 1 416 } 417 ] 418 }` 419 420 testJSONMarshal(t, o, want) 421 } 422 423 func TestPlan_Marshal(t *testing.T) { 424 testJSONMarshal(t, &Plan{}, "{}") 425 426 o := &Plan{ 427 Name: String("name"), 428 Space: Int(1), 429 Collaborators: Int(1), 430 PrivateRepos: Int(1), 431 FilledSeats: Int(1), 432 Seats: Int(1), 433 } 434 want := `{ 435 "name": "name", 436 "space": 1, 437 "collaborators": 1, 438 "private_repos": 1, 439 "filled_seats": 1, 440 "seats": 1 441 }` 442 443 testJSONMarshal(t, o, want) 444 }