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