github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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/ccv2"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/ghttp"
    10  )
    11  
    12  var _ = Describe("Organization", func() {
    13  	var client *Client
    14  
    15  	BeforeEach(func() {
    16  		client = NewTestClient()
    17  	})
    18  
    19  	Describe("GetOrganizations", func() {
    20  		Context("when no errors are encountered", func() {
    21  			Context("when results are paginated", func() {
    22  				BeforeEach(func() {
    23  					response1 := `{
    24  					"next_url": "/v2/organizations?q=some-query:some-value&page=2",
    25  					"resources": [
    26  						{
    27  							"metadata": {
    28  								"guid": "org-guid-1"
    29  							},
    30  							"entity": {
    31  								"name": "org-1",
    32  								"quota_definition_guid": "some-quota-guid"
    33  							}
    34  						},
    35  						{
    36  							"metadata": {
    37  								"guid": "org-guid-2"
    38  							},
    39  							"entity": {
    40  								"name": "org-2",
    41  								"quota_definition_guid": "some-quota-guid"
    42  							}
    43  						}
    44  					]
    45  				}`
    46  					response2 := `{
    47  					"next_url": null,
    48  					"resources": [
    49  						{
    50  							"metadata": {
    51  								"guid": "org-guid-3"
    52  							},
    53  							"entity": {
    54  								"name": "org-3",
    55  								"quota_definition_guid": "some-quota-guid"
    56  							}
    57  						},
    58  						{
    59  							"metadata": {
    60  								"guid": "org-guid-4"
    61  							},
    62  							"entity": {
    63  								"name": "org-4",
    64  								"quota_definition_guid": "some-quota-guid"
    65  							}
    66  						}
    67  					]
    68  				}`
    69  					server.AppendHandlers(
    70  						CombineHandlers(
    71  							VerifyRequest(http.MethodGet, "/v2/organizations", "q=some-query:some-value"),
    72  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    73  						))
    74  					server.AppendHandlers(
    75  						CombineHandlers(
    76  							VerifyRequest(http.MethodGet, "/v2/organizations", "q=some-query:some-value&page=2"),
    77  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
    78  						))
    79  				})
    80  
    81  				It("returns paginated results and all warnings", func() {
    82  					orgs, warnings, err := client.GetOrganizations([]Query{{
    83  						Filter:   "some-query",
    84  						Operator: EqualOperator,
    85  						Value:    "some-value",
    86  					}})
    87  
    88  					Expect(err).NotTo(HaveOccurred())
    89  					Expect(orgs).To(Equal([]Organization{
    90  						{GUID: "org-guid-1", Name: "org-1", QuotaDefinitionGUID: "some-quota-guid"},
    91  						{GUID: "org-guid-2", Name: "org-2", QuotaDefinitionGUID: "some-quota-guid"},
    92  						{GUID: "org-guid-3", Name: "org-3", QuotaDefinitionGUID: "some-quota-guid"},
    93  						{GUID: "org-guid-4", Name: "org-4", QuotaDefinitionGUID: "some-quota-guid"},
    94  					}))
    95  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    96  				})
    97  			})
    98  		})
    99  
   100  		Context("when an error is encountered", func() {
   101  			BeforeEach(func() {
   102  				response := `{
   103    "code": 10001,
   104    "description": "Some Error",
   105    "error_code": "CF-SomeError"
   106  }`
   107  				server.AppendHandlers(
   108  					CombineHandlers(
   109  						VerifyRequest(http.MethodGet, "/v2/organizations"),
   110  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   111  					))
   112  			})
   113  
   114  			It("returns an error and all warnings", func() {
   115  				_, warnings, err := client.GetOrganizations(nil)
   116  
   117  				Expect(err).To(MatchError(UnexpectedResponseError{
   118  					ResponseCode: http.StatusTeapot,
   119  					CCErrorResponse: CCErrorResponse{
   120  						Code:        10001,
   121  						Description: "Some Error",
   122  						ErrorCode:   "CF-SomeError",
   123  					},
   124  				}))
   125  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   126  			})
   127  		})
   128  	})
   129  })