github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/application/rename_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/applications/applicationsfakes"
     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  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("Rename command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		requirementsFactory *requirementsfakes.FakeFactory
    23  		configRepo          coreconfig.Repository
    24  		appRepo             *applicationsfakes.FakeRepository
    25  		deps                commandregistry.Dependency
    26  	)
    27  
    28  	updateCommandDependency := func(pluginCall bool) {
    29  		deps.UI = ui
    30  		deps.Config = configRepo
    31  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    32  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("rename").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		ui = &testterm.FakeUI{}
    37  		configRepo = testconfig.NewRepositoryWithDefaults()
    38  		requirementsFactory = new(requirementsfakes.FakeFactory)
    39  		appRepo = new(applicationsfakes.FakeRepository)
    40  	})
    41  
    42  	runCommand := func(args ...string) bool {
    43  		return testcmd.RunCLICommand("rename", args, requirementsFactory, updateCommandDependency, false, ui)
    44  	}
    45  
    46  	Describe("requirements", func() {
    47  		It("fails with usage when not invoked with an old name and a new name", func() {
    48  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    49  			runCommand("foo")
    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("my-app", "my-new-app")).To(BeFalse())
    58  		})
    59  
    60  		It("fails if a space is not targeted", func() {
    61  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    62  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"})
    63  			Expect(runCommand("my-app", "my-new-app")).To(BeFalse())
    64  		})
    65  	})
    66  
    67  	It("renames an application", func() {
    68  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    69  		requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    70  
    71  		app := models.Application{}
    72  		app.Name = "my-app"
    73  		app.GUID = "my-app-guid"
    74  		applicationReq := new(requirementsfakes.FakeApplicationRequirement)
    75  		applicationReq.GetApplicationReturns(app)
    76  		requirementsFactory.NewApplicationRequirementReturns(applicationReq)
    77  
    78  		runCommand("my-app", "my-new-app")
    79  
    80  		appGUID, params := appRepo.UpdateArgsForCall(0)
    81  		Expect(appGUID).To(Equal(app.GUID))
    82  		Expect(*params.Name).To(Equal("my-new-app"))
    83  		Expect(ui.Outputs()).To(ContainSubstrings(
    84  			[]string{"Renaming app", "my-app", "my-new-app", "my-org", "my-space", "my-user"},
    85  			[]string{"OK"},
    86  		))
    87  	})
    88  })