github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/service_brokers_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("service-brokers Command", func() {
    20  	var (
    21  		cmd             *v7.ServiceBrokersCommand
    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  		binaryName = "faceman"
    39  		fakeConfig.BinaryNameReturns(binaryName)
    40  
    41  		cmd = &v7.ServiceBrokersCommand{
    42  			BaseCommand: v7.BaseCommand{
    43  				UI:          testUI,
    44  				Config:      fakeConfig,
    45  				SharedActor: fakeSharedActor,
    46  				Actor:       fakeActor,
    47  			},
    48  		}
    49  	})
    50  
    51  	JustBeforeEach(func() {
    52  		executeErr = cmd.Execute(nil)
    53  	})
    54  
    55  	When("checking target fails", func() {
    56  		BeforeEach(func() {
    57  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    58  		})
    59  
    60  		It("returns an error", func() {
    61  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    62  
    63  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    64  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    65  			Expect(checkTargetedOrg).To(BeFalse())
    66  			Expect(checkTargetedSpace).To(BeFalse())
    67  		})
    68  	})
    69  
    70  	When("the user is not logged in", func() {
    71  		var expectedErr error
    72  
    73  		BeforeEach(func() {
    74  			expectedErr = errors.New("some current user error")
    75  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    76  		})
    77  
    78  		It("return an error", func() {
    79  			Expect(executeErr).To(Equal(expectedErr))
    80  		})
    81  	})
    82  
    83  	When("the user is logged in and a space is targetted", func() {
    84  		BeforeEach(func() {
    85  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    86  				Name: "some-org",
    87  				GUID: "some-org-guid",
    88  			})
    89  
    90  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    91  				Name: "some-space",
    92  				GUID: "some-space-guid",
    93  			})
    94  
    95  			fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    96  		})
    97  
    98  		It("displays a message with the username", func() {
    99  			Expect(testUI.Out).To(Say("Getting service brokers as %s...", "steve"))
   100  		})
   101  
   102  		It("calls the GetServiceBrokersActor", func() {
   103  			Expect(fakeActor.GetServiceBrokersCallCount()).To(Equal(1))
   104  		})
   105  
   106  		When("there are no service brokers", func() {
   107  			BeforeEach(func() {
   108  				fakeActor.GetServiceBrokersReturns([]resources.ServiceBroker{}, v7action.Warnings{"service-broker-warnings"}, nil)
   109  			})
   110  
   111  			It("says there are no service brokers", func() {
   112  				Expect(testUI.Out).To(Say("No service brokers found"))
   113  				Expect(testUI.Err).To(Say("service-broker-warnings"))
   114  				Expect(testUI.Out).NotTo(Say("name\\s+url"), "printing table header when table is empty")
   115  				Expect(executeErr).NotTo(HaveOccurred())
   116  			})
   117  		})
   118  
   119  		When("there is one service broker", func() {
   120  			BeforeEach(func() {
   121  				serviceBrokers := []resources.ServiceBroker{
   122  					{Name: "foo", URL: "http://foo.url", GUID: "guid-foo"},
   123  				}
   124  				fakeActor.GetServiceBrokersReturns(serviceBrokers, v7action.Warnings{"service-broker-warnings"}, nil)
   125  			})
   126  
   127  			It("prints a table header and the broker details", func() {
   128  				Expect(testUI.Out).To(Say("name\\s+url"))
   129  				Expect(testUI.Out).To(Say("foo\\s+http://foo.url"))
   130  				Expect(testUI.Err).To(Say("service-broker-warnings"))
   131  				Expect(executeErr).NotTo(HaveOccurred())
   132  			})
   133  		})
   134  
   135  		When("there are many service brokers", func() {
   136  			BeforeEach(func() {
   137  				serviceBrokers := []resources.ServiceBroker{
   138  					{Name: "foo", URL: "http://foo.url", GUID: "guid-foo"},
   139  					{Name: "bar", URL: "https://bar.com", GUID: "guid-bar"},
   140  				}
   141  				fakeActor.GetServiceBrokersReturns(serviceBrokers, v7action.Warnings{"service-broker-warnings"}, nil)
   142  			})
   143  
   144  			It("prints a table header and the broker details", func() {
   145  				Expect(testUI.Out).To(Say("name\\s+url"))
   146  				Expect(testUI.Out).To(Say("foo\\s+http://foo.url"))
   147  				Expect(testUI.Out).To(Say("bar\\s+https://bar.com"))
   148  				Expect(testUI.Err).To(Say("service-broker-warnings"))
   149  				Expect(executeErr).NotTo(HaveOccurred())
   150  			})
   151  		})
   152  
   153  		When("calling the GetServiceBrokersActor returns an error", func() {
   154  			BeforeEach(func() {
   155  				fakeActor.GetServiceBrokersReturns([]resources.ServiceBroker{}, v7action.Warnings{"service-broker-warnings"}, errors.New("fake service-brokers error"))
   156  			})
   157  
   158  			It("prints the error and warnings", func() {
   159  				Expect(executeErr).To(MatchError("fake service-brokers error"))
   160  				Expect(testUI.Err).To(Say("service-broker-warnings"))
   161  			})
   162  		})
   163  	})
   164  })