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