github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/service/rename_service_test.go (about)

     1  package service_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/cf/util/testhelpers/commands"
    11  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    12  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("rename-service command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		config              coreconfig.Repository
    23  		serviceRepo         *apifakes.FakeServiceRepository
    24  		requirementsFactory *requirementsfakes.FakeFactory
    25  		deps                commandregistry.Dependency
    26  
    27  		serviceInstance models.ServiceInstance
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.UI = ui
    32  		deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo)
    33  		deps.Config = config
    34  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("rename-service").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{}
    39  		config = testconfig.NewRepositoryWithDefaults()
    40  		serviceRepo = new(apifakes.FakeServiceRepository)
    41  		requirementsFactory = new(requirementsfakes.FakeFactory)
    42  
    43  		serviceInstance = models.ServiceInstance{}
    44  		serviceInstance.Name = "different-name"
    45  		serviceInstance.GUID = "different-name-guid"
    46  		serviceReq := new(requirementsfakes.FakeServiceInstanceRequirement)
    47  		serviceReq.GetServiceInstanceReturns(serviceInstance)
    48  		requirementsFactory.NewServiceInstanceRequirementReturns(serviceReq)
    49  	})
    50  
    51  	runCommand := func(args ...string) bool {
    52  		return testcmd.RunCLICommand("rename-service", args, requirementsFactory, updateCommandDependency, false, ui)
    53  	}
    54  
    55  	Describe("requirements", func() {
    56  		It("Fails with usage when exactly two parameters not passed", func() {
    57  			runCommand("whatever")
    58  			Expect(ui.Outputs()).To(ContainSubstrings(
    59  				[]string{"Incorrect Usage", "Requires", "arguments"},
    60  			))
    61  		})
    62  
    63  		It("fails when not logged in", func() {
    64  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    65  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    66  			Expect(runCommand("banana", "fppants")).To(BeFalse())
    67  		})
    68  
    69  		It("fails when a space is not targeted", func() {
    70  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    71  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    72  
    73  			Expect(runCommand("banana", "faaaaasdf")).To(BeFalse())
    74  		})
    75  	})
    76  
    77  	Context("when logged in and a space is targeted", func() {
    78  		BeforeEach(func() {
    79  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    80  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    81  		})
    82  
    83  		It("renames the service, obviously", func() {
    84  			runCommand("my-service", "new-name")
    85  
    86  			Expect(ui.Outputs()).To(ContainSubstrings(
    87  				[]string{"Renaming service", "different-name", "new-name", "my-org", "my-space", "my-user"},
    88  				[]string{"OK"},
    89  			))
    90  
    91  			actualServiceInstance, actualServiceName := serviceRepo.RenameServiceArgsForCall(0)
    92  			Expect(actualServiceInstance).To(Equal(serviceInstance))
    93  			Expect(actualServiceName).To(Equal("new-name"))
    94  		})
    95  	})
    96  })