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

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/command/commandfakes"
     8  	. "code.cloudfoundry.org/cli/command/v7"
     9  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    10  	"code.cloudfoundry.org/cli/resources"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("rename Command", func() {
    19  	var (
    20  		input           *Buffer
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeActor
    25  		cmd             RenameCommand
    26  		executeErr      error
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		input = NewBuffer()
    31  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    32  		fakeConfig = new(commandfakes.FakeConfig)
    33  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    34  		fakeActor = new(v7fakes.FakeActor)
    35  
    36  		cmd = RenameCommand{
    37  			BaseCommand: BaseCommand{
    38  				UI:          testUI,
    39  				Config:      fakeConfig,
    40  				SharedActor: fakeSharedActor,
    41  				Actor:       fakeActor,
    42  			},
    43  		}
    44  
    45  		cmd.RequiredArgs.OldAppName = "old-app-name"
    46  		cmd.RequiredArgs.NewAppName = "new-app-name"
    47  
    48  		fakeConfig.CurrentUserReturns(configv3.User{Name: "username"}, nil)
    49  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "targeted-org"})
    50  		fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "targeted-space"})
    51  		fakeActor.RenameApplicationByNameAndSpaceGUIDReturns(resources.Application{Name: "new-app-name"}, v7action.Warnings{"rename-app-warning"}, nil)
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("checking target succeeds", func() {
    59  
    60  		It("renames the app", func() {
    61  			Expect(testUI.Out).To(Say("Renaming app old-app-name to new-app-name in org targeted-org / space targeted-space as username..."))
    62  			Expect(testUI.Err).To(Say("rename-app-warning"))
    63  			Expect(testUI.Out).To(Say("OK"))
    64  		})
    65  	})
    66  
    67  	When("app and space are not targeted", func() {
    68  		var (
    69  			returnedError error
    70  		)
    71  		BeforeEach(func() {
    72  			returnedError = errors.New("no org found")
    73  			fakeSharedActor.CheckTargetReturns(returnedError)
    74  		})
    75  		It("fails", func() {
    76  			targetOrg, targetSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    77  			Expect(targetOrg).To(BeTrue())
    78  			Expect(targetSpace).To(BeTrue())
    79  			Expect(executeErr).To(MatchError(returnedError))
    80  		})
    81  	})
    82  
    83  	When("there is no CurrentUser", func() {
    84  		var (
    85  			returnedError error
    86  		)
    87  		BeforeEach(func() {
    88  			returnedError = errors.New("current user not found")
    89  			fakeConfig.CurrentUserReturns(configv3.User{}, returnedError)
    90  		})
    91  		It("returns the CurrentUser error", func() {
    92  			Expect(executeErr).To(MatchError(returnedError))
    93  		})
    94  	})
    95  
    96  	When("the actor returns an error", func() {
    97  		var (
    98  			returnedError error
    99  		)
   100  		BeforeEach(func() {
   101  			returnedError = errors.New("app rename failed!")
   102  			fakeActor.RenameApplicationByNameAndSpaceGUIDReturns(resources.Application{Name: "new-app-name"}, v7action.Warnings{"rename-app-warning"}, returnedError)
   103  		})
   104  
   105  		It("returns the error", func() {
   106  			Expect(testUI.Out).To(Say("Renaming app old-app-name to new-app-name in org targeted-org / space targeted-space as username..."))
   107  			Expect(testUI.Err).To(Say("rename-app-warning"))
   108  			Expect(executeErr).To(MatchError(returnedError))
   109  		})
   110  	})
   111  })