github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/account/accountv2/accounts_test.go (about) 1 package accountv2 2 3 import ( 4 "log" 5 "net/http" 6 7 bluemix "github.com/IBM-Cloud/bluemix-go" 8 "github.com/IBM-Cloud/bluemix-go/client" 9 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 10 "github.com/IBM-Cloud/bluemix-go/session" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 "github.com/onsi/gomega/ghttp" 15 ) 16 17 // List Account by org 18 19 var _ = Describe("Accounts", func() { 20 var server *ghttp.Server 21 AfterEach(func() { 22 server.Close() 23 }) 24 25 Describe("fing account by org", func() { 26 Context("Server return account by org", func() { 27 BeforeEach(func() { 28 server = ghttp.NewServer() 29 server.AppendHandlers( 30 ghttp.CombineHandlers( 31 ghttp.VerifyRequest(http.MethodPost, "/coe/v2/getaccounts"), 32 ghttp.VerifyBody([]byte(`{"organizations_region":[{"guid":"5718c403-58aa-40fd-9854-4001901e83bb","region":"us-south"}]}`)), 33 ghttp.RespondWith(http.StatusOK, `{ 34 "total_results":1, 35 "resources":[ 36 { 37 "metadata":{ 38 "guid":"0e50220c5f6e1a13fdabe1c5cc87be32", 39 "url":"/coe/v2/accounts/0e50220c5f6e1a13fdabe1c5cc87be32", 40 "created_at":"2014-08-13T12:00:42.620Z", 41 "updated_at":"2017-04-28T23:33:44.415Z" 42 }, 43 "entity":{ 44 "name":"sakshi agarwal's Account", 45 "type":"TRIAL", 46 "state":"ACTIVE", 47 "owner":"a94cd8d9-5f77-4e2e-b2b4-adb23dd26117", 48 "owner_userid":"sakshiag@in.ibm.com", 49 "owner_unique_id":"270006H0V6", 50 "customer_id":"500198622", 51 "country_code":"USA", 52 "currency_code":"USD", 53 "billing_country_code":"USA", 54 "terms_and_conditions":{ 55 56 }, 57 "tags":[ 58 59 ], 60 "team_directory_enabled":true, 61 "organizations_region":[ 62 { 63 "guid":"5718c403-58aa-40fd-9854-4001901e83bb", 64 "region":"us-south" 65 }, 66 { 67 "guid":"900d4fda-0ed2-4679-a0ad-1ffmccp2b3ddd3", 68 "region":"eu-gb" 69 } 70 ], 71 "linkages":[ 72 { 73 "origin":"IMS", 74 "state":"LINKABLE" 75 } 76 ], 77 "bluemix_subscriptions":[ 78 { 79 "type":"TRIAL", 80 "state":"ACTIVE", 81 "payment_method":{ 82 "type":"TRIAL_CREDIT", 83 "started":"08/13/2014 12:00:39", 84 "ended":"07/15/2018" 85 }, 86 "subscription_id":"500096081", 87 "part_number":"COE-Trial", 88 "subscriptionTags":[ 89 ] 90 } 91 ], 92 "subscription_id":"500096081", 93 "configuration_id":"", 94 "onboarded":-1 95 } 96 } 97 ] 98 } 99 `), 100 ), 101 ) 102 }) 103 104 It("should return account by org", func() { 105 myaccount, err := newAccounts(server.URL()).FindByOrg("5718c403-58aa-40fd-9854-4001901e83bb", "us-south") 106 Expect(err).To(Succeed()) 107 Expect(myaccount).ShouldNot(BeNil()) 108 Expect(myaccount.GUID).Should(Equal("0e50220c5f6e1a13fdabe1c5cc87be32")) 109 Expect(myaccount.OwnerGUID).Should(Equal("a94cd8d9-5f77-4e2e-b2b4-adb23dd26117")) 110 111 }) 112 113 }) 114 115 Context("Server return no account by org", func() { 116 BeforeEach(func() { 117 server = ghttp.NewServer() 118 server.AppendHandlers( 119 ghttp.CombineHandlers( 120 ghttp.VerifyRequest(http.MethodPost, "/coe/v2/getaccounts"), 121 ghttp.VerifyBody([]byte(`{"organizations_region":[{"guid":"5718c403-58aa-40fd-9854-4001901e83bb","region":"xyz"}]}`)), 122 ghttp.RespondWith(http.StatusOK, `{ 123 "total_results": 0, 124 "resources": [ 125 ] 126 127 }`), 128 ), 129 ) 130 }) 131 132 It("should return no account", func() { 133 myaccount, err := newAccounts(server.URL()).FindByOrg("5718c403-58aa-40fd-9854-4001901e83bb", "xyz") 134 Expect(err).To(HaveOccurred()) 135 Expect(myaccount).To(BeNil()) 136 }) 137 138 }) 139 Context("Server return error", func() { 140 BeforeEach(func() { 141 server = ghttp.NewServer() 142 server.SetAllowUnhandledRequests(true) 143 server.AppendHandlers( 144 ghttp.CombineHandlers( 145 ghttp.VerifyRequest(http.MethodPost, "/coe/v2/getaccounts"), 146 ghttp.VerifyBody([]byte(`{"organizations_region":[{"guid":"5718c403-58aa-40fd-9854-4001901e83bb","region":"us-south"}]}`)), 147 ghttp.RespondWith(http.StatusInternalServerError, `{ 148 149 }`), 150 ), 151 ) 152 }) 153 154 It("should return error", func() { 155 myaccount, err := newAccounts(server.URL()).FindByOrg("5718c403-58aa-40fd-9854-4001901e83bb", "us-south") 156 Expect(err).To(HaveOccurred()) 157 Expect(myaccount).To(BeNil()) 158 }) 159 160 }) 161 162 }) 163 }) 164 165 //List Accounts 166 167 var _ = Describe("List Accounts", func() { 168 var server *ghttp.Server 169 AfterEach(func() { 170 server.Close() 171 }) 172 173 Describe("List Accounts", func() { 174 Context("Server return account", func() { 175 BeforeEach(func() { 176 server = ghttp.NewServer() 177 server.AppendHandlers( 178 ghttp.CombineHandlers( 179 ghttp.VerifyRequest(http.MethodGet, "/coe/v2/accounts"), 180 ghttp.RespondWith(http.StatusOK, `{ 181 "next_url":null, 182 "total_results":2, 183 "resources":[ 184 { 185 "metadata":{ 186 "guid":"707977742b81692deed713861fd1a320", 187 "url":"/coe/v2/accounts/707977742b81692deed713861fd1a320", 188 "created_at":"2014-07-21T18:44:57.115Z", 189 "updated_at":"2017-04-28T23:37:41.414Z" 190 }, 191 "entity":{ 192 "name":"Richard Gebhardt's Account", 193 "type":"TRIAL", 194 "state":"ACTIVE", 195 "owner":"62478eea-b929-4331-af4e-9acdf95e6f59", 196 "owner_userid":"gebhardt@us.ibm.com", 197 "owner_unique_id":"060001S23E", 198 "customer_id":"500000244", 199 "country_code":"USA", 200 "currency_code":"USD", 201 "billing_country_code":"USA", 202 "terms_and_conditions":{ 203 }, 204 "tags":[ 205 206 ], 207 "team_directory_enabled":true, 208 "organizations_region":[ 209 { 210 "guid":"3d6dfcd2-9d2a-408f-aa57-42383fcd0e3b", 211 "region":"us-south" 212 }, 213 { 214 "guid":"ae27ccdd-7240-4f96-82b9-2b1713391c8e", 215 "region":"us-south" 216 }, 217 { 218 "guid":"6d01a41a-c124-41c5-bb05-4c4a7509feab", 219 "region":"eu-gb" 220 }, 221 { 222 "guid":"fe49a9a9-95c5-40a3-844a-1c6f77fbd4ef", 223 "region":"eu-gb" 224 }, 225 { 226 "guid":"3391b99e-1163-4960-a3bc-be90e701bae4", 227 "region":"au-syd" 228 }, 229 { 230 "guid":"443d63ce-9ef5-473c-807a-6d63ffa26f0a", 231 "region":"eu-de" 232 }, 233 { 234 "guid":"bef1d514-2f57-44c2-84be-e4b07f7c3e0a", 235 "region":"eu-de" 236 } 237 ], 238 "linkages":[ 239 { 240 "origin":"IMS", 241 "state":"LINKABLE" 242 } 243 ], 244 "bluemix_subscriptions":[ 245 { 246 "type":"TRIAL", 247 "state":"ACTIVE", 248 "payment_method":{ 249 "type":"TRIAL_CREDIT", 250 "started":"05/28/2014 21:08:38", 251 "ended":"07/15/2018" 252 }, 253 "subscription_id":"500036292", 254 "part_number":"COE-Trial", 255 "subscriptionTags":[ 256 ] 257 } 258 ], 259 "subscription_id":"500036292", 260 "configuration_id":"", 261 "onboarded":-1 262 } 263 }, 264 { 265 "metadata":{ 266 "guid":"0e50220c5f6e1a13fdabe1c5cc87be32", 267 "url":"/coe/v2/accounts/0e50220c5f6e1a13fdabe1c5cc87be32", 268 "created_at":"2014-08-13T12:00:42.620Z", 269 "updated_at":"2017-04-28T23:33:44.415Z" 270 }, 271 "entity":{ 272 "name":"sakshi agarwal's Account", 273 "type":"TRIAL", 274 "state":"ACTIVE", 275 "owner":"a94cd8d9-5f77-4e2e-b2b4-adb23dd26117", 276 "owner_userid":"sakshiag@in.ibm.com", 277 "owner_unique_id":"270006H0V6", 278 "customer_id":"500198622", 279 "country_code":"USA", 280 "currency_code":"USD", 281 "billing_country_code":"USA", 282 "terms_and_conditions":{ 283 }, 284 "tags":[ 285 ], 286 "team_directory_enabled":true, 287 "organizations_region":[ 288 { 289 "guid":"5718c403-58aa-40fd-9854-4001901e83bb", 290 "region":"us-south" 291 }, 292 { 293 "guid":"900d4fda-0ed2-4679-a0ad-1ffmccp2b3ddd3", 294 "region":"eu-gb" 295 } 296 ], 297 "linkages":[ 298 { 299 "origin":"IMS", 300 "state":"LINKABLE" 301 } 302 ], 303 "bluemix_subscriptions":[ 304 { 305 "type":"TRIAL", 306 "state":"ACTIVE", 307 "payment_method":{ 308 "type":"TRIAL_CREDIT", 309 "started":"08/13/2014 12:00:39", 310 "ended":"07/15/2018" 311 }, 312 "subscription_id":"500096081", 313 "part_number":"COE-Trial", 314 "subscriptionTags":[ 315 ] 316 } 317 ], 318 "subscription_id":"500096081", 319 "configuration_id":"", 320 "onboarded":-1 321 } 322 } 323 ] 324 } 325 `), 326 ), 327 ) 328 }) 329 330 It("should return accounts", func() { 331 myaccounts, err := newAccounts(server.URL()).List() 332 333 Expect(err).To(Succeed()) 334 Expect(myaccounts).ShouldNot(BeNil()) 335 Expect(len(myaccounts)).To(Equal(2)) 336 account1 := myaccounts[0] 337 account2 := myaccounts[1] 338 Expect(account1.GUID).Should(Equal("707977742b81692deed713861fd1a320")) 339 Expect(account1.OwnerGUID).Should(Equal("62478eea-b929-4331-af4e-9acdf95e6f59")) 340 Expect(account2.GUID).Should(Equal("0e50220c5f6e1a13fdabe1c5cc87be32")) 341 Expect(account2.OwnerGUID).Should(Equal("a94cd8d9-5f77-4e2e-b2b4-adb23dd26117")) 342 343 }) 344 345 }) 346 Context("Server return no accounts", func() { 347 BeforeEach(func() { 348 server = ghttp.NewServer() 349 server.AppendHandlers( 350 ghttp.CombineHandlers( 351 ghttp.VerifyRequest(http.MethodGet, "/coe/v2/accounts"), 352 ghttp.RespondWith(http.StatusNotFound, `{ 353 "total_results": 0, 354 "resources": [ 355 ] 356 357 }`), 358 ), 359 ) 360 }) 361 362 It("should return no accounts", func() { 363 myaccounts, err := newAccounts(server.URL()).List() 364 Expect(err).To(HaveOccurred()) 365 Expect(len(myaccounts)).To(Equal(0)) 366 }) 367 368 }) 369 Context("Server return error", func() { 370 BeforeEach(func() { 371 server = ghttp.NewServer() 372 server.SetAllowUnhandledRequests(true) 373 server.AppendHandlers( 374 ghttp.CombineHandlers( 375 ghttp.VerifyRequest(http.MethodGet, "/coe/v2/accounts"), 376 ghttp.RespondWith(http.StatusInternalServerError, `{ 377 378 }`), 379 ), 380 ) 381 }) 382 383 It("should return error", func() { 384 myaccounts, err := newAccounts(server.URL()).List() 385 Expect(err).To(HaveOccurred()) 386 Expect(myaccounts).To(BeNil()) 387 }) 388 389 }) 390 391 }) 392 }) 393 394 //list account by owner id 395 396 var _ = Describe("List Accounts by owner id", func() { 397 var server *ghttp.Server 398 AfterEach(func() { 399 server.Close() 400 }) 401 402 Describe("List Accounts by owner id", func() { 403 Context("Server return account", func() { 404 BeforeEach(func() { 405 server = ghttp.NewServer() 406 server.AppendHandlers( 407 ghttp.CombineHandlers( 408 ghttp.VerifyRequest(http.MethodGet, "/coe/v2/accounts"), 409 ghttp.RespondWith(http.StatusOK, `{ 410 "next_url":null, 411 "total_results":1, 412 "resources":[ 413 { 414 "metadata":{ 415 "guid":"0e50220c5f6e1a13fdabe1c5cc87be32", 416 "url":"/coe/v2/accounts/0e50220c5f6e1a13fdabe1c5cc87be32", 417 "created_at":"2014-08-13T12:00:42.620Z", 418 "updated_at":"2017-04-28T23:33:44.415Z" 419 }, 420 "entity":{ 421 "name":"sakshi agarwal's Account", 422 "type":"TRIAL", 423 "state":"ACTIVE", 424 "owner":"a94cd8d9-5f77-4e2e-b2b4-adb23dd26117", 425 "owner_userid":"sakshiag@in.ibm.com", 426 "owner_unique_id":"270006H0V6", 427 "customer_id":"500198622", 428 "country_code":"USA", 429 "currency_code":"USD", 430 "billing_country_code":"USA", 431 "terms_and_conditions":{ 432 }, 433 "tags":[ 434 ], 435 "team_directory_enabled":true, 436 "organizations_region":[ 437 { 438 "guid":"5718c403-58aa-40fd-9854-4001901e83bb", 439 "region":"us-south" 440 }, 441 { 442 "guid":"900d4fda-0ed2-4679-a0ad-1ffmccp2b3ddd3", 443 "region":"eu-gb" 444 } 445 ], 446 "linkages":[ 447 { 448 "origin":"IMS", 449 "state":"LINKABLE" 450 } 451 ], 452 "bluemix_subscriptions":[ 453 { 454 "type":"TRIAL", 455 "state":"ACTIVE", 456 "payment_method":{ 457 "type":"TRIAL_CREDIT", 458 "started":"08/13/2014 12:00:39", 459 "ended":"07/15/2018" 460 }, 461 "subscription_id":"500096081", 462 "part_number":"COE-Trial", 463 "subscriptionTags":[ 464 ] 465 } 466 ], 467 "subscription_id":"500096081", 468 "configuration_id":"", 469 "onboarded":-1 470 } 471 } 472 ] 473 } 474 `), 475 ), 476 ) 477 }) 478 479 It("should return accounts by owner id", func() { 480 myaccounts, err := newAccounts(server.URL()).FindByOwner("sakshiag@in.ibm.com") 481 Expect(err).To(Succeed()) 482 Expect(myaccounts).ShouldNot(BeNil()) 483 Expect(myaccounts.GUID).Should(Equal("0e50220c5f6e1a13fdabe1c5cc87be32")) 484 Expect(myaccounts.OwnerGUID).Should(Equal("a94cd8d9-5f77-4e2e-b2b4-adb23dd26117")) 485 486 }) 487 488 }) 489 Context("Server return no accounts", func() { 490 BeforeEach(func() { 491 server = ghttp.NewServer() 492 server.AppendHandlers( 493 ghttp.CombineHandlers( 494 ghttp.VerifyRequest(http.MethodGet, "/coe/v2/accounts"), 495 ghttp.RespondWith(http.StatusNotFound, `{ 496 "total_results": 0, 497 "resources": [ 498 ] 499 500 }`), 501 ), 502 ) 503 }) 504 505 It("should return no accounts", func() { 506 myaccounts, err := newAccounts(server.URL()).FindByOwner("sakshiag@in.ibm.com") 507 Expect(err).To(HaveOccurred()) 508 Expect(myaccounts).To(BeNil()) 509 }) 510 511 }) 512 Context("Server return error", func() { 513 BeforeEach(func() { 514 server = ghttp.NewServer() 515 server.SetAllowUnhandledRequests(true) 516 server.AppendHandlers( 517 ghttp.CombineHandlers( 518 ghttp.VerifyRequest(http.MethodGet, "/coe/v2/accounts"), 519 ghttp.RespondWith(http.StatusInternalServerError, `{ 520 521 }`), 522 ), 523 ) 524 }) 525 526 It("should return error", func() { 527 myaccounts, err := newAccounts(server.URL()).FindByOwner("sakshiag@in.ibm.com") 528 Expect(err).To(HaveOccurred()) 529 Expect(myaccounts).To(BeNil()) 530 }) 531 532 }) 533 534 }) 535 }) 536 537 //get account by account id 538 539 var _ = Describe("Get Account by account id", func() { 540 var server *ghttp.Server 541 AfterEach(func() { 542 server.Close() 543 }) 544 545 Describe("Get Account by account id", func() { 546 Context("Server return account", func() { 547 BeforeEach(func() { 548 server = ghttp.NewServer() 549 server.AppendHandlers( 550 ghttp.CombineHandlers( 551 ghttp.VerifyRequest(http.MethodGet, "/coe/v2/accounts/e9021a4d06e9b108b4a221a3cec47e3d"), 552 ghttp.RespondWith(http.StatusOK, `{ 553 "metadata": { 554 "guid": "e9021a4d06e9b108b4a221a3cec47e3d", 555 "url": "/coe/v2/accounts/e9021a4d06e9b108b4a221a3cec47e3d", 556 "created_at": "2015-09-02T10:35:17.288Z", 557 "updated_at": "2017-04-28T23:44:27.510Z" 558 }, 559 "entity": { 560 "name": "Praveen G's Account", 561 "type": "TRIAL", 562 "state": "ACTIVE", 563 "owner": "a5d45507-836d-4609-8d6f-2972a05c9420", 564 "owner_userid": "praveek9@in.ibm.com", 565 "owner_unique_id": "2700067HF8", 566 "customer_id": "501280599", 567 "country_code": "IND", 568 "currency_code": "INR", 569 "billing_country_code": "IND", 570 "terms_and_conditions": {}, 571 "tags": [], 572 "team_directory_enabled": true, 573 "organizations_region": [ 574 { 575 "guid": "278f5173-c6b7-41b8-a2da-517daf27162c", 576 "region": "eu-gb" 577 }, 578 { 579 "guid": "fc14269d-8ecd-403a-b3a8-0556372b4537", 580 "region": "us-south" 581 } 582 ], 583 "linkages": [ 584 { 585 "origin": "IMS", 586 "state": "LINKABLE" 587 } 588 ], 589 "bluemix_subscriptions": [ 590 { 591 "type": "TRIAL", 592 "state": "ACTIVE", 593 "payment_method": { 594 "type": "TRIAL_CREDIT", 595 "started": "09/02/2015 10:35:12", 596 "ended": "07/15/2018" 597 }, 598 "subscription_id": "500711198", 599 "part_number": "COE-Trial", 600 "subscriptionTags": [], 601 "history": [] 602 } 603 ], 604 "subscription_id": "500711198", 605 "configuration_id": "", 606 "onboarded": -1 607 } 608 } 609 610 `), 611 ), 612 ) 613 }) 614 615 It("should return account by account id", func() { 616 myaccounts, err := newAccounts(server.URL()).Get("e9021a4d06e9b108b4a221a3cec47e3d") 617 Expect(err).To(Succeed()) 618 Expect(myaccounts).ShouldNot(BeNil()) 619 Expect(myaccounts.Name).Should(Equal("Praveen G's Account")) 620 Expect(myaccounts.GUID).Should(Equal("e9021a4d06e9b108b4a221a3cec47e3d")) 621 622 }) 623 624 }) 625 Context("Server return error", func() { 626 BeforeEach(func() { 627 server = ghttp.NewServer() 628 server.SetAllowUnhandledRequests(true) 629 server.AppendHandlers( 630 ghttp.CombineHandlers( 631 ghttp.VerifyRequest(http.MethodGet, "/coe/v2/accounts/e9021a4d06e9b108b4a221a3cec47e3d"), 632 ghttp.RespondWith(http.StatusInternalServerError, `{ 633 634 }`), 635 ), 636 ) 637 }) 638 639 It("should return error", func() { 640 myaccounts, err := newAccounts(server.URL()).Get("e9021a4d06e9b108b4a221a3cec47e3d") 641 Expect(err).To(HaveOccurred()) 642 Expect(myaccounts).To(BeNil()) 643 }) 644 645 }) 646 647 }) 648 }) 649 650 func newAccounts(url string) Accounts { 651 652 sess, err := session.New() 653 if err != nil { 654 log.Fatal(err) 655 } 656 conf := sess.Config.Copy() 657 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 658 conf.Endpoint = &url 659 660 client := client.Client{ 661 Config: conf, 662 ServiceName: bluemix.AccountService, 663 } 664 665 return newAccountAPI(&client) 666 }