github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/rename_service_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
     7  
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/translatableerror"
     9  
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
    12  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
    13  	. "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
    14  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    15  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("rename-service command test", func() {
    22  	const (
    23  		currentName = "current-name"
    24  		newName     = "new-name"
    25  		spaceName   = "fake-space-name"
    26  		spaceGUID   = "fake-space-guid"
    27  		orgName     = "fake-org-name"
    28  		username    = "fake-username"
    29  	)
    30  
    31  	var (
    32  		cmd             RenameServiceCommand
    33  		testUI          *ui.UI
    34  		fakeConfig      *commandfakes.FakeConfig
    35  		fakeSharedActor *commandfakes.FakeSharedActor
    36  		fakeActor       *v7fakes.FakeActor
    37  		executeErr      error
    38  	)
    39  
    40  	JustBeforeEach(func() {
    41  		executeErr = cmd.Execute(nil)
    42  	})
    43  
    44  	BeforeEach(func() {
    45  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    46  		fakeConfig = new(commandfakes.FakeConfig)
    47  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    48  		fakeActor = new(v7fakes.FakeActor)
    49  
    50  		cmd = RenameServiceCommand{
    51  			BaseCommand: BaseCommand{
    52  				UI:          testUI,
    53  				Config:      fakeConfig,
    54  				SharedActor: fakeSharedActor,
    55  				Actor:       fakeActor,
    56  			},
    57  		}
    58  
    59  		setPositionalFlags(&cmd, currentName, newName)
    60  
    61  		fakeConfig.TargetedSpaceReturns(configv3.Space{GUID: spaceGUID, Name: spaceName})
    62  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: orgName})
    63  		fakeActor.GetCurrentUserReturns(configv3.User{Name: username}, nil)
    64  
    65  		fakeActor.RenameServiceInstanceReturns(v7action.Warnings{"rename instance warning"}, nil)
    66  	})
    67  
    68  	It("checks the user is logged in, and targeting an org and space", func() {
    69  		Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    70  		orgChecked, spaceChecked := fakeSharedActor.CheckTargetArgsForCall(0)
    71  		Expect(orgChecked).To(BeTrue())
    72  		Expect(spaceChecked).To(BeTrue())
    73  	})
    74  
    75  	It("prints messages", func() {
    76  		Expect(testUI.Out).To(SatisfyAll(
    77  			Say(`Renaming service %s to %s in org %s / space %s as %s...\n`, currentName, newName, orgName, spaceName, username),
    78  			Say(`OK`),
    79  		))
    80  	})
    81  
    82  	It("delegates the rename to the actor", func() {
    83  		Expect(fakeActor.RenameServiceInstanceCallCount()).To(Equal(1))
    84  		actualCurrentName, actualSpaceGUID, actualNewName := fakeActor.RenameServiceInstanceArgsForCall(0)
    85  		Expect(actualCurrentName).To(Equal(currentName))
    86  		Expect(actualSpaceGUID).To(Equal(spaceGUID))
    87  		Expect(actualNewName).To(Equal(newName))
    88  	})
    89  
    90  	It("prints warnings", func() {
    91  		Expect(testUI.Err).To(Say("rename instance warning"))
    92  	})
    93  
    94  	It("does not return an error", func() {
    95  		Expect(executeErr).NotTo(HaveOccurred())
    96  	})
    97  
    98  	When("checking the target returns an error", func() {
    99  		BeforeEach(func() {
   100  			fakeSharedActor.CheckTargetReturns(errors.New("explode"))
   101  		})
   102  
   103  		It("returns the error", func() {
   104  			Expect(executeErr).To(MatchError("explode"))
   105  		})
   106  	})
   107  
   108  	When("getting the user returns an error", func() {
   109  		BeforeEach(func() {
   110  			fakeActor.GetCurrentUserReturns(configv3.User{}, errors.New("bang"))
   111  		})
   112  
   113  		It("returns the error", func() {
   114  			Expect(executeErr).To(MatchError("bang"))
   115  		})
   116  	})
   117  
   118  	When("the service instance cannot be found", func() {
   119  		BeforeEach(func() {
   120  			fakeActor.RenameServiceInstanceReturns(
   121  				v7action.Warnings{"rename instance warning"},
   122  				actionerror.ServiceInstanceNotFoundError{Name: currentName},
   123  			)
   124  		})
   125  
   126  		It("prints a tip, warnings and returns an error", func() {
   127  			Expect(testUI.Err).To(Say("rename instance warning"))
   128  			Expect(testUI.Out).To(Say(`TIP: Use 'cf services' to view all services in this org and space\.`))
   129  			Expect(executeErr).To(MatchError(translatableerror.ServiceInstanceNotFoundError{Name: currentName}))
   130  		})
   131  	})
   132  
   133  	When("renaming the service instance fails", func() {
   134  		BeforeEach(func() {
   135  			fakeActor.RenameServiceInstanceReturns(
   136  				v7action.Warnings{"rename instance warning"},
   137  				errors.New("unknown bad thing"),
   138  			)
   139  		})
   140  
   141  		It("prints warnings and returns an error", func() {
   142  			Expect(testUI.Err).To(Say("rename instance warning"))
   143  			Expect(testUI.Out).NotTo(Say("TIP"))
   144  			Expect(executeErr).To(MatchError("unknown bad thing"))
   145  		})
   146  	})
   147  })