github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/space_quota_test.go (about)

     1  package ccv2_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/ccv2"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Space Quotas", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("GetSpaceQuotaDefinition", func() {
    22  		When("no errors are encountered", func() {
    23  			BeforeEach(func() {
    24  				response := `{
    25  					"metadata": {
    26  						"guid": "space-quota-guid",
    27  						"url": "/v2/space_quota_definitions/space-quota-guid",
    28  						"updated_at": null
    29  					},
    30  					"entity": {
    31  						"name": "space-quota"
    32  					}
    33  				}`
    34  				server.AppendHandlers(
    35  					CombineHandlers(
    36  						VerifyRequest(http.MethodGet, "/v2/space_quota_definitions/space-quota-guid"),
    37  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    38  					),
    39  				)
    40  			})
    41  
    42  			It("returns the Space Quota", func() {
    43  				spaceQuota, warnings, err := client.GetSpaceQuotaDefinition("space-quota-guid")
    44  				Expect(err).NotTo(HaveOccurred())
    45  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    46  				Expect(spaceQuota).To(Equal(SpaceQuota{
    47  					Name: "space-quota",
    48  					GUID: "space-quota-guid",
    49  				}))
    50  			})
    51  		})
    52  
    53  		When("the request returns an error", func() {
    54  			BeforeEach(func() {
    55  				response := `{
    56  					"code": 210002,
    57  					"description": "The space quota could not be found: some-space-quota-guid",
    58  					"error_code": "CF-AppNotFound"
    59  				}`
    60  				server.AppendHandlers(
    61  					CombineHandlers(
    62  						VerifyRequest(http.MethodGet, "/v2/space_quota_definitions/some-space-quota-guid"),
    63  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    64  					),
    65  				)
    66  			})
    67  
    68  			It("returns the error and warnings", func() {
    69  				_, warnings, err := client.GetSpaceQuotaDefinition("some-space-quota-guid")
    70  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "The space quota could not be found: some-space-quota-guid"}))
    71  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    72  			})
    73  		})
    74  	})
    75  
    76  	Describe("GetSpaceQuotas", func() {
    77  		When("the organization is not found", func() {
    78  			var badOrgGuid = "bad-org-guid"
    79  			BeforeEach(func() {
    80  				response := fmt.Sprintf(`{
    81  					"description": "The organization could not be found: %s",
    82  					"error_code": "CF-OrganizationNotFound",
    83  					"code": 30003
    84  			 }`, badOrgGuid)
    85  				server.AppendHandlers(
    86  					CombineHandlers(
    87  						VerifyRequest(http.MethodGet, fmt.Sprintf("/v2/organizations/%s/space_quota_definitions", badOrgGuid)),
    88  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    89  					))
    90  			})
    91  
    92  			It("returns an error indicating that the org was not found", func() {
    93  				_, warnings, err := client.GetSpaceQuotas(badOrgGuid)
    94  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: fmt.Sprintf("The organization could not be found: %s", badOrgGuid)}))
    95  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    96  			})
    97  		})
    98  
    99  		When("the organization is found", func() {
   100  			BeforeEach(func() {
   101  				response := `{
   102  				  "resources": [
   103    					  {
   104    					    "metadata": {
   105    					      "guid": "some-space-quota-guid-1"
   106    					    },
   107    					    "entity": {
   108    					      "name": "some-quota-1",
   109    					      "organization_guid": "some-org-guid"
   110    					    }
   111    					  },
   112    					  {
   113    					    "metadata": {
   114    					      "guid": "some-space-quota-guid-2"
   115    					    },
   116    					    "entity": {
   117    					      "name": "some-quota-2",
   118    					      "organization_guid": "some-org-guid"
   119    					    }
   120    					  }
   121    					]
   122  				}`
   123  
   124  				server.AppendHandlers(
   125  					CombineHandlers(
   126  						VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/space_quota_definitions"),
   127  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   128  					),
   129  				)
   130  			})
   131  
   132  			It("returns all the space quotas for the org guid", func() {
   133  				spaceQuotas, warnings, err := client.GetSpaceQuotas("some-org-guid")
   134  				Expect(spaceQuotas).To(ConsistOf([]SpaceQuota{
   135  					SpaceQuota{GUID: "some-space-quota-guid-1", Name: "some-quota-1"},
   136  					SpaceQuota{GUID: "some-space-quota-guid-2", Name: "some-quota-2"},
   137  				}))
   138  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   139  				Expect(err).ToNot(HaveOccurred())
   140  			})
   141  		})
   142  	})
   143  
   144  	Describe("SetSpaceQuotas", func() {
   145  		When("the calls succeeds", func() {
   146  			BeforeEach(func() {
   147  				response := `{
   148  					"description": "Could not find VCAP::CloudController::Space with guid: some-space-guid",
   149  					"error_code": "CF-InvalidRelation",
   150  					"code": 1002
   151  				 }`
   152  
   153  				server.AppendHandlers(
   154  					CombineHandlers(
   155  						VerifyRequest(http.MethodPut, "/v2/space_quota_definitions/some-space-quota-guid/spaces/some-space-guid"),
   156  						RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   157  					),
   158  				)
   159  			})
   160  
   161  			It("sets the space quota for the space and retruns warnings", func() {
   162  				warnings, err := client.SetSpaceQuota("some-space-guid", "some-space-quota-guid")
   163  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   164  				Expect(err).To(MatchError(ccerror.InvalidRelationError{Message: "Could not find VCAP::CloudController::Space with guid: some-space-guid"}))
   165  			})
   166  		})
   167  
   168  		When("the calls fails", func() {
   169  			BeforeEach(func() {
   170  				response := `{
   171  					"metadata": {
   172  						"guid": "space-quota-guid"
   173  					  },
   174  					  "entity": {
   175  						"name": "some-space-quota",
   176  						"organization_guid": "some-org-guid"
   177  					  }
   178  					
   179  				}`
   180  
   181  				server.AppendHandlers(
   182  					CombineHandlers(
   183  						VerifyRequest(http.MethodPut, "/v2/space_quota_definitions/some-space-quota-guid/spaces/some-space-guid"),
   184  						RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   185  					),
   186  				)
   187  			})
   188  
   189  			It("sets the space quota for the space and retruns warnings", func() {
   190  				warnings, err := client.SetSpaceQuota("some-space-guid", "some-space-quota-guid")
   191  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   192  				Expect(err).ToNot(HaveOccurred())
   193  			})
   194  		})
   195  
   196  	})
   197  })