github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/service_plan_visibility_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  	"code.cloudfoundry.org/cli/resources"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/ginkgo/extensions/table"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("Service Plan Visibility", func() {
    17  	const planGUID = "fake-service-plan-guid"
    18  	var client *Client
    19  
    20  	BeforeEach(func() {
    21  		client, _ = NewTestClient()
    22  	})
    23  
    24  	Describe("GetServicePlanVisibility", func() {
    25  		const withOrganizations = `
    26          {
    27              "type": "organization",
    28              "organizations": [
    29                  {
    30                      "name": "org-1",
    31                      "guid": "org-1-guid"
    32                  },
    33                  {
    34                      "name": "org-2",
    35                      "guid": "org-2-guid"
    36                  }
    37              ]
    38          }`
    39  
    40  		const withSpace = `
    41          {
    42              "type": "space",
    43              "space": {
    44                  "name": "space-1",
    45                  "guid": "space-1-guid"
    46              }
    47          }`
    48  
    49  		DescribeTable(
    50  			"getting a service plan visibility",
    51  			func(response string, expected resources.ServicePlanVisibility) {
    52  				server.AppendHandlers(
    53  					CombineHandlers(
    54  						VerifyRequest(http.MethodGet, fmt.Sprintf("/v3/service_plans/%s/visibility", planGUID)),
    55  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}),
    56  					),
    57  				)
    58  
    59  				result, warnings, err := client.GetServicePlanVisibility(planGUID)
    60  				Expect(err).NotTo(HaveOccurred())
    61  				Expect(warnings).To(ConsistOf("warning-1"))
    62  				Expect(result).To(Equal(expected))
    63  			},
    64  			Entry("admin", `{"type":"admin"}`, resources.ServicePlanVisibility{Type: "admin"}),
    65  			Entry("public", `{"type":"public"}`, resources.ServicePlanVisibility{Type: "public"}),
    66  			Entry("orgs", withOrganizations, resources.ServicePlanVisibility{
    67  				Type: "organization",
    68  				Organizations: []resources.ServicePlanVisibilityDetail{
    69  					{Name: "org-1", GUID: "org-1-guid"},
    70  					{Name: "org-2", GUID: "org-2-guid"},
    71  				},
    72  			}),
    73  			Entry("space", withSpace, resources.ServicePlanVisibility{
    74  				Type:  "space",
    75  				Space: resources.ServicePlanVisibilityDetail{Name: "space-1", GUID: "space-1-guid"},
    76  			}),
    77  		)
    78  
    79  		When("the the server responds with error", func() {
    80  			It("returns an error", func() {
    81  				response := `{
    82  					"errors": [
    83  						{
    84  							"code": 42424,
    85  							"detail": "Some detailed error message",
    86  							"title": "CF-SomeErrorTitle"
    87  						},
    88  						{
    89  							"code": 11111,
    90  							"detail": "Some other detailed error message",
    91  							"title": "CF-SomeOtherErrorTitle"
    92  						}
    93  					]
    94  				}`
    95  				server.AppendHandlers(
    96  					RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-2"}}),
    97  				)
    98  
    99  				_, warnings, err := client.GetServicePlanVisibility(planGUID)
   100  				Expect(err).To(MatchError(ccerror.MultiError{
   101  					ResponseCode: http.StatusTeapot,
   102  					Errors: []ccerror.V3Error{
   103  						{
   104  							Code:   42424,
   105  							Detail: "Some detailed error message",
   106  							Title:  "CF-SomeErrorTitle",
   107  						},
   108  						{
   109  							Code:   11111,
   110  							Detail: "Some other detailed error message",
   111  							Title:  "CF-SomeOtherErrorTitle",
   112  						},
   113  					},
   114  				}))
   115  				Expect(warnings).To(ConsistOf("warning-2"))
   116  			})
   117  		})
   118  	})
   119  
   120  	Describe("UpdateServicePlanVisibility", func() {
   121  		It("sets the plan visibility", func() {
   122  			server.AppendHandlers(
   123  				CombineHandlers(
   124  					VerifyRequest(http.MethodPost, fmt.Sprintf("/v3/service_plans/%s/visibility", planGUID)),
   125  					VerifyBody([]byte(`{"type":"public"}`)),
   126  					RespondWith(http.StatusOK, `{"type":"public"}`, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   127  				),
   128  			)
   129  
   130  			result, warnings, err := client.UpdateServicePlanVisibility(
   131  				planGUID,
   132  				resources.ServicePlanVisibility{
   133  					Type: "public",
   134  				},
   135  			)
   136  			Expect(err).NotTo(HaveOccurred())
   137  			Expect(warnings).To(ConsistOf("warning-1"))
   138  			Expect(result).To(Equal(resources.ServicePlanVisibility{
   139  				Type: "public",
   140  			}))
   141  		})
   142  
   143  		When("the the server responds with error", func() {
   144  			It("returns an error", func() {
   145  				response := `{
   146  					"errors": [
   147  						{
   148  							"code": 42424,
   149  							"detail": "Some detailed error message",
   150  							"title": "CF-SomeErrorTitle"
   151  						},
   152  						{
   153  							"code": 11111,
   154  							"detail": "Some other detailed error message",
   155  							"title": "CF-SomeOtherErrorTitle"
   156  						}
   157  					]
   158  				}`
   159  				server.AppendHandlers(
   160  					RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   161  				)
   162  
   163  				_, warnings, err := client.UpdateServicePlanVisibility(
   164  					planGUID,
   165  					resources.ServicePlanVisibility{
   166  						Type: "public",
   167  					},
   168  				)
   169  				Expect(err).To(MatchError(ccerror.MultiError{
   170  					ResponseCode: http.StatusTeapot,
   171  					Errors: []ccerror.V3Error{
   172  						{
   173  							Code:   42424,
   174  							Detail: "Some detailed error message",
   175  							Title:  "CF-SomeErrorTitle",
   176  						},
   177  						{
   178  							Code:   11111,
   179  							Detail: "Some other detailed error message",
   180  							Title:  "CF-SomeOtherErrorTitle",
   181  						},
   182  					},
   183  				}))
   184  				Expect(warnings).To(ConsistOf("warning-2"))
   185  			})
   186  		})
   187  	})
   188  
   189  	Describe("DeleteServicePlanVisibility", func() {
   190  		const orgGUID = "org-guid"
   191  
   192  		It("deletes the org visibility", func() {
   193  			server.AppendHandlers(
   194  				CombineHandlers(
   195  					VerifyRequest(http.MethodDelete, fmt.Sprintf("/v3/service_plans/%s/visibility/%s", planGUID, orgGUID)),
   196  					RespondWith(http.StatusNoContent, nil, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   197  				),
   198  			)
   199  
   200  			warnings, err := client.DeleteServicePlanVisibility(planGUID, orgGUID)
   201  			Expect(err).NotTo(HaveOccurred())
   202  			Expect(warnings).To(ConsistOf("warning-1"))
   203  		})
   204  
   205  		When("the the server responds with error", func() {
   206  			It("returns an error", func() {
   207  				response := `{
   208  					"errors": [
   209  						{
   210  							"code": 42424,
   211  							"detail": "Some detailed error message",
   212  							"title": "CF-SomeErrorTitle"
   213  						},
   214  						{
   215  							"code": 11111,
   216  							"detail": "Some other detailed error message",
   217  							"title": "CF-SomeOtherErrorTitle"
   218  						}
   219  					]
   220  				}`
   221  				server.AppendHandlers(
   222  					RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   223  				)
   224  
   225  				warnings, err := client.DeleteServicePlanVisibility(planGUID, orgGUID)
   226  				Expect(err).To(MatchError(ccerror.MultiError{
   227  					ResponseCode: http.StatusTeapot,
   228  					Errors: []ccerror.V3Error{
   229  						{
   230  							Code:   42424,
   231  							Detail: "Some detailed error message",
   232  							Title:  "CF-SomeErrorTitle",
   233  						},
   234  						{
   235  							Code:   11111,
   236  							Detail: "Some other detailed error message",
   237  							Title:  "CF-SomeOtherErrorTitle",
   238  						},
   239  					},
   240  				}))
   241  				Expect(warnings).To(ConsistOf("warning-2"))
   242  			})
   243  		})
   244  	})
   245  })