github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/bind_service_test.go (about) 1 package service_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 7 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 8 "github.com/cloudfoundry/cli/cf/command_registry" 9 "github.com/cloudfoundry/cli/cf/configuration/core_config" 10 "github.com/cloudfoundry/cli/cf/models" 11 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 12 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 13 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 14 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 18 . "github.com/cloudfoundry/cli/testhelpers/matchers" 19 ) 20 21 var _ = Describe("bind-service command", func() { 22 var ( 23 ui *testterm.FakeUI 24 requirementsFactory *testreq.FakeReqFactory 25 config core_config.Repository 26 serviceBindingRepo *testapi.FakeServiceBindingRepo 27 deps command_registry.Dependency 28 ) 29 30 updateCommandDependency := func(pluginCall bool) { 31 deps.Ui = ui 32 deps.Config = config 33 deps.RepoLocator = deps.RepoLocator.SetServiceBindingRepository(serviceBindingRepo) 34 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("bind-service").SetDependency(deps, pluginCall)) 35 } 36 37 BeforeEach(func() { 38 ui = &testterm.FakeUI{} 39 config = testconfig.NewRepositoryWithDefaults() 40 requirementsFactory = &testreq.FakeReqFactory{} 41 serviceBindingRepo = &testapi.FakeServiceBindingRepo{} 42 }) 43 44 var callBindService = func(args []string) bool { 45 return testcmd.RunCliCommand("bind-service", args, requirementsFactory, updateCommandDependency, false) 46 } 47 48 It("fails requirements when not logged in", func() { 49 Expect(callBindService([]string{"service", "app"})).To(BeFalse()) 50 }) 51 52 Context("when logged in", func() { 53 BeforeEach(func() { 54 requirementsFactory.LoginSuccess = true 55 }) 56 57 It("binds a service instance to an app", func() { 58 app := models.Application{} 59 app.Name = "my-app" 60 app.Guid = "my-app-guid" 61 serviceInstance := models.ServiceInstance{} 62 serviceInstance.Name = "my-service" 63 serviceInstance.Guid = "my-service-guid" 64 requirementsFactory.Application = app 65 requirementsFactory.ServiceInstance = serviceInstance 66 callBindService([]string{"my-app", "my-service"}) 67 68 Expect(requirementsFactory.ApplicationName).To(Equal("my-app")) 69 Expect(requirementsFactory.ServiceInstanceName).To(Equal("my-service")) 70 71 Expect(ui.Outputs).To(ContainSubstrings( 72 []string{"Binding service", "my-service", "my-app", "my-org", "my-space", "my-user"}, 73 []string{"OK"}, 74 []string{"TIP", "my-app"}, 75 )) 76 Expect(serviceBindingRepo.CreateServiceInstanceGuid).To(Equal("my-service-guid")) 77 Expect(serviceBindingRepo.CreateApplicationGuid).To(Equal("my-app-guid")) 78 }) 79 80 It("warns the user when the service instance is already bound to the given app", func() { 81 app := models.Application{} 82 app.Name = "my-app" 83 app.Guid = "my-app-guid" 84 serviceInstance := models.ServiceInstance{} 85 serviceInstance.Name = "my-service" 86 serviceInstance.Guid = "my-service-guid" 87 requirementsFactory.Application = app 88 requirementsFactory.ServiceInstance = serviceInstance 89 serviceBindingRepo = &testapi.FakeServiceBindingRepo{CreateErrorCode: "90003"} 90 callBindService([]string{"my-app", "my-service"}) 91 92 Expect(ui.Outputs).To(ContainSubstrings( 93 []string{"Binding service"}, 94 []string{"OK"}, 95 []string{"my-app", "is already bound", "my-service"}, 96 )) 97 }) 98 99 It("warns the user when the error is non HttpError ", func() { 100 app := models.Application{} 101 app.Name = "my-app1" 102 app.Guid = "my-app1-guid1" 103 serviceInstance := models.ServiceInstance{} 104 serviceInstance.Name = "my-service1" 105 serviceInstance.Guid = "my-service1-guid1" 106 requirementsFactory.Application = app 107 requirementsFactory.ServiceInstance = serviceInstance 108 serviceBindingRepo = &testapi.FakeServiceBindingRepo{CreateNonHttpErrCode: "1001"} 109 callBindService([]string{"my-app1", "my-service1"}) 110 Expect(ui.Outputs).To(ContainSubstrings( 111 []string{"Binding service", "my-service", "my-app", "my-org", "my-space", "my-user"}, 112 []string{"FAILED"}, 113 []string{"1001"}, 114 )) 115 }) 116 117 It("fails with usage when called without a service instance and app", func() { 118 callBindService([]string{"my-service"}) 119 Expect(ui.Outputs).To(ContainSubstrings( 120 []string{"Incorrect Usage", "Requires", "arguments"}, 121 )) 122 123 ui = &testterm.FakeUI{} 124 callBindService([]string{"my-app"}) 125 Expect(ui.Outputs).To(ContainSubstrings( 126 []string{"Incorrect Usage", "Requires", "arguments"}, 127 )) 128 129 ui = &testterm.FakeUI{} 130 callBindService([]string{"my-app", "my-service"}) 131 Expect(ui.Outputs).ToNot(ContainSubstrings( 132 []string{"Incorrect Usage", "Requires", "arguments"}, 133 )) 134 }) 135 136 Context("when passing arbitrary params", func() { 137 var ( 138 app models.Application 139 serviceInstance models.ServiceInstance 140 ) 141 142 BeforeEach(func() { 143 app = models.Application{} 144 app.Name = "my-app" 145 app.Guid = "my-app-guid" 146 147 serviceInstance = models.ServiceInstance{} 148 serviceInstance.Name = "my-service" 149 serviceInstance.Guid = "my-service-guid" 150 151 requirementsFactory.Application = app 152 requirementsFactory.ServiceInstance = serviceInstance 153 }) 154 155 Context("as a json string", func() { 156 It("successfully creates a service and passes the params as a json string", func() { 157 callBindService([]string{"my-app", "my-service", "-c", `{"foo": "bar"}`}) 158 159 Expect(ui.Outputs).To(ContainSubstrings( 160 []string{"Binding service", "my-service", "my-app", "my-org", "my-space", "my-user"}, 161 []string{"OK"}, 162 []string{"TIP"}, 163 )) 164 Expect(serviceBindingRepo.CreateServiceInstanceGuid).To(Equal("my-service-guid")) 165 Expect(serviceBindingRepo.CreateApplicationGuid).To(Equal("my-app-guid")) 166 Expect(serviceBindingRepo.CreateParams).To(Equal(map[string]interface{}{"foo": "bar"})) 167 }) 168 169 Context("that are not valid json", func() { 170 It("returns an error to the UI", func() { 171 callBindService([]string{"my-app", "my-service", "-c", `bad-json`}) 172 173 Expect(ui.Outputs).To(ContainSubstrings( 174 []string{"FAILED"}, 175 []string{"Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object."}, 176 )) 177 }) 178 }) 179 }) 180 181 Context("as a file that contains json", func() { 182 var jsonFile *os.File 183 var params string 184 185 BeforeEach(func() { 186 params = "{\"foo\": \"bar\"}" 187 }) 188 189 AfterEach(func() { 190 if jsonFile != nil { 191 jsonFile.Close() 192 os.Remove(jsonFile.Name()) 193 } 194 }) 195 196 JustBeforeEach(func() { 197 var err error 198 jsonFile, err = ioutil.TempFile("", "") 199 Expect(err).ToNot(HaveOccurred()) 200 201 err = ioutil.WriteFile(jsonFile.Name(), []byte(params), os.ModePerm) 202 Expect(err).NotTo(HaveOccurred()) 203 }) 204 205 It("successfully creates a service and passes the params as a json", func() { 206 callBindService([]string{"my-app", "my-service", "-c", jsonFile.Name()}) 207 208 Expect(ui.Outputs).To(ContainSubstrings( 209 []string{"Binding service", "my-service", "my-app", "my-org", "my-space", "my-user"}, 210 []string{"OK"}, 211 []string{"TIP"}, 212 )) 213 Expect(serviceBindingRepo.CreateServiceInstanceGuid).To(Equal("my-service-guid")) 214 Expect(serviceBindingRepo.CreateApplicationGuid).To(Equal("my-app-guid")) 215 Expect(serviceBindingRepo.CreateParams).To(Equal(map[string]interface{}{"foo": "bar"})) 216 }) 217 218 Context("that are not valid json", func() { 219 BeforeEach(func() { 220 params = "bad-json" 221 }) 222 223 It("returns an error to the UI", func() { 224 callBindService([]string{"my-app", "my-service", "-c", jsonFile.Name()}) 225 226 Expect(ui.Outputs).To(ContainSubstrings( 227 []string{"FAILED"}, 228 []string{"Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object."}, 229 )) 230 }) 231 }) 232 }) 233 }) 234 }) 235 })