| 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 |
client, mux, _, teardown := setup() |
| 19 |
defer teardown() |
| 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 |
client, _, _, teardown := setup() |
| 72 |
defer teardown() |
| 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 |
client, mux, _, teardown := setup() |
| 81 |
defer teardown() |
| 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 |
client, _, _, teardown := setup() |
| 124 |
defer teardown() |
| 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 |
client, mux, _, teardown := setup() |
| 133 |
defer teardown() |
| 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 |
client, _, _, teardown := setup() |
| 176 |
defer teardown() |
| 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 |
client, mux, _, teardown := setup() |
| 185 |
defer teardown() |
| 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 |
client, _, _, teardown := setup() |
| 238 |
defer teardown() |
| 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 |
client, mux, _, teardown := setup() |
| 247 |
defer teardown() |
| 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 |
client, _, _, teardown := setup() |
| 290 |
defer teardown() |
| 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 |
client, mux, _, teardown := setup() |
| 299 |
defer teardown() |
| 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 |
client, _, _, teardown := setup() |
| 342 |
defer teardown() |
| 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 |
testJSONMarshal(t, &MinutesUsedBreakdown{}, "{}") |
| 351 |
|
| 352 |
u := &MinutesUsedBreakdown{ |
| 353 |
"UBUNTU": 1, |
| 354 |
"MACOS": 1, |
| 355 |
"WINDOWS": 1, |
| 356 |
} |
| 357 |
|
| 358 |
want := `{ |
| 359 |
"UBUNTU": 1, |
| 360 |
"MACOS": 1, |
| 361 |
"WINDOWS": 1 |
| 362 |
}` |
| 363 |
|
| 364 |
testJSONMarshal(t, u, want) |
| 365 |
} |
| 366 |
|
| 367 |
func TestActionBilling_Marshal(t *testing.T) { |
| 368 |
testJSONMarshal(t, &MinutesUsedBreakdown{}, "{}") |
| 369 |
|
| 370 |
u := &ActionBilling{ |
| 371 |
TotalMinutesUsed: 1, |
| 372 |
TotalPaidMinutesUsed: 1, |
| 373 |
IncludedMinutes: 1, |
| 374 |
MinutesUsedBreakdown: MinutesUsedBreakdown{ |
| 375 |
"UBUNTU": 1, |
| 376 |
"MACOS": 1, |
| 377 |
"WINDOWS": 1, |
| 378 |
}, |
| 379 |
} |
| 380 |
|
| 381 |
want := `{ |
| 382 |
"total_minutes_used": 1, |
| 383 |
"total_paid_minutes_used": 1, |
| 384 |
"included_minutes": 1, |
| 385 |
"minutes_used_breakdown": { |
| 386 |
"UBUNTU": 1, |
| 387 |
"MACOS": 1, |
| 388 |
"WINDOWS": 1 |
| 389 |
} |
| 390 |
}` |
| 391 |
|
| 392 |
testJSONMarshal(t, u, want) |
| 393 |
} |
| 394 |
|
| 395 |
func TestPackageBilling_Marshal(t *testing.T) { |
| 396 |
testJSONMarshal(t, &PackageBilling{}, "{}") |
| 397 |
|
| 398 |
u := &PackageBilling{ |
| 399 |
TotalGigabytesBandwidthUsed: 1, |
| 400 |
TotalPaidGigabytesBandwidthUsed: 1, |
| 401 |
IncludedGigabytesBandwidth: 1, |
| 402 |
} |
| 403 |
|
| 404 |
want := `{ |
| 405 |
"total_gigabytes_bandwidth_used": 1, |
| 406 |
"total_paid_gigabytes_bandwidth_used": 1, |
| 407 |
"included_gigabytes_bandwidth": 1 |
| 408 |
}` |
| 409 |
|
| 410 |
testJSONMarshal(t, u, want) |
| 411 |
} |
| 412 |
|
| 413 |
func TestStorageBilling_Marshal(t *testing.T) { |
| 414 |
testJSONMarshal(t, &StorageBilling{}, "{}") |
| 415 |
|
| 416 |
u := &StorageBilling{ |
| 417 |
DaysLeftInBillingCycle: 1, |
| 418 |
EstimatedPaidStorageForMonth: 1, |
| 419 |
EstimatedStorageForMonth: 1, |
| 420 |
} |
| 421 |
|
| 422 |
want := `{ |
| 423 |
"days_left_in_billing_cycle": 1, |
| 424 |
"estimated_paid_storage_for_month": 1, |
| 425 |
"estimated_storage_for_month": 1 |
| 426 |
}` |
| 427 |
|
| 428 |
testJSONMarshal(t, u, want) |
| 429 |
} |
| 430 |
|
| 431 |
func TestBillingService_GetAdvancedSecurityActiveCommittersOrg(t *testing.T) { |
| 432 |
client, mux, _, teardown := setup() |
| 433 |
defer teardown() |
| 434 |
|
| 435 |
mux.HandleFunc("/orgs/o/settings/billing/advanced-security", func(w http.ResponseWriter, r *http.Request) { |
| 436 |
testMethod(t, r, "GET") |
| 437 |
fmt.Fprint(w, `{ |
| 438 |
"total_advanced_security_committers": 2, |
| 439 |
"repositories": [ |
| 440 |
{ |
| 441 |
"name": "octocat-org/Hello-World", |
| 442 |
"advanced_security_committers": 2, |
| 443 |
"advanced_security_committers_breakdown": [ |
| 444 |
{ |
| 445 |
"user_login": "octokitten", |
| 446 |
"last_pushed_date": "2021-10-25" |
| 447 |
} |
| 448 |
] |
| 449 |
} |
| 450 |
] |
| 451 |
}`) |
| 452 |
}) |
| 453 |
|
| 454 |
ctx := context.Background() |
| 455 |
opts := &ListOptions{Page: 2, PerPage: 50} |
| 456 |
hook, _, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "o", opts) |
| 457 |
if err != nil { |
| 458 |
t.Errorf("Billing.GetAdvancedSecurityActiveCommittersOrg returned error: %v", err) |
| 459 |
} |
| 460 |
|
| 461 |
want := &ActiveCommitters{ |
| 462 |
TotalAdvancedSecurityCommitters: 2, |
| 463 |
Repositories: []*RepositoryActiveCommitters{ |
| 464 |
{ |
| 465 |
Name: String("octocat-org/Hello-World"), |
| 466 |
AdvancedSecurityCommitters: Int(2), |
| 467 |
AdvancedSecurityCommittersBreakdown: []*AdvancedSecurityCommittersBreakdown{ |
| 468 |
{ |
| 469 |
UserLogin: String("octokitten"), |
| 470 |
LastPushedDate: String("2021-10-25"), |
| 471 |
}, |
| 472 |
}, |
| 473 |
}, |
| 474 |
}, |
| 475 |
} |
| 476 |
if !cmp.Equal(hook, want) { |
| 477 |
t.Errorf("Billing.GetAdvancedSecurityActiveCommittersOrg returned %+v, want %+v", hook, want) |
| 478 |
} |
| 479 |
|
| 480 |
const methodName = "GetAdvancedSecurityActiveCommittersOrg" |
| 481 |
testBadOptions(t, methodName, func() (err error) { |
| 482 |
_, _, err = client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "\n", nil) |
| 483 |
return err |
| 484 |
}) |
| 485 |
|
| 486 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 487 |
got, resp, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "o", nil) |
| 488 |
if got != nil { |
| 489 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 490 |
} |
| 491 |
return resp, err |
| 492 |
}) |
| 493 |
} |
| 494 |
|
| 495 |
func TestBillingService_GetAdvancedSecurityActiveCommittersOrg_invalidOrg(t *testing.T) { |
| 496 |
client, _, _, teardown := setup() |
| 497 |
defer teardown() |
| 498 |
|
| 499 |
ctx := context.Background() |
| 500 |
_, _, err := client.Billing.GetAdvancedSecurityActiveCommittersOrg(ctx, "%", nil) |
| 501 |
testURLParseError(t, err) |
| 502 |
} |