github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/cf/commands/space/rename_space_test.go (about)

     1  package space_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    13  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    14  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  
    18  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    19  )
    20  
    21  var _ = Describe("rename-space command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		configRepo          coreconfig.Repository
    25  		requirementsFactory *requirementsfakes.FakeFactory
    26  		spaceRepo           *spacesfakes.FakeSpaceRepository
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.UI = ui
    32  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    33  		deps.Config = configRepo
    34  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("rename-space").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = new(testterm.FakeUI)
    39  		configRepo = testconfig.NewRepositoryWithDefaults()
    40  		requirementsFactory = new(requirementsfakes.FakeFactory)
    41  		spaceRepo = new(spacesfakes.FakeSpaceRepository)
    42  	})
    43  
    44  	var callRenameSpace = func(args []string) bool {
    45  		return testcmd.RunCLICommand("rename-space", args, requirementsFactory, updateCommandDependency, false, ui)
    46  	}
    47  
    48  	Describe("when the user is not logged in", func() {
    49  		It("does not pass requirements", func() {
    50  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    51  
    52  			Expect(callRenameSpace([]string{"my-space", "my-new-space"})).To(BeFalse())
    53  		})
    54  	})
    55  
    56  	Describe("when the user has not targeted an org", func() {
    57  		It("does not pass requirements", func() {
    58  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    59  			targetedOrgReq := new(requirementsfakes.FakeTargetedOrgRequirement)
    60  			targetedOrgReq.ExecuteReturns(errors.New("no org targeted"))
    61  			requirementsFactory.NewTargetedOrgRequirementReturns(targetedOrgReq)
    62  
    63  			Expect(callRenameSpace([]string{"my-space", "my-new-space"})).To(BeFalse())
    64  		})
    65  	})
    66  
    67  	Describe("when the user provides fewer than two args", func() {
    68  		It("fails with usage", func() {
    69  			callRenameSpace([]string{"foo"})
    70  			Expect(ui.Outputs()).To(ContainSubstrings(
    71  				[]string{"Incorrect Usage", "Requires", "arguments"},
    72  			))
    73  		})
    74  	})
    75  
    76  	Describe("when the user is logged in and has provided an old and new space name", func() {
    77  		var space models.Space
    78  		BeforeEach(func() {
    79  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    80  			requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement))
    81  			space = models.Space{}
    82  			space.Name = "the-old-space-name"
    83  			space.GUID = "the-old-space-guid"
    84  			spaceReq := new(requirementsfakes.FakeSpaceRequirement)
    85  			spaceReq.GetSpaceReturns(space)
    86  			requirementsFactory.NewSpaceRequirementReturns(spaceReq)
    87  		})
    88  
    89  		It("renames a space", func() {
    90  			originalSpaceName := configRepo.SpaceFields().Name
    91  			callRenameSpace([]string{"the-old-space-name", "my-new-space"})
    92  
    93  			Expect(ui.Outputs()).To(ContainSubstrings(
    94  				[]string{"Renaming space", "the-old-space-name", "my-new-space", "my-org", "my-user"},
    95  				[]string{"OK"},
    96  			))
    97  
    98  			spaceGUID, name := spaceRepo.RenameArgsForCall(0)
    99  			Expect(spaceGUID).To(Equal("the-old-space-guid"))
   100  			Expect(name).To(Equal("my-new-space"))
   101  			Expect(configRepo.SpaceFields().Name).To(Equal(originalSpaceName))
   102  		})
   103  
   104  		Describe("renaming the space the user has targeted", func() {
   105  			BeforeEach(func() {
   106  				configRepo.SetSpaceFields(space.SpaceFields)
   107  			})
   108  
   109  			It("renames the targeted space", func() {
   110  				callRenameSpace([]string{"the-old-space-name", "my-new-space-name"})
   111  				Expect(configRepo.SpaceFields().Name).To(Equal("my-new-space-name"))
   112  			})
   113  		})
   114  	})
   115  })