github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv3/relationship_test.go (about) 1 package ccv3_test 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("Relationship", func() { 15 var ( 16 client *Client 17 ) 18 19 BeforeEach(func() { 20 client = NewTestClient() 21 }) 22 23 Describe("Relationship.MarshalJSON", func() { 24 Context("when the isolation segment is specified by name", func() { 25 It("contains the name in the marshaled JSON", func() { 26 body, err := json.Marshal(Relationship{GUID: "some-iso-guid"}) 27 expectedJSON := `{ 28 "data": { 29 "guid": "some-iso-guid" 30 } 31 }` 32 33 Expect(err).NotTo(HaveOccurred()) 34 Expect(body).To(MatchJSON(expectedJSON)) 35 }) 36 }) 37 38 Context("when the isolation segment is the empty string", func() { 39 It("contains null in the marshaled JSON", func() { 40 body, err := json.Marshal(Relationship{GUID: ""}) 41 expectedJSON := `{ 42 "data": { 43 "guid": null 44 } 45 }` 46 47 Expect(err).NotTo(HaveOccurred()) 48 Expect(body).To(MatchJSON(expectedJSON)) 49 }) 50 }) 51 }) 52 53 Describe("AssignSpaceToIsolationSegment", func() { 54 Context("when the assignment is successful", func() { 55 BeforeEach(func() { 56 response := `{ 57 "data": { 58 "guid": "some-isolation-segment-guid" 59 } 60 }` 61 62 requestBody := map[string]map[string]string{ 63 "data": {"guid": "some-iso-guid"}, 64 } 65 server.AppendHandlers( 66 CombineHandlers( 67 VerifyRequest(http.MethodPatch, "/v3/spaces/some-space-guid/relationships/isolation_segment"), 68 VerifyJSONRepresenting(requestBody), 69 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 70 ), 71 ) 72 }) 73 74 It("returns all relationships and warnings", func() { 75 relationship, warnings, err := client.AssignSpaceToIsolationSegment("some-space-guid", "some-iso-guid") 76 Expect(err).NotTo(HaveOccurred()) 77 Expect(warnings).To(ConsistOf("this is a warning")) 78 Expect(relationship).To(Equal(Relationship{ 79 GUID: "some-isolation-segment-guid", 80 })) 81 }) 82 }) 83 }) 84 85 Describe("GetSpaceIsolationSegment", func() { 86 Context("when getting the isolation segment is successful", func() { 87 BeforeEach(func() { 88 response := `{ 89 "data": { 90 "guid": "some-isolation-segment-guid" 91 } 92 }` 93 94 server.AppendHandlers( 95 CombineHandlers( 96 VerifyRequest(http.MethodGet, "/v3/spaces/some-space-guid/relationships/isolation_segment"), 97 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 98 ), 99 ) 100 }) 101 102 It("returns the relationship and warnings", func() { 103 relationship, warnings, err := client.GetSpaceIsolationSegment("some-space-guid") 104 Expect(err).NotTo(HaveOccurred()) 105 Expect(warnings).To(ConsistOf("this is a warning")) 106 Expect(relationship).To(Equal(Relationship{ 107 GUID: "some-isolation-segment-guid", 108 })) 109 }) 110 }) 111 }) 112 113 Describe("RevokeIsolationSegmentFromOrganization ", func() { 114 Context("when relationship exists", func() { 115 BeforeEach(func() { 116 server.AppendHandlers( 117 CombineHandlers( 118 VerifyRequest(http.MethodDelete, "/v3/isolation_segments/segment-guid/relationships/organizations/org-guid"), 119 RespondWith(http.StatusOK, "", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 120 ), 121 ) 122 }) 123 124 It("revoke the relationship", func() { 125 warnings, err := client.RevokeIsolationSegmentFromOrganization("segment-guid", "org-guid") 126 Expect(err).ToNot(HaveOccurred()) 127 Expect(warnings).To(ConsistOf("this is a warning")) 128 129 Expect(server.ReceivedRequests()).To(HaveLen(3)) 130 }) 131 }) 132 }) 133 134 Context("when relationship exists", func() { 135 BeforeEach(func() { 136 response := `{ 137 "errors": [ 138 { 139 "code": 10008, 140 "detail": "The request is semantically invalid: command presence", 141 "title": "CF-UnprocessableEntity" 142 } 143 ] 144 }` 145 146 server.AppendHandlers( 147 CombineHandlers( 148 VerifyRequest(http.MethodDelete, "/v3/isolation_segments/segment-guid/relationships/organizations/org-guid"), 149 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 150 ), 151 ) 152 }) 153 154 It("revoke the relationship", func() { 155 warnings, err := client.RevokeIsolationSegmentFromOrganization("segment-guid", "org-guid") 156 Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ 157 ResponseCode: http.StatusTeapot, 158 V3ErrorResponse: ccerror.V3ErrorResponse{ 159 []ccerror.V3Error{ 160 { 161 Code: 10008, 162 Detail: "The request is semantically invalid: command presence", 163 Title: "CF-UnprocessableEntity", 164 }, 165 }, 166 }, 167 })) 168 Expect(warnings).To(ConsistOf("this is a warning")) 169 170 Expect(server.ReceivedRequests()).To(HaveLen(3)) 171 }) 172 }) 173 174 Describe("GetOrganizationDefaultIsolationSegment", func() { 175 Context("when getting the isolation segment is successful", func() { 176 BeforeEach(func() { 177 response := `{ 178 "data": { 179 "guid": "some-isolation-segment-guid" 180 } 181 }` 182 183 server.AppendHandlers( 184 CombineHandlers( 185 VerifyRequest(http.MethodGet, "/v3/organizations/some-org-guid/relationships/default_isolation_segment"), 186 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 187 ), 188 ) 189 }) 190 191 It("returns the relationship and warnings", func() { 192 relationship, warnings, err := client.GetOrganizationDefaultIsolationSegment("some-org-guid") 193 Expect(err).NotTo(HaveOccurred()) 194 Expect(warnings).To(ConsistOf("this is a warning")) 195 Expect(relationship).To(Equal(Relationship{ 196 GUID: "some-isolation-segment-guid", 197 })) 198 }) 199 }) 200 201 Context("when getting the isolation segment fails with an error", func() { 202 BeforeEach(func() { 203 response := `{ 204 "errors": [ 205 { 206 "detail": "Organization not found", 207 "title": "CF-ResourceNotFound", 208 "code": 10010 209 } 210 ] 211 }` 212 server.AppendHandlers( 213 CombineHandlers( 214 VerifyRequest(http.MethodGet, "/v3/organizations/some-org-guid/relationships/default_isolation_segment"), 215 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 216 ), 217 ) 218 }) 219 220 It("returns the relationship and warnings", func() { 221 _, warnings, err := client.GetOrganizationDefaultIsolationSegment("some-org-guid") 222 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 223 Message: "Organization not found", 224 })) 225 Expect(warnings).To(ConsistOf("this is a warning")) 226 }) 227 }) 228 }) 229 })