github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv3/space_test.go (about)

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