github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/rename_space_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("rename-space Command", func() {
    20  	var (
    21  		cmd             RenameSpaceCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		input           *Buffer
    27  		binaryName      string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		input = NewBuffer()
    33  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v7fakes.FakeActor)
    37  
    38  		cmd = RenameSpaceCommand{
    39  			BaseCommand: BaseCommand{
    40  				UI:          testUI,
    41  				Config:      fakeConfig,
    42  				SharedActor: fakeSharedActor,
    43  				Actor:       fakeActor,
    44  			},
    45  		}
    46  
    47  		cmd.RequiredArgs.OldSpaceName = "old-space-name"
    48  		cmd.RequiredArgs.NewSpaceName = "new-space-name"
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    53  	})
    54  
    55  	JustBeforeEach(func() {
    56  		executeErr = cmd.Execute(nil)
    57  	})
    58  
    59  	When("checking target errors", func() {
    60  		BeforeEach(func() {
    61  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    62  		})
    63  
    64  		It("returns the NotLoggedInError", func() {
    65  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    66  
    67  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    68  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    69  			Expect(checkTargetedOrg).To(BeTrue())
    70  			Expect(checkTargetedSpace).To(BeFalse())
    71  		})
    72  
    73  	})
    74  
    75  	When("checking target succeeds", func() {
    76  		When("getting the current user returns an error", func() {
    77  			var returnedErr error
    78  
    79  			BeforeEach(func() {
    80  				returnedErr = errors.New("some error")
    81  				fakeConfig.CurrentUserReturns(configv3.User{}, returnedErr)
    82  			})
    83  
    84  			It("returns the error", func() {
    85  				Expect(executeErr).To(MatchError(returnedErr))
    86  			})
    87  		})
    88  
    89  		When("when the command succeeds", func() {
    90  			BeforeEach(func() {
    91  				fakeConfig.CurrentUserReturns(configv3.User{Name: "username"}, nil)
    92  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-targeted-org", GUID: "org-guid"})
    93  				fakeActor.RenameSpaceByNameAndOrganizationGUIDReturns(
    94  					resources.Space{GUID: "old-space-guid", Name: "new-space-name"},
    95  					v7action.Warnings{"warning-1", "warning-2"},
    96  					nil,
    97  				)
    98  			})
    99  
   100  			It("renames the space in the targeted org", func() {
   101  				Expect(executeErr).NotTo(HaveOccurred())
   102  
   103  				Expect(testUI.Err).To(Say("warning-1"))
   104  				Expect(testUI.Err).To(Say("warning-2"))
   105  
   106  				Expect(fakeActor.RenameSpaceByNameAndOrganizationGUIDCallCount()).To(Equal(1))
   107  				oldSpaceName, newSpaceName, orgArg := fakeActor.RenameSpaceByNameAndOrganizationGUIDArgsForCall(0)
   108  				Expect(oldSpaceName).To(Equal("old-space-name"))
   109  				Expect(newSpaceName).To(Equal("new-space-name"))
   110  				Expect(orgArg).To(Equal("org-guid"))
   111  			})
   112  
   113  			When("renaming a targeted space", func() {
   114  				BeforeEach(func() {
   115  					fakeConfig.TargetedSpaceReturns(configv3.Space{GUID: "old-space-guid", Name: "old-space-name"})
   116  				})
   117  
   118  				It("targets the renamed space", func() {
   119  					Expect(executeErr).ToNot(HaveOccurred())
   120  
   121  					Expect(testUI.Err).To(Say("warning-1"))
   122  					Expect(testUI.Err).To(Say("warning-2"))
   123  
   124  					Expect(fakeConfig.V7SetSpaceInformationCallCount()).To(Equal(1))
   125  					newSpaceGUID, newSpaceName := fakeConfig.V7SetSpaceInformationArgsForCall(0)
   126  					Expect(newSpaceGUID).To(Equal("old-space-guid"))
   127  					Expect(newSpaceName).To(Equal("new-space-name"))
   128  				})
   129  			})
   130  		})
   131  	})
   132  })