github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/delete_org_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/v7" 10 "code.cloudfoundry.org/cli/command/v7/v7fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("delete-org Command", func() { 19 var ( 20 cmd DeleteOrgCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v7fakes.FakeDeleteOrganizationActor 25 input *Buffer 26 binaryName string 27 executeErr error 28 ) 29 30 BeforeEach(func() { 31 input = NewBuffer() 32 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 33 fakeConfig = new(commandfakes.FakeConfig) 34 fakeSharedActor = new(commandfakes.FakeSharedActor) 35 fakeActor = new(v7fakes.FakeDeleteOrganizationActor) 36 37 cmd = DeleteOrgCommand{ 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 Actor: fakeActor, 42 } 43 44 cmd.RequiredArgs.Organization = "some-org" 45 binaryName = "faceman" 46 fakeConfig.BinaryNameReturns(binaryName) 47 }) 48 49 JustBeforeEach(func() { 50 executeErr = cmd.Execute(nil) 51 }) 52 53 When("a cloud controller API endpoint is set", func() { 54 BeforeEach(func() { 55 fakeConfig.TargetReturns("some-url") 56 }) 57 58 When("checking target fails", func() { 59 BeforeEach(func() { 60 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 61 }) 62 63 It("returns an error", func() { 64 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 65 66 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 67 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 68 Expect(checkTargetedOrg).To(BeFalse()) 69 Expect(checkTargetedSpace).To(BeFalse()) 70 }) 71 }) 72 73 When("the user is logged in", func() { 74 When("getting the current user returns an error", func() { 75 var returnedErr error 76 77 BeforeEach(func() { 78 returnedErr = errors.New("some error") 79 fakeConfig.CurrentUserReturns(configv3.User{}, returnedErr) 80 }) 81 82 It("returns the error", func() { 83 Expect(executeErr).To(MatchError(returnedErr)) 84 }) 85 }) 86 87 When("getting the current user does not return an error", func() { 88 BeforeEach(func() { 89 fakeConfig.CurrentUserReturns( 90 configv3.User{Name: "some-user"}, 91 nil) 92 }) 93 94 When("the '-f' flag is provided", func() { 95 BeforeEach(func() { 96 cmd.Force = true 97 }) 98 99 When("no errors are encountered", func() { 100 BeforeEach(func() { 101 fakeActor.DeleteOrganizationReturns(v7action.Warnings{"warning-1", "warning-2"}, nil) 102 }) 103 104 It("does not prompt for user confirmation, displays warnings, and deletes the org", func() { 105 Expect(executeErr).ToNot(HaveOccurred()) 106 107 Expect(testUI.Out).ToNot(Say(`Really delete the org some-org, including its spaces, apps, service instances, routes, private domains and space-scoped service brokers\? \[yN\]:`)) 108 Expect(testUI.Out).To(Say("Deleting org some-org as some-user...")) 109 110 Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(1)) 111 orgName := fakeActor.DeleteOrganizationArgsForCall(0) 112 Expect(orgName).To(Equal("some-org")) 113 114 Expect(testUI.Err).To(Say("warning-1")) 115 Expect(testUI.Err).To(Say("warning-2")) 116 Expect(testUI.Out).To(Say("OK")) 117 }) 118 }) 119 120 When("an error is encountered deleting the org", func() { 121 When("the organization does not exist", func() { 122 BeforeEach(func() { 123 fakeActor.DeleteOrganizationReturns( 124 v7action.Warnings{"warning-1", "warning-2"}, 125 actionerror.OrganizationNotFoundError{ 126 Name: "some-org", 127 }, 128 ) 129 }) 130 131 It("returns an OrganizationNotFoundError and displays all warnings", func() { 132 Expect(executeErr).NotTo(HaveOccurred()) 133 134 Expect(testUI.Out).To(Say("Deleting org some-org as some-user...")) 135 136 Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(1)) 137 orgName := fakeActor.DeleteOrganizationArgsForCall(0) 138 Expect(orgName).To(Equal("some-org")) 139 140 Expect(testUI.Err).To(Say("warning-1")) 141 Expect(testUI.Err).To(Say("warning-2")) 142 143 Expect(testUI.Err).To(Say(`Org 'some-org' does not exist\.`)) 144 Expect(testUI.Out).To(Say("OK")) 145 }) 146 }) 147 148 When("the organization does exist", func() { 149 var returnedErr error 150 151 BeforeEach(func() { 152 returnedErr = errors.New("some error") 153 fakeActor.DeleteOrganizationReturns(v7action.Warnings{"warning-1", "warning-2"}, returnedErr) 154 }) 155 156 It("returns the error, displays all warnings, and does not delete the org", func() { 157 Expect(executeErr).To(MatchError(returnedErr)) 158 159 Expect(testUI.Out).To(Say("Deleting org some-org as some-user...")) 160 161 Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(1)) 162 orgName := fakeActor.DeleteOrganizationArgsForCall(0) 163 Expect(orgName).To(Equal("some-org")) 164 165 Expect(testUI.Err).To(Say("warning-1")) 166 Expect(testUI.Err).To(Say("warning-2")) 167 }) 168 }) 169 }) 170 }) 171 172 // Testing the prompt. 173 When("the '-f' flag is not provided", func() { 174 When("the user chooses the default", func() { 175 BeforeEach(func() { 176 _, err := input.Write([]byte("\n")) 177 Expect(err).ToNot(HaveOccurred()) 178 }) 179 180 It("does not delete the org", func() { 181 Expect(executeErr).ToNot(HaveOccurred()) 182 183 Expect(testUI.Out).To(Say(`Organization 'some-org' has not been deleted\.`)) 184 185 Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(0)) 186 }) 187 }) 188 189 When("the user inputs no", func() { 190 BeforeEach(func() { 191 _, err := input.Write([]byte("n\n")) 192 Expect(err).ToNot(HaveOccurred()) 193 }) 194 195 It("does not delete the org", func() { 196 Expect(executeErr).ToNot(HaveOccurred()) 197 198 Expect(testUI.Out).To(Say(`Organization 'some-org' has not been deleted\.`)) 199 200 Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(0)) 201 }) 202 }) 203 204 When("the user inputs yes", func() { 205 BeforeEach(func() { 206 _, err := input.Write([]byte("y\n")) 207 Expect(err).ToNot(HaveOccurred()) 208 }) 209 210 It("deletes the org", func() { 211 Expect(executeErr).ToNot(HaveOccurred()) 212 213 Expect(testUI.Out).To(Say(`Really delete the org some-org, including its spaces, apps, service instances, routes, private domains and space-scoped service brokers\? \[yN\]:`)) 214 Expect(testUI.Out).To(Say("Deleting org some-org as some-user...")) 215 216 Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(1)) 217 orgName := fakeActor.DeleteOrganizationArgsForCall(0) 218 Expect(orgName).To(Equal("some-org")) 219 220 Expect(testUI.Out).To(Say("OK")) 221 }) 222 }) 223 224 When("the user input is invalid", func() { 225 BeforeEach(func() { 226 _, err := input.Write([]byte("e\n\n")) 227 Expect(err).ToNot(HaveOccurred()) 228 }) 229 230 It("asks the user again", func() { 231 Expect(executeErr).NotTo(HaveOccurred()) 232 233 Expect(testUI.Out).To(Say(`Really delete the org some-org, including its spaces, apps, service instances, routes, private domains and space-scoped service brokers\? \[yN\]:`)) 234 Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`)) 235 Expect(testUI.Out).To(Say(`Really delete the org some-org, including its spaces, apps, service instances, routes, private domains and space-scoped service brokers\? \[yN\]:`)) 236 237 Expect(fakeActor.DeleteOrganizationCallCount()).To(Equal(0)) 238 }) 239 }) 240 241 When("displaying the prompt returns an error", func() { 242 // if nothing is written to input, display bool prompt returns EOF 243 It("returns the error", func() { 244 Expect(executeErr).To(MatchError("EOF")) 245 }) 246 }) 247 }) 248 249 When("the user deletes the currently targeted org", func() { 250 BeforeEach(func() { 251 cmd.Force = true 252 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 253 }) 254 255 It("clears the targeted org and space from the config", func() { 256 Expect(executeErr).ToNot(HaveOccurred()) 257 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 258 }) 259 }) 260 261 When("the user deletes an org that's not the currently targeted org", func() { 262 BeforeEach(func() { 263 cmd.Force = true 264 }) 265 266 It("does not clear the targeted org and space from the config", func() { 267 Expect(executeErr).ToNot(HaveOccurred()) 268 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 269 }) 270 }) 271 }) 272 }) 273 }) 274 })