github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/service_key_test.go (about) 1 package ccv2_test 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/ghttp" 10 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 12 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 13 ) 14 15 var _ = Describe("Service Key", func() { 16 var ( 17 client *Client 18 warnings Warnings 19 executeErr error 20 serviceKey ServiceKey 21 parameters map[string]interface{} 22 ) 23 24 BeforeEach(func() { 25 client = NewTestClient() 26 parameters = map[string]interface{}{"the-service-broker": "wants this object"} 27 }) 28 29 JustBeforeEach(func() { 30 serviceKey, warnings, executeErr = client.CreateServiceKey("some-service-instance-guid", "some-service-key-name", parameters) 31 }) 32 33 When("the create is successful", func() { 34 BeforeEach(func() { 35 expectedRequestBody := map[string]interface{}{ 36 "service_instance_guid": "some-service-instance-guid", 37 "name": "some-service-key-name", 38 "parameters": map[string]interface{}{ 39 "the-service-broker": "wants this object", 40 }, 41 } 42 response := ` 43 { 44 "metadata": { 45 "guid": "some-service-key-guid" 46 }, 47 "entity": { 48 "name": "some-service-key-name", 49 "service_instance_guid": "some-service-instance-guid", 50 "credentials": { "some-username" : "some-password", "port": 31023 } 51 } 52 }` 53 server.AppendHandlers( 54 CombineHandlers( 55 VerifyRequest(http.MethodPost, "/v2/service_keys"), 56 VerifyJSONRepresenting(expectedRequestBody), 57 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning"}}), 58 ), 59 ) 60 }) 61 62 It("returns the created object and warnings", func() { 63 Expect(executeErr).NotTo(HaveOccurred()) 64 65 Expect(serviceKey).To(BeEquivalentTo(ServiceKey{ 66 GUID: "some-service-key-guid", 67 Name: "some-service-key-name", 68 ServiceInstanceGUID: "some-service-instance-guid", 69 Credentials: map[string]interface{}{"some-username": "some-password", "port": json.Number("31023")}, 70 })) 71 Expect(warnings).To(ConsistOf(Warnings{"warning"})) 72 }) 73 74 When("The request cannot be serialized", func() { 75 BeforeEach(func() { 76 parameters = make(map[string]interface{}) 77 parameters["data"] = make(chan bool) 78 }) 79 80 It("returns the serialization error", func() { 81 Expect(executeErr).To(MatchError("json: unsupported type: chan bool")) 82 }) 83 }) 84 }) 85 86 When("the create is not successful", func() { 87 When("the create returns a ServiceKeyNameTaken error", func() { 88 BeforeEach(func() { 89 response := ` 90 { 91 "description": "The service key name is taken: some-service-key-name", 92 "error_code": "CF-ServiceKeyNameTaken", 93 "code": 360001 94 }` 95 96 server.AppendHandlers( 97 CombineHandlers( 98 VerifyRequest(http.MethodPost, "/v2/service_keys"), 99 RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 100 ), 101 ) 102 }) 103 104 It("returns the error and warnings", func() { 105 Expect(executeErr).To(MatchError(ccerror.ServiceKeyTakenError{Message: "The service key name is taken: some-service-key-name"})) 106 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 107 }) 108 }) 109 110 When("the create returns a generic error", func() { 111 BeforeEach(func() { 112 response := ` 113 { 114 "description": "Something went wrong", 115 "error_code": "CF-SomeErrorCode", 116 "code": 2108219482 117 }` 118 119 server.AppendHandlers( 120 CombineHandlers( 121 VerifyRequest(http.MethodPost, "/v2/service_keys"), 122 RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 123 ), 124 ) 125 }) 126 127 It("returns the error and warnings", func() { 128 Expect(executeErr).To(MatchError(ccerror.BadRequestError{Message: "Something went wrong"})) 129 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 130 }) 131 }) 132 133 When("the create returns a invalid JSON", func() { 134 BeforeEach(func() { 135 response := `{"entity": {"name": 4}}` 136 137 server.AppendHandlers( 138 CombineHandlers( 139 VerifyRequest(http.MethodPost, "/v2/service_keys"), 140 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 141 ), 142 ) 143 }) 144 145 It("returns the error and warnings", func() { 146 Expect(executeErr).To(HaveOccurred()) 147 Expect(executeErr).To(BeAssignableToTypeOf(&json.UnmarshalTypeError{})) 148 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 149 }) 150 }) 151 }) 152 })