github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicebroker/rename_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/models"
     8  	"code.cloudfoundry.org/cli/cf/requirements"
     9  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    10  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    11  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    12  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    13  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("rename-service-broker command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		requirementsFactory *requirementsfakes.FakeFactory
    22  		configRepo          coreconfig.Repository
    23  		serviceBrokerRepo   *apifakes.FakeServiceBrokerRepository
    24  		deps                commandregistry.Dependency
    25  	)
    26  
    27  	updateCommandDependency := func(pluginCall bool) {
    28  		deps.UI = ui
    29  		deps.RepoLocator = deps.RepoLocator.SetServiceBrokerRepository(serviceBrokerRepo)
    30  		deps.Config = configRepo
    31  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("rename-service-broker").SetDependency(deps, pluginCall))
    32  	}
    33  
    34  	BeforeEach(func() {
    35  		configRepo = testconfig.NewRepositoryWithDefaults()
    36  
    37  		ui = &testterm.FakeUI{}
    38  		requirementsFactory = new(requirementsfakes.FakeFactory)
    39  		serviceBrokerRepo = new(apifakes.FakeServiceBrokerRepository)
    40  	})
    41  
    42  	runCommand := func(args ...string) bool {
    43  		return testcmd.RunCLICommand("rename-service-broker", args, requirementsFactory, updateCommandDependency, false, ui)
    44  	}
    45  
    46  	Describe("requirements", func() {
    47  		It("fails with usage when not invoked with exactly two args", func() {
    48  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    49  			runCommand("welp")
    50  			Expect(ui.Outputs()).To(ContainSubstrings(
    51  				[]string{"Incorrect Usage", "Requires", "arguments"},
    52  			))
    53  		})
    54  
    55  		It("fails when not logged in", func() {
    56  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    57  			Expect(runCommand("okay", "DO---IIIIT")).To(BeFalse())
    58  		})
    59  	})
    60  
    61  	Context("when logged in", func() {
    62  		BeforeEach(func() {
    63  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    64  			broker := models.ServiceBroker{}
    65  			broker.Name = "my-found-broker"
    66  			broker.GUID = "my-found-broker-guid"
    67  			serviceBrokerRepo.FindByNameReturns(broker, nil)
    68  		})
    69  
    70  		It("renames the given service broker", func() {
    71  			runCommand("my-broker", "my-new-broker")
    72  			Expect(serviceBrokerRepo.FindByNameCallCount()).To(Equal(1))
    73  			Expect(serviceBrokerRepo.FindByNameArgsForCall(0)).To(Equal("my-broker"))
    74  
    75  			Expect(ui.Outputs()).To(ContainSubstrings(
    76  				[]string{"Renaming service broker", "my-found-broker", "my-new-broker", "my-user"},
    77  				[]string{"OK"},
    78  			))
    79  
    80  			Expect(serviceBrokerRepo.RenameCallCount()).To(Equal(1))
    81  			guid, name := serviceBrokerRepo.RenameArgsForCall(0)
    82  			Expect(guid).To(Equal("my-found-broker-guid"))
    83  			Expect(name).To(Equal("my-new-broker"))
    84  		})
    85  	})
    86  })