github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/organization_quota_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("OrganizationQuota", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("GetOrganizationQuota", func() {
    22  
    23  		When("getting the organization quota does not return an error", func() {
    24  			BeforeEach(func() {
    25  				response := `{
    26  				"metadata": {
    27  					"guid": "some-org-quota-guid"
    28  				},
    29  				"entity": {
    30  					"name": "some-org-quota"
    31  				}
    32  			}`
    33  				server.AppendHandlers(
    34  					CombineHandlers(
    35  						VerifyRequest(http.MethodGet, "/v2/quota_definitions/some-org-quota-guid"),
    36  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    37  					),
    38  				)
    39  			})
    40  
    41  			It("returns the organization quota", func() {
    42  				orgQuota, warnings, err := client.GetOrganizationQuota("some-org-quota-guid")
    43  				Expect(err).NotTo(HaveOccurred())
    44  				Expect(warnings).To(ConsistOf("warning-1"))
    45  				Expect(orgQuota).To(Equal(OrganizationQuota{
    46  					GUID: "some-org-quota-guid",
    47  					Name: "some-org-quota",
    48  				}))
    49  			})
    50  		})
    51  
    52  		When("the organization quota returns an error", func() {
    53  			BeforeEach(func() {
    54  				response := `{
    55  				  "description": "Quota Definition could not be found: some-org-quota-guid",
    56  				  "error_code": "CF-QuotaDefinitionNotFound",
    57  				  "code": 240001
    58  				}`
    59  				server.AppendHandlers(
    60  					CombineHandlers(
    61  						VerifyRequest(http.MethodGet, "/v2/quota_definitions/some-org-quota-guid"),
    62  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    63  					),
    64  				)
    65  			})
    66  
    67  			It("returns the error", func() {
    68  				_, warnings, err := client.GetOrganizationQuota("some-org-quota-guid")
    69  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
    70  					Message: "Quota Definition could not be found: some-org-quota-guid",
    71  				}))
    72  				Expect(warnings).To(ConsistOf("warning-1"))
    73  			})
    74  		})
    75  
    76  	})
    77  
    78  	Describe("GetOrganizationQuotas", func() {
    79  		var (
    80  			quotas     []OrganizationQuota
    81  			warnings   Warnings
    82  			executeErr error
    83  		)
    84  
    85  		JustBeforeEach(func() {
    86  			quotas, warnings, executeErr = client.GetOrganizationQuotas(Filter{
    87  				Type:     "some-query",
    88  				Operator: constant.EqualOperator,
    89  				Values:   []string{"some-value"},
    90  			})
    91  		})
    92  
    93  		When("listing the quotas succeeds", func() {
    94  			BeforeEach(func() {
    95  				response1 := `{
    96  					"next_url": "/v2/quota_definitions?q=some-query:some-value&page=2",
    97  					"resources": [
    98  						{
    99  							"metadata": {
   100  								"guid": "some-org-quota-guid-1"
   101  							},
   102  							"entity": {
   103  								"name": "some-quota-name-1"
   104  							}
   105  						},
   106  						{
   107  							"metadata": {
   108  								"guid": "some-org-quota-guid-2"
   109  							},
   110  							"entity": {
   111  								"name": "some-quota-name-2"
   112  							}
   113  						}
   114  					]
   115  				}`
   116  
   117  				response2 := `{
   118  					"next_url": null,
   119  					"resources": [
   120  						{
   121  							"metadata": {
   122  								"guid": "some-org-quota-guid-3"
   123  							},
   124  							"entity": {
   125  								"name": "some-quota-name-3"
   126  							}
   127  						},
   128  						{
   129  							"metadata": {
   130  								"guid": "some-org-quota-guid-4"
   131  							},
   132  							"entity": {
   133  								"name": "some-quota-name-4"
   134  							}
   135  						}
   136  					]
   137  				}`
   138  				server.AppendHandlers(
   139  					CombineHandlers(
   140  						VerifyRequest(http.MethodGet, "/v2/quota_definitions", "q=some-query:some-value"),
   141  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   142  					),
   143  				)
   144  				server.AppendHandlers(
   145  					CombineHandlers(
   146  						VerifyRequest(http.MethodGet, "/v2/quota_definitions", "q=some-query:some-value&page=2"),
   147  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   148  					),
   149  				)
   150  			})
   151  
   152  			It("returns paginated results and all warnings", func() {
   153  				Expect(executeErr).NotTo(HaveOccurred())
   154  				Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"}))
   155  				Expect(quotas).To(Equal([]OrganizationQuota{
   156  					{
   157  						GUID: "some-org-quota-guid-1",
   158  						Name: "some-quota-name-1",
   159  					},
   160  					{
   161  						GUID: "some-org-quota-guid-2",
   162  						Name: "some-quota-name-2",
   163  					},
   164  					{
   165  						GUID: "some-org-quota-guid-3",
   166  						Name: "some-quota-name-3",
   167  					},
   168  					{
   169  						GUID: "some-org-quota-guid-4",
   170  						Name: "some-quota-name-4",
   171  					},
   172  				}))
   173  
   174  			})
   175  		})
   176  
   177  		Context("when the server errors", func() {
   178  			BeforeEach(func() {
   179  				response := `{
   180  					"code": 10001,
   181  					"description": "Some Error",
   182  					"error_code": "CF-SomeError"
   183  				}`
   184  				server.AppendHandlers(
   185  					CombineHandlers(
   186  						VerifyRequest(http.MethodGet, "/v2/quota_definitions", "q=some-query:some-value"),
   187  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   188  					),
   189  				)
   190  			})
   191  
   192  			It("returns warnings and errors", func() {
   193  				Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{
   194  					ResponseCode: http.StatusTeapot,
   195  					V2ErrorResponse: ccerror.V2ErrorResponse{
   196  						Code:        10001,
   197  						Description: "Some Error",
   198  						ErrorCode:   "CF-SomeError",
   199  					},
   200  				}))
   201  				Expect(warnings).To(ConsistOf("warning-1"))
   202  			})
   203  		})
   204  	})
   205  })