github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/servicebroker/delete_service_broker_test.go (about)

     1  package servicebroker_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  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("delete-service-broker command", func() {
    18  	var (
    19  		ui                  *testterm.FakeUI
    20  		configRepo          core_config.Repository
    21  		brokerRepo          *testapi.FakeServiceBrokerRepo
    22  		requirementsFactory *testreq.FakeReqFactory
    23  		deps                command_registry.Dependency
    24  	)
    25  
    26  	updateCommandDependency := func(pluginCall bool) {
    27  		deps.Ui = ui
    28  		deps.RepoLocator = deps.RepoLocator.SetServiceBrokerRepository(brokerRepo)
    29  		deps.Config = configRepo
    30  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("delete-service-broker").SetDependency(deps, pluginCall))
    31  	}
    32  
    33  	BeforeEach(func() {
    34  		ui = &testterm.FakeUI{Inputs: []string{"y"}}
    35  		brokerRepo = &testapi.FakeServiceBrokerRepo{}
    36  		configRepo = testconfig.NewRepositoryWithDefaults()
    37  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true}
    38  	})
    39  
    40  	runCommand := func(args ...string) bool {
    41  		return testcmd.RunCliCommand("delete-service-broker", args, requirementsFactory, updateCommandDependency, false)
    42  	}
    43  
    44  	Describe("requirements", func() {
    45  		It("fails with usage when called without a broker's name", func() {
    46  			runCommand()
    47  			Expect(ui.Outputs).To(ContainSubstrings(
    48  				[]string{"Incorrect Usage", "Requires an argument"},
    49  			))
    50  		})
    51  
    52  		It("fails requirements when not logged in", func() {
    53  			requirementsFactory.LoginSuccess = false
    54  
    55  			Expect(runCommand("-f", "my-broker")).To(BeFalse())
    56  		})
    57  	})
    58  
    59  	Context("when the service broker exists", func() {
    60  		BeforeEach(func() {
    61  			brokerRepo.FindByNameServiceBroker = models.ServiceBroker{
    62  				Name: "service-broker-to-delete",
    63  				Guid: "service-broker-to-delete-guid",
    64  			}
    65  		})
    66  
    67  		It("deletes the service broker with the given name", func() {
    68  			runCommand("service-broker-to-delete")
    69  
    70  			Expect(brokerRepo.FindByNameName).To(Equal("service-broker-to-delete"))
    71  			Expect(brokerRepo.DeletedServiceBrokerGuid).To(Equal("service-broker-to-delete-guid"))
    72  			Expect(ui.Prompts).To(ContainSubstrings([]string{"Really delete the service-broker service-broker-to-delete"}))
    73  
    74  			Expect(ui.Outputs).To(ContainSubstrings(
    75  				[]string{"Deleting service broker", "service-broker-to-delete", "my-user"},
    76  				[]string{"OK"},
    77  			))
    78  		})
    79  
    80  		It("does not prompt when the -f flag is provided", func() {
    81  			runCommand("-f", "service-broker-to-delete")
    82  
    83  			Expect(brokerRepo.FindByNameName).To(Equal("service-broker-to-delete"))
    84  			Expect(brokerRepo.DeletedServiceBrokerGuid).To(Equal("service-broker-to-delete-guid"))
    85  
    86  			Expect(ui.Prompts).To(BeEmpty())
    87  			Expect(ui.Outputs).To(ContainSubstrings(
    88  				[]string{"Deleting service broker", "service-broker-to-delete", "my-user"},
    89  				[]string{"OK"},
    90  			))
    91  		})
    92  	})
    93  
    94  	Context("when the service broker does not exist", func() {
    95  		BeforeEach(func() {
    96  			brokerRepo.FindByNameNotFound = true
    97  		})
    98  
    99  		It("warns the user", func() {
   100  			ui.Inputs = []string{}
   101  			runCommand("-f", "service-broker-to-delete")
   102  
   103  			Expect(brokerRepo.FindByNameName).To(Equal("service-broker-to-delete"))
   104  			Expect(brokerRepo.DeletedServiceBrokerGuid).To(Equal(""))
   105  			Expect(ui.Outputs).To(ContainSubstrings(
   106  				[]string{"Deleting service broker", "service-broker-to-delete"},
   107  				[]string{"OK"},
   108  			))
   109  
   110  			Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"service-broker-to-delete", "does not exist"}))
   111  		})
   112  	})
   113  })