github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/unbind_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/command/commandfakes"
    10  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    11  	. "github.com/liamawhite/cli-with-i18n/command/v2"
    12  	"github.com/liamawhite/cli-with-i18n/command/v2/v2fakes"
    13  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    14  	"github.com/liamawhite/cli-with-i18n/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       *v2fakes.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(v2fakes.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  	Context("when a cloud controller API endpoint is set", func() {
    56  		BeforeEach(func() {
    57  			fakeConfig.TargetReturns("some-url")
    58  		})
    59  
    60  		Context("when checking target fails", func() {
    61  			BeforeEach(func() {
    62  				fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName})
    63  			})
    64  
    65  			It("returns an error", func() {
    66  				Expect(executeErr).To(MatchError(translatableerror.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  		Context("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  			Context("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  			Context("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  				Context("when unbinding the service instance results in an error not related to service binding", func() {
   118  					BeforeEach(func() {
   119  						fakeActor.UnbindServiceBySpaceReturns(nil, actionerror.ApplicationNotFoundError{Name: "some-app"})
   120  					})
   121  
   122  					It("should return the error", func() {
   123  						Expect(executeErr).To(MatchError(translatableerror.ApplicationNotFoundError{
   124  							Name: "some-app",
   125  						}))
   126  					})
   127  				})
   128  
   129  				Context("when the service binding does not exist", func() {
   130  					BeforeEach(func() {
   131  						fakeActor.UnbindServiceBySpaceReturns(
   132  							[]string{"foo", "bar"},
   133  							v2action.ServiceBindingNotFoundError{})
   134  					})
   135  
   136  					It("displays warnings and 'OK'", func() {
   137  						Expect(executeErr).NotTo(HaveOccurred())
   138  
   139  						Expect(testUI.Err).To(Say("foo"))
   140  						Expect(testUI.Err).To(Say("bar"))
   141  						Expect(testUI.Err).To(Say("Binding between some-service and some-app did not exist"))
   142  						Expect(testUI.Out).To(Say("OK"))
   143  					})
   144  				})
   145  
   146  				Context("when the service binding exists", func() {
   147  					It("displays OK", func() {
   148  						Expect(executeErr).ToNot(HaveOccurred())
   149  
   150  						Expect(testUI.Out).To(Say("OK"))
   151  						Expect(testUI.Err).NotTo(Say("Binding between some-service and some-app did not exist"))
   152  
   153  						Expect(fakeActor.UnbindServiceBySpaceCallCount()).To(Equal(1))
   154  						appName, serviceInstanceName, spaceGUID := fakeActor.UnbindServiceBySpaceArgsForCall(0)
   155  						Expect(appName).To(Equal("some-app"))
   156  						Expect(serviceInstanceName).To(Equal("some-service"))
   157  						Expect(spaceGUID).To(Equal("some-space-guid"))
   158  					})
   159  				})
   160  			})
   161  		})
   162  	})
   163  })