github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/command/v7/unshare_service_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/v7action" 7 "code.cloudfoundry.org/cli/types" 8 "code.cloudfoundry.org/cli/util/configv3" 9 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 . "code.cloudfoundry.org/cli/command/v7" 12 "code.cloudfoundry.org/cli/command/v7/v7fakes" 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("unshare-service command", func() { 20 var ( 21 cmd UnshareServiceCommand 22 input *Buffer 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v7fakes.FakeActor 27 executeErr error 28 ) 29 30 const ( 31 expectedServiceInstanceName = "fake-service-instance-name" 32 expectedSpaceName = "fake-space-name" 33 expectedTargetedSpaceGuid = "fake-space-guid" 34 expectedTargetedOrgName = "fake-org-name" 35 expectedTargetedOrgGuid = "fake-org-guid" 36 expectedUser = "fake-username" 37 ) 38 39 testActorInteractions := func() { 40 It("calls the actor to share in specified space and targeted org", func() { 41 Expect(fakeActor.UnshareServiceInstanceFromSpaceAndOrgCallCount()).To(Equal(1)) 42 43 actualServiceInstance, actualTargetedSpace, actualTargetedOrg, actualSharingParams := fakeActor.UnshareServiceInstanceFromSpaceAndOrgArgsForCall(0) 44 Expect(actualServiceInstance).To(Equal(expectedServiceInstanceName)) 45 Expect(actualTargetedSpace).To(Equal(expectedTargetedSpaceGuid)) 46 Expect(actualTargetedOrg).To(Equal(expectedTargetedOrgGuid)) 47 Expect(actualSharingParams).To(Equal(v7action.ServiceInstanceSharingParams{ 48 SpaceName: expectedSpaceName, 49 OrgName: types.OptionalString{}, 50 })) 51 }) 52 53 When("the unshare completes successfully", func() { 54 BeforeEach(func() { 55 fakeActor.UnshareServiceInstanceFromSpaceAndOrgReturns(v7action.Warnings{"warning one", "warning two"}, nil) 56 }) 57 58 It("returns an OK message", func() { 59 Expect(executeErr).To(BeNil()) 60 61 Expect(testUI.Out).To( 62 Say(`Unsharing service instance %s from org %s / space %s as %s`, 63 expectedServiceInstanceName, 64 expectedTargetedOrgName, 65 expectedSpaceName, 66 expectedUser)) 67 Expect(testUI.Out).To(Say(`OK`)) 68 Expect(testUI.Err).To(SatisfyAll( 69 Say("warning one"), 70 Say("warning two"), 71 )) 72 }) 73 }) 74 75 When("organization flag is specified", func() { 76 var expectedSpecifiedOrgName = "fake-org-name" 77 78 BeforeEach(func() { 79 setFlag(&cmd, "-o", types.NewOptionalString(expectedSpecifiedOrgName)) 80 }) 81 82 It("calls the actor to share in specified space and org", func() { 83 Expect(fakeActor.UnshareServiceInstanceFromSpaceAndOrgCallCount()).To(Equal(1)) 84 85 _, _, _, actualSharingParams := fakeActor.UnshareServiceInstanceFromSpaceAndOrgArgsForCall(0) 86 Expect(actualSharingParams).To(Equal(v7action.ServiceInstanceSharingParams{ 87 SpaceName: expectedSpaceName, 88 OrgName: types.NewOptionalString(expectedSpecifiedOrgName), 89 })) 90 }) 91 }) 92 93 When("the actor errors", func() { 94 BeforeEach(func() { 95 fakeSharedActor.CheckTargetReturns(nil) 96 fakeActor.UnshareServiceInstanceFromSpaceAndOrgReturns(v7action.Warnings{"actor warning"}, errors.New("test error")) 97 }) 98 99 It("prints all warnings and fails with an error", func() { 100 Expect(executeErr).To(Not(BeNil())) 101 Expect(testUI.Err).To(Say("actor warning")) 102 Expect(executeErr).To(MatchError("test error")) 103 104 }) 105 }) 106 } 107 108 BeforeEach(func() { 109 input = NewBuffer() 110 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 111 fakeConfig = new(commandfakes.FakeConfig) 112 fakeSharedActor = new(commandfakes.FakeSharedActor) 113 fakeActor = new(v7fakes.FakeActor) 114 115 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: expectedTargetedOrgName}) 116 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: expectedSpaceName, GUID: expectedTargetedSpaceGuid}) 117 fakeActor.GetCurrentUserReturns(configv3.User{Name: expectedUser}, nil) 118 119 cmd = UnshareServiceCommand{ 120 BaseCommand: BaseCommand{ 121 UI: testUI, 122 Config: fakeConfig, 123 SharedActor: fakeSharedActor, 124 Actor: fakeActor, 125 }, 126 } 127 }) 128 129 JustBeforeEach(func() { 130 executeErr = cmd.Execute(nil) 131 }) 132 133 It("checks the user is logged in, and targeting an org and space", func() { 134 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 135 orgChecked, spaceChecked := fakeSharedActor.CheckTargetArgsForCall(0) 136 Expect(orgChecked).To(BeTrue()) 137 Expect(spaceChecked).To(BeTrue()) 138 }) 139 140 Context("user not targeting space", func() { 141 BeforeEach(func() { 142 fakeSharedActor.CheckTargetReturns(errors.New("space not targeted")) 143 }) 144 145 It("fails the command", func() { 146 Expect(executeErr).To(Not(BeNil())) 147 Expect(executeErr).To(MatchError("space not targeted")) 148 }) 149 }) 150 151 Context("user is targeting a space and org", func() { 152 BeforeEach(func() { 153 cmd.RequiredArgs.ServiceInstance = expectedServiceInstanceName 154 setFlag(&cmd, "-s", expectedSpaceName) 155 156 fakeSharedActor.CheckTargetReturns(nil) 157 fakeConfig.TargetedSpaceReturns(configv3.Space{GUID: expectedTargetedSpaceGuid}) 158 fakeConfig.TargetedOrganizationReturns(configv3.Organization{GUID: expectedTargetedOrgGuid, Name: expectedTargetedOrgName}) 159 fakeActor.GetCurrentUserReturns(configv3.User{Name: expectedUser}, nil) 160 }) 161 162 It("prompts the user", func() { 163 Expect(testUI.Err).To(Say(`WARNING: Unsharing this service instance will remove any existing bindings originating from the service instance in the space "%s". This could cause apps to stop working.`, expectedSpaceName)) 164 Expect(testUI.Out).To(SatisfyAll( 165 Say(`Really unshare the service instance %s from space %s\? \[yN\]:`, 166 expectedServiceInstanceName, 167 expectedSpaceName), 168 )) 169 }) 170 171 When("the user says yes", func() { 172 BeforeEach(func() { 173 _, err := input.Write([]byte("y\n")) 174 Expect(err).NotTo(HaveOccurred()) 175 }) 176 177 testActorInteractions() 178 }) 179 180 When("the user says no", func() { 181 BeforeEach(func() { 182 _, err := input.Write([]byte("n\n")) 183 Expect(err).NotTo(HaveOccurred()) 184 }) 185 186 It("does not call the actor", func() { 187 Expect(fakeActor.UnshareServiceInstanceFromSpaceAndOrgCallCount()).To(BeZero()) 188 }) 189 190 It("says the delete was cancelled", func() { 191 Expect(executeErr).NotTo(HaveOccurred()) 192 Expect(testUI.Out).To(Say("Unshare cancelled\n")) 193 }) 194 }) 195 196 When("the -f flag is specified", func() { 197 BeforeEach(func() { 198 setFlag(&cmd, "-f") 199 }) 200 201 It("does not prompt the user", func() { 202 Expect(testUI.Out).NotTo(Say("Really unshare")) 203 }) 204 205 testActorInteractions() 206 }) 207 }) 208 209 Context("pre-unshare errors", func() { 210 When("checking the target returns an error", func() { 211 BeforeEach(func() { 212 fakeSharedActor.CheckTargetReturns(errors.New("explode")) 213 }) 214 215 It("returns the error", func() { 216 Expect(executeErr).To(MatchError("explode")) 217 }) 218 }) 219 220 When("getting the username fails", func() { 221 BeforeEach(func() { 222 input.Write([]byte("y\n")) 223 fakeActor.GetCurrentUserReturns(configv3.User{}, errors.New("boom")) 224 }) 225 226 It("returns the error", func() { 227 Expect(executeErr).To(MatchError("boom")) 228 }) 229 }) 230 }) 231 })