github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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/api/cloudcontroller/ccerror"
     8  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Spaces", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("GetSpaces", func() {
    22  		Context("when spaces exist", func() {
    23  			BeforeEach(func() {
    24  				response1 := fmt.Sprintf(`{
    25  	"pagination": {
    26  		"next": {
    27  			"href": "%s/v3/spaces?names=some-space-name&page=2&per_page=2"
    28  		}
    29  	},
    30    "resources": [
    31      {
    32        "name": "space-name-1",
    33        "guid": "space-guid-1"
    34      },
    35      {
    36        "name": "space-name-2",
    37        "guid": "space-guid-2"
    38      }
    39    ]
    40  }`, server.URL())
    41  				response2 := `{
    42  	"pagination": {
    43  		"next": null
    44  	},
    45  	"resources": [
    46  	  {
    47        "name": "space-name-3",
    48  		  "guid": "space-guid-3"
    49  		}
    50  	]
    51  }`
    52  
    53  				server.AppendHandlers(
    54  					CombineHandlers(
    55  						VerifyRequest(http.MethodGet, "/v3/spaces", "names=some-space-name"),
    56  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    57  					),
    58  				)
    59  				server.AppendHandlers(
    60  					CombineHandlers(
    61  						VerifyRequest(http.MethodGet, "/v3/spaces", "names=some-space-name&page=2&per_page=2"),
    62  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
    63  					),
    64  				)
    65  			})
    66  
    67  			It("returns the queried spaces and all warnings", func() {
    68  				spaces, warnings, err := client.GetSpaces(Query{
    69  					Key:    NameFilter,
    70  					Values: []string{"some-space-name"},
    71  				})
    72  				Expect(err).NotTo(HaveOccurred())
    73  
    74  				Expect(spaces).To(ConsistOf(
    75  					Space{Name: "space-name-1", GUID: "space-guid-1"},
    76  					Space{Name: "space-name-2", GUID: "space-guid-2"},
    77  					Space{Name: "space-name-3", GUID: "space-guid-3"},
    78  				))
    79  				Expect(warnings).To(ConsistOf("this is a warning", "this is another warning"))
    80  			})
    81  		})
    82  
    83  		Context("when the cloud controller returns errors and warnings", func() {
    84  			BeforeEach(func() {
    85  				response := `{
    86    "errors": [
    87      {
    88        "code": 10008,
    89        "detail": "The request is semantically invalid: command presence",
    90        "title": "CF-UnprocessableEntity"
    91      },
    92      {
    93        "code": 10010,
    94        "detail": "Space not found",
    95        "title": "CF-SpaceNotFound"
    96      }
    97    ]
    98  }`
    99  				server.AppendHandlers(
   100  					CombineHandlers(
   101  						VerifyRequest(http.MethodGet, "/v3/spaces"),
   102  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   103  					),
   104  				)
   105  			})
   106  
   107  			It("returns the error and all warnings", func() {
   108  				_, warnings, err := client.GetSpaces()
   109  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
   110  					ResponseCode: http.StatusTeapot,
   111  					V3ErrorResponse: ccerror.V3ErrorResponse{
   112  						Errors: []ccerror.V3Error{
   113  							{
   114  								Code:   10008,
   115  								Detail: "The request is semantically invalid: command presence",
   116  								Title:  "CF-UnprocessableEntity",
   117  							},
   118  							{
   119  								Code:   10010,
   120  								Detail: "Space not found",
   121  								Title:  "CF-SpaceNotFound",
   122  							},
   123  						},
   124  					},
   125  				}))
   126  				Expect(warnings).To(ConsistOf("this is a warning"))
   127  			})
   128  		})
   129  	})
   130  })