github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/organization_test.go (about)

     1  package ccv3_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/ccv3"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Organizations", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("GetOrganizations", func() {
    22  		Context("when organizations exist", func() {
    23  			BeforeEach(func() {
    24  				response1 := fmt.Sprintf(`{
    25  	"pagination": {
    26  		"next": {
    27  			"href": "%s/v3/organizations?names=some-org-name&page=2&per_page=2"
    28  		}
    29  	},
    30    "resources": [
    31      {
    32        "name": "org-name-1",
    33        "guid": "org-guid-1"
    34      },
    35      {
    36        "name": "org-name-2",
    37        "guid": "org-guid-2"
    38      }
    39    ]
    40  }`, server.URL())
    41  				response2 := `{
    42  	"pagination": {
    43  		"next": null
    44  	},
    45  	"resources": [
    46  	  {
    47        "name": "org-name-3",
    48  		  "guid": "org-guid-3"
    49  		}
    50  	]
    51  }`
    52  				server.AppendHandlers(
    53  					CombineHandlers(
    54  						VerifyRequest(http.MethodGet, "/v3/organizations", "names=some-org-name"),
    55  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    56  					),
    57  				)
    58  				server.AppendHandlers(
    59  					CombineHandlers(
    60  						VerifyRequest(http.MethodGet, "/v3/organizations", "names=some-org-name&page=2&per_page=2"),
    61  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
    62  					),
    63  				)
    64  			})
    65  
    66  			It("returns the queried organizations and all warnings", func() {
    67  				organizations, warnings, err := client.GetOrganizations(Query{
    68  					Key:    NameFilter,
    69  					Values: []string{"some-org-name"},
    70  				})
    71  				Expect(err).NotTo(HaveOccurred())
    72  
    73  				Expect(organizations).To(ConsistOf(
    74  					Organization{Name: "org-name-1", GUID: "org-guid-1"},
    75  					Organization{Name: "org-name-2", GUID: "org-guid-2"},
    76  					Organization{Name: "org-name-3", GUID: "org-guid-3"},
    77  				))
    78  				Expect(warnings).To(ConsistOf("this is a warning", "this is another warning"))
    79  			})
    80  		})
    81  
    82  		Context("when the cloud controller returns errors and warnings", func() {
    83  			BeforeEach(func() {
    84  				response := `{
    85    "errors": [
    86      {
    87        "code": 10008,
    88        "detail": "The request is semantically invalid: command presence",
    89        "title": "CF-UnprocessableEntity"
    90      },
    91      {
    92        "code": 10010,
    93        "detail": "Org not found",
    94        "title": "CF-ResourceNotFound"
    95      }
    96    ]
    97  }`
    98  				server.AppendHandlers(
    99  					CombineHandlers(
   100  						VerifyRequest(http.MethodGet, "/v3/organizations"),
   101  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   102  					),
   103  				)
   104  			})
   105  
   106  			It("returns the error and all warnings", func() {
   107  				_, warnings, err := client.GetOrganizations()
   108  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
   109  					ResponseCode: http.StatusTeapot,
   110  					V3ErrorResponse: ccerror.V3ErrorResponse{
   111  						Errors: []ccerror.V3Error{
   112  							{
   113  								Code:   10008,
   114  								Detail: "The request is semantically invalid: command presence",
   115  								Title:  "CF-UnprocessableEntity",
   116  							},
   117  							{
   118  								Code:   10010,
   119  								Detail: "Org not found",
   120  								Title:  "CF-ResourceNotFound",
   121  							},
   122  						},
   123  					},
   124  				}))
   125  				Expect(warnings).To(ConsistOf("this is a warning"))
   126  			})
   127  		})
   128  	})
   129  
   130  	Describe("GetIsolationSegmentOrganizationsByIsolationSegment", func() {
   131  		Context("when organizations exist", func() {
   132  			BeforeEach(func() {
   133  				response1 := fmt.Sprintf(`{
   134  	"pagination": {
   135  		"next": {
   136  			"href": "%s/v3/isolation_segments/some-iso-guid/organizations?page=2&per_page=2"
   137  		}
   138  	},
   139    "resources": [
   140      {
   141        "name": "org-name-1",
   142        "guid": "org-guid-1"
   143      },
   144      {
   145        "name": "org-name-2",
   146        "guid": "org-guid-2"
   147      }
   148    ]
   149  }`, server.URL())
   150  				response2 := `{
   151  	"pagination": {
   152  		"next": null
   153  	},
   154  	"resources": [
   155  	  {
   156        "name": "org-name-3",
   157  		  "guid": "org-guid-3"
   158  		}
   159  	]
   160  }`
   161  				server.AppendHandlers(
   162  					CombineHandlers(
   163  						VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-guid/organizations"),
   164  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   165  					),
   166  				)
   167  				server.AppendHandlers(
   168  					CombineHandlers(
   169  						VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-guid/organizations", "page=2&per_page=2"),
   170  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   171  					),
   172  				)
   173  			})
   174  
   175  			It("returns the queried organizations and all warnings", func() {
   176  				organizations, warnings, err := client.GetIsolationSegmentOrganizationsByIsolationSegment("some-iso-guid")
   177  				Expect(err).NotTo(HaveOccurred())
   178  
   179  				Expect(organizations).To(ConsistOf(
   180  					Organization{Name: "org-name-1", GUID: "org-guid-1"},
   181  					Organization{Name: "org-name-2", GUID: "org-guid-2"},
   182  					Organization{Name: "org-name-3", GUID: "org-guid-3"},
   183  				))
   184  				Expect(warnings).To(ConsistOf("this is a warning", "this is another warning"))
   185  			})
   186  		})
   187  
   188  		Context("when the cloud controller returns errors and warnings", func() {
   189  			BeforeEach(func() {
   190  				response := `{
   191    "errors": [
   192      {
   193        "code": 10008,
   194        "detail": "The request is semantically invalid: command presence",
   195        "title": "CF-UnprocessableEntity"
   196      },
   197  		{
   198        "code": 10010,
   199        "detail": "Isolation segment not found",
   200        "title": "CF-ResourceNotFound"
   201      }
   202    ]
   203  }`
   204  				server.AppendHandlers(
   205  					CombineHandlers(
   206  						VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-seg/organizations"),
   207  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   208  					),
   209  				)
   210  			})
   211  
   212  			It("returns the error and all warnings", func() {
   213  				_, warnings, err := client.GetIsolationSegmentOrganizationsByIsolationSegment("some-iso-seg")
   214  				Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{
   215  					ResponseCode: http.StatusTeapot,
   216  					V3ErrorResponse: ccerror.V3ErrorResponse{
   217  						Errors: []ccerror.V3Error{
   218  							{
   219  								Code:   10008,
   220  								Detail: "The request is semantically invalid: command presence",
   221  								Title:  "CF-UnprocessableEntity",
   222  							},
   223  							{
   224  								Code:   10010,
   225  								Detail: "Isolation segment not found",
   226  								Title:  "CF-ResourceNotFound",
   227  							},
   228  						},
   229  					},
   230  				}))
   231  				Expect(warnings).To(ConsistOf("this is a warning"))
   232  			})
   233  		})
   234  	})
   235  })