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