github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicebroker/delete_service_broker_test.go (about) 1 package servicebroker_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/apifakes" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 12 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 13 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 14 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("delete-service-broker command", func() { 20 var ( 21 ui *testterm.FakeUI 22 configRepo coreconfig.Repository 23 brokerRepo *apifakes.FakeServiceBrokerRepository 24 requirementsFactory *requirementsfakes.FakeFactory 25 deps commandregistry.Dependency 26 ) 27 28 updateCommandDependency := func(pluginCall bool) { 29 deps.UI = ui 30 deps.RepoLocator = deps.RepoLocator.SetServiceBrokerRepository(brokerRepo) 31 deps.Config = configRepo 32 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-service-broker").SetDependency(deps, pluginCall)) 33 } 34 35 BeforeEach(func() { 36 ui = &testterm.FakeUI{Inputs: []string{"y"}} 37 brokerRepo = new(apifakes.FakeServiceBrokerRepository) 38 configRepo = testconfig.NewRepositoryWithDefaults() 39 requirementsFactory = new(requirementsfakes.FakeFactory) 40 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 41 }) 42 43 runCommand := func(args ...string) bool { 44 return testcmd.RunCLICommand("delete-service-broker", args, requirementsFactory, updateCommandDependency, false, ui) 45 } 46 47 Describe("requirements", func() { 48 It("fails with usage when called without a broker's name", func() { 49 runCommand() 50 Expect(ui.Outputs()).To(ContainSubstrings( 51 []string{"Incorrect Usage", "Requires an argument"}, 52 )) 53 }) 54 55 It("fails requirements when not logged in", func() { 56 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 57 58 Expect(runCommand("-f", "my-broker")).To(BeFalse()) 59 }) 60 }) 61 62 Context("when the service broker exists", func() { 63 BeforeEach(func() { 64 brokerRepo.FindByNameReturns(models.ServiceBroker{ 65 Name: "service-broker-to-delete", 66 GUID: "service-broker-to-delete-guid", 67 }, nil) 68 }) 69 70 It("deletes the service broker with the given name", func() { 71 runCommand("service-broker-to-delete") 72 Expect(brokerRepo.FindByNameCallCount()).To(Equal(1)) 73 Expect(brokerRepo.FindByNameArgsForCall(0)).To(Equal("service-broker-to-delete")) 74 Expect(brokerRepo.DeleteCallCount()).To(Equal(1)) 75 Expect(brokerRepo.DeleteArgsForCall(0)).To(Equal("service-broker-to-delete-guid")) 76 Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service-broker service-broker-to-delete"})) 77 78 Expect(ui.Outputs()).To(ContainSubstrings( 79 []string{"Deleting service broker", "service-broker-to-delete", "my-user"}, 80 []string{"OK"}, 81 )) 82 }) 83 84 It("does not prompt when the -f flag is provided", func() { 85 runCommand("-f", "service-broker-to-delete") 86 87 Expect(brokerRepo.FindByNameArgsForCall(0)).To(Equal("service-broker-to-delete")) 88 Expect(brokerRepo.DeleteArgsForCall(0)).To(Equal("service-broker-to-delete-guid")) 89 90 Expect(ui.Prompts).To(BeEmpty()) 91 Expect(ui.Outputs()).To(ContainSubstrings( 92 []string{"Deleting service broker", "service-broker-to-delete", "my-user"}, 93 []string{"OK"}, 94 )) 95 }) 96 }) 97 98 Context("when the service broker does not exist", func() { 99 BeforeEach(func() { 100 brokerRepo.FindByNameReturns(models.ServiceBroker{}, errors.NewModelNotFoundError("Service Broker", "service-broker-to-delete")) 101 }) 102 103 It("warns the user", func() { 104 ui.Inputs = []string{} 105 runCommand("-f", "service-broker-to-delete") 106 107 Expect(brokerRepo.FindByNameCallCount()).To(Equal(1)) 108 Expect(brokerRepo.FindByNameArgsForCall(0)).To(Equal("service-broker-to-delete")) 109 Expect(brokerRepo.DeleteCallCount()).To(BeZero()) 110 Expect(ui.Outputs()).To(ContainSubstrings( 111 []string{"Deleting service broker", "service-broker-to-delete"}, 112 []string{"OK"}, 113 )) 114 115 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"service-broker-to-delete", "does not exist"})) 116 }) 117 }) 118 })