github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/account/accountv1/accounts_test.go (about)

     1  package accountv1
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  
     9  	bluemix "github.com/IBM-Cloud/bluemix-go"
    10  	"github.com/IBM-Cloud/bluemix-go/client"
    11  	bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
    12  	"github.com/IBM-Cloud/bluemix-go/session"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	"github.com/onsi/gomega/ghttp"
    17  )
    18  
    19  var _ = Describe("Accountsv1", func() {
    20  
    21  	accountGuid := "9a0d1cdd086428060e43b333decd27dd"
    22  	userEmail := "praveek9@in.ibm.com"
    23  	userGuid := "e9021a4d06e9b108b4a221a3cec47e3d"
    24  
    25  	accountInviteResponse := AccountInviteResponse{
    26  		Id:    userGuid,
    27  		Email: userEmail,
    28  		State: "PENDING",
    29  	}
    30  
    31  	jsonAccountInviteResponse, _ := json.Marshal(accountInviteResponse)
    32  
    33  	var server *ghttp.Server
    34  	AfterEach(func() {
    35  		server.Close()
    36  	})
    37  
    38  	Describe("Invite user to account", func() {
    39  		Context("Server returns response", func() {
    40  			BeforeEach(func() {
    41  				server = ghttp.NewServer()
    42  				server.AppendHandlers(
    43  					ghttp.CombineHandlers(
    44  						ghttp.VerifyRequest(http.MethodPost,
    45  							fmt.Sprintf("/v2/accounts/%s/users", accountGuid)),
    46  						ghttp.RespondWith(http.StatusOK, jsonAccountInviteResponse),
    47  					),
    48  				)
    49  			})
    50  
    51  			It("Should return a response", func() {
    52  				resp, err := newAccounts(server.URL()).InviteAccountUser(accountGuid, userEmail)
    53  				Expect(err).To(Succeed())
    54  				Expect(resp).ShouldNot(BeNil())
    55  				Expect(resp).Should(Equal(accountInviteResponse))
    56  			})
    57  		})
    58  	})
    59  
    60  	Describe("Delete a user from an account", func() {
    61  		Context("Deleting user", func() {
    62  			BeforeEach(func() {
    63  				server = ghttp.NewServer()
    64  				server.AppendHandlers(
    65  					ghttp.CombineHandlers(
    66  						ghttp.VerifyRequest(http.MethodDelete,
    67  							fmt.Sprintf("/v2/accounts/%s/users/%s", accountGuid, userGuid)),
    68  						ghttp.RespondWith(http.StatusOK, jsonAccountInviteResponse),
    69  					),
    70  				)
    71  			})
    72  
    73  			It("Should not return an error", func() {
    74  				resp := newAccounts(server.URL()).DeleteAccountUser(accountGuid, userGuid)
    75  				Expect(resp).To(Succeed())
    76  				Expect(resp).Should(BeNil())
    77  			})
    78  		})
    79  	})
    80  })
    81  
    82  func newAccounts(url string) Accounts {
    83  
    84  	sess, err := session.New()
    85  	if err != nil {
    86  		log.Fatal(err)
    87  	}
    88  	conf := sess.Config.Copy()
    89  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
    90  	conf.Endpoint = &url
    91  
    92  	client := client.Client{
    93  		Config:      conf,
    94  		ServiceName: bluemix.AccountService,
    95  	}
    96  
    97  	return newAccountAPI(&client)
    98  }