github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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/sharedaction" 7 "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/command" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 . "code.cloudfoundry.org/cli/command/v2" 11 "code.cloudfoundry.org/cli/command/v2/v2fakes" 12 "code.cloudfoundry.org/cli/util/configv3" 13 "code.cloudfoundry.org/cli/util/ui" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 ) 18 19 var _ = Describe("unbind-service Command", func() { 20 var ( 21 cmd UnbindServiceCommand 22 testUI *ui.UI 23 fakeConfig *commandfakes.FakeConfig 24 fakeSharedActor *commandfakes.FakeSharedActor 25 fakeActor *v2fakes.FakeUnbindServiceActor 26 binaryName string 27 executeErr error 28 ) 29 30 BeforeEach(func() { 31 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 32 fakeConfig = new(commandfakes.FakeConfig) 33 fakeSharedActor = new(commandfakes.FakeSharedActor) 34 fakeActor = new(v2fakes.FakeUnbindServiceActor) 35 36 cmd = UnbindServiceCommand{ 37 UI: testUI, 38 Config: fakeConfig, 39 SharedActor: fakeSharedActor, 40 Actor: fakeActor, 41 } 42 43 cmd.RequiredArgs.AppName = "some-app" 44 cmd.RequiredArgs.ServiceInstanceName = "some-service" 45 46 binaryName = "faceman" 47 fakeConfig.BinaryNameReturns("faceman") 48 }) 49 50 JustBeforeEach(func() { 51 executeErr = cmd.Execute(nil) 52 }) 53 54 Context("when a cloud controller API endpoint is set", func() { 55 BeforeEach(func() { 56 fakeConfig.TargetReturns("some-url") 57 }) 58 59 Context("when checking target fails", func() { 60 BeforeEach(func() { 61 fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName}) 62 }) 63 64 It("returns an error", func() { 65 Expect(executeErr).To(MatchError(command.NotLoggedInError{BinaryName: binaryName})) 66 67 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 68 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 69 Expect(checkTargetedOrg).To(BeTrue()) 70 Expect(checkTargetedSpace).To(BeTrue()) 71 }) 72 }) 73 74 Context("when the user is logged in, and an org and space are targeted", func() { 75 BeforeEach(func() { 76 fakeConfig.HasTargetedOrganizationReturns(true) 77 fakeConfig.HasTargetedSpaceReturns(true) 78 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 79 GUID: "some-org-guid", 80 Name: "some-org", 81 }) 82 fakeConfig.TargetedSpaceReturns(configv3.Space{ 83 GUID: "some-space-guid", 84 Name: "some-space", 85 }) 86 }) 87 88 Context("when getting the current user returns an error", func() { 89 var expectedErr error 90 91 BeforeEach(func() { 92 expectedErr = errors.New("got bananapants??") 93 fakeConfig.CurrentUserReturns( 94 configv3.User{}, 95 expectedErr) 96 }) 97 98 It("returns the error", func() { 99 Expect(executeErr).To(MatchError(expectedErr)) 100 }) 101 }) 102 103 Context("when getting the current user does not return an error", func() { 104 BeforeEach(func() { 105 fakeConfig.CurrentUserReturns( 106 configv3.User{Name: "some-user"}, 107 nil) 108 }) 109 110 It("displays flavor text", func() { 111 Expect(executeErr).ToNot(HaveOccurred()) 112 113 Expect(testUI.Out).To(Say("Unbinding app some-app from service some-service in org some-org / space some-space as some-user...")) 114 }) 115 116 Context("when unbinding the service instance results in an error not related to service binding", func() { 117 BeforeEach(func() { 118 fakeActor.UnbindServiceBySpaceReturns( 119 nil, 120 v2action.ApplicationNotFoundError{Name: "some-app"}) 121 }) 122 123 It("should return the error", func() { 124 Expect(executeErr).To(MatchError(command.ApplicationNotFoundError{ 125 Name: "some-app", 126 })) 127 }) 128 }) 129 130 Context("when the service binding does not exist", func() { 131 BeforeEach(func() { 132 fakeActor.UnbindServiceBySpaceReturns( 133 []string{"foo", "bar"}, 134 v2action.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 Context("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 }) 162 }) 163 }) 164 })