github.com/arunkumar7540/cli@v6.45.0+incompatible/api/cloudcontroller/ccv3/space_test.go (about) 1 package ccv3_test 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/types" 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/constant" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 . "github.com/onsi/gomega/ghttp" 15 ) 16 17 var _ = Describe("Spaces", func() { 18 var client *Client 19 20 BeforeEach(func() { 21 client, _ = NewTestClient() 22 }) 23 24 Describe("GetSpaces", func() { 25 var ( 26 query Query 27 28 spaces []Space 29 warnings Warnings 30 executeErr error 31 ) 32 33 JustBeforeEach(func() { 34 spaces, warnings, executeErr = client.GetSpaces(query) 35 }) 36 37 When("spaces exist", func() { 38 BeforeEach(func() { 39 response1 := fmt.Sprintf(`{ 40 "pagination": { 41 "next": { 42 "href": "%s/v3/spaces?names=some-space-name&page=2&per_page=2" 43 } 44 }, 45 "resources": [ 46 { 47 "name": "space-name-1", 48 "guid": "space-guid-1", 49 "relationships": { 50 "organization": { 51 "data": { "guid": "org-guid-1" } 52 } 53 } 54 }, 55 { 56 "name": "space-name-2", 57 "guid": "space-guid-2", 58 "relationships": { 59 "organization": { 60 "data": { "guid": "org-guid-2" } 61 } 62 } 63 } 64 ] 65 }`, server.URL()) 66 response2 := `{ 67 "pagination": { 68 "next": null 69 }, 70 "resources": [ 71 { 72 "name": "space-name-3", 73 "guid": "space-guid-3", 74 "relationships": { 75 "organization": { 76 "data": { "guid": "org-guid-3" } 77 } 78 } 79 } 80 ] 81 }` 82 83 server.AppendHandlers( 84 CombineHandlers( 85 VerifyRequest(http.MethodGet, "/v3/spaces", "names=some-space-name"), 86 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 87 ), 88 ) 89 server.AppendHandlers( 90 CombineHandlers( 91 VerifyRequest(http.MethodGet, "/v3/spaces", "names=some-space-name&page=2&per_page=2"), 92 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 93 ), 94 ) 95 96 query = Query{ 97 Key: NameFilter, 98 Values: []string{"some-space-name"}, 99 } 100 }) 101 102 It("returns the queried spaces and all warnings", func() { 103 Expect(executeErr).NotTo(HaveOccurred()) 104 105 Expect(spaces).To(ConsistOf( 106 Space{Name: "space-name-1", GUID: "space-guid-1", Relationships: Relationships{ 107 constant.RelationshipTypeOrganization: Relationship{GUID: "org-guid-1"}, 108 }}, 109 Space{Name: "space-name-2", GUID: "space-guid-2", Relationships: Relationships{ 110 constant.RelationshipTypeOrganization: Relationship{GUID: "org-guid-2"}, 111 }}, 112 Space{Name: "space-name-3", GUID: "space-guid-3", Relationships: Relationships{ 113 constant.RelationshipTypeOrganization: Relationship{GUID: "org-guid-3"}, 114 }}, 115 )) 116 Expect(warnings).To(ConsistOf("this is a warning", "this is another warning")) 117 }) 118 }) 119 120 When("the cloud controller returns errors and warnings", func() { 121 BeforeEach(func() { 122 response := `{ 123 "errors": [ 124 { 125 "code": 10008, 126 "detail": "The request is semantically invalid: command presence", 127 "title": "CF-UnprocessableEntity" 128 }, 129 { 130 "code": 10010, 131 "detail": "Space not found", 132 "title": "CF-SpaceNotFound" 133 } 134 ] 135 }` 136 server.AppendHandlers( 137 CombineHandlers( 138 VerifyRequest(http.MethodGet, "/v3/spaces"), 139 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 140 ), 141 ) 142 }) 143 144 It("returns the error and all warnings", func() { 145 Expect(executeErr).To(MatchError(ccerror.MultiError{ 146 ResponseCode: http.StatusTeapot, 147 Errors: []ccerror.V3Error{ 148 { 149 Code: 10008, 150 Detail: "The request is semantically invalid: command presence", 151 Title: "CF-UnprocessableEntity", 152 }, 153 { 154 Code: 10010, 155 Detail: "Space not found", 156 Title: "CF-SpaceNotFound", 157 }, 158 }, 159 })) 160 Expect(warnings).To(ConsistOf("this is a warning")) 161 }) 162 }) 163 }) 164 165 Describe("UpdateSpace", func() { 166 var ( 167 spaceToUpdate Space 168 updatedSpace Space 169 warnings Warnings 170 executeErr error 171 ) 172 173 JustBeforeEach(func() { 174 updatedSpace, warnings, executeErr = client.UpdateSpace(spaceToUpdate) 175 }) 176 177 When("the organization is updated successfully", func() { 178 BeforeEach(func() { 179 response := `{ 180 "guid": "some-space-guid", 181 "name": "some-space-name", 182 "metadata": { 183 "labels": { 184 "k1":"v1", 185 "k2":"v2" 186 } 187 } 188 }` 189 190 expectedBody := map[string]interface{}{ 191 "name": "some-space-name", 192 "metadata": map[string]interface{}{ 193 "labels": map[string]string{ 194 "k1": "v1", 195 "k2": "v2", 196 }, 197 }, 198 } 199 200 server.AppendHandlers( 201 CombineHandlers( 202 VerifyRequest(http.MethodPatch, "/v3/spaces/some-guid"), 203 VerifyJSONRepresenting(expectedBody), 204 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 205 ), 206 ) 207 208 spaceToUpdate = Space{ 209 Name: "some-space-name", 210 GUID: "some-guid", 211 Metadata: &Metadata{ 212 Labels: map[string]types.NullString{ 213 "k1": types.NewNullString("v1"), 214 "k2": types.NewNullString("v2"), 215 }, 216 }, 217 } 218 }) 219 220 It("should include the labels in the JSON", func() { 221 Expect(executeErr).ToNot(HaveOccurred()) 222 Expect(server.ReceivedRequests()).To(HaveLen(3)) 223 Expect(len(warnings)).To(Equal(1)) 224 Expect(warnings).To(ConsistOf("this is a warning")) 225 Expect(updatedSpace.Metadata.Labels).To(BeEquivalentTo( 226 map[string]types.NullString{ 227 "k1": types.NewNullString("v1"), 228 "k2": types.NewNullString("v2"), 229 })) 230 }) 231 232 }) 233 234 }) 235 })