github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/unbind_service_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/api/cloudcontroller/ccv2"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    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("unbind-service Command", func() {
    21  	var (
    22  		cmd             UnbindServiceCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeUnbindServiceActor
    27  		binaryName      string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v6fakes.FakeUnbindServiceActor)
    36  
    37  		cmd = UnbindServiceCommand{
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  			Actor:       fakeActor,
    42  		}
    43  
    44  		cmd.RequiredArgs.AppName = "some-app"
    45  		cmd.RequiredArgs.ServiceInstanceName = "some-service"
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns("faceman")
    49  	})
    50  
    51  	JustBeforeEach(func() {
    52  		executeErr = cmd.Execute(nil)
    53  	})
    54  
    55  	When("a cloud controller API endpoint is set", func() {
    56  		BeforeEach(func() {
    57  			fakeConfig.TargetReturns("some-url")
    58  		})
    59  
    60  		When("checking target fails", func() {
    61  			BeforeEach(func() {
    62  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    63  			})
    64  
    65  			It("returns an error", func() {
    66  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    67  
    68  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    69  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    70  				Expect(checkTargetedOrg).To(BeTrue())
    71  				Expect(checkTargetedSpace).To(BeTrue())
    72  			})
    73  		})
    74  
    75  		When("the user is logged in, and an org and space are targeted", func() {
    76  			BeforeEach(func() {
    77  				fakeConfig.HasTargetedOrganizationReturns(true)
    78  				fakeConfig.HasTargetedSpaceReturns(true)
    79  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    80  					GUID: "some-org-guid",
    81  					Name: "some-org",
    82  				})
    83  				fakeConfig.TargetedSpaceReturns(configv3.Space{
    84  					GUID: "some-space-guid",
    85  					Name: "some-space",
    86  				})
    87  			})
    88  
    89  			When("getting the current user returns an error", func() {
    90  				var expectedErr error
    91  
    92  				BeforeEach(func() {
    93  					expectedErr = errors.New("got bananapants??")
    94  					fakeConfig.CurrentUserReturns(
    95  						configv3.User{},
    96  						expectedErr)
    97  				})
    98  
    99  				It("returns the error", func() {
   100  					Expect(executeErr).To(MatchError(expectedErr))
   101  				})
   102  			})
   103  
   104  			When("getting the current user does not return an error", func() {
   105  				BeforeEach(func() {
   106  					fakeConfig.CurrentUserReturns(
   107  						configv3.User{Name: "some-user"},
   108  						nil)
   109  				})
   110  
   111  				It("displays flavor text", func() {
   112  					Expect(executeErr).ToNot(HaveOccurred())
   113  
   114  					Expect(testUI.Out).To(Say("Unbinding app some-app from service some-service in org some-org / space some-space as some-user..."))
   115  				})
   116  
   117  				When("unbinding the service instance results in an error not related to service binding", func() {
   118  					BeforeEach(func() {
   119  						fakeActor.UnbindServiceBySpaceReturns(v2action.ServiceBinding{}, nil, actionerror.ApplicationNotFoundError{Name: "some-app"})
   120  					})
   121  
   122  					It("should return the error", func() {
   123  						Expect(executeErr).To(MatchError(actionerror.ApplicationNotFoundError{
   124  							Name: "some-app",
   125  						}))
   126  					})
   127  				})
   128  
   129  				When("the service binding does not exist", func() {
   130  					BeforeEach(func() {
   131  						fakeActor.UnbindServiceBySpaceReturns(
   132  							v2action.ServiceBinding{},
   133  							[]string{"foo", "bar"},
   134  							actionerror.ServiceBindingNotFoundError{})
   135  					})
   136  
   137  					It("displays warnings and 'OK'", func() {
   138  						Expect(executeErr).NotTo(HaveOccurred())
   139  
   140  						Expect(testUI.Err).To(Say("foo"))
   141  						Expect(testUI.Err).To(Say("bar"))
   142  						Expect(testUI.Err).To(Say("Binding between some-service and some-app did not exist"))
   143  						Expect(testUI.Out).To(Say("OK"))
   144  					})
   145  				})
   146  
   147  				When("the service binding exists", func() {
   148  					It("displays OK", func() {
   149  						Expect(executeErr).ToNot(HaveOccurred())
   150  
   151  						Expect(testUI.Out).To(Say("OK"))
   152  						Expect(testUI.Err).NotTo(Say("Binding between some-service and some-app did not exist"))
   153  
   154  						Expect(fakeActor.UnbindServiceBySpaceCallCount()).To(Equal(1))
   155  						appName, serviceInstanceName, spaceGUID := fakeActor.UnbindServiceBySpaceArgsForCall(0)
   156  						Expect(appName).To(Equal("some-app"))
   157  						Expect(serviceInstanceName).To(Equal("some-service"))
   158  						Expect(spaceGUID).To(Equal("some-space-guid"))
   159  					})
   160  
   161  					When("the service unbind is async", func() {
   162  						BeforeEach(func() {
   163  							fakeActor.UnbindServiceBySpaceReturns(
   164  								v2action.ServiceBinding{LastOperation: ccv2.LastOperation{State: constant.LastOperationInProgress}},
   165  								nil,
   166  								nil)
   167  						})
   168  
   169  						It("displays usefull message", func() {
   170  							Expect(executeErr).ToNot(HaveOccurred())
   171  
   172  							Expect(testUI.Out).To(Say("OK"))
   173  							Expect(testUI.Out).To(Say("Unbinding in progress. Use 'faceman service some-service' to check operation status."))
   174  							Expect(testUI.Err).NotTo(Say("Binding between some-service and some-app did not exist"))
   175  
   176  							Expect(fakeActor.UnbindServiceBySpaceCallCount()).To(Equal(1))
   177  							appName, serviceInstanceName, spaceGUID := fakeActor.UnbindServiceBySpaceArgsForCall(0)
   178  							Expect(appName).To(Equal("some-app"))
   179  							Expect(serviceInstanceName).To(Equal("some-service"))
   180  							Expect(spaceGUID).To(Equal("some-space-guid"))
   181  						})
   182  					})
   183  				})
   184  			})
   185  		})
   186  	})
   187  })