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