github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/service_bindings_test.go (about) 1 package api_test 2 3 import ( 4 "net/http" 5 "time" 6 7 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 8 "code.cloudfoundry.org/cli/cf/errors" 9 "code.cloudfoundry.org/cli/cf/models" 10 "code.cloudfoundry.org/cli/cf/net" 11 "code.cloudfoundry.org/cli/cf/terminal/terminalfakes" 12 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 13 14 . "code.cloudfoundry.org/cli/cf/api" 15 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 "github.com/onsi/gomega/ghttp" 19 ) 20 21 var _ = Describe("ServiceBindingsRepository", func() { 22 var ( 23 server *ghttp.Server 24 configRepo coreconfig.ReadWriter 25 repo CloudControllerServiceBindingRepository 26 ) 27 28 BeforeEach(func() { 29 server = ghttp.NewServer() 30 configRepo = testconfig.NewRepositoryWithDefaults() 31 configRepo.SetAPIEndpoint(server.URL()) 32 gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 33 repo = NewCloudControllerServiceBindingRepository(configRepo, gateway) 34 }) 35 36 AfterEach(func() { 37 server.Close() 38 }) 39 40 Describe("Create", func() { 41 var requestBody string 42 43 Context("when the service binding can be created", func() { 44 JustBeforeEach(func() { 45 server.AppendHandlers( 46 ghttp.CombineHandlers( 47 ghttp.VerifyRequest("POST", "/v2/service_bindings"), 48 ghttp.VerifyJSON(requestBody), 49 ghttp.RespondWith(http.StatusCreated, nil), 50 ), 51 ) 52 }) 53 54 Context("no parameters passed", func() { 55 BeforeEach(func() { 56 requestBody = `{ 57 "app_guid":"my-app-guid", 58 "service_instance_guid":"my-service-instance-guid" 59 }` 60 }) 61 62 It("creates the service binding", func() { 63 err := repo.Create("my-service-instance-guid", "my-app-guid", nil) 64 Expect(err).NotTo(HaveOccurred()) 65 66 Expect(server.ReceivedRequests()).To(HaveLen(1)) 67 }) 68 }) 69 70 Context("when there are arbitrary parameters", func() { 71 BeforeEach(func() { 72 requestBody = `{ 73 "app_guid":"my-app-guid", 74 "service_instance_guid":"my-service-instance-guid", 75 "parameters": { "foo": "bar" } 76 }` 77 }) 78 79 It("send the parameters as part of the request body", func() { 80 err := repo.Create( 81 "my-service-instance-guid", 82 "my-app-guid", 83 map[string]interface{}{"foo": "bar"}, 84 ) 85 Expect(err).NotTo(HaveOccurred()) 86 87 Expect(server.ReceivedRequests()).To(HaveLen(1)) 88 }) 89 90 Context("and there is a failure during serialization", func() { 91 It("returns the serialization error", func() { 92 paramsMap := make(map[string]interface{}) 93 paramsMap["data"] = make(chan bool) 94 95 err := repo.Create("my-service-instance-guid", "my-app-guid", paramsMap) 96 Expect(err).To(MatchError("json: unsupported type: chan bool")) 97 }) 98 }) 99 }) 100 }) 101 102 Context("when an API error occurs", func() { 103 BeforeEach(func() { 104 server.AppendHandlers( 105 ghttp.CombineHandlers( 106 ghttp.VerifyRequest("POST", "/v2/service_bindings"), 107 ghttp.VerifyJSON(`{ 108 "app_guid":"my-app-guid", 109 "service_instance_guid":"my-service-instance-guid" 110 }`), 111 ghttp.RespondWith(http.StatusBadRequest, `{ 112 "code":90003, 113 "description":"The app space binding to service is taken: 7b959018-110a-4913-ac0a-d663e613cdea 346bf237-7eef-41a7-b892-68fb08068f09" 114 }`), 115 ), 116 ) 117 }) 118 119 It("returns an error", func() { 120 err := repo.Create("my-service-instance-guid", "my-app-guid", nil) 121 Expect(err).To(HaveOccurred()) 122 Expect(err.(errors.HTTPError).ErrorCode()).To(Equal("90003")) 123 }) 124 }) 125 }) 126 127 Describe("Delete", func() { 128 var serviceInstance models.ServiceInstance 129 130 BeforeEach(func() { 131 serviceInstance.GUID = "my-service-instance-guid" 132 }) 133 134 Context("when binding does exist", func() { 135 BeforeEach(func() { 136 server.AppendHandlers( 137 ghttp.CombineHandlers( 138 ghttp.VerifyRequest("DELETE", "/v2/service_bindings/service-binding-2-guid"), 139 ghttp.RespondWith(http.StatusOK, nil), 140 ), 141 ) 142 143 serviceInstance.ServiceBindings = []models.ServiceBindingFields{ 144 { 145 URL: "/v2/service_bindings/service-binding-1-guid", 146 AppGUID: "app-1-guid", 147 }, 148 { 149 URL: "/v2/service_bindings/service-binding-2-guid", 150 AppGUID: "app-2-guid", 151 }, 152 } 153 }) 154 155 It("deletes the service binding with the given guid", func() { 156 found, err := repo.Delete(serviceInstance, "app-2-guid") 157 Expect(err).NotTo(HaveOccurred()) 158 Expect(found).To(BeTrue()) 159 160 Expect(server.ReceivedRequests()).To(HaveLen(1)) 161 }) 162 }) 163 164 Context("when binding does not exist", func() { 165 It("does not return an error", func() { 166 found, err := repo.Delete(serviceInstance, "app-3-guid") 167 Expect(err).NotTo(HaveOccurred()) 168 Expect(found).To(BeFalse()) 169 170 Expect(server.ReceivedRequests()).To(HaveLen(0)) 171 }) 172 }) 173 }) 174 175 Describe("ListAllForService", func() { 176 Context("when binding does exist", func() { 177 BeforeEach(func() { 178 server.AppendHandlers( 179 ghttp.CombineHandlers( 180 ghttp.VerifyRequest("GET", "/v2/service_instances/service-instance-guid/service_bindings"), 181 ghttp.RespondWith(http.StatusOK, `{ 182 "total_results": 2, 183 "total_pages": 2, 184 "next_url": "/v2/service_instances/service-instance-guid/service_bindings?page=2", 185 "resources": [ 186 { 187 "metadata": { 188 "guid": "service-binding-1-guid", 189 "url": "/v2/service_bindings/service-binding-1-guid", 190 "created_at": "2016-04-22T19:33:31Z", 191 "updated_at": null 192 }, 193 "entity": { 194 "app_guid": "app-guid-1" 195 } 196 } 197 ] 198 }`)), 199 ghttp.CombineHandlers( 200 ghttp.VerifyRequest("GET", "/v2/service_instances/service-instance-guid/service_bindings", "page=2"), 201 ghttp.RespondWith(http.StatusOK, `{ 202 "total_results": 2, 203 "total_pages": 2, 204 "resources": [ 205 { 206 "metadata": { 207 "guid": "service-binding-2-guid", 208 "url": "/v2/service_bindings/service-binding-2-guid", 209 "created_at": "2016-04-22T19:33:31Z", 210 "updated_at": null 211 }, 212 "entity": { 213 "app_guid": "app-guid-2" 214 } 215 } 216 ] 217 }`)), 218 ) 219 }) 220 221 It("returns the list of service instances", func() { 222 bindings, err := repo.ListAllForService("service-instance-guid") 223 Expect(err).NotTo(HaveOccurred()) 224 225 Expect(bindings).To(HaveLen(2)) 226 Expect(bindings[0].AppGUID).To(Equal("app-guid-1")) 227 Expect(bindings[1].AppGUID).To(Equal("app-guid-2")) 228 }) 229 }) 230 231 Context("when the service does not exist", func() { 232 BeforeEach(func() { 233 server.AppendHandlers(ghttp.CombineHandlers( 234 ghttp.VerifyRequest("GET", "/v2/service_instances/service-instance-guid/service_bindings"), 235 ghttp.RespondWith(http.StatusGatewayTimeout, nil), 236 )) 237 }) 238 239 It("returns an error", func() { 240 _, err := repo.ListAllForService("service-instance-guid") 241 Expect(err).To(HaveOccurred()) 242 243 Expect(server.ReceivedRequests()).To(HaveLen(1)) 244 }) 245 }) 246 }) 247 })