github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/rename_service_broker_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  	v7 "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-service-broker command", func() {
    20  	const (
    21  		binaryName    = "cf-command"
    22  		oldBrokerName = "old-name"
    23  		newBrokerName = "new-name"
    24  	)
    25  
    26  	var (
    27  		cmd                          *v7.RenameServiceBrokerCommand
    28  		fakeUpdateServiceBrokerActor *v7fakes.FakeActor
    29  		fakeSharedActor              *commandfakes.FakeSharedActor
    30  		fakeConfig                   *commandfakes.FakeConfig
    31  		testUI                       *ui.UI
    32  		executeErr                   error
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		fakeUpdateServiceBrokerActor = &v7fakes.FakeActor{}
    37  		fakeSharedActor = &commandfakes.FakeSharedActor{}
    38  		testUI = ui.NewTestUI(NewBuffer(), NewBuffer(), NewBuffer())
    39  		fakeConfig = &commandfakes.FakeConfig{}
    40  		cmd = &v7.RenameServiceBrokerCommand{
    41  			BaseCommand: v7.BaseCommand{
    42  				Actor:       fakeUpdateServiceBrokerActor,
    43  				SharedActor: fakeSharedActor,
    44  				UI:          testUI,
    45  				Config:      fakeConfig,
    46  			},
    47  		}
    48  
    49  		setPositionalFlags(cmd, oldBrokerName, newBrokerName)
    50  	})
    51  
    52  	When("logged in", func() {
    53  		const guid = "fake-service-broker-guid"
    54  
    55  		BeforeEach(func() {
    56  			fakeUpdateServiceBrokerActor.GetServiceBrokerByNameReturns(
    57  				resources.ServiceBroker{GUID: guid},
    58  				v7action.Warnings{"get broker by name warning"},
    59  				nil,
    60  			)
    61  
    62  			fakeConfig.CurrentUserReturns(configv3.User{Name: "user"}, nil)
    63  		})
    64  
    65  		When("rename succeeds", func() {
    66  			BeforeEach(func() {
    67  				fakeUpdateServiceBrokerActor.UpdateServiceBrokerReturns(v7action.Warnings{"update service broker warning"}, nil)
    68  				executeErr = cmd.Execute(nil)
    69  			})
    70  
    71  			It("does not return any errors", func() {
    72  				Expect(executeErr).NotTo(HaveOccurred())
    73  			})
    74  
    75  			It("calls actor correctly and passes the new name", func() {
    76  				Expect(fakeUpdateServiceBrokerActor.UpdateServiceBrokerCallCount()).To(Equal(1))
    77  				serviceBrokerGUID, model := fakeUpdateServiceBrokerActor.UpdateServiceBrokerArgsForCall(0)
    78  				Expect(serviceBrokerGUID).To(Equal(guid))
    79  				Expect(model).To(Equal(
    80  					resources.ServiceBroker{
    81  						Name: newBrokerName,
    82  					},
    83  				))
    84  			})
    85  
    86  			It("yields warnings", func() {
    87  				Expect(testUI.Err).To(Say("get broker by name warning"))
    88  				Expect(testUI.Err).To(Say("update service broker warning"))
    89  			})
    90  
    91  			It("obtains broker GUID correctly by name", func() {
    92  				Expect(fakeUpdateServiceBrokerActor.GetServiceBrokerByNameCallCount()).To(Equal(1))
    93  				Expect(fakeUpdateServiceBrokerActor.GetServiceBrokerByNameArgsForCall(0)).To(Equal(oldBrokerName))
    94  			})
    95  		})
    96  
    97  		When("the UpdateServiceBroker actor fails to get the broker name", func() {
    98  			BeforeEach(func() {
    99  				fakeUpdateServiceBrokerActor.GetServiceBrokerByNameReturns(
   100  					resources.ServiceBroker{},
   101  					v7action.Warnings{"some-warning"},
   102  					actionerror.ServiceBrokerNotFoundError{
   103  						Name: oldBrokerName,
   104  					},
   105  				)
   106  			})
   107  
   108  			It("returns the error and displays all warnings", func() {
   109  				err := cmd.Execute(nil)
   110  				Expect(err).To(MatchError(actionerror.ServiceBrokerNotFoundError{Name: oldBrokerName}))
   111  				Expect(testUI.Err).To(Say("some-warning"))
   112  			})
   113  		})
   114  
   115  		When("the UpdateServiceBroker actor fails to rename the broker", func() {
   116  			It("returns the error and displays any warnings", func() {
   117  				fakeUpdateServiceBrokerActor.UpdateServiceBrokerReturns(v7action.Warnings{"a-warning"}, errors.New("something went wrong"))
   118  
   119  				err := cmd.Execute(nil)
   120  
   121  				Expect(err).To(HaveOccurred())
   122  				Expect(err.Error()).To(Equal("something went wrong"))
   123  				Expect(testUI.Err).To(Say("a-warning"))
   124  			})
   125  		})
   126  
   127  		When("it fails to get the current user", func() {
   128  			BeforeEach(func() {
   129  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("no user found"))
   130  			})
   131  
   132  			It("returns the error and displays all warnings", func() {
   133  				err := cmd.Execute(nil)
   134  
   135  				Expect(err).To(HaveOccurred())
   136  				Expect(err).To(MatchError("no user found"))
   137  			})
   138  		})
   139  	})
   140  
   141  	When("not logged in", func() {
   142  		BeforeEach(func() {
   143  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{
   144  				BinaryName: binaryName,
   145  			})
   146  		})
   147  
   148  		It("returns an error", func() {
   149  			err := cmd.Execute(nil)
   150  
   151  			Expect(err).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
   152  
   153  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   154  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   155  			Expect(checkTargetedOrg).To(BeFalse())
   156  			Expect(checkTargetedSpace).To(BeFalse())
   157  		})
   158  	})
   159  })