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

     1  package space_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  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  )
    17  
    18  var _ = Describe("rename-space command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		configRepo          core_config.Repository
    22  		requirementsFactory *testreq.FakeReqFactory
    23  		spaceRepo           *testapi.FakeSpaceRepository
    24  		deps                command_registry.Dependency
    25  	)
    26  
    27  	updateCommandDependency := func(pluginCall bool) {
    28  		deps.Ui = ui
    29  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    30  		deps.Config = configRepo
    31  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("rename-space").SetDependency(deps, pluginCall))
    32  	}
    33  
    34  	BeforeEach(func() {
    35  		ui = new(testterm.FakeUI)
    36  		configRepo = testconfig.NewRepositoryWithDefaults()
    37  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedOrgSuccess: true}
    38  		spaceRepo = &testapi.FakeSpaceRepository{}
    39  	})
    40  
    41  	var callRenameSpace = func(args []string) bool {
    42  		return testcmd.RunCliCommand("rename-space", args, requirementsFactory, updateCommandDependency, false)
    43  	}
    44  
    45  	Describe("when the user is not logged in", func() {
    46  		It("does not pass requirements", func() {
    47  			requirementsFactory.LoginSuccess = false
    48  
    49  			Expect(callRenameSpace([]string{"my-space", "my-new-space"})).To(BeFalse())
    50  		})
    51  	})
    52  
    53  	Describe("when the user has not targeted an org", func() {
    54  		It("does not pass requirements", func() {
    55  			requirementsFactory.TargetedOrgSuccess = false
    56  
    57  			Expect(callRenameSpace([]string{"my-space", "my-new-space"})).To(BeFalse())
    58  		})
    59  	})
    60  
    61  	Describe("when the user provides fewer than two args", func() {
    62  		It("fails with usage", func() {
    63  			callRenameSpace([]string{"foo"})
    64  			Expect(ui.Outputs).To(ContainSubstrings(
    65  				[]string{"Incorrect Usage", "Requires", "arguments"},
    66  			))
    67  		})
    68  	})
    69  
    70  	Describe("when the user is logged in and has provided an old and new space name", func() {
    71  		BeforeEach(func() {
    72  			space := models.Space{}
    73  			space.Name = "the-old-space-name"
    74  			space.Guid = "the-old-space-guid"
    75  			requirementsFactory.Space = space
    76  		})
    77  
    78  		It("renames a space", func() {
    79  			originalSpaceName := configRepo.SpaceFields().Name
    80  			callRenameSpace([]string{"the-old-space-name", "my-new-space"})
    81  
    82  			Expect(ui.Outputs).To(ContainSubstrings(
    83  				[]string{"Renaming space", "the-old-space-name", "my-new-space", "my-org", "my-user"},
    84  				[]string{"OK"},
    85  			))
    86  
    87  			Expect(spaceRepo.RenameSpaceGuid).To(Equal("the-old-space-guid"))
    88  			Expect(spaceRepo.RenameNewName).To(Equal("my-new-space"))
    89  			Expect(configRepo.SpaceFields().Name).To(Equal(originalSpaceName))
    90  		})
    91  
    92  		Describe("renaming the space the user has targeted", func() {
    93  			BeforeEach(func() {
    94  				configRepo.SetSpaceFields(requirementsFactory.Space.SpaceFields)
    95  			})
    96  
    97  			It("renames the targeted space", func() {
    98  				callRenameSpace([]string{"the-old-space-name", "my-new-space-name"})
    99  				Expect(configRepo.SpaceFields().Name).To(Equal("my-new-space-name"))
   100  			})
   101  		})
   102  	})
   103  })