github.com/sleungcy-sap/cli@v7.1.0+incompatible/cf/api/route_service_binding_repository_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/errors"
    11  	"code.cloudfoundry.org/cli/cf/net"
    12  
    13  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    14  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    15  
    16  	"github.com/onsi/gomega/ghttp"
    17  
    18  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("RouteServiceBindingsRepository", func() {
    24  	var (
    25  		ccServer                *ghttp.Server
    26  		configRepo              coreconfig.ReadWriter
    27  		routeServiceBindingRepo api.CloudControllerRouteServiceBindingRepository
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		ccServer = ghttp.NewServer()
    32  		configRepo = testconfig.NewRepositoryWithDefaults()
    33  		configRepo.SetAPIEndpoint(ccServer.URL())
    34  
    35  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    36  		routeServiceBindingRepo = api.NewCloudControllerRouteServiceBindingRepository(configRepo, gateway)
    37  	})
    38  
    39  	AfterEach(func() {
    40  		ccServer.Close()
    41  	})
    42  
    43  	Describe("Bind", func() {
    44  		var (
    45  			serviceInstanceGUID string
    46  			routeGUID           string
    47  		)
    48  
    49  		BeforeEach(func() {
    50  			serviceInstanceGUID = "service-instance-guid"
    51  			routeGUID = "route-guid"
    52  		})
    53  
    54  		It("creates the service binding when the service instance is managed", func() {
    55  			ccServer.AppendHandlers(
    56  				ghttp.CombineHandlers(
    57  					ghttp.VerifyRequest("PUT", fmt.Sprintf("/v2/service_instances/%s/routes/%s", serviceInstanceGUID, routeGUID)),
    58  					ghttp.RespondWith(http.StatusNoContent, nil),
    59  				),
    60  			)
    61  			err := routeServiceBindingRepo.Bind(serviceInstanceGUID, routeGUID, false, "")
    62  			Expect(err).NotTo(HaveOccurred())
    63  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
    64  		})
    65  
    66  		It("creates the service binding when the service instance is user-provided", func() {
    67  			ccServer.AppendHandlers(
    68  				ghttp.CombineHandlers(
    69  					ghttp.VerifyRequest("PUT", fmt.Sprintf("/v2/user_provided_service_instances/%s/routes/%s", serviceInstanceGUID, routeGUID)),
    70  					ghttp.RespondWith(http.StatusCreated, nil),
    71  				),
    72  			)
    73  			err := routeServiceBindingRepo.Bind(serviceInstanceGUID, routeGUID, true, "")
    74  			Expect(err).NotTo(HaveOccurred())
    75  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
    76  		})
    77  
    78  		It("creates the service binding with the provided body wrapped in parameters", func() {
    79  			ccServer.AppendHandlers(
    80  				ghttp.CombineHandlers(
    81  					ghttp.VerifyRequest("PUT", fmt.Sprintf("/v2/user_provided_service_instances/%s/routes/%s", serviceInstanceGUID, routeGUID)),
    82  					ghttp.RespondWith(http.StatusCreated, nil),
    83  					ghttp.VerifyJSON(`{"parameters":{"some":"json"}}`),
    84  				),
    85  			)
    86  			err := routeServiceBindingRepo.Bind(serviceInstanceGUID, routeGUID, true, `{"some":"json"}`)
    87  			Expect(err).NotTo(HaveOccurred())
    88  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
    89  		})
    90  
    91  		Context("when an API error occurs", func() {
    92  			BeforeEach(func() {
    93  				ccServer.AppendHandlers(
    94  					ghttp.CombineHandlers(
    95  						ghttp.VerifyRequest("PUT", fmt.Sprintf("/v2/service_instances/%s/routes/%s", serviceInstanceGUID, routeGUID)),
    96  						ghttp.RespondWith(http.StatusBadRequest, `{"code":61003,"description":"Route does not exist"}`),
    97  					),
    98  				)
    99  			})
   100  
   101  			It("returns an HTTPError", func() {
   102  				err := routeServiceBindingRepo.Bind(serviceInstanceGUID, routeGUID, false, "")
   103  				Expect(err).To(HaveOccurred())
   104  				httpErr, ok := err.(errors.HTTPError)
   105  				Expect(ok).To(BeTrue())
   106  
   107  				Expect(httpErr.ErrorCode()).To(Equal("61003"))
   108  				Expect(httpErr.Error()).To(ContainSubstring("Route does not exist"))
   109  			})
   110  		})
   111  	})
   112  
   113  	Describe("Unbind", func() {
   114  		var (
   115  			serviceInstanceGUID string
   116  			routeGUID           string
   117  		)
   118  
   119  		BeforeEach(func() {
   120  			serviceInstanceGUID = "service-instance-guid"
   121  			routeGUID = "route-guid"
   122  		})
   123  
   124  		It("deletes the service binding when unbinding a managed service instance", func() {
   125  			ccServer.AppendHandlers(
   126  				ghttp.CombineHandlers(
   127  					ghttp.VerifyRequest("DELETE", fmt.Sprintf("/v2/service_instances/%s/routes/%s", serviceInstanceGUID, routeGUID)),
   128  					ghttp.RespondWith(http.StatusNoContent, nil),
   129  				),
   130  			)
   131  			err := routeServiceBindingRepo.Unbind(serviceInstanceGUID, routeGUID, false)
   132  			Expect(err).NotTo(HaveOccurred())
   133  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   134  		})
   135  
   136  		It("deletes the service binding when the service instance is user-provided", func() {
   137  			ccServer.AppendHandlers(
   138  				ghttp.CombineHandlers(
   139  					ghttp.VerifyRequest("DELETE", fmt.Sprintf("/v2/user_provided_service_instances/%s/routes/%s", serviceInstanceGUID, routeGUID)),
   140  					ghttp.RespondWith(http.StatusNoContent, nil),
   141  				),
   142  			)
   143  			err := routeServiceBindingRepo.Unbind(serviceInstanceGUID, routeGUID, true)
   144  			Expect(err).NotTo(HaveOccurred())
   145  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   146  		})
   147  
   148  		Context("when an API error occurs", func() {
   149  			BeforeEach(func() {
   150  				ccServer.AppendHandlers(
   151  					ghttp.CombineHandlers(
   152  						ghttp.VerifyRequest("DELETE", fmt.Sprintf("/v2/service_instances/%s/routes/%s", serviceInstanceGUID, routeGUID)),
   153  						ghttp.RespondWith(http.StatusBadRequest, `{"code":61003,"description":"Route does not exist"}`),
   154  					),
   155  				)
   156  			})
   157  
   158  			It("returns an HTTPError", func() {
   159  				err := routeServiceBindingRepo.Unbind(serviceInstanceGUID, routeGUID, false)
   160  				Expect(err).To(HaveOccurred())
   161  				httpErr, ok := err.(errors.HTTPError)
   162  				Expect(ok).To(BeTrue())
   163  
   164  				Expect(httpErr.ErrorCode()).To(Equal("61003"))
   165  				Expect(httpErr.Error()).To(ContainSubstring("Route does not exist"))
   166  			})
   167  		})
   168  	})
   169  })