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