github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv2/domain_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("Domain", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client = NewTestClient() 19 }) 20 21 Describe("GetSharedDomain", func() { 22 Context("when the shared domain exists", func() { 23 BeforeEach(func() { 24 response := `{ 25 "metadata": { 26 "guid": "shared-domain-guid", 27 "updated_at": null 28 }, 29 "entity": { 30 "name": "shared-domain-1.com", 31 "router_group_guid": "some-router-group-guid", 32 "router_group_type": "http" 33 } 34 }` 35 server.AppendHandlers( 36 CombineHandlers( 37 VerifyRequest(http.MethodGet, "/v2/shared_domains/shared-domain-guid"), 38 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 39 ), 40 ) 41 }) 42 43 It("returns the shared domain and all warnings", func() { 44 domain, warnings, err := client.GetSharedDomain("shared-domain-guid") 45 Expect(err).NotTo(HaveOccurred()) 46 Expect(domain).To(Equal(Domain{ 47 Name: "shared-domain-1.com", 48 GUID: "shared-domain-guid", 49 RouterGroupGUID: "some-router-group-guid", 50 RouterGroupType: constant.HTTPRouterGroup, 51 })) 52 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 53 }) 54 }) 55 56 Context("when the shared domain does not exist", func() { 57 BeforeEach(func() { 58 response := `{ 59 "code": 130002, 60 "description": "The domain could not be found: shared-domain-guid", 61 "error_code": "CF-DomainNotFound" 62 }` 63 server.AppendHandlers( 64 CombineHandlers( 65 VerifyRequest(http.MethodGet, "/v2/shared_domains/shared-domain-guid"), 66 RespondWith(http.StatusNotFound, response), 67 ), 68 ) 69 }) 70 71 It("returns an error", func() { 72 domain, _, err := client.GetSharedDomain("shared-domain-guid") 73 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 74 Message: "The domain could not be found: shared-domain-guid", 75 })) 76 Expect(domain).To(Equal(Domain{})) 77 }) 78 }) 79 }) 80 81 Describe("GetPrivateDomain", func() { 82 Context("when the private domain exists", func() { 83 BeforeEach(func() { 84 response := `{ 85 "metadata": { 86 "guid": "private-domain-guid", 87 "updated_at": null 88 }, 89 "entity": { 90 "name": "private-domain-1.com" 91 } 92 }` 93 server.AppendHandlers( 94 CombineHandlers( 95 VerifyRequest(http.MethodGet, "/v2/private_domains/private-domain-guid"), 96 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 97 ), 98 ) 99 }) 100 101 It("returns the private domain and all warnings", func() { 102 domain, warnings, err := client.GetPrivateDomain("private-domain-guid") 103 Expect(err).NotTo(HaveOccurred()) 104 Expect(domain).To(Equal(Domain{Name: "private-domain-1.com", GUID: "private-domain-guid"})) 105 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 106 }) 107 }) 108 109 Context("when the private domain does not exist", func() { 110 BeforeEach(func() { 111 response := `{ 112 "code": 130002, 113 "description": "The domain could not be found: private-domain-guid", 114 "error_code": "CF-DomainNotFound" 115 }` 116 server.AppendHandlers( 117 CombineHandlers( 118 VerifyRequest(http.MethodGet, "/v2/private_domains/private-domain-guid"), 119 RespondWith(http.StatusNotFound, response), 120 ), 121 ) 122 }) 123 124 It("returns an error", func() { 125 domain, _, err := client.GetPrivateDomain("private-domain-guid") 126 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 127 Message: "The domain could not be found: private-domain-guid", 128 })) 129 Expect(domain).To(Equal(Domain{})) 130 }) 131 }) 132 }) 133 134 Describe("GetSharedDomains", func() { 135 Context("when the cloud controller does not return an error", func() { 136 BeforeEach(func() { 137 response1 := `{ 138 "next_url": "/v2/shared_domains?q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2", 139 "resources": [ 140 { 141 "metadata": { 142 "guid": "domain-guid-1" 143 }, 144 "entity": { 145 "name": "domain-name-1", 146 "router_group_guid": "some-router-group-guid-1", 147 "router_group_type": "http" 148 } 149 }, 150 { 151 "metadata": { 152 "guid": "domain-guid-2" 153 }, 154 "entity": { 155 "name": "domain-name-2", 156 "router_group_guid": "some-router-group-guid-2", 157 "router_group_type": "http" 158 } 159 } 160 ] 161 }` 162 response2 := `{ 163 "next_url": null, 164 "resources": [ 165 { 166 "metadata": { 167 "guid": "domain-guid-3" 168 }, 169 "entity": { 170 "name": "domain-name-3", 171 "router_group_guid": "some-router-group-guid-3", 172 "router_group_type": "http" 173 } 174 }, 175 { 176 "metadata": { 177 "guid": "domain-guid-4" 178 }, 179 "entity": { 180 "name": "domain-name-4", 181 "router_group_guid": "some-router-group-guid-4", 182 "router_group_type": "http" 183 } 184 } 185 ] 186 }` 187 server.AppendHandlers( 188 CombineHandlers( 189 VerifyRequest(http.MethodGet, "/v2/shared_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4"), 190 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 191 ), 192 ) 193 server.AppendHandlers( 194 CombineHandlers( 195 VerifyRequest(http.MethodGet, "/v2/shared_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2"), 196 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 197 ), 198 ) 199 }) 200 201 It("returns the shared domain and warnings", func() { 202 domains, warnings, err := client.GetSharedDomains(Query{ 203 Filter: NameFilter, 204 Operator: InOperator, 205 Values: []string{"domain-name-1", "domain-name-2", "domain-name-3", "domain-name-4"}, 206 }) 207 Expect(err).NotTo(HaveOccurred()) 208 Expect(domains).To(Equal([]Domain{ 209 { 210 GUID: "domain-guid-1", 211 Name: "domain-name-1", 212 RouterGroupGUID: "some-router-group-guid-1", 213 RouterGroupType: constant.HTTPRouterGroup, 214 }, 215 { 216 GUID: "domain-guid-2", 217 Name: "domain-name-2", 218 RouterGroupGUID: "some-router-group-guid-2", 219 RouterGroupType: constant.HTTPRouterGroup, 220 }, 221 { 222 GUID: "domain-guid-3", 223 Name: "domain-name-3", 224 RouterGroupGUID: "some-router-group-guid-3", 225 RouterGroupType: constant.HTTPRouterGroup, 226 }, 227 { 228 GUID: "domain-guid-4", 229 Name: "domain-name-4", 230 RouterGroupGUID: "some-router-group-guid-4", 231 RouterGroupType: constant.HTTPRouterGroup, 232 }, 233 })) 234 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 235 }) 236 }) 237 238 Context("when the cloud controller returns an error", func() { 239 BeforeEach(func() { 240 response := `{ 241 "code": 1, 242 "description": "some error description", 243 "error_code": "CF-SomeError" 244 }` 245 server.AppendHandlers( 246 CombineHandlers( 247 VerifyRequest(http.MethodGet, "/v2/shared_domains"), 248 RespondWith(http.StatusInternalServerError, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 249 ), 250 ) 251 }) 252 253 It("returns the warnings and error", func() { 254 domains, warnings, err := client.GetSharedDomains() 255 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 256 V2ErrorResponse: ccerror.V2ErrorResponse{ 257 Code: 1, 258 Description: "some error description", 259 ErrorCode: "CF-SomeError", 260 }, 261 ResponseCode: http.StatusInternalServerError, 262 })) 263 Expect(domains).To(Equal([]Domain{})) 264 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 265 }) 266 }) 267 }) 268 269 Describe("GetOrganizationPrivateDomains", func() { 270 Context("when the cloud controller does not return an error", func() { 271 BeforeEach(func() { 272 response1 := `{ 273 "next_url": "/v2/organizations/some-org-guid/private_domains?page=2", 274 "resources": [ 275 { 276 "metadata": { 277 "guid": "private-domain-guid-1" 278 }, 279 "entity": { 280 "name": "private-domain-name-1" 281 } 282 }, 283 { 284 "metadata": { 285 "guid": "private-domain-guid-2" 286 }, 287 "entity": { 288 "name": "private-domain-name-2" 289 } 290 } 291 ] 292 }` 293 response2 := `{ 294 "next_url": null, 295 "resources": [ 296 { 297 "metadata": { 298 "guid": "private-domain-guid-3" 299 }, 300 "entity": { 301 "name": "private-domain-name-3" 302 } 303 }, 304 { 305 "metadata": { 306 "guid": "private-domain-guid-4" 307 }, 308 "entity": { 309 "name": "private-domain-name-4" 310 } 311 } 312 ] 313 }` 314 server.AppendHandlers( 315 CombineHandlers( 316 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains"), 317 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 318 ), 319 ) 320 server.AppendHandlers( 321 CombineHandlers( 322 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains", "page=2"), 323 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 324 ), 325 ) 326 }) 327 328 It("returns the domains and warnings", func() { 329 domains, warnings, err := client.GetOrganizationPrivateDomains("some-org-guid") 330 Expect(err).NotTo(HaveOccurred()) 331 Expect(domains).To(Equal([]Domain{ 332 { 333 Name: "private-domain-name-1", 334 GUID: "private-domain-guid-1", 335 }, 336 { 337 Name: "private-domain-name-2", 338 GUID: "private-domain-guid-2", 339 }, 340 { 341 Name: "private-domain-name-3", 342 GUID: "private-domain-guid-3", 343 }, 344 { 345 Name: "private-domain-name-4", 346 GUID: "private-domain-guid-4", 347 }, 348 })) 349 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 350 }) 351 }) 352 353 Context("when the client includes includes query parameters for name", func() { 354 It("it includes the query parameters in the request", func() { 355 server.AppendHandlers( 356 CombineHandlers( 357 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains", "q=name:private-domain-name"), 358 RespondWith(http.StatusOK, ""), 359 ), 360 ) 361 362 client.GetOrganizationPrivateDomains("some-org-guid", Query{ 363 Filter: NameFilter, 364 Operator: EqualOperator, 365 Values: []string{"private-domain-name"}, 366 }) 367 }) 368 }) 369 370 Context("when the cloud controller returns an error", func() { 371 BeforeEach(func() { 372 response := `{ 373 "description": "The organization could not be found: glah", 374 "error_code": "CF-OrganizationNotFound", 375 "code": 30003 376 }` 377 server.AppendHandlers( 378 CombineHandlers( 379 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains"), 380 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 381 ), 382 ) 383 }) 384 385 It("returns the warnings and error", func() { 386 domains, warnings, err := client.GetOrganizationPrivateDomains("some-org-guid") 387 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 388 Message: "The organization could not be found: glah", 389 })) 390 Expect(domains).To(Equal([]Domain{})) 391 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 392 }) 393 }) 394 }) 395 })