github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/create_service_broker_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("create-service-broker Command", func() {
    21  	var (
    22  		cmd             CreateServiceBrokerCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeCreateServiceBrokerActor
    27  		binaryName      string
    28  		executeErr      error
    29  		extraArgs       []string
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v6fakes.FakeCreateServiceBrokerActor)
    37  
    38  		binaryName = "faceman"
    39  		fakeConfig.BinaryNameReturns("faceman")
    40  		extraArgs = nil
    41  		cmd = CreateServiceBrokerCommand{
    42  			UI:          testUI,
    43  			Config:      fakeConfig,
    44  			SharedActor: fakeSharedActor,
    45  			Actor:       fakeActor,
    46  			RequiredArgs: flag.ServiceBrokerArgs{
    47  				ServiceBroker: "cool-broker",
    48  				Username:      "admin",
    49  				Password:      "password",
    50  				URL:           "https://broker.com",
    51  			},
    52  			SpaceScoped: false,
    53  		}
    54  
    55  	})
    56  
    57  	JustBeforeEach(func() {
    58  		executeErr = cmd.Execute(extraArgs)
    59  	})
    60  
    61  	When("the user provides arguments", func() {
    62  		BeforeEach(func() {
    63  			extraArgs = []string{"some-extra-arg"}
    64  		})
    65  
    66  		It("fails with a TooManyArgumentsError", func() {
    67  			Expect(executeErr).To(MatchError(translatableerror.TooManyArgumentsError{
    68  				ExtraArgument: "some-extra-arg",
    69  			}))
    70  		})
    71  	})
    72  
    73  	When("the user does not have an org and space targeted", func() {
    74  		BeforeEach(func() {
    75  			fakeSharedActor.CheckTargetReturns(nil)
    76  			fakeConfig.CurrentUserReturns(
    77  				configv3.User{Name: "some-user"},
    78  				nil)
    79  		})
    80  
    81  		When("fetching the current user fails", func() {
    82  			BeforeEach(func() {
    83  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("no user"))
    84  			})
    85  
    86  			It("returns an error", func() {
    87  				Expect(executeErr).To(MatchError("no user"))
    88  			})
    89  		})
    90  
    91  		Context("not space scoped", func() {
    92  			When("creating a service broker is successful", func() {
    93  				BeforeEach(func() {
    94  					fakeActor.CreateServiceBrokerReturns(v2action.ServiceBroker{}, []string{"a-warning", "another-warning"}, nil)
    95  				})
    96  
    97  				It("displays a message indicating that it is creating the service broker", func() {
    98  					Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1))
    99  					serviceBroker, username, password, url, spaceGUID := fakeActor.CreateServiceBrokerArgsForCall(0)
   100  					Expect(serviceBroker).To(Equal("cool-broker"))
   101  					Expect(username).To(Equal("admin"))
   102  					Expect(password).To(Equal("password"))
   103  					Expect(url).To(Equal("https://broker.com"))
   104  					Expect(spaceGUID).To(Equal(""))
   105  
   106  					Expect(testUI.Out).To(Say("Creating service broker cool-broker as some-user..."))
   107  					Expect(testUI.Out).To(Say("OK"))
   108  					Expect(executeErr).NotTo(HaveOccurred())
   109  				})
   110  
   111  				It("displays all warnings", func() {
   112  					Expect(testUI.Err).To(Say("a-warning"))
   113  					Expect(testUI.Err).To(Say("another-warning"))
   114  				})
   115  			})
   116  
   117  			When("creating a service broker is unsuccessful", func() {
   118  				BeforeEach(func() {
   119  					fakeActor.CreateServiceBrokerReturns(v2action.ServiceBroker{}, []string{"a-warning", "another-warning"}, errors.New("invalid-broker-name"))
   120  				})
   121  
   122  				It("prints the error and warnings returned by Cloud Controller", func() {
   123  					Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1))
   124  
   125  					Expect(testUI.Out).To(Say("Creating service broker cool-broker as some-user..."))
   126  					Expect(testUI.Err).To(Say("a-warning"))
   127  					Expect(testUI.Err).To(Say("another-warning"))
   128  					Expect(executeErr).To(MatchError("invalid-broker-name"))
   129  				})
   130  			})
   131  		})
   132  
   133  		Context("space scoped", func() {
   134  			BeforeEach(func() {
   135  				fakeSharedActor.CheckTargetReturns(actionerror.NoSpaceTargetedError{BinaryName: binaryName})
   136  				cmd.SpaceScoped = true
   137  			})
   138  
   139  			It("returns an error", func() {
   140  				Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(0))
   141  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   142  				Expect(executeErr).To(MatchError(actionerror.NoSpaceTargetedError{BinaryName: binaryName}))
   143  			})
   144  		})
   145  	})
   146  
   147  	When("the user is targeting an org and space", func() {
   148  		BeforeEach(func() {
   149  			fakeSharedActor.CheckTargetReturns(nil)
   150  			fakeConfig.CurrentUserReturns(
   151  				configv3.User{Name: "some-user"},
   152  				nil)
   153  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
   154  				GUID: "some-org-guid",
   155  				Name: "some-org",
   156  			})
   157  			fakeConfig.TargetedSpaceReturns(configv3.Space{
   158  				GUID: "some-space-guid",
   159  				Name: "some-space",
   160  			})
   161  			fakeActor.CreateServiceBrokerReturns(v2action.ServiceBroker{}, []string{"a-warning", "another-warning"}, nil)
   162  		})
   163  
   164  		Context("space scoped", func() {
   165  			BeforeEach(func() {
   166  				cmd.SpaceScoped = true
   167  			})
   168  
   169  			When("all arguments are passed and are valid", func() {
   170  				It("registers a space scoped service broker", func() {
   171  					Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1))
   172  					serviceBroker, username, password, url, spaceGUID := fakeActor.CreateServiceBrokerArgsForCall(0)
   173  					Expect(serviceBroker).To(Equal("cool-broker"))
   174  					Expect(username).To(Equal("admin"))
   175  					Expect(password).To(Equal("password"))
   176  					Expect(url).To(Equal("https://broker.com"))
   177  					Expect(spaceGUID).To(Equal("some-space-guid"))
   178  
   179  					Expect(testUI.Out).To(Say("Creating service broker cool-broker in org some-org / space some-space as some-user..."))
   180  					Expect(testUI.Out).To(Say("OK"))
   181  					Expect(testUI.Err).To(Say("a-warning"))
   182  					Expect(testUI.Err).To(Say("another-warning"))
   183  					Expect(executeErr).NotTo(HaveOccurred())
   184  				})
   185  			})
   186  		})
   187  
   188  		Context("not space scoped", func() {
   189  			When("all arguments are passed and are valid", func() {
   190  				It("registers a service broker", func() {
   191  					Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1))
   192  					Expect(testUI.Out).To(Say("Creating service broker cool-broker as some-user..."))
   193  					Expect(testUI.Out).To(Say("OK"))
   194  					Expect(testUI.Err).To(Say("a-warning"))
   195  					Expect(testUI.Err).To(Say("another-warning"))
   196  					Expect(executeErr).NotTo(HaveOccurred())
   197  				})
   198  			})
   199  		})
   200  	})
   201  })