github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/unbind_service_test.go (about) 1 package service_test 2 3 import ( 4 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 5 "github.com/cloudfoundry/cli/cf/command_registry" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/models" 8 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 9 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 10 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 11 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 15 . "github.com/cloudfoundry/cli/testhelpers/matchers" 16 ) 17 18 var _ = Describe("unbind-service command", func() { 19 var ( 20 app models.Application 21 ui *testterm.FakeUI 22 config core_config.Repository 23 serviceInstance models.ServiceInstance 24 requirementsFactory *testreq.FakeReqFactory 25 serviceBindingRepo *testapi.FakeServiceBindingRepo 26 deps command_registry.Dependency 27 ) 28 29 updateCommandDependency := func(pluginCall bool) { 30 deps.Ui = ui 31 deps.RepoLocator = deps.RepoLocator.SetServiceBindingRepository(serviceBindingRepo) 32 deps.Config = config 33 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("unbind-service").SetDependency(deps, pluginCall)) 34 } 35 36 BeforeEach(func() { 37 app.Name = "my-app" 38 app.Guid = "my-app-guid" 39 40 ui = &testterm.FakeUI{} 41 serviceInstance.Name = "my-service" 42 serviceInstance.Guid = "my-service-guid" 43 44 config = testconfig.NewRepositoryWithDefaults() 45 requirementsFactory = &testreq.FakeReqFactory{} 46 requirementsFactory.Application = app 47 requirementsFactory.ServiceInstance = serviceInstance 48 49 serviceBindingRepo = &testapi.FakeServiceBindingRepo{} 50 }) 51 52 callUnbindService := func(args []string) bool { 53 return testcmd.RunCliCommand("unbind-service", args, requirementsFactory, updateCommandDependency, false) 54 } 55 56 Context("when not logged in", func() { 57 It("fails requirements when not logged in", func() { 58 Expect(testcmd.RunCliCommand("unbind-service", []string{"my-service", "my-app"}, requirementsFactory, updateCommandDependency, false)).To(BeFalse()) 59 }) 60 }) 61 62 Context("when logged in", func() { 63 BeforeEach(func() { 64 requirementsFactory.LoginSuccess = true 65 }) 66 67 Context("when the service instance exists", func() { 68 It("unbinds a service from an app", func() { 69 callUnbindService([]string{"my-app", "my-service"}) 70 71 Expect(requirementsFactory.ApplicationName).To(Equal("my-app")) 72 Expect(requirementsFactory.ServiceInstanceName).To(Equal("my-service")) 73 74 Expect(ui.Outputs).To(ContainSubstrings( 75 []string{"Unbinding app", "my-service", "my-app", "my-org", "my-space", "my-user"}, 76 []string{"OK"}, 77 )) 78 Expect(serviceBindingRepo.DeleteServiceInstance).To(Equal(serviceInstance)) 79 Expect(serviceBindingRepo.DeleteApplicationGuid).To(Equal("my-app-guid")) 80 }) 81 }) 82 83 Context("when the service instance does not exist", func() { 84 BeforeEach(func() { 85 serviceBindingRepo.DeleteBindingNotFound = true 86 }) 87 88 It("warns the user the the service instance does not exist", func() { 89 callUnbindService([]string{"my-app", "my-service"}) 90 91 Expect(requirementsFactory.ApplicationName).To(Equal("my-app")) 92 Expect(requirementsFactory.ServiceInstanceName).To(Equal("my-service")) 93 94 Expect(ui.Outputs).To(ContainSubstrings( 95 []string{"Unbinding app", "my-service", "my-app"}, 96 []string{"OK"}, 97 []string{"my-service", "my-app", "did not exist"}, 98 )) 99 Expect(serviceBindingRepo.DeleteServiceInstance).To(Equal(serviceInstance)) 100 Expect(serviceBindingRepo.DeleteApplicationGuid).To(Equal("my-app-guid")) 101 }) 102 }) 103 104 It("when no parameters are given the command fails with usage", func() { 105 callUnbindService([]string{"my-service"}) 106 Expect(ui.Outputs).To(ContainSubstrings( 107 []string{"Incorrect Usage", "Requires", "argument"}, 108 )) 109 110 ui = &testterm.FakeUI{} 111 callUnbindService([]string{"my-app"}) 112 Expect(ui.Outputs).To(ContainSubstrings( 113 []string{"Incorrect Usage", "Requires", "argument"}, 114 )) 115 116 ui = &testterm.FakeUI{} 117 callUnbindService([]string{"my-app", "my-service"}) 118 Expect(ui.Outputs).ToNot(ContainSubstrings( 119 []string{"Incorrect Usage", "Requires", "argument"}, 120 )) 121 }) 122 }) 123 })