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