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