github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv2/space_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  var _ = Describe("Space", func() {
    14  	var client *Client
    15  
    16  	BeforeEach(func() {
    17  		client = NewTestClient()
    18  	})
    19  
    20  	Describe("GetSpaces", func() {
    21  		Context("when no errors are encountered", func() {
    22  			Context("when results are paginated", func() {
    23  				BeforeEach(func() {
    24  					response1 := `{
    25  						"next_url": "/v2/spaces?q=some-query:some-value&page=2&order-by=name",
    26  						"resources": [
    27  							{
    28  								"metadata": {
    29  									"guid": "space-guid-1"
    30  								},
    31  								"entity": {
    32  									"name": "space-1",
    33  									"allow_ssh": false,
    34  									"space_quota_definition_guid": "some-space-quota-guid-1",
    35  									"organization_guid": "org-guid-1"
    36  								}
    37  							},
    38  							{
    39  								"metadata": {
    40  									"guid": "space-guid-2"
    41  								},
    42  								"entity": {
    43  									"name": "space-2",
    44  									"allow_ssh": true,
    45  									"space_quota_definition_guid": "some-space-quota-guid-2",
    46  									"organization_guid": "org-guid-2"
    47  								}
    48  							}
    49  						]
    50  					}`
    51  					response2 := `{
    52  						"next_url": null,
    53  						"resources": [
    54  							{
    55  								"metadata": {
    56  									"guid": "space-guid-3"
    57  								},
    58  								"entity": {
    59  									"name": "space-3",
    60  									"allow_ssh": false,
    61  									"space_quota_definition_guid": "some-space-quota-guid-3",
    62  									"organization_guid": "org-guid-3"
    63  								}
    64  							},
    65  							{
    66  								"metadata": {
    67  									"guid": "space-guid-4"
    68  								},
    69  								"entity": {
    70  									"name": "space-4",
    71  									"allow_ssh": true,
    72  									"space_quota_definition_guid": "some-space-quota-guid-4",
    73  									"organization_guid": "org-guid-4"
    74  								}
    75  							}
    76  						]
    77  					}`
    78  					server.AppendHandlers(
    79  						CombineHandlers(
    80  							VerifyRequest(http.MethodGet, "/v2/spaces", "q=some-query:some-value&order-by=name"),
    81  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    82  						))
    83  					server.AppendHandlers(
    84  						CombineHandlers(
    85  							VerifyRequest(http.MethodGet, "/v2/spaces", "q=some-query:some-value&page=2&order-by=name"),
    86  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
    87  						))
    88  				})
    89  
    90  				It("returns paginated results and all warnings", func() {
    91  					spaces, warnings, err := client.GetSpaces(Query{
    92  						Filter:   "some-query",
    93  						Operator: EqualOperator,
    94  						Values:   []string{"some-value"},
    95  					})
    96  
    97  					Expect(err).NotTo(HaveOccurred())
    98  					Expect(spaces).To(Equal([]Space{
    99  						{
   100  							GUID:                     "space-guid-1",
   101  							OrganizationGUID:         "org-guid-1",
   102  							Name:                     "space-1",
   103  							AllowSSH:                 false,
   104  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-1",
   105  						},
   106  						{
   107  							GUID:                     "space-guid-2",
   108  							OrganizationGUID:         "org-guid-2",
   109  							Name:                     "space-2",
   110  							AllowSSH:                 true,
   111  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-2",
   112  						},
   113  						{
   114  							GUID:                     "space-guid-3",
   115  							OrganizationGUID:         "org-guid-3",
   116  							Name:                     "space-3",
   117  							AllowSSH:                 false,
   118  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-3",
   119  						},
   120  						{
   121  							GUID:                     "space-guid-4",
   122  							OrganizationGUID:         "org-guid-4",
   123  							Name:                     "space-4",
   124  							AllowSSH:                 true,
   125  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-4",
   126  						},
   127  					}))
   128  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   129  				})
   130  			})
   131  		})
   132  
   133  		Context("when an error is encountered", func() {
   134  			BeforeEach(func() {
   135  				response := `{
   136    "code": 10001,
   137    "description": "Some Error",
   138    "error_code": "CF-SomeError"
   139  }`
   140  				server.AppendHandlers(
   141  					CombineHandlers(
   142  						VerifyRequest(http.MethodGet, "/v2/spaces", "order-by=name"),
   143  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   144  					))
   145  			})
   146  
   147  			It("returns an error and all warnings", func() {
   148  				_, warnings, err := client.GetSpaces()
   149  
   150  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   151  					ResponseCode: http.StatusTeapot,
   152  					V2ErrorResponse: ccerror.V2ErrorResponse{
   153  						Code:        10001,
   154  						Description: "Some Error",
   155  						ErrorCode:   "CF-SomeError",
   156  					},
   157  				}))
   158  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   159  			})
   160  		})
   161  	})
   162  
   163  	Describe("GetStagingSpacesBySecurityGroup", func() {
   164  		Context("when no errors are encountered", func() {
   165  			Context("when results are paginated", func() {
   166  				BeforeEach(func() {
   167  					response1 := `{
   168  						"next_url": "/v2/security_groups/security-group-guid/staging_spaces?page=2",
   169  						"resources": [
   170  							{
   171  								"metadata": {
   172  									"guid": "space-guid-1"
   173  								},
   174  								"entity": {
   175  									"name": "space-1",
   176  									"allow_ssh": false,
   177  									"space_quota_definition_guid": "some-space-quota-guid-1",
   178  									"organization_guid": "org-guid-1"
   179  								}
   180  							},
   181  							{
   182  								"metadata": {
   183  									"guid": "space-guid-2"
   184  								},
   185  								"entity": {
   186  									"name": "space-2",
   187  									"allow_ssh": true,
   188  									"space_quota_definition_guid": "some-space-quota-guid-2",
   189  									"organization_guid": "org-guid-2"
   190  								}
   191  							}
   192  						]
   193  					}`
   194  					response2 := `{
   195  						"next_url": null,
   196  						"resources": [
   197  							{
   198  								"metadata": {
   199  									"guid": "space-guid-3"
   200  								},
   201  								"entity": {
   202  									"name": "space-3",
   203  									"allow_ssh": false,
   204  									"space_quota_definition_guid": "some-space-quota-guid-3",
   205  									"organization_guid": "org-guid-3"
   206  								}
   207  							},
   208  							{
   209  								"metadata": {
   210  									"guid": "space-guid-4"
   211  								},
   212  								"entity": {
   213  									"name": "space-4",
   214  									"allow_ssh": true,
   215  									"space_quota_definition_guid": "some-space-quota-guid-4",
   216  									"organization_guid": "org-guid-4"
   217  								}
   218  							}
   219  						]
   220  					}`
   221  					server.AppendHandlers(
   222  						CombineHandlers(
   223  							VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/staging_spaces", ""),
   224  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   225  						))
   226  					server.AppendHandlers(
   227  						CombineHandlers(
   228  							VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/staging_spaces", "page=2"),
   229  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   230  						))
   231  				})
   232  
   233  				It("returns paginated results and all warnings", func() {
   234  					spaces, warnings, err := client.GetStagingSpacesBySecurityGroup("security-group-guid")
   235  
   236  					Expect(err).NotTo(HaveOccurred())
   237  					Expect(spaces).To(Equal([]Space{
   238  						{
   239  							GUID:                     "space-guid-1",
   240  							OrganizationGUID:         "org-guid-1",
   241  							Name:                     "space-1",
   242  							AllowSSH:                 false,
   243  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-1",
   244  						},
   245  						{
   246  							GUID:                     "space-guid-2",
   247  							OrganizationGUID:         "org-guid-2",
   248  							Name:                     "space-2",
   249  							AllowSSH:                 true,
   250  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-2",
   251  						},
   252  						{
   253  							GUID:                     "space-guid-3",
   254  							OrganizationGUID:         "org-guid-3",
   255  							Name:                     "space-3",
   256  							AllowSSH:                 false,
   257  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-3",
   258  						},
   259  						{
   260  							GUID:                     "space-guid-4",
   261  							OrganizationGUID:         "org-guid-4",
   262  							Name:                     "space-4",
   263  							AllowSSH:                 true,
   264  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-4",
   265  						},
   266  					}))
   267  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   268  				})
   269  			})
   270  		})
   271  
   272  		Context("when an error is encountered", func() {
   273  			BeforeEach(func() {
   274  				response := `{
   275    "code": 10001,
   276    "description": "Some Error",
   277    "error_code": "CF-SomeError"
   278  }`
   279  				server.AppendHandlers(
   280  					CombineHandlers(
   281  						VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/staging_spaces"),
   282  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   283  					))
   284  			})
   285  
   286  			It("returns an error and all warnings", func() {
   287  				_, warnings, err := client.GetStagingSpacesBySecurityGroup("security-group-guid")
   288  
   289  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   290  					ResponseCode: http.StatusTeapot,
   291  					V2ErrorResponse: ccerror.V2ErrorResponse{
   292  						Code:        10001,
   293  						Description: "Some Error",
   294  						ErrorCode:   "CF-SomeError",
   295  					},
   296  				}))
   297  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   298  			})
   299  		})
   300  	})
   301  
   302  	Describe("GetRunningSpacesBySecurityGroup", func() {
   303  		Context("when no errors are encountered", func() {
   304  			Context("when results are paginated", func() {
   305  				BeforeEach(func() {
   306  					response1 := `{
   307  						"next_url": "/v2/security_groups/security-group-guid/spaces?page=2",
   308  						"resources": [
   309  							{
   310  								"metadata": {
   311  									"guid": "space-guid-1"
   312  								},
   313  								"entity": {
   314  									"name": "space-1",
   315  									"allow_ssh": false,
   316  									"space_quota_definition_guid": "some-space-quota-guid-1",
   317  									"organization_guid": "org-guid-1"
   318  								}
   319  							},
   320  							{
   321  								"metadata": {
   322  									"guid": "space-guid-2"
   323  								},
   324  								"entity": {
   325  									"name": "space-2",
   326  									"allow_ssh": true,
   327  									"space_quota_definition_guid": "some-space-quota-guid-2",
   328  									"organization_guid": "org-guid-2"
   329  								}
   330  							}
   331  						]
   332  					}`
   333  					response2 := `{
   334  						"next_url": null,
   335  						"resources": [
   336  							{
   337  								"metadata": {
   338  									"guid": "space-guid-3"
   339  								},
   340  								"entity": {
   341  									"name": "space-3",
   342  									"allow_ssh": false,
   343  									"space_quota_definition_guid": "some-space-quota-guid-3",
   344  									"organization_guid": "org-guid-3"
   345  								}
   346  							},
   347  							{
   348  								"metadata": {
   349  									"guid": "space-guid-4"
   350  								},
   351  								"entity": {
   352  									"name": "space-4",
   353  									"allow_ssh": true,
   354  									"space_quota_definition_guid": "some-space-quota-guid-4",
   355  									"organization_guid": "org-guid-4"
   356  								}
   357  							}
   358  						]
   359  					}`
   360  					server.AppendHandlers(
   361  						CombineHandlers(
   362  							VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/spaces", ""),
   363  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   364  						))
   365  					server.AppendHandlers(
   366  						CombineHandlers(
   367  							VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/spaces", "page=2"),
   368  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   369  						))
   370  				})
   371  
   372  				It("returns paginated results and all warnings", func() {
   373  					spaces, warnings, err := client.GetRunningSpacesBySecurityGroup("security-group-guid")
   374  
   375  					Expect(err).NotTo(HaveOccurred())
   376  					Expect(spaces).To(Equal([]Space{
   377  						{
   378  							GUID:                     "space-guid-1",
   379  							OrganizationGUID:         "org-guid-1",
   380  							Name:                     "space-1",
   381  							AllowSSH:                 false,
   382  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-1",
   383  						},
   384  						{
   385  							GUID:                     "space-guid-2",
   386  							OrganizationGUID:         "org-guid-2",
   387  							Name:                     "space-2",
   388  							AllowSSH:                 true,
   389  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-2",
   390  						},
   391  						{
   392  							GUID:                     "space-guid-3",
   393  							OrganizationGUID:         "org-guid-3",
   394  							Name:                     "space-3",
   395  							AllowSSH:                 false,
   396  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-3",
   397  						},
   398  						{
   399  							GUID:                     "space-guid-4",
   400  							OrganizationGUID:         "org-guid-4",
   401  							Name:                     "space-4",
   402  							AllowSSH:                 true,
   403  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-4",
   404  						},
   405  					}))
   406  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   407  				})
   408  			})
   409  		})
   410  
   411  		Context("when an error is encountered", func() {
   412  			BeforeEach(func() {
   413  				response := `{
   414    "code": 10001,
   415    "description": "Some Error",
   416    "error_code": "CF-SomeError"
   417  }`
   418  				server.AppendHandlers(
   419  					CombineHandlers(
   420  						VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/spaces"),
   421  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   422  					))
   423  			})
   424  
   425  			It("returns an error and all warnings", func() {
   426  				_, warnings, err := client.GetRunningSpacesBySecurityGroup("security-group-guid")
   427  
   428  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   429  					ResponseCode: http.StatusTeapot,
   430  					V2ErrorResponse: ccerror.V2ErrorResponse{
   431  						Code:        10001,
   432  						Description: "Some Error",
   433  						ErrorCode:   "CF-SomeError",
   434  					},
   435  				}))
   436  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   437  			})
   438  		})
   439  	})
   440  })