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

     1  package api_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/net"
    11  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    12  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    13  	testnet "code.cloudfoundry.org/cli/util/testhelpers/net"
    14  
    15  	. "code.cloudfoundry.org/cli/cf/api"
    16  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    17  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	"github.com/onsi/gomega/ghttp"
    21  )
    22  
    23  var _ = Describe("Service Brokers Repo", func() {
    24  	It("lists services brokers", func() {
    25  		firstRequest := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    26  			Method: "GET",
    27  			Path:   "/v2/service_brokers",
    28  			Response: testnet.TestResponse{
    29  				Status: http.StatusOK,
    30  				Body: `{
    31  				  "next_url": "/v2/service_brokers?page=2",
    32  				  "resources": [
    33  					{
    34  					  "metadata": {
    35  						"guid":"found-guid-1"
    36  					  },
    37  					  "entity": {
    38  						"name": "found-name-1",
    39  						"broker_url": "http://found.example.com-1",
    40  						"auth_username": "found-username-1",
    41  						"auth_password": "found-password-1"
    42  					  }
    43  					}
    44  				  ]
    45  				}`,
    46  			},
    47  		})
    48  
    49  		secondRequest := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    50  			Method: "GET",
    51  			Path:   "/v2/service_brokers?page=2",
    52  			Response: testnet.TestResponse{
    53  				Status: http.StatusOK,
    54  				Body: `
    55  				{
    56  				  "resources": [
    57  					{
    58  					  "metadata": {
    59  						"guid":"found-guid-2"
    60  					  },
    61  					  "entity": {
    62  						"name": "found-name-2",
    63  						"broker_url": "http://found.example.com-2",
    64  						"auth_username": "found-username-2",
    65  						"auth_password": "found-password-2"
    66  					  }
    67  					}
    68  				  ]
    69  				}`,
    70  			},
    71  		})
    72  
    73  		ts, handler, repo := createServiceBrokerRepo(firstRequest, secondRequest)
    74  		defer ts.Close()
    75  
    76  		serviceBrokers := []models.ServiceBroker{}
    77  		apiErr := repo.ListServiceBrokers(func(broker models.ServiceBroker) bool {
    78  			serviceBrokers = append(serviceBrokers, broker)
    79  			return true
    80  		})
    81  
    82  		Expect(len(serviceBrokers)).To(Equal(2))
    83  		Expect(serviceBrokers[0].GUID).To(Equal("found-guid-1"))
    84  		Expect(serviceBrokers[1].GUID).To(Equal("found-guid-2"))
    85  		Expect(handler).To(HaveAllRequestsCalled())
    86  		Expect(apiErr).NotTo(HaveOccurred())
    87  	})
    88  
    89  	Describe("FindByName", func() {
    90  		It("returns the service broker with the given name", func() {
    91  			responseBody := `
    92  {"resources": [{
    93    "metadata": {"guid":"found-guid"},
    94    "entity": {
    95    	"name": "found-name",
    96  		"broker_url": "http://found.example.com",
    97  		"auth_username": "found-username",
    98  		"auth_password": "found-password"
    99    }
   100  }]}`
   101  
   102  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   103  				Method:   "GET",
   104  				Path:     "/v2/service_brokers?q=name%3Amy-broker",
   105  				Response: testnet.TestResponse{Status: http.StatusOK, Body: responseBody},
   106  			})
   107  
   108  			ts, handler, repo := createServiceBrokerRepo(req)
   109  			defer ts.Close()
   110  
   111  			foundBroker, apiErr := repo.FindByName("my-broker")
   112  			expectedBroker := models.ServiceBroker{}
   113  			expectedBroker.Name = "found-name"
   114  			expectedBroker.URL = "http://found.example.com"
   115  			expectedBroker.Username = "found-username"
   116  			expectedBroker.Password = "found-password"
   117  			expectedBroker.GUID = "found-guid"
   118  
   119  			Expect(handler).To(HaveAllRequestsCalled())
   120  			Expect(apiErr).NotTo(HaveOccurred())
   121  			Expect(foundBroker).To(Equal(expectedBroker))
   122  		})
   123  
   124  		It("returns an error when the service broker cannot be found", func() {
   125  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   126  				Method:   "GET",
   127  				Path:     "/v2/service_brokers?q=name%3Amy-broker",
   128  				Response: testnet.TestResponse{Status: http.StatusOK, Body: `{ "resources": [ ] }`},
   129  			})
   130  
   131  			ts, handler, repo := createServiceBrokerRepo(req)
   132  			defer ts.Close()
   133  
   134  			_, apiErr := repo.FindByName("my-broker")
   135  
   136  			Expect(handler).To(HaveAllRequestsCalled())
   137  			Expect(apiErr).To(HaveOccurred())
   138  			Expect(apiErr.Error()).To(Equal("Service Broker my-broker not found"))
   139  		})
   140  
   141  		It("returns an error when listing service brokers returns an api error", func() {
   142  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   143  				Method: "GET",
   144  				Path:   "/v2/service_brokers?q=name%3Amy-broker",
   145  				Response: testnet.TestResponse{Status: http.StatusForbidden, Body: `{
   146  				  "code": 10003,
   147  				  "description": "You are not authorized to perform the requested action",
   148  				  "error_code": "CF-NotAuthorized"
   149  			  }`},
   150  			})
   151  
   152  			ts, handler, repo := createServiceBrokerRepo(req)
   153  			defer ts.Close()
   154  
   155  			_, apiErr := repo.FindByName("my-broker")
   156  
   157  			Expect(handler).To(HaveAllRequestsCalled())
   158  			Expect(apiErr).To(HaveOccurred())
   159  			Expect(apiErr.Error()).To(Equal("Server error, status code: 403, error code: 10003, message: You are not authorized to perform the requested action"))
   160  		})
   161  	})
   162  
   163  	Describe("FindByGUID", func() {
   164  		It("returns the service broker with the given guid", func() {
   165  			responseBody := `
   166  {
   167     "metadata": {
   168        "guid": "found-guid",
   169        "url": "/v2/service_brokers/found-guid",
   170        "created_at": "2014-07-24T21:21:54+00:00",
   171        "updated_at": "2014-07-25T17:03:40+00:00"
   172     },
   173     "entity": {
   174        "name": "found-name",
   175        "broker_url": "http://found.example.com",
   176        "auth_username": "found-username"
   177     }
   178  }
   179  `
   180  
   181  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   182  				Method:   "GET",
   183  				Path:     "/v2/service_brokers/found-guid",
   184  				Response: testnet.TestResponse{Status: http.StatusOK, Body: responseBody},
   185  			})
   186  
   187  			ts, handler, repo := createServiceBrokerRepo(req)
   188  			defer ts.Close()
   189  
   190  			foundBroker, apiErr := repo.FindByGUID("found-guid")
   191  			expectedBroker := models.ServiceBroker{}
   192  			expectedBroker.Name = "found-name"
   193  			expectedBroker.URL = "http://found.example.com"
   194  			expectedBroker.Username = "found-username"
   195  			expectedBroker.GUID = "found-guid"
   196  
   197  			Expect(handler).To(HaveAllRequestsCalled())
   198  			Expect(apiErr).NotTo(HaveOccurred())
   199  			Expect(foundBroker).To(Equal(expectedBroker))
   200  		})
   201  
   202  		It("returns an error when the service broker cannot be found", func() {
   203  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   204  				Method: "GET",
   205  				Path:   "/v2/service_brokers/bogus-guid",
   206  				//This error code may not reflect reality.  Check it, change the code to match, and remove this comment.
   207  				Response: testnet.TestResponse{Status: http.StatusNotFound, Body: `{"error_code":"ServiceBrokerNotFound","description":"Service Broker bogus-guid not found","code":270042}`},
   208  			})
   209  
   210  			ts, handler, repo := createServiceBrokerRepo(req)
   211  			defer ts.Close()
   212  
   213  			_, apiErr := repo.FindByGUID("bogus-guid")
   214  
   215  			Expect(handler).To(HaveAllRequestsCalled())
   216  			Expect(apiErr).To(HaveOccurred())
   217  			Expect(apiErr.Error()).To(Equal("Server error, status code: 404, error code: 270042, message: Service Broker bogus-guid not found"))
   218  		})
   219  	})
   220  
   221  	Describe("Create", func() {
   222  		var (
   223  			ccServer *ghttp.Server
   224  			repo     CloudControllerServiceBrokerRepository
   225  		)
   226  
   227  		BeforeEach(func() {
   228  			ccServer = ghttp.NewServer()
   229  
   230  			configRepo := testconfig.NewRepositoryWithDefaults()
   231  			configRepo.SetAPIEndpoint(ccServer.URL())
   232  			gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
   233  			repo = NewCloudControllerServiceBrokerRepository(configRepo, gateway)
   234  		})
   235  
   236  		AfterEach(func() {
   237  			ccServer.Close()
   238  		})
   239  
   240  		It("creates the service broker with the given name, URL, username and password", func() {
   241  			ccServer.AppendHandlers(
   242  				ghttp.CombineHandlers(
   243  					ghttp.VerifyRequest("POST", "/v2/service_brokers"),
   244  					ghttp.VerifyJSON(`
   245  						{
   246  						    "name": "foobroker",
   247  						    "broker_url": "http://example.com",
   248  						    "auth_username": "foouser",
   249  						    "auth_password": "password"
   250  						}
   251  					`),
   252  					ghttp.RespondWith(http.StatusCreated, nil),
   253  				),
   254  			)
   255  
   256  			err := repo.Create("foobroker", "http://example.com", "foouser", "password", "")
   257  			Expect(err).NotTo(HaveOccurred())
   258  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   259  		})
   260  
   261  		It("creates the service broker with the correct params when given a space GUID", func() {
   262  			ccServer.AppendHandlers(
   263  				ghttp.CombineHandlers(
   264  					ghttp.VerifyRequest("POST", "/v2/service_brokers"),
   265  					ghttp.VerifyJSON(`
   266  						{
   267  								"name": "foobroker",
   268  								"broker_url": "http://example.com",
   269  								"auth_username": "foouser",
   270  								"auth_password": "password",
   271  								"space_guid": "space-guid"
   272  						}
   273  					`),
   274  					ghttp.RespondWith(http.StatusCreated, nil),
   275  				),
   276  			)
   277  
   278  			err := repo.Create("foobroker", "http://example.com", "foouser", "password", "space-guid")
   279  			Expect(err).NotTo(HaveOccurred())
   280  			Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   281  		})
   282  	})
   283  
   284  	Describe("Update", func() {
   285  		It("updates the service broker with the given guid", func() {
   286  			expectedReqBody := `{"broker_url":"http://update.example.com","auth_username":"update-foouser","auth_password":"update-password"}`
   287  
   288  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   289  				Method:   "PUT",
   290  				Path:     "/v2/service_brokers/my-guid",
   291  				Matcher:  testnet.RequestBodyMatcher(expectedReqBody),
   292  				Response: testnet.TestResponse{Status: http.StatusOK},
   293  			})
   294  
   295  			ts, handler, repo := createServiceBrokerRepo(req)
   296  			defer ts.Close()
   297  			serviceBroker := models.ServiceBroker{}
   298  			serviceBroker.GUID = "my-guid"
   299  			serviceBroker.Name = "foobroker"
   300  			serviceBroker.URL = "http://update.example.com"
   301  			serviceBroker.Username = "update-foouser"
   302  			serviceBroker.Password = "update-password"
   303  
   304  			apiErr := repo.Update(serviceBroker)
   305  
   306  			Expect(handler).To(HaveAllRequestsCalled())
   307  			Expect(apiErr).NotTo(HaveOccurred())
   308  		})
   309  	})
   310  
   311  	Describe("Rename", func() {
   312  		It("renames the service broker with the given guid", func() {
   313  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   314  				Method:   "PUT",
   315  				Path:     "/v2/service_brokers/my-guid",
   316  				Matcher:  testnet.RequestBodyMatcher(`{"name":"update-foobroker"}`),
   317  				Response: testnet.TestResponse{Status: http.StatusOK},
   318  			})
   319  
   320  			ts, handler, repo := createServiceBrokerRepo(req)
   321  			defer ts.Close()
   322  
   323  			apiErr := repo.Rename("my-guid", "update-foobroker")
   324  
   325  			Expect(handler).To(HaveAllRequestsCalled())
   326  			Expect(apiErr).NotTo(HaveOccurred())
   327  		})
   328  	})
   329  
   330  	Describe("Delete", func() {
   331  		It("deletes the service broker with the given guid", func() {
   332  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   333  				Method:   "DELETE",
   334  				Path:     "/v2/service_brokers/my-guid",
   335  				Response: testnet.TestResponse{Status: http.StatusNoContent},
   336  			})
   337  
   338  			ts, handler, repo := createServiceBrokerRepo(req)
   339  			defer ts.Close()
   340  
   341  			apiErr := repo.Delete("my-guid")
   342  
   343  			Expect(handler).To(HaveAllRequestsCalled())
   344  			Expect(apiErr).NotTo(HaveOccurred())
   345  		})
   346  	})
   347  })
   348  
   349  func createServiceBrokerRepo(requests ...testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo ServiceBrokerRepository) {
   350  	ts, handler = testnet.NewServer(requests)
   351  	configRepo := testconfig.NewRepositoryWithDefaults()
   352  	configRepo.SetAPIEndpoint(ts.URL)
   353  	gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
   354  	repo = NewCloudControllerServiceBrokerRepository(configRepo, gateway)
   355  	return
   356  }