github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/ghttp" 11 ) 12 13 var _ = Describe("Organization", func() { 14 var client *Client 15 16 BeforeEach(func() { 17 client = NewTestClient() 18 }) 19 20 Describe("GetOrganization", func() { 21 Context("when the organization exists", func() { 22 BeforeEach(func() { 23 response := `{ 24 "metadata": { 25 "guid": "some-org-guid" 26 }, 27 "entity": { 28 "name": "some-org", 29 "quota_definition_guid": "some-quota-guid" 30 } 31 }` 32 server.AppendHandlers( 33 CombineHandlers( 34 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid"), 35 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 36 )) 37 }) 38 39 It("returns the org and all warnings", func() { 40 orgs, warnings, err := client.GetOrganization("some-org-guid") 41 42 Expect(err).NotTo(HaveOccurred()) 43 Expect(orgs).To(Equal( 44 Organization{ 45 GUID: "some-org-guid", 46 Name: "some-org", 47 QuotaDefinitionGUID: "some-quota-guid", 48 }, 49 )) 50 Expect(warnings).To(ConsistOf("warning-1")) 51 }) 52 }) 53 54 Context("when an error is encountered", func() { 55 BeforeEach(func() { 56 response := `{ 57 "code": 10001, 58 "description": "Some Error", 59 "error_code": "CF-SomeError" 60 }` 61 server.AppendHandlers( 62 CombineHandlers( 63 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid"), 64 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 65 )) 66 }) 67 68 It("returns an error and all warnings", func() { 69 _, warnings, err := client.GetOrganization("some-org-guid") 70 71 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 72 ResponseCode: http.StatusTeapot, 73 V2ErrorResponse: ccerror.V2ErrorResponse{ 74 Code: 10001, 75 Description: "Some Error", 76 ErrorCode: "CF-SomeError", 77 }, 78 })) 79 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 80 }) 81 }) 82 }) 83 84 Describe("GetOrganizations", func() { 85 Context("when no errors are encountered", func() { 86 Context("when results are paginated", func() { 87 BeforeEach(func() { 88 response1 := `{ 89 "next_url": "/v2/organizations?q=some-query:some-value&page=2&order-by=name", 90 "resources": [ 91 { 92 "metadata": { 93 "guid": "org-guid-1" 94 }, 95 "entity": { 96 "name": "org-1", 97 "quota_definition_guid": "some-quota-guid", 98 "default_isolation_segment_guid": "some-default-isolation-segment-guid" 99 } 100 }, 101 { 102 "metadata": { 103 "guid": "org-guid-2" 104 }, 105 "entity": { 106 "name": "org-2", 107 "quota_definition_guid": "some-quota-guid" 108 } 109 } 110 ] 111 }` 112 response2 := `{ 113 "next_url": null, 114 "resources": [ 115 { 116 "metadata": { 117 "guid": "org-guid-3" 118 }, 119 "entity": { 120 "name": "org-3", 121 "quota_definition_guid": "some-quota-guid" 122 } 123 }, 124 { 125 "metadata": { 126 "guid": "org-guid-4" 127 }, 128 "entity": { 129 "name": "org-4", 130 "quota_definition_guid": "some-quota-guid" 131 } 132 } 133 ] 134 }` 135 server.AppendHandlers( 136 CombineHandlers( 137 VerifyRequest(http.MethodGet, "/v2/organizations", "q=some-query:some-value&order-by=name"), 138 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}), 139 )) 140 server.AppendHandlers( 141 CombineHandlers( 142 VerifyRequest(http.MethodGet, "/v2/organizations", "q=some-query:some-value&page=2&order-by=name"), 143 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}), 144 )) 145 }) 146 147 It("returns paginated results and all warnings", func() { 148 orgs, warnings, err := client.GetOrganizations(Query{ 149 Filter: "some-query", 150 Operator: EqualOperator, 151 Values: []string{"some-value"}, 152 }) 153 154 Expect(err).NotTo(HaveOccurred()) 155 Expect(orgs).To(Equal([]Organization{ 156 { 157 GUID: "org-guid-1", 158 Name: "org-1", 159 QuotaDefinitionGUID: "some-quota-guid", 160 DefaultIsolationSegmentGUID: "some-default-isolation-segment-guid", 161 }, 162 { 163 GUID: "org-guid-2", 164 Name: "org-2", 165 QuotaDefinitionGUID: "some-quota-guid", 166 DefaultIsolationSegmentGUID: "", 167 }, 168 { 169 GUID: "org-guid-3", 170 Name: "org-3", 171 QuotaDefinitionGUID: "some-quota-guid", 172 DefaultIsolationSegmentGUID: "", 173 }, 174 { 175 GUID: "org-guid-4", 176 Name: "org-4", 177 QuotaDefinitionGUID: "some-quota-guid", 178 DefaultIsolationSegmentGUID: "", 179 }, 180 })) 181 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 182 }) 183 }) 184 }) 185 186 Context("when an error is encountered", func() { 187 BeforeEach(func() { 188 response := `{ 189 "code": 10001, 190 "description": "Some Error", 191 "error_code": "CF-SomeError" 192 }` 193 server.AppendHandlers( 194 CombineHandlers( 195 VerifyRequest(http.MethodGet, "/v2/organizations", "order-by=name"), 196 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 197 )) 198 }) 199 200 It("returns an error and all warnings", func() { 201 _, warnings, err := client.GetOrganizations() 202 203 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 204 ResponseCode: http.StatusTeapot, 205 V2ErrorResponse: ccerror.V2ErrorResponse{ 206 Code: 10001, 207 Description: "Some Error", 208 ErrorCode: "CF-SomeError", 209 }, 210 })) 211 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 212 }) 213 }) 214 }) 215 })