github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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/cf/util/testhelpers/configuration" 13 testnet "code.cloudfoundry.org/cli/cf/util/testhelpers/net" 14 15 . "code.cloudfoundry.org/cli/cf/api" 16 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 17 . "code.cloudfoundry.org/cli/cf/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 }, nil) 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 }, nil) 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 }, nil) 81 Expect(handler).To(HaveAllRequestsCalled()) 82 Expect(apiErr).NotTo(HaveOccurred()) 83 }) 84 85 It("creates user provided service instances with tags", func() { 86 req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ 87 Method: "POST", 88 Path: "/v2/user_provided_service_instances", 89 Matcher: testnet.RequestBodyMatcher(`{"name":"my-custom-service","credentials":{},"space_guid":"my-space-guid","syslog_drain_url":"","route_service_url":"","tags": ["tag1", "tag2"]}`), 90 Response: testnet.TestResponse{Status: http.StatusCreated}, 91 }) 92 93 ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req}) 94 defer ts.Close() 95 96 apiErr := repo.Create("my-custom-service", "", "", map[string]interface{}{}, []string{"tag1", "tag2"}) 97 Expect(handler).To(HaveAllRequestsCalled()) 98 Expect(apiErr).NotTo(HaveOccurred()) 99 }) 100 }) 101 102 Context("Update()", func() { 103 It("can update a user provided service, given a service instance with a guid", func() { 104 req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ 105 Method: "PUT", 106 Path: "/v2/user_provided_service_instances/my-instance-guid", 107 Matcher: testnet.RequestBodyMatcher(`{"credentials":{"host":"example.com","password":"secret","user":"me"},"syslog_drain_url":"syslog://example.com","route_service_url":"","tags": ["tag3", "tag4"]}`), 108 Response: testnet.TestResponse{Status: http.StatusCreated}, 109 }) 110 111 ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req}) 112 defer ts.Close() 113 114 params := map[string]interface{}{ 115 "host": "example.com", 116 "user": "me", 117 "password": "secret", 118 } 119 serviceInstance := models.ServiceInstanceFields{} 120 serviceInstance.GUID = "my-instance-guid" 121 serviceInstance.Params = params 122 serviceInstance.SysLogDrainURL = "syslog://example.com" 123 serviceInstance.RouteServiceURL = "" 124 serviceInstance.Tags = []string{"tag3", "tag4"} 125 126 apiErr := repo.Update(serviceInstance) 127 Expect(handler).To(HaveAllRequestsCalled()) 128 Expect(apiErr).NotTo(HaveOccurred()) 129 }) 130 }) 131 132 Context("GetSummaries()", func() { 133 It("returns all user created service in []models.UserProvidedService", func() { 134 responseStr := testnet.TestResponse{Status: http.StatusOK, Body: ` 135 { 136 "total_results": 2, 137 "total_pages": 1, 138 "prev_url": null, 139 "next_url": null, 140 "resources": [ 141 { 142 "metadata": { 143 "guid": "2d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4", 144 "url": "/v2/user_provided_service_instances/2d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4", 145 "created_at": "2015-01-15T22:57:08Z", 146 "updated_at": null 147 }, 148 "entity": { 149 "name": "test_service", 150 "credentials": {}, 151 "space_guid": "f36dbf3e-eff1-4336-ae5c-aff01dd8ce94", 152 "type": "user_provided_service_instance", 153 "syslog_drain_url": "", 154 "space_url": "/v2/spaces/f36dbf3e-eff1-4336-ae5c-aff01dd8ce94", 155 "service_bindings_url": "/v2/user_provided_service_instances/2d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4/service_bindings" 156 } 157 }, 158 { 159 "metadata": { 160 "guid": "9d0a1eb6-b6e5-4b92-b1da-91c5ed26b3b4", 161 "url": "/v2/user_provided_service_instances/9d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4", 162 "created_at": "2015-01-15T22:57:08Z", 163 "updated_at": null 164 }, 165 "entity": { 166 "name": "test_service2", 167 "credentials": { 168 "password": "admin", 169 "username": "admin" 170 }, 171 "space_guid": "f36dbf3e-eff1-4336-ae5c-aff01dd8ce94", 172 "type": "user_provided_service_instance", 173 "syslog_drain_url": "sample/drainURL", 174 "space_url": "/v2/spaces/f36dbf3e-eff1-4336-ae5c-aff01dd8ce94", 175 "service_bindings_url": "/v2/user_provided_service_instances/2d0a1eb6-b6e5-4b92-b1da-91c5e826b3b4/service_bindings" 176 } 177 } 178 ] 179 }`} 180 181 req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{ 182 Method: "GET", 183 Path: "/v2/user_provided_service_instances", 184 Response: responseStr, 185 }) 186 187 ts, handler, repo := createUserProvidedServiceInstanceRepo([]testnet.TestRequest{req}) 188 defer ts.Close() 189 190 summaries, apiErr := repo.GetSummaries() 191 Expect(apiErr).NotTo(HaveOccurred()) 192 Expect(handler).To(HaveAllRequestsCalled()) 193 Expect(len(summaries.Resources)).To(Equal(2)) 194 195 Expect(summaries.Resources[0].Name).To(Equal("test_service")) 196 Expect(summaries.Resources[1].Name).To(Equal("test_service2")) 197 Expect(summaries.Resources[1].Credentials["username"]).To(Equal("admin")) 198 Expect(summaries.Resources[1].SysLogDrainURL).To(Equal("sample/drainURL")) 199 }) 200 }) 201 202 }) 203 204 func createUserProvidedServiceInstanceRepo(req []testnet.TestRequest) (ts *httptest.Server, handler *testnet.TestHandler, repo UserProvidedServiceInstanceRepository) { 205 ts, handler = testnet.NewServer(req) 206 configRepo := testconfig.NewRepositoryWithDefaults() 207 configRepo.SetAPIEndpoint(ts.URL) 208 gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 209 repo = NewCCUserProvidedServiceInstanceRepository(configRepo, gateway) 210 return 211 }