github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/user_provided_service_instances_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  )
    21  
    22  var _ = Describe("UserProvidedServiceRepository", func() {
    23  
    24  	Context("Create()", func() {
    25  		It("creates a user provided service with a name and credentials", func() {
    26  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    27  				Method:   "POST",
    28  				Path:     "/v2/user_provided_service_instances",
    29  				Matcher:  testnet.RequestBodyMatcher(`{"name":"my-custom-service","credentials":{"host":"example.com","password":"secret","user":"me"},"space_guid":"my-space-guid","syslog_drain_url":"","route_service_url":""}`),
    30  				Response: testnet.TestResponse{Status: http.StatusCreated},
    31  			})
    32  
    33  			ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req})
    34  			defer ts.Close()
    35  
    36  			apiErr := repo.Create("my-custom-service", "", "", map[string]interface{}{
    37  				"host":     "example.com",
    38  				"user":     "me",
    39  				"password": "secret",
    40  			})
    41  			Expect(handler).To(HaveAllRequestsCalled())
    42  			Expect(apiErr).NotTo(HaveOccurred())
    43  		})
    44  
    45  		It("creates user provided service instances with syslog drains", func() {
    46  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    47  				Method:   "POST",
    48  				Path:     "/v2/user_provided_service_instances",
    49  				Matcher:  testnet.RequestBodyMatcher(`{"name":"my-custom-service","credentials":{"host":"example.com","password":"secret","user":"me"},"space_guid":"my-space-guid","syslog_drain_url":"syslog://example.com","route_service_url":""}`),
    50  				Response: testnet.TestResponse{Status: http.StatusCreated},
    51  			})
    52  
    53  			ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req})
    54  			defer ts.Close()
    55  
    56  			apiErr := repo.Create("my-custom-service", "syslog://example.com", "", map[string]interface{}{
    57  				"host":     "example.com",
    58  				"user":     "me",
    59  				"password": "secret",
    60  			})
    61  			Expect(handler).To(HaveAllRequestsCalled())
    62  			Expect(apiErr).NotTo(HaveOccurred())
    63  		})
    64  
    65  		It("creates user provided service instances with route service url", func() {
    66  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    67  				Method:   "POST",
    68  				Path:     "/v2/user_provided_service_instances",
    69  				Matcher:  testnet.RequestBodyMatcher(`{"name":"my-custom-service","credentials":{"host":"example.com","password":"secret","user":"me"},"space_guid":"my-space-guid","syslog_drain_url":"","route_service_url":"https://example.com"}`),
    70  				Response: testnet.TestResponse{Status: http.StatusCreated},
    71  			})
    72  
    73  			ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req})
    74  			defer ts.Close()
    75  
    76  			apiErr := repo.Create("my-custom-service", "", "https://example.com", map[string]interface{}{
    77  				"host":     "example.com",
    78  				"user":     "me",
    79  				"password": "secret",
    80  			})
    81  			Expect(handler).To(HaveAllRequestsCalled())
    82  			Expect(apiErr).NotTo(HaveOccurred())
    83  		})
    84  	})
    85  
    86  	Context("Update()", func() {
    87  		It("can update a user provided service, given a service instance with a guid", func() {
    88  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    89  				Method:   "PUT",
    90  				Path:     "/v2/user_provided_service_instances/my-instance-guid",
    91  				Matcher:  testnet.RequestBodyMatcher(`{"credentials":{"host":"example.com","password":"secret","user":"me"},"syslog_drain_url":"syslog://example.com","route_service_url":""}`),
    92  				Response: testnet.TestResponse{Status: http.StatusCreated},
    93  			})
    94  
    95  			ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req})
    96  			defer ts.Close()
    97  
    98  			params := map[string]interface{}{
    99  				"host":     "example.com",
   100  				"user":     "me",
   101  				"password": "secret",
   102  			}
   103  			serviceInstance := models.ServiceInstanceFields{}
   104  			serviceInstance.GUID = "my-instance-guid"
   105  			serviceInstance.Params = params
   106  			serviceInstance.SysLogDrainURL = "syslog://example.com"
   107  			serviceInstance.RouteServiceURL = ""
   108  
   109  			apiErr := repo.Update(serviceInstance)
   110  			Expect(handler).To(HaveAllRequestsCalled())
   111  			Expect(apiErr).NotTo(HaveOccurred())
   112  		})
   113  	})
   114  
   115  	Context("GetSummaries()", func() {
   116  		It("returns all user created service in []models.UserProvidedService", func() {
   117  			responseStr := testnet.TestResponse{Status: http.StatusOK, Body: `
   118  {
   119     "total_results": 2,
   120     "total_pages": 1,
   121     "prev_url": null,
   122     "next_url": null,
   123     "resources": [
   124        {
   125           "metadata": {
   126              "guid": "2d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4",
   127              "url": "/v2/user_provided_service_instances/2d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4",
   128              "created_at": "2015-01-15T22:57:08Z",
   129              "updated_at": null
   130           },
   131           "entity": {
   132              "name": "test_service",
   133              "credentials": {},
   134              "space_guid": "f36dbf3e-eff1-4336-ae5c-aff01dd8ce94",
   135              "type": "user_provided_service_instance",
   136              "syslog_drain_url": "",
   137              "space_url": "/v2/spaces/f36dbf3e-eff1-4336-ae5c-aff01dd8ce94",
   138              "service_bindings_url": "/v2/user_provided_service_instances/2d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4/service_bindings"
   139           }
   140        },
   141        {
   142           "metadata": {
   143              "guid": "9d0a1eb6-b6e5-4b92-b1da-91c5ed26b3b4",
   144              "url": "/v2/user_provided_service_instances/9d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4",
   145              "created_at": "2015-01-15T22:57:08Z",
   146              "updated_at": null
   147           },
   148           "entity": {
   149              "name": "test_service2",
   150              "credentials": {
   151  							"password": "admin",
   152                "username": "admin"
   153  						},
   154              "space_guid": "f36dbf3e-eff1-4336-ae5c-aff01dd8ce94",
   155              "type": "user_provided_service_instance",
   156              "syslog_drain_url": "sample/drainURL",
   157              "space_url": "/v2/spaces/f36dbf3e-eff1-4336-ae5c-aff01dd8ce94",
   158              "service_bindings_url": "/v2/user_provided_service_instances/2d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4/service_bindings"
   159           }
   160        }
   161     ]
   162  }`}
   163  
   164  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   165  				Method:   "GET",
   166  				Path:     "/v2/user_provided_service_instances",
   167  				Response: responseStr,
   168  			})
   169  
   170  			ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req})
   171  			defer ts.Close()
   172  
   173  			summaries, apiErr := repo.GetSummaries()
   174  			Expect(apiErr).NotTo(HaveOccurred())
   175  			Expect(handler).To(HaveAllRequestsCalled())
   176  			Expect(len(summaries.Resources)).To(Equal(2))
   177  
   178  			Expect(summaries.Resources[0].Name).To(Equal("test_service"))
   179  			Expect(summaries.Resources[1].Name).To(Equal("test_service2"))
   180  			Expect(summaries.Resources[1].Credentials["username"]).To(Equal("admin"))
   181  			Expect(summaries.Resources[1].SysLogDrainURL).To(Equal("sample/drainURL"))
   182  		})
   183  	})
   184  
   185  })
   186  
   187  func createUserProvidedServiceInstanceRepo(req []testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo UserProvidedServiceInstanceRepository) {
   188  	ts, handler = testnet.NewServer(req)
   189  	configRepo := testconfig.NewRepositoryWithDefaults()
   190  	configRepo.SetAPIEndpoint(ts.URL)
   191  	gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
   192  	repo = NewCCUserProvidedServiceInstanceRepository(configRepo, gateway)
   193  	return
   194  }