github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/bind_service_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/actionerror"
     7  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     8  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     9  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror"
    10  	"github.com/liamawhite/cli-with-i18n/command/commandfakes"
    11  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    12  	. "github.com/liamawhite/cli-with-i18n/command/v2"
    13  	"github.com/liamawhite/cli-with-i18n/command/v2/v2fakes"
    14  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    15  	"github.com/liamawhite/cli-with-i18n/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("bind-service Command", func() {
    22  	var (
    23  		cmd             BindServiceCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v2fakes.FakeBindServiceActor
    28  		binaryName      string
    29  		executeErr      error
    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(v2fakes.FakeBindServiceActor)
    37  
    38  		cmd = BindServiceCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		cmd.RequiredArgs.AppName = "some-app"
    46  		cmd.RequiredArgs.ServiceInstanceName = "some-service"
    47  		cmd.ParametersAsJSON = map[string]interface{}{
    48  			"some-parameter": "some-value",
    49  		}
    50  
    51  		binaryName = "faceman"
    52  		fakeConfig.BinaryNameReturns("faceman")
    53  		fakeConfig.ExperimentalReturns(true)
    54  	})
    55  
    56  	JustBeforeEach(func() {
    57  		executeErr = cmd.Execute(nil)
    58  	})
    59  
    60  	Context("when a cloud controller API endpoint is set", func() {
    61  		BeforeEach(func() {
    62  			fakeConfig.TargetReturns("some-url")
    63  		})
    64  
    65  		Context("when checking target fails", func() {
    66  			BeforeEach(func() {
    67  				fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName})
    68  			})
    69  
    70  			It("returns an error", func() {
    71  				Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName}))
    72  
    73  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    74  				_, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    75  				Expect(checkTargetedOrg).To(BeTrue())
    76  				Expect(checkTargetedSpace).To(BeTrue())
    77  			})
    78  		})
    79  
    80  		Context("when the user is logged in, and an org and space are targeted", func() {
    81  			BeforeEach(func() {
    82  				fakeConfig.HasTargetedOrganizationReturns(true)
    83  				fakeConfig.HasTargetedSpaceReturns(true)
    84  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    85  					GUID: "some-org-guid",
    86  					Name: "some-org",
    87  				})
    88  				fakeConfig.TargetedSpaceReturns(configv3.Space{
    89  					GUID: "some-space-guid",
    90  					Name: "some-space",
    91  				})
    92  			})
    93  
    94  			Context("when getting the current user returns an error", func() {
    95  				var expectedErr error
    96  
    97  				BeforeEach(func() {
    98  					expectedErr = errors.New("got bananapants??")
    99  					fakeConfig.CurrentUserReturns(
   100  						configv3.User{},
   101  						expectedErr)
   102  				})
   103  
   104  				It("returns the error", func() {
   105  					Expect(executeErr).To(MatchError(expectedErr))
   106  				})
   107  			})
   108  
   109  			Context("when getting the current user does not return an error", func() {
   110  				BeforeEach(func() {
   111  					fakeConfig.CurrentUserReturns(
   112  						configv3.User{Name: "some-user"},
   113  						nil)
   114  				})
   115  
   116  				It("displays flavor text", func() {
   117  					Expect(testUI.Out).To(Say("Binding service some-service to app some-app in org some-org / space some-space as some-user..."))
   118  
   119  					Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   120  				})
   121  
   122  				Context("when the service was already bound", func() {
   123  					BeforeEach(func() {
   124  						fakeActor.BindServiceBySpaceReturns(
   125  							[]string{"foo", "bar"},
   126  							ccerror.ServiceBindingTakenError{})
   127  					})
   128  
   129  					It("displays warnings and 'OK'", func() {
   130  						Expect(executeErr).NotTo(HaveOccurred())
   131  
   132  						Expect(testUI.Err).To(Say("foo"))
   133  						Expect(testUI.Err).To(Say("bar"))
   134  						Expect(testUI.Out).To(Say("App some-app is already bound to some-service."))
   135  						Expect(testUI.Out).To(Say("OK"))
   136  					})
   137  				})
   138  
   139  				Context("when binding the service instance results in an error other than ServiceBindingTakenError", func() {
   140  					BeforeEach(func() {
   141  						fakeActor.BindServiceBySpaceReturns(
   142  							nil,
   143  							actionerror.ApplicationNotFoundError{Name: "some-app"})
   144  					})
   145  
   146  					It("should return the error", func() {
   147  						Expect(executeErr).To(MatchError(translatableerror.ApplicationNotFoundError{
   148  							Name: "some-app",
   149  						}))
   150  					})
   151  				})
   152  
   153  				Context("when the service binding is successful", func() {
   154  					BeforeEach(func() {
   155  						fakeActor.BindServiceBySpaceReturns(
   156  							v2action.Warnings{"some-warning", "another-warning"},
   157  							nil,
   158  						)
   159  					})
   160  
   161  					It("displays OK and the TIP", func() {
   162  						Expect(executeErr).ToNot(HaveOccurred())
   163  
   164  						Expect(testUI.Out).To(Say("OK"))
   165  						Expect(testUI.Out).To(Say("TIP: Use 'faceman restage some-app' to ensure your env variable changes take effect"))
   166  						Expect(testUI.Err).To(Say("some-warning"))
   167  						Expect(testUI.Err).To(Say("another-warning"))
   168  
   169  						Expect(fakeActor.BindServiceBySpaceCallCount()).To(Equal(1))
   170  						appName, serviceInstanceName, spaceGUID, parameters := fakeActor.BindServiceBySpaceArgsForCall(0)
   171  						Expect(appName).To(Equal("some-app"))
   172  						Expect(serviceInstanceName).To(Equal("some-service"))
   173  						Expect(spaceGUID).To(Equal("some-space-guid"))
   174  						Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"}))
   175  					})
   176  				})
   177  			})
   178  		})
   179  	})
   180  })