github.com/google/go-github/v71@v71.0.0/github/billing_test.go (about) 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 TestBillingService_GetActionsBillingOrg(t *testing.T) { 18 t.Parallel() 19 client, mux, _ := setup(t) 20 21 mux.HandleFunc("/orgs/o/settings/billing/actions", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 fmt.Fprint(w, `{ 24 "total_minutes_used": 305.0, 25 "total_paid_minutes_used": 0.0, 26 "included_minutes": 3000.0, 27 "minutes_used_breakdown": { 28 "UBUNTU": 205, 29 "MACOS": 10, 30 "WINDOWS": 90 31 } 32 }`) 33 }) 34 35 ctx := context.Background() 36 hook, _, err := client.Billing.GetActionsBillingOrg(ctx, "o") 37 if err != nil { 38 t.Errorf("Billing.GetActionsBillingOrg returned error: %v", err) 39 } 40 41 want := &ActionBilling{ 42 TotalMinutesUsed: 305.0, 43 TotalPaidMinutesUsed: 0.0, 44 IncludedMinutes: 3000.0, 45 MinutesUsedBreakdown: MinutesUsedBreakdown{ 46 "UBUNTU": 205, 47 "MACOS": 10, 48 "WINDOWS": 90, 49 }, 50 } 51 if !cmp.Equal(hook, want) { 52 t.Errorf("Billing.GetActionsBillingOrg returned %+v, want %+v", hook, want) 53 } 54 55 const methodName = "GetActionsBillingOrg" 56 testBadOptions(t, methodName, func() (err error) { 57 _, _, err = client.Billing.GetActionsBillingOrg(ctx, "\n") 58 return err 59 }) 60 61 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 62 got, resp, err := client.Billing.GetActionsBillingOrg(ctx, "o") 63 if got != nil { 64 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 65 } 66 return resp, err 67 }) 68 } 69 70 func TestBillingService_GetActionsBillingOrg_invalidOrg(t *testing.T) { 71 t.Parallel() 72 client, _, _ := setup(t) 73 74 ctx := context.Background() 75 _, _, err := client.Billing.GetActionsBillingOrg(ctx, "%") 76 testURLParseError(t, err) 77 } 78 79 func TestBillingService_GetPackagesBillingOrg(t *testing.T) { 80 t.Parallel() 81 client, mux, _ := setup(t) 82 83 mux.HandleFunc("/orgs/o/settings/billing/packages", func(w http.ResponseWriter, r *http.Request) { 84 testMethod(t, r, "GET") 85 fmt.Fprint(w, `{ 86 "total_gigabytes_bandwidth_used": 50, 87 "total_paid_gigabytes_bandwidth_used": 40, 88 "included_gigabytes_bandwidth": 10 89 }`) 90 }) 91 92 ctx := context.Background() 93 hook, _, err := client.Billing.GetPackagesBillingOrg(ctx, "o") 94 if err != nil { 95 t.Errorf("Billing.GetPackagesBillingOrg returned error: %v", err) 96 } 97 98 want := &PackageBilling{ 99 TotalGigabytesBandwidthUsed: 50, 100 TotalPaidGigabytesBandwidthUsed: 40, 101 IncludedGigabytesBandwidth: 10, 102 } 103 if !cmp.Equal(hook, want) { 104 t.Errorf("Billing.GetPackagesBillingOrg returned %+v, want %+v", hook, want) 105 } 106 107 const methodName = "GetPackagesBillingOrg" 108 testBadOptions(t, methodName, func() (err error) { 109 _, _, err = client.Billing.GetPackagesBillingOrg(ctx, "\n") 110 return err 111 }) 112 113 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 114 got, resp, err := client.Billing.GetPackagesBillingOrg(ctx, "o") 115 if got != nil { 116 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 117 } 118 return resp, err 119 }) 120 } 121 122 func TestBillingService_GetPackagesBillingOrg_invalidOrg(t *testing.T) { 123 t.Parallel() 124 client, _, _ := setup(t) 125 126 ctx := context.Background() 127 _, _, err := client.Billing.GetPackagesBillingOrg(ctx, "%") 128 testURLParseError(t, err) 129 } 130 131 func TestBillingService_GetStorageBillingOrg(t *testing.T) { 132 t.Parallel() 133 client, mux, _ := setup(t) 134 135 mux.HandleFunc("/orgs/o/settings/billing/shared-storage", func(w http.ResponseWriter, r *http.Request) { 136 testMethod(t, r, "GET") 137 fmt.Fprint(w, `{ 138 "days_left_in_billing_cycle": 20, 139 "estimated_paid_storage_for_month": 15.25, 140 "estimated_storage_for_month": 40 141 }`) 142 }) 143 144 ctx := context.Background() 145 hook, _, err := client.Billing.GetStorageBillingOrg(ctx, "o") 146 if err != nil { 147 t.Errorf("Billing.GetStorageBillingOrg returned error: %v", err) 148 } 149 150 want := &StorageBilling{ 151 DaysLeftInBillingCycle: 20, 152 EstimatedPaidStorageForMonth: 15.25, 153 EstimatedStorageForMonth: 40, 154 } 155 if !cmp.Equal(hook, want) { 156 t.Errorf("Billing.GetStorageBillingOrg returned %+v, want %+v", hook, want) 157 } 158 159 const methodName = "GetStorageBillingOrg" 160 testBadOptions(t, methodName, func() (err error) { 161 _, _, err = client.Billing.GetStorageBillingOrg(ctx, "\n") 162 return err 163 }) 164 165 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 166 got, resp, err := client.Billing.GetStorageBillingOrg(ctx, "o") 167 if got != nil { 168 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 169 } 170 return resp, err 171 }) 172 } 173 174 func TestBillingService_GetStorageBillingOrg_invalidOrg(t *testing.T) { 175 t.Parallel() 176 client, _, _ := setup(t) 177 178 ctx := context.Background() 179 _, _, err := client.Billing.GetStorageBillingOrg(ctx, "%") 180 testURLParseError(t, err) 181 } 182 183 func TestBillingService_GetActionsBillingUser(t *testing.T) { 184 t.Parallel() 185 client, mux, _ := setup(t) 186 187 mux.HandleFunc("/users/u/settings/billing/actions", func(w http.ResponseWriter, r *http.Request) { 188 testMethod(t, r, "GET") 189 fmt.Fprint(w, `{ 190 "total_minutes_used": 10, 191 "total_paid_minutes_used": 0, 192 "included_minutes": 3000, 193 "minutes_used_breakdown": { 194 "UBUNTU": 205, 195 "MACOS": 10, 196 "WINDOWS": 90 197 } 198 }`) 199 }) 200 201 ctx := context.Background() 202 hook, _, err := client.Billing.GetActionsBillingUser(ctx, "u") 203 if err != nil { 204 t.Errorf("Billing.GetActionsBillingUser returned error: %v", err) 205 } 206 207 want := &ActionBilling{ 208 TotalMinutesUsed: 10, 209 TotalPaidMinutesUsed: 0, 210 IncludedMinutes: 3000, 211 MinutesUsedBreakdown: MinutesUsedBreakdown{ 212 "UBUNTU": 205, 213 "MACOS": 10, 214 "WINDOWS": 90, 215 }, 216 } 217 if !cmp.Equal(hook, want) { 218 t.Errorf("Billing.GetActionsBillingUser returned %+v, want %+v", hook, want) 219 } 220 221 const methodName = "GetActionsBillingUser" 222 testBadOptions(t, methodName, func() (err error) { 223 _, _, err = client.Billing.GetActionsBillingOrg(ctx, "\n") 224 return err 225 }) 226 227 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 228 got, resp, err := client.Billing.GetActionsBillingUser(ctx, "o") 229 if got != nil { 230 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 231 } 232 return resp, err 233 }) 234 } 235 236 func TestBillingService_GetActionsBillingUser_invalidUser(t *testing.T) { 237 t.Parallel() 238 client, _, _ := setup(t) 239 240 ctx := context.Background() 241 _, _, err := client.Billing.GetActionsBillingUser(ctx, "%") 242 testURLParseError(t, err) 243 } 244 245 func TestBillingService_GetPackagesBillingUser(t *testing.T) { 246 t.Parallel() 247 client, mux, _ := setup(t) 248 249 mux.HandleFunc("/users/u/settings/billing/packages", func(w http.ResponseWriter, r *http.Request) { 250 testMethod(t, r, "GET") 251 fmt.Fprint(w, `{ 252 "total_gigabytes_bandwidth_used": 50, 253 "total_paid_gigabytes_bandwidth_used": 40, 254 "included_gigabytes_bandwidth": 10 255 }`) 256 }) 257 258 ctx := context.Background() 259 hook, _, err := client.Billing.GetPackagesBillingUser(ctx, "u") 260 if err != nil { 261 t.Errorf("Billing.GetPackagesBillingUser returned error: %v", err) 262 } 263 264 want := &PackageBilling{ 265 TotalGigabytesBandwidthUsed: 50, 266 TotalPaidGigabytesBandwidthUsed: 40, 267 IncludedGigabytesBandwidth: 10, 268 } 269 if !cmp.Equal(hook, want) { 270 t.Errorf("Billing.GetPackagesBillingUser returned %+v, want %+v", hook, want) 271 } 272 273 const methodName = "GetPackagesBillingUser" 274 testBadOptions(t, methodName, func() (err error) { 275 _, _, err = client.Billing.GetPackagesBillingUser(ctx, "\n") 276 return err 277 }) 278 279 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 280 got, resp, err := client.Billing.GetPackagesBillingUser(ctx, "o") 281 if got != nil { 282 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 283 } 284 return resp, err 285 }) 286 } 287 288 func TestBillingService_GetPackagesBillingUser_invalidUser(t *testing.T) { 289 t.Parallel() 290 client, _, _ := setup(t) 291 292 ctx := context.Background() 293 _, _, err := client.Billing.GetPackagesBillingUser(ctx, "%") 294 testURLParseError(t, err) 295 } 296 297 func TestBillingService_GetStorageBillingUser(t *testing.T) { 298 t.Parallel() 299 client, mux, _ := setup(t) 300 301 mux.HandleFunc("/users/u/settings/billing/shared-storage", func(w http.ResponseWriter, r *http.Request) { 302 testMethod(t, r, "GET") 303 fmt.Fprint(w, `{ 304 "days_left_in_billing_cycle": 20, 305 "estimated_paid_storage_for_month": 15.25, 306 "estimated_storage_for_month": 40 307 }`) 308 }) 309 310 ctx := context.Background() 311 hook, _, err := client.Billing.GetStorageBillingUser(ctx, "u") 312 if err != nil { 313 t.Errorf("Billing.GetStorageBillingUser returned error: %v", err) 314 } 315 316 want := &StorageBilling{ 317 DaysLeftInBillingCycle: 20, 318 EstimatedPaidStorageForMonth: 15.25, 319 EstimatedStorageForMonth: 40, 320 } 321 if !cmp.Equal(hook, want) { 322 t.Errorf("Billing.GetStorageBillingUser returned %+v, want %+v", hook, want) 323 } 324 325 const methodName = "GetStorageBillingUser" 326 testBadOptions(t, methodName, func() (err error) { 327 _, _, err = client.Billing.GetStorageBillingUser(ctx, "\n") 328 return err 329 }) 330 331 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 332 got, resp, err := client.Billing.GetStorageBillingUser(ctx, "o") 333 if got != nil { 334 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 335 } 336 return resp, err 337 }) 338 } 339 340 func TestBillingService_GetStorageBillingUser_invalidUser(t *testing.T) { 341 t.Parallel() 342 client, _, _ := setup(t) 343 344 ctx := context.Background() 345 _, _, err := client.Billing.GetStorageBillingUser(ctx, "%") 346 testURLParseError(t, err) 347 } 348 349 func TestMinutesUsedBreakdown_Marshal(t *testing.T) { 350 t.Parallel() 351 testJSONMarshal(t, &MinutesUsedBreakdown{}, "{}") 352 353 u := &MinutesUsedBreakdown{ 354 "UBUNTU": 1, 355 "MACOS": 1, 356 "WINDOWS": 1, 357 } 358 359 want := `{ 360 "UBUNTU": 1, 361 "MACOS": 1, 362 "WINDOWS": 1 363 }` 364 365 testJSONMarshal(t, u, want) 366 } 367 368 func TestActionBilling_Marshal(t *testing.T) { 369 t.Parallel() 370 testJSONMarshal(t, &MinutesUsedBreakdown{}, "{}") 371 372 u := &ActionBilling{ 373 TotalMinutesUsed: 1, 374 TotalPaidMinutesUsed: 1, 375 IncludedMinutes: 1, 376 MinutesUsedBreakdown: MinutesUsedBreakdown{ 377 "UBUNTU": 1, 378 "MACOS": 1, 379 "WINDOWS": 1, 380 }, 381 } 382 383 want := `{ 384 "total_minutes_used": 1, 385 "total_paid_minutes_used": 1, 386 "included_minutes": 1, 387 "minutes_used_breakdown": { 388 "UBUNTU": 1, 389 "MACOS": 1, 390 "WINDOWS": 1 391 } 392 }` 393 394 testJSONMarshal(t, u, want) 395 } 396 397 func TestPackageBilling_Marshal(t *testing.T) { 398 t.Parallel() 399 testJSONMarshal(t, &PackageBilling{}, "{}") 400 401 u := &PackageBilling{ 402 TotalGigabytesBandwidthUsed: 1, 403 TotalPaidGigabytesBandwidthUsed: 1, 404 IncludedGigabytesBandwidth: 1, 405 } 406 407 want := `{ 408 "total_gigabytes_bandwidth_used": 1, 409 "total_paid_gigabytes_bandwidth_used": 1, 410 "included_gigabytes_bandwidth": 1 411 }` 412 413 testJSONMarshal(t, u, want) 414 } 415 416 func TestStorageBilling_Marshal(t *testing.T) { 417 t.Parallel() 418 testJSONMarshal(t, &StorageBilling{}, "{}") 419 420 u := &StorageBilling{ 421 DaysLeftInBillingCycle: 1, 422 EstimatedPaidStorageForMonth: 1, 423 EstimatedStorageForMonth: 1, 424 } 425 426 want := `{ 427 "days_left_in_billing_cycle": 1, 428 "estimated_paid_storage_for_month": 1, 429 "estimated_storage_for_month": 1 430 }` 431 432 testJSONMarshal(t, u, want) 433 } 434 435 func TestBillingService_GetAdvancedSecurityActiveCommittersOrg(t *testing.T) { 436 t.Parallel() 437 client, mux, _ := setup(t) 438 439 mux.HandleFunc("/orgs/o/settings/billing/advanced-security", func(w http.ResponseWriter, r *http.Request) { 440 testMethod(t, r, "GET") 441 fmt.Fprint(w, `{ 442 "total_advanced_security_committers": 2, 443 "total_count": 2, 444 "maximum_advanced_security_committers": 3, 445 "purchased_advanced_security_committers": 4, 446 "repositories": [ 447 { 448 "name": "octocat-org/Hello-World", 449 "advanced_security_committers": 2, 450 "advanced_security_committers_breakdown": [ 451 { 452 "user_login": "octokitten", 453 "last_pushed_date": "2021-10-25" 454 } 455 ] 456 } 457 ] 458 }`) 459 }) 460 461 ctx := context.Background() 462 opts := &ListOptions{Page: 2, PerPage: 50} 463 hook, _, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "o", opts) 464 if err != nil { 465 t.Errorf("Billing.GetAdvancedSecurityActiveCommittersOrg returned error: %v", err) 466 } 467 468 want := &ActiveCommitters{ 469 TotalAdvancedSecurityCommitters: 2, 470 TotalCount: 2, 471 MaximumAdvancedSecurityCommitters: 3, 472 PurchasedAdvancedSecurityCommitters: 4, 473 Repositories: []*RepositoryActiveCommitters{ 474 { 475 Name: Ptr("octocat-org/Hello-World"), 476 AdvancedSecurityCommitters: Ptr(2), 477 AdvancedSecurityCommittersBreakdown: []*AdvancedSecurityCommittersBreakdown{ 478 { 479 UserLogin: Ptr("octokitten"), 480 LastPushedDate: Ptr("2021-10-25"), 481 }, 482 }, 483 }, 484 }, 485 } 486 if !cmp.Equal(hook, want) { 487 t.Errorf("Billing.GetAdvancedSecurityActiveCommittersOrg returned %+v, want %+v", hook, want) 488 } 489 490 const methodName = "GetAdvancedSecurityActiveCommittersOrg" 491 testBadOptions(t, methodName, func() (err error) { 492 _, _, err = client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "\n", nil) 493 return err 494 }) 495 496 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 497 got, resp, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "o", nil) 498 if got != nil { 499 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 500 } 501 return resp, err 502 }) 503 } 504 505 func TestBillingService_GetAdvancedSecurityActiveCommittersOrg_invalidOrg(t *testing.T) { 506 t.Parallel() 507 client, _, _ := setup(t) 508 509 ctx := context.Background() 510 _, _, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "%", nil) 511 testURLParseError(t, err) 512 }