github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/service_credential_binding_test.go (about) 1 package ccv3_test 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "net/http" 8 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/ccv3fakes" 12 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 13 "code.cloudfoundry.org/cli/resources" 14 "code.cloudfoundry.org/cli/types" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("Service Credential Bindings", func() { 20 var ( 21 requester *ccv3fakes.FakeRequester 22 client *Client 23 ) 24 25 BeforeEach(func() { 26 requester = new(ccv3fakes.FakeRequester) 27 client, _ = NewFakeRequesterTestClient(requester) 28 }) 29 30 Describe("CreateServiceCredentialBinding", func() { 31 When("the request succeeds", func() { 32 It("returns warnings and no errors", func() { 33 requester.MakeRequestReturns("fake-job-url", Warnings{"fake-warning"}, nil) 34 35 binding := resources.ServiceCredentialBinding{ 36 ServiceInstanceGUID: "fake-service-instance-guid", 37 AppGUID: "fake-app-guid", 38 } 39 40 jobURL, warnings, err := client.CreateServiceCredentialBinding(binding) 41 42 Expect(jobURL).To(Equal(JobURL("fake-job-url"))) 43 Expect(warnings).To(ConsistOf("fake-warning")) 44 Expect(err).NotTo(HaveOccurred()) 45 46 Expect(requester.MakeRequestCallCount()).To(Equal(1)) 47 Expect(requester.MakeRequestArgsForCall(0)).To(Equal(RequestParams{ 48 RequestName: internal.PostServiceCredentialBindingRequest, 49 RequestBody: binding, 50 })) 51 }) 52 }) 53 54 When("the request fails", func() { 55 It("returns errors and warnings", func() { 56 requester.MakeRequestReturns("", Warnings{"fake-warning"}, errors.New("bang")) 57 58 jobURL, warnings, err := client.CreateServiceCredentialBinding(resources.ServiceCredentialBinding{}) 59 60 Expect(jobURL).To(BeEmpty()) 61 Expect(warnings).To(ConsistOf("fake-warning")) 62 Expect(err).To(MatchError("bang")) 63 }) 64 }) 65 }) 66 67 Describe("GetServiceCredentialBindings", func() { 68 var ( 69 query []Query 70 bindings []resources.ServiceCredentialBinding 71 includedApps []resources.Application 72 warnings Warnings 73 executeErr error 74 ) 75 76 BeforeEach(func() { 77 requester.MakeListRequestCalls(func(requestParams RequestParams) (IncludedResources, Warnings, error) { 78 types := []resources.ServiceCredentialBindingType{resources.KeyBinding, resources.AppBinding} 79 for i := 1; i <= 3; i++ { 80 Expect(requestParams.AppendToList(resources.ServiceCredentialBinding{ 81 GUID: fmt.Sprintf("credential-binding-%d-guid", i), 82 Name: fmt.Sprintf("credential-binding-%d-name", i), 83 ServiceInstanceGUID: fmt.Sprintf("si-%d-guid", i), 84 AppGUID: fmt.Sprintf("app-%d-guid", i), 85 Type: types[i%2], 86 })).NotTo(HaveOccurred()) 87 } 88 return IncludedResources{Apps: includedApps}, Warnings{"warning-1", "warning-2"}, nil 89 }) 90 91 includedApps = nil 92 93 query = []Query{ 94 {Key: ServiceInstanceGUIDFilter, Values: []string{"si-1-guid", "si-2-guid", "si-3-guid", "si-4-guid"}}, 95 } 96 }) 97 98 JustBeforeEach(func() { 99 bindings, warnings, executeErr = client.GetServiceCredentialBindings(query...) 100 }) 101 102 It("makes the correct call", func() { 103 Expect(requester.MakeListRequestCallCount()).To(Equal(1)) 104 actualParams := requester.MakeListRequestArgsForCall(0) 105 Expect(actualParams.RequestName).To(Equal(internal.GetServiceCredentialBindingsRequest)) 106 Expect(actualParams.Query).To(ConsistOf(Query{Key: ServiceInstanceGUIDFilter, Values: []string{"si-1-guid", "si-2-guid", "si-3-guid", "si-4-guid"}})) 107 Expect(actualParams.ResponseBody).To(BeAssignableToTypeOf(resources.ServiceCredentialBinding{})) 108 }) 109 110 It("returns a list of service credential bindings", func() { 111 Expect(executeErr).ToNot(HaveOccurred()) 112 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 113 114 Expect(bindings).To(ConsistOf( 115 resources.ServiceCredentialBinding{ 116 GUID: "credential-binding-1-guid", 117 Name: "credential-binding-1-name", 118 ServiceInstanceGUID: "si-1-guid", 119 AppGUID: "app-1-guid", 120 Type: resources.AppBinding, 121 }, 122 resources.ServiceCredentialBinding{ 123 GUID: "credential-binding-2-guid", 124 Name: "credential-binding-2-name", 125 ServiceInstanceGUID: "si-2-guid", 126 AppGUID: "app-2-guid", 127 Type: resources.KeyBinding, 128 }, 129 resources.ServiceCredentialBinding{ 130 GUID: "credential-binding-3-guid", 131 Name: "credential-binding-3-name", 132 ServiceInstanceGUID: "si-3-guid", 133 AppGUID: "app-3-guid", 134 Type: resources.AppBinding, 135 }, 136 )) 137 }) 138 139 When("app resources are included via the query", func() { 140 BeforeEach(func() { 141 query = append(query, Query{Key: Include, Values: []string{"app"}}) 142 143 includedApps = []resources.Application{ 144 {GUID: "app-1-guid", Name: "app-1", SpaceGUID: "space-1-guid"}, 145 {GUID: "app-3-guid", Name: "app-3", SpaceGUID: "space-2-guid"}, 146 } 147 }) 148 149 It("returns the app names", func() { 150 Expect(executeErr).ToNot(HaveOccurred()) 151 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 152 153 Expect(bindings[0].AppName).To(Equal("app-1")) 154 Expect(bindings[0].AppSpaceGUID).To(Equal("space-1-guid")) 155 Expect(bindings[1].AppName).To(BeEmpty()) 156 Expect(bindings[1].AppSpaceGUID).To(BeEmpty()) 157 Expect(bindings[2].AppName).To(Equal("app-3")) 158 Expect(bindings[2].AppSpaceGUID).To(Equal("space-2-guid")) 159 }) 160 }) 161 162 When("the cloud controller returns errors and warnings", func() { 163 BeforeEach(func() { 164 errors := []ccerror.V3Error{ 165 { 166 Code: 42424, 167 Detail: "Some detailed error message", 168 Title: "CF-SomeErrorTitle", 169 }, 170 { 171 Code: 11111, 172 Detail: "Some other detailed error message", 173 Title: "CF-SomeOtherErrorTitle", 174 }, 175 } 176 177 requester.MakeListRequestReturns( 178 IncludedResources{}, 179 Warnings{"this is a warning"}, 180 ccerror.MultiError{ResponseCode: http.StatusTeapot, Errors: errors}, 181 ) 182 }) 183 184 It("returns the error and all warnings", func() { 185 Expect(executeErr).To(MatchError(ccerror.MultiError{ 186 ResponseCode: http.StatusTeapot, 187 Errors: []ccerror.V3Error{ 188 { 189 Code: 42424, 190 Detail: "Some detailed error message", 191 Title: "CF-SomeErrorTitle", 192 }, 193 { 194 Code: 11111, 195 Detail: "Some other detailed error message", 196 Title: "CF-SomeOtherErrorTitle", 197 }, 198 }, 199 })) 200 Expect(warnings).To(ConsistOf("this is a warning")) 201 }) 202 }) 203 }) 204 205 Describe("DeleteServiceCredentialBinding", func() { 206 const ( 207 guid = "fake-service-credential-binding-guid" 208 jobURL = JobURL("fake-job-url") 209 ) 210 211 It("makes the right request", func() { 212 client.DeleteServiceCredentialBinding(guid) 213 214 Expect(requester.MakeRequestCallCount()).To(Equal(1)) 215 Expect(requester.MakeRequestArgsForCall(0)).To(Equal(RequestParams{ 216 RequestName: internal.DeleteServiceCredentialBindingRequest, 217 URIParams: internal.Params{"service_credential_binding_guid": guid}, 218 })) 219 }) 220 221 When("the request succeeds", func() { 222 BeforeEach(func() { 223 requester.MakeRequestReturns(jobURL, Warnings{"fake-warning"}, nil) 224 }) 225 226 It("returns warnings and no errors", func() { 227 job, warnings, err := client.DeleteServiceCredentialBinding(guid) 228 229 Expect(job).To(Equal(jobURL)) 230 Expect(warnings).To(ConsistOf("fake-warning")) 231 Expect(err).NotTo(HaveOccurred()) 232 }) 233 }) 234 235 When("the request fails", func() { 236 BeforeEach(func() { 237 requester.MakeRequestReturns("", Warnings{"fake-warning"}, errors.New("bang")) 238 }) 239 240 It("returns errors and warnings", func() { 241 jobURL, warnings, err := client.DeleteServiceCredentialBinding(guid) 242 243 Expect(jobURL).To(BeEmpty()) 244 Expect(warnings).To(ConsistOf("fake-warning")) 245 Expect(err).To(MatchError("bang")) 246 }) 247 }) 248 }) 249 250 Describe("GetServiceCredentialBindingDetails", func() { 251 const guid = "fake-guid" 252 253 var ( 254 details resources.ServiceCredentialBindingDetails 255 warnings Warnings 256 executeErr error 257 ) 258 259 BeforeEach(func() { 260 requester.MakeRequestCalls(func(params RequestParams) (JobURL, Warnings, error) { 261 json.Unmarshal([]byte(`{"credentials":{"foo":"bar"}}`), params.ResponseBody) 262 return "", Warnings{"warning-1", "warning-2"}, nil 263 }) 264 }) 265 266 JustBeforeEach(func() { 267 details, warnings, executeErr = client.GetServiceCredentialBindingDetails(guid) 268 }) 269 270 It("makes the correct call", func() { 271 Expect(requester.MakeRequestCallCount()).To(Equal(1)) 272 actualParams := requester.MakeRequestArgsForCall(0) 273 Expect(actualParams.RequestName).To(Equal(internal.GetServiceCredentialBindingDetailsRequest)) 274 Expect(actualParams.URIParams).To(HaveKeyWithValue("service_credential_binding_guid", guid)) 275 Expect(actualParams.ResponseBody).To(BeAssignableToTypeOf(&resources.ServiceCredentialBindingDetails{})) 276 }) 277 278 It("returns details of service credential bindings", func() { 279 Expect(executeErr).ToNot(HaveOccurred()) 280 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 281 Expect(details).To(Equal(resources.ServiceCredentialBindingDetails{ 282 Credentials: types.JSONObject{"foo": "bar"}, 283 })) 284 }) 285 286 When("the cloud controller returns errors and warnings", func() { 287 BeforeEach(func() { 288 errors := []ccerror.V3Error{ 289 { 290 Code: 42424, 291 Detail: "Some detailed error message", 292 Title: "CF-SomeErrorTitle", 293 }, 294 { 295 Code: 11111, 296 Detail: "Some other detailed error message", 297 Title: "CF-SomeOtherErrorTitle", 298 }, 299 } 300 301 requester.MakeRequestReturns( 302 "", 303 Warnings{"this is a warning"}, 304 ccerror.MultiError{ResponseCode: http.StatusTeapot, Errors: errors}, 305 ) 306 }) 307 308 It("returns the error and all warnings", func() { 309 Expect(executeErr).To(MatchError(ccerror.MultiError{ 310 ResponseCode: http.StatusTeapot, 311 Errors: []ccerror.V3Error{ 312 { 313 Code: 42424, 314 Detail: "Some detailed error message", 315 Title: "CF-SomeErrorTitle", 316 }, 317 { 318 Code: 11111, 319 Detail: "Some other detailed error message", 320 Title: "CF-SomeOtherErrorTitle", 321 }, 322 }, 323 })) 324 Expect(warnings).To(ConsistOf("this is a warning")) 325 }) 326 }) 327 }) 328 })