github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/api/cloudcontroller/ccv2/organization_test.go (about) 1 package ccv2_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("Organization", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client = NewTestClient() 19 }) 20 21 Describe("DeleteOrganization", func() { 22 Context("when no errors are encountered", func() { 23 BeforeEach(func() { 24 jsonResponse := `{ 25 "metadata": { 26 "guid": "job-guid", 27 "created_at": "2016-06-08T16:41:27Z", 28 "url": "/v2/jobs/job-guid" 29 }, 30 "entity": { 31 "guid": "job-guid", 32 "status": "queued" 33 } 34 }` 35 36 server.AppendHandlers( 37 CombineHandlers( 38 VerifyRequest(http.MethodDelete, "/v2/organizations/some-organization-guid", "recursive=true&async=true"), 39 RespondWith(http.StatusAccepted, jsonResponse, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 40 )) 41 }) 42 43 It("deletes the Organization and returns all warnings", func() { 44 job, warnings, err := client.DeleteOrganization("some-organization-guid") 45 46 Expect(err).NotTo(HaveOccurred()) 47 Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"})) 48 Expect(job.GUID).To(Equal("job-guid")) 49 Expect(job.Status).To(Equal(constant.JobStatusQueued)) 50 }) 51 }) 52 53 Context("when an error is encountered", func() { 54 BeforeEach(func() { 55 response := `{ 56 "code": 30003, 57 "description": "The Organization could not be found: some-organization-guid", 58 "error_code": "CF-OrganizationNotFound" 59 }` 60 server.AppendHandlers( 61 CombineHandlers( 62 VerifyRequest(http.MethodDelete, "/v2/organizations/some-organization-guid", "recursive=true&async=true"), 63 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 64 )) 65 }) 66 67 It("returns an error and all warnings", func() { 68 _, warnings, err := client.DeleteOrganization("some-organization-guid") 69 70 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 71 Message: "The Organization could not be found: some-organization-guid", 72 })) 73 Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"})) 74 }) 75 }) 76 }) 77 78 Describe("GetOrganization", func() { 79 Context("when the organization exists", func() { 80 BeforeEach(func() { 81 response := `{ 82 "metadata": { 83 "guid": "some-org-guid" 84 }, 85 "entity": { 86 "name": "some-org", 87 "quota_definition_guid": "some-quota-guid" 88 } 89 }` 90 server.AppendHandlers( 91 CombineHandlers( 92 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid"), 93 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 94 )) 95 }) 96 97 It("returns the org and all warnings", func() { 98 orgs, warnings, err := client.GetOrganization("some-org-guid") 99 100 Expect(err).NotTo(HaveOccurred()) 101 Expect(orgs).To(Equal( 102 Organization{ 103 GUID: "some-org-guid", 104 Name: "some-org", 105 QuotaDefinitionGUID: "some-quota-guid", 106 }, 107 )) 108 Expect(warnings).To(ConsistOf("warning-1")) 109 }) 110 }) 111 112 Context("when an error is encountered", func() { 113 BeforeEach(func() { 114 response := `{ 115 "code": 10001, 116 "description": "Some Error", 117 "error_code": "CF-SomeError" 118 }` 119 server.AppendHandlers( 120 CombineHandlers( 121 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid"), 122 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 123 )) 124 }) 125 126 It("returns an error and all warnings", func() { 127 _, warnings, err := client.GetOrganization("some-org-guid") 128 129 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 130 ResponseCode: http.StatusTeapot, 131 V2ErrorResponse: ccerror.V2ErrorResponse{ 132 Code: 10001, 133 Description: "Some Error", 134 ErrorCode: "CF-SomeError", 135 }, 136 })) 137 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 138 }) 139 }) 140 }) 141 142 Describe("GetOrganizations", func() { 143 Context("when no errors are encountered", func() { 144 Context("when results are paginated", func() { 145 BeforeEach(func() { 146 response1 := `{ 147 "next_url": "/v2/organizations?q=some-query:some-value&page=2&order-by=name", 148 "resources": [ 149 { 150 "metadata": { 151 "guid": "org-guid-1" 152 }, 153 "entity": { 154 "name": "org-1", 155 "quota_definition_guid": "some-quota-guid", 156 "default_isolation_segment_guid": "some-default-isolation-segment-guid" 157 } 158 }, 159 { 160 "metadata": { 161 "guid": "org-guid-2" 162 }, 163 "entity": { 164 "name": "org-2", 165 "quota_definition_guid": "some-quota-guid" 166 } 167 } 168 ] 169 }` 170 response2 := `{ 171 "next_url": null, 172 "resources": [ 173 { 174 "metadata": { 175 "guid": "org-guid-3" 176 }, 177 "entity": { 178 "name": "org-3", 179 "quota_definition_guid": "some-quota-guid" 180 } 181 }, 182 { 183 "metadata": { 184 "guid": "org-guid-4" 185 }, 186 "entity": { 187 "name": "org-4", 188 "quota_definition_guid": "some-quota-guid" 189 } 190 } 191 ] 192 }` 193 server.AppendHandlers( 194 CombineHandlers( 195 VerifyRequest(http.MethodGet, "/v2/organizations", "q=some-query:some-value&order-by=name"), 196 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}), 197 )) 198 server.AppendHandlers( 199 CombineHandlers( 200 VerifyRequest(http.MethodGet, "/v2/organizations", "q=some-query:some-value&page=2&order-by=name"), 201 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}), 202 )) 203 }) 204 205 It("returns paginated results and all warnings", func() { 206 orgs, warnings, err := client.GetOrganizations(Filter{ 207 Type: "some-query", 208 Operator: constant.EqualOperator, 209 Values: []string{"some-value"}, 210 }) 211 212 Expect(err).NotTo(HaveOccurred()) 213 Expect(orgs).To(Equal([]Organization{ 214 { 215 GUID: "org-guid-1", 216 Name: "org-1", 217 QuotaDefinitionGUID: "some-quota-guid", 218 DefaultIsolationSegmentGUID: "some-default-isolation-segment-guid", 219 }, 220 { 221 GUID: "org-guid-2", 222 Name: "org-2", 223 QuotaDefinitionGUID: "some-quota-guid", 224 DefaultIsolationSegmentGUID: "", 225 }, 226 { 227 GUID: "org-guid-3", 228 Name: "org-3", 229 QuotaDefinitionGUID: "some-quota-guid", 230 DefaultIsolationSegmentGUID: "", 231 }, 232 { 233 GUID: "org-guid-4", 234 Name: "org-4", 235 QuotaDefinitionGUID: "some-quota-guid", 236 DefaultIsolationSegmentGUID: "", 237 }, 238 })) 239 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 240 }) 241 }) 242 }) 243 244 Context("when an error is encountered", func() { 245 BeforeEach(func() { 246 response := `{ 247 "code": 10001, 248 "description": "Some Error", 249 "error_code": "CF-SomeError" 250 }` 251 server.AppendHandlers( 252 CombineHandlers( 253 VerifyRequest(http.MethodGet, "/v2/organizations", "order-by=name"), 254 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 255 )) 256 }) 257 258 It("returns an error and all warnings", func() { 259 _, warnings, err := client.GetOrganizations() 260 261 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 262 ResponseCode: http.StatusTeapot, 263 V2ErrorResponse: ccerror.V2ErrorResponse{ 264 Code: 10001, 265 Description: "Some Error", 266 ErrorCode: "CF-SomeError", 267 }, 268 })) 269 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 270 }) 271 }) 272 }) 273 })