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