code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/create_service_broker_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/command/commandfakes"
     8  	v7 "code.cloudfoundry.org/cli/command/v7"
     9  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gbytes"
    15  )
    16  
    17  var _ = Describe("create-service-broker Command", func() {
    18  	var (
    19  		cmd             *v7.CreateServiceBrokerCommand
    20  		testUI          *ui.UI
    21  		fakeConfig      *commandfakes.FakeConfig
    22  		fakeSharedActor *commandfakes.FakeSharedActor
    23  		fakeActor       *v7fakes.FakeActor
    24  		input           *Buffer
    25  		binaryName      string
    26  		executeErr      error
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		input = NewBuffer()
    31  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    32  		fakeConfig = new(commandfakes.FakeConfig)
    33  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    34  		fakeActor = new(v7fakes.FakeActor)
    35  		fakeActor.CreateServiceBrokerReturns(v7action.Warnings{"some default warning"}, nil)
    36  
    37  		binaryName = "faceman"
    38  		fakeConfig.BinaryNameReturns(binaryName)
    39  
    40  		cmd = &v7.CreateServiceBrokerCommand{
    41  			BaseCommand: v7.BaseCommand{
    42  				UI:          testUI,
    43  				Config:      fakeConfig,
    44  				SharedActor: fakeSharedActor,
    45  				Actor:       fakeActor,
    46  			},
    47  		}
    48  
    49  		setPositionalFlags(cmd, "service-broker-name", "username", "password", "https://example.org/super-broker")
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("checking target fails", func() {
    57  		BeforeEach(func() {
    58  			fakeSharedActor.CheckTargetReturns(errors.New("an error occurred"))
    59  		})
    60  
    61  		It("returns an error", func() {
    62  			Expect(executeErr).To(MatchError("an error occurred"))
    63  		})
    64  	})
    65  
    66  	When("fetching the current user fails", func() {
    67  		BeforeEach(func() {
    68  			fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("an error occurred"))
    69  		})
    70  
    71  		It("return an error", func() {
    72  			Expect(executeErr).To(MatchError("an error occurred"))
    73  		})
    74  	})
    75  
    76  	When("fetching the current user succeeds", func() {
    77  		BeforeEach(func() {
    78  			fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    79  		})
    80  
    81  		It("checks that there is a valid target", func() {
    82  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    83  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    84  			Expect(checkTargetedOrg).To(BeFalse())
    85  			Expect(checkTargetedSpace).To(BeFalse())
    86  		})
    87  
    88  		It("displays a message with the username", func() {
    89  			Expect(testUI.Out).To(Say(`Creating service broker %s as %s\.\.\.`, "service-broker-name", "steve"))
    90  		})
    91  
    92  		It("passes the data to the actor layer", func() {
    93  			Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1))
    94  
    95  			model := fakeActor.CreateServiceBrokerArgsForCall(0)
    96  
    97  			Expect(model.Name).To(Equal("service-broker-name"))
    98  			Expect(model.Username).To(Equal("username"))
    99  			Expect(model.Password).To(Equal("password"))
   100  			Expect(model.URL).To(Equal("https://example.org/super-broker"))
   101  			Expect(model.SpaceGUID).To(Equal(""))
   102  		})
   103  
   104  		It("displays the warnings", func() {
   105  			Expect(testUI.Err).To(Say("some default warning"))
   106  		})
   107  
   108  		It("displays OK", func() {
   109  			Expect(testUI.Out).To(Say("OK"))
   110  		})
   111  
   112  		When("the actor returns an error", func() {
   113  			BeforeEach(func() {
   114  				fakeActor.CreateServiceBrokerReturns(v7action.Warnings{"service-broker-warnings"}, errors.New("fake create-service-broker error"))
   115  			})
   116  
   117  			It("prints the error and warnings", func() {
   118  				Expect(testUI.Out).NotTo(Say("OK"))
   119  				Expect(executeErr).To(MatchError("fake create-service-broker error"))
   120  				Expect(testUI.Err).To(Say("service-broker-warnings"))
   121  			})
   122  		})
   123  
   124  		When("creating a space scoped broker", func() {
   125  			BeforeEach(func() {
   126  				cmd.SpaceScoped = true
   127  				fakeConfig.TargetedSpaceReturns(configv3.Space{
   128  					Name: "fake-space-name",
   129  					GUID: "fake-space-guid",
   130  				})
   131  				fakeConfig.TargetedOrganizationNameReturns("fake-org-name")
   132  			})
   133  
   134  			It("checks that a space is targeted", func() {
   135  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   136  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   137  				Expect(checkTargetedOrg).To(BeTrue())
   138  				Expect(checkTargetedSpace).To(BeTrue())
   139  			})
   140  
   141  			It("displays the space name in the message", func() {
   142  				Expect(testUI.Out).To(Say(`Creating service broker %s in org %s / space %s as %s\.\.\.`, "service-broker-name", "fake-org-name", "fake-space-name", "steve"))
   143  			})
   144  
   145  			It("looks up the space guid and passes it to the actor", func() {
   146  				Expect(fakeActor.CreateServiceBrokerCallCount()).To(Equal(1))
   147  
   148  				model := fakeActor.CreateServiceBrokerArgsForCall(0)
   149  				Expect(model.SpaceGUID).To(Equal("fake-space-guid"))
   150  			})
   151  		})
   152  	})
   153  })