github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/service_keys_test.go (about)

     1  package mccpv2
     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  var _ = Describe("ServiceKeys", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  
    23  	Describe("Create", func() {
    24  		Context("When creation is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodPost, "/v2/service_keys"),
    30  						ghttp.VerifyBody([]byte(`{"name":"testkey","service_instance_guid":"f91adfe2-76c9-4649-939e-b01c37a3704c"}`)),
    31  						ghttp.RespondWith(http.StatusCreated, `{							 	
    32  								"metadata": {
    33  									"guid": "c4432b8e-cb14-4225-aa10-1f775b3b1c92",
    34  									"url": "/v2/service_keys/c4432b8e-cb14-4225-aa10-1f775b3b1c92",
    35  									"created_at": "2017-05-04T08:02:50Z",
    36  									"updated_at": null
    37  								},
    38  								"entity": {
    39  										"name": "testkey",
    40  										"service_instance_guid": "f91adfe2-76c9-4649-939e-b01c37a3704c",
    41  										"credentials": {
    42  											"username": "c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix",
    43  											"password":"[PRIVATE DATA HIDDEN]",
    44  											"host": "c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix.cloudant.com",
    45  											"port": 443,
    46  											"url": "https://c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix:8b54e955ebd83c07e1f6cb78edeece5ad53f92ffa75962daa0087265768f4ee6@c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix.cloudant.com"
    47  										},
    48  										"service_instance_url": "/v2/service_instances/f91adfe2-76c9-4649-939e-b01c37a3704c"
    49  									}
    50  
    51  						}`),
    52  					),
    53  				)
    54  			})
    55  
    56  			It("should return service key created", func() {
    57  				myservicekey, err := newServiceKeys(server.URL()).Create("f91adfe2-76c9-4649-939e-b01c37a3704c", "testkey", nil)
    58  				Expect(err).NotTo(HaveOccurred())
    59  				Expect(myservicekey).ShouldNot(BeNil())
    60  				Expect(myservicekey.Metadata.GUID).Should(Equal("c4432b8e-cb14-4225-aa10-1f775b3b1c92"))
    61  				Expect(myservicekey.Entity.Name).Should(Equal("testkey"))
    62  				Expect(myservicekey.Entity.ServiceInstanceGUID).Should(Equal("f91adfe2-76c9-4649-939e-b01c37a3704c"))
    63  				Expect(myservicekey.Entity.Credentials).ShouldNot(BeNil())
    64  			})
    65  		})
    66  		Context("When creation is failed", func() {
    67  			BeforeEach(func() {
    68  				server = ghttp.NewServer()
    69  				server.SetAllowUnhandledRequests(true)
    70  				server.AppendHandlers(
    71  					ghttp.CombineHandlers(
    72  						ghttp.VerifyRequest(http.MethodPost, "/v2/service_keys"),
    73  						ghttp.VerifyBody([]byte(`{"name":"testkey","service_instance_guid":"f91adfe2-76c9-4649-939e-b01c37a3704c"}`)),
    74  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create service key`),
    75  					),
    76  				)
    77  			})
    78  
    79  			It("should return error during service key creation", func() {
    80  				myservicekey, err := newServiceKeys(server.URL()).Create("f91adfe2-76c9-4649-939e-b01c37a3704c", "testkey", nil)
    81  				Expect(err).To(HaveOccurred())
    82  				Expect(myservicekey).Should(BeNil())
    83  			})
    84  		})
    85  
    86  	})
    87  
    88  	Describe("Get", func() {
    89  		Context("When read of service key is successful", func() {
    90  			BeforeEach(func() {
    91  				server = ghttp.NewServer()
    92  				server.AppendHandlers(
    93  					ghttp.CombineHandlers(
    94  						ghttp.VerifyRequest(http.MethodGet, "/v2/service_keys/c4432b8e-cb14-4225-aa10-1f775b3b1c92"),
    95  						ghttp.RespondWith(http.StatusOK, `{
    96  							"metadata": {
    97  									"guid": "c4432b8e-cb14-4225-aa10-1f775b3b1c92",
    98  									"url": "/v2/service_keys/c4432b8e-cb14-4225-aa10-1f775b3b1c92",
    99  									"created_at": "2017-05-04T08:02:50Z",
   100  									"updated_at": null
   101  								},
   102  							"entity": {
   103  									"name": "testkey",
   104  									"service_instance_guid": "f91adfe2-76c9-4649-939e-b01c37a3704c",
   105  									"credentials": {
   106  										"username": "c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix",
   107  										"password":"[PRIVATE DATA HIDDEN]",
   108  										"host": "c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix.cloudant.com",
   109  										"port": 443,
   110  										"url": "https://c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix:8b54e955ebd83c07e1f6cb78edeece5ad53f92ffa75962daa0087265768f4ee6@c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix.cloudant.com"
   111  									},
   112  									"service_instance_url": "/v2/service_instances/f91adfe2-76c9-4649-939e-b01c37a3704c"
   113  								}
   114  
   115  						}`),
   116  					),
   117  				)
   118  			})
   119  
   120  			It("should return service key", func() {
   121  				myservicekey, err := newServiceKeys(server.URL()).Get("c4432b8e-cb14-4225-aa10-1f775b3b1c92")
   122  				Expect(err).NotTo(HaveOccurred())
   123  				Expect(myservicekey).ShouldNot(BeNil())
   124  				Expect(myservicekey.Metadata.GUID).Should(Equal("c4432b8e-cb14-4225-aa10-1f775b3b1c92"))
   125  				Expect(myservicekey.Entity.Name).Should(Equal("testkey"))
   126  				Expect(myservicekey.Entity.ServiceInstanceGUID).Should(Equal("f91adfe2-76c9-4649-939e-b01c37a3704c"))
   127  				Expect(myservicekey.Entity.Credentials).ShouldNot(BeNil())
   128  			})
   129  		})
   130  		Context("When service key retrieve is failed", func() {
   131  			BeforeEach(func() {
   132  				server = ghttp.NewServer()
   133  				server.SetAllowUnhandledRequests(true)
   134  				server.AppendHandlers(
   135  					ghttp.CombineHandlers(
   136  						ghttp.VerifyRequest(http.MethodGet, "/v2/service_keys/c4432b8e-cb14-4225-aa10-1f775b3b1c92"),
   137  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve service key`),
   138  					),
   139  				)
   140  			})
   141  
   142  			It("should return error when service key is retrieved", func() {
   143  				myservicekey, err := newServiceKeys(server.URL()).Get("c4432b8e-cb14-4225-aa10-1f775b3b1c92")
   144  				Expect(err).To(HaveOccurred())
   145  				Expect(myservicekey).Should(BeNil())
   146  			})
   147  		})
   148  	})
   149  
   150  	Describe("Delete", func() {
   151  		Context("When delete of service key is successful", func() {
   152  			BeforeEach(func() {
   153  				server = ghttp.NewServer()
   154  				server.AppendHandlers(
   155  					ghttp.CombineHandlers(
   156  						ghttp.VerifyRequest(http.MethodDelete, "/v2/service_keys/c4432b8e-cb14-4225-aa10-1f775b3b1c92"),
   157  						ghttp.RespondWith(http.StatusOK, `{							
   158  						}`),
   159  					),
   160  				)
   161  			})
   162  
   163  			It("should delete service key", func() {
   164  				err := newServiceKeys(server.URL()).Delete("c4432b8e-cb14-4225-aa10-1f775b3b1c92")
   165  				Expect(err).NotTo(HaveOccurred())
   166  			})
   167  		})
   168  		Context("When service key delete is failed", func() {
   169  			BeforeEach(func() {
   170  				server = ghttp.NewServer()
   171  				server.SetAllowUnhandledRequests(true)
   172  				server.AppendHandlers(
   173  					ghttp.CombineHandlers(
   174  						ghttp.VerifyRequest(http.MethodDelete, "/v2/service_keys/c4432b8e-cb14-4225-aa10-1f775b3b1c92"),
   175  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete service key`),
   176  					),
   177  				)
   178  			})
   179  
   180  			It("should return error service key delete", func() {
   181  				err := newServiceKeys(server.URL()).Delete("c4432b8e-cb14-4225-aa10-1f775b3b1c92")
   182  				Expect(err).To(HaveOccurred())
   183  			})
   184  		})
   185  	})
   186  
   187  })
   188  
   189  var _ = Describe("Service Key by Name", func() {
   190  	var server *ghttp.Server
   191  	AfterEach(func() {
   192  		server.Close()
   193  	})
   194  	Describe("FindByName()", func() {
   195  		Context("Server return service key by name", func() {
   196  			BeforeEach(func() {
   197  				server = ghttp.NewServer()
   198  				server.AppendHandlers(
   199  					ghttp.CombineHandlers(
   200  						ghttp.VerifyRequest(http.MethodGet, "/v2/service_instances/f91adfe2-76c9-4649-939e-b01c37a3704c/service_keys"),
   201  						ghttp.RespondWith(http.StatusOK, `{
   202  							"total_results": 1,
   203  							"total_pages": 1,
   204  							"prev_url": null,
   205  							"next_url": null,
   206  							"resources": [
   207  								{
   208  									"metadata": {
   209  										"guid": "c4432b8e-cb14-4225-aa10-1f775b3b1c92",
   210  										"url": "/v2/service_keys/c4432b8e-cb14-4225-aa10-1f775b3b1c92",
   211  										"created_at": "2017-05-04T08:02:50Z",
   212  										"updated_at": null
   213  									},
   214  									"entity": {
   215  										"name": "testkey",
   216  										"service_instance_guid": "f91adfe2-76c9-4649-939e-b01c37a3704c",
   217  										"credentials": {
   218  											"username": "c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix",
   219  											"password":"[PRIVATE DATA HIDDEN]",
   220  											"host": "c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix.cloudant.com",
   221  											"port": 443,
   222  											"url": "https://c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix:8b54e955ebd83c07e1f6cb78edeece5ad53f92ffa75962daa0087265768f4ee6@c59f2b23-c2ee-4bc2-baad-2d231da22c01-bluemix.cloudant.com"
   223  										},
   224  										"service_instance_url": "/v2/service_instances/f91adfe2-76c9-4649-939e-b01c37a3704c"
   225  									}
   226  								}
   227  							]
   228  
   229  						}`),
   230  					),
   231  				)
   232  			})
   233  
   234  			It("should return service key", func() {
   235  				myservicekey, err := newServiceKeys(server.URL()).FindByName("f91adfe2-76c9-4649-939e-b01c37a3704c", "testkey")
   236  				Expect(err).NotTo(HaveOccurred())
   237  				Expect(myservicekey).ShouldNot(BeNil())
   238  				Expect(myservicekey.GUID).Should(Equal("c4432b8e-cb14-4225-aa10-1f775b3b1c92"))
   239  				Expect(myservicekey.Name).Should(Equal("testkey"))
   240  				Expect(myservicekey.ServiceInstanceGUID).Should(Equal("f91adfe2-76c9-4649-939e-b01c37a3704c"))
   241  				Expect(myservicekey.Credentials).ShouldNot(BeNil())
   242  			})
   243  
   244  		})
   245  
   246  		Context("Server return no service key", func() {
   247  			BeforeEach(func() {
   248  				server = ghttp.NewServer()
   249  				server.AppendHandlers(
   250  					ghttp.CombineHandlers(
   251  						ghttp.VerifyRequest(http.MethodGet, "/v2/service_instances/f91adfe2-76c9-4649-939e-b01c37a3704c/service_keys"),
   252  						ghttp.RespondWith(http.StatusOK, `{
   253  							"total_results": 0,
   254  							"resources": [
   255  							]
   256  															
   257  						}`),
   258  					),
   259  				)
   260  			})
   261  
   262  			It("should return no service key", func() {
   263  				myservicekey, err := newServiceKeys(server.URL()).FindByName("f91adfe2-76c9-4649-939e-b01c37a3704c", "testkey")
   264  				Expect(err).To(HaveOccurred())
   265  				Expect(myservicekey).To(BeNil())
   266  			})
   267  
   268  		})
   269  		Context("Server return error", func() {
   270  			BeforeEach(func() {
   271  				server = ghttp.NewServer()
   272  				server.SetAllowUnhandledRequests(true)
   273  				server.AppendHandlers(
   274  					ghttp.CombineHandlers(
   275  						ghttp.VerifyRequest(http.MethodGet, "/v2/service_instances/f91adfe2-76c9-4649-939e-b01c37a3704c/service_keys"),
   276  						ghttp.RespondWith(http.StatusInternalServerError, `{
   277  															
   278  						}`),
   279  					),
   280  				)
   281  			})
   282  
   283  			It("should return error service key", func() {
   284  				myservicekey, err := newServiceKeys(server.URL()).FindByName("f91adfe2-76c9-4649-939e-b01c37a3704c", "testkey")
   285  				Expect(err).To(HaveOccurred())
   286  				Expect(myservicekey).To(BeNil())
   287  			})
   288  
   289  		})
   290  
   291  	})
   292  })
   293  
   294  func newServiceKeys(url string) ServiceKeys {
   295  
   296  	sess, err := session.New()
   297  	if err != nil {
   298  		log.Fatal(err)
   299  	}
   300  	conf := sess.Config.Copy()
   301  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   302  	conf.Endpoint = &url
   303  
   304  	client := client.Client{
   305  		Config:      conf,
   306  		ServiceName: bluemix.MccpService,
   307  	}
   308  	return newServiceKeyAPI(&client)
   309  }