github.com/loafoe/cli@v7.1.0+incompatible/command/v7/delete_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/flag" 10 . "code.cloudfoundry.org/cli/command/v7" 11 "code.cloudfoundry.org/cli/command/v7/v7fakes" 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("delete Command", func() { 20 var ( 21 cmd DeleteCommand 22 testUI *ui.UI 23 fakeConfig *commandfakes.FakeConfig 24 fakeSharedActor *commandfakes.FakeSharedActor 25 fakeActor *v7fakes.FakeActor 26 input *Buffer 27 binaryName string 28 executeErr error 29 app string 30 ) 31 32 BeforeEach(func() { 33 input = NewBuffer() 34 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 35 fakeConfig = new(commandfakes.FakeConfig) 36 fakeSharedActor = new(commandfakes.FakeSharedActor) 37 fakeActor = new(v7fakes.FakeActor) 38 39 binaryName = "faceman" 40 fakeConfig.BinaryNameReturns(binaryName) 41 app = "some-app" 42 43 cmd = DeleteCommand{ 44 RequiredArgs: flag.AppName{AppName: app}, 45 46 BaseCommand: BaseCommand{ 47 UI: testUI, 48 Config: fakeConfig, 49 SharedActor: fakeSharedActor, 50 Actor: fakeActor, 51 }, 52 } 53 54 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 55 Name: "some-org", 56 GUID: "some-org-guid", 57 }) 58 59 fakeConfig.TargetedSpaceReturns(configv3.Space{ 60 Name: "some-space", 61 GUID: "some-space-guid", 62 }) 63 64 fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil) 65 }) 66 67 JustBeforeEach(func() { 68 executeErr = cmd.Execute(nil) 69 }) 70 71 When("the -r flag is provided", func() { 72 BeforeEach(func() { 73 cmd.DeleteMappedRoutes = true 74 cmd.Force = false 75 _, err := input.Write([]byte("y\n")) 76 Expect(err).ToNot(HaveOccurred()) 77 78 fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, nil) 79 }) 80 81 It("asks for a special prompt about deleting associated routes", func() { 82 Expect(executeErr).ToNot(HaveOccurred()) 83 84 Expect(testUI.Err).To(Say("some-warning")) 85 Expect(testUI.Out).To(Say(`Deleting the app and associated routes will make apps with this route, in any org, unreachable\.`)) 86 Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`)) 87 Expect(testUI.Out).To(Say("OK")) 88 Expect(testUI.Out).NotTo(Say(`App 'some-app' does not exist\.`)) 89 }) 90 91 It("deletes application and mapped routes", func() { 92 actualName, actualSpace, deleteMappedRoutes := fakeActor.DeleteApplicationByNameAndSpaceArgsForCall(0) 93 Expect(actualName).To(Equal(app)) 94 Expect(actualSpace).To(Equal(fakeConfig.TargetedSpace().GUID)) 95 Expect(deleteMappedRoutes).To(BeTrue()) 96 }) 97 98 When("the route is mapped to a different app", func() { 99 BeforeEach(func() { 100 fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, actionerror.RouteBoundToMultipleAppsError{}) 101 }) 102 103 It("returns the error", func() { 104 Expect(executeErr).To(MatchError(actionerror.RouteBoundToMultipleAppsError{})) 105 }) 106 107 It("defers showing a tip", func() { 108 Expect(testUI.Out).NotTo(Say("TIP")) 109 testUI.FlushDeferred() 110 Expect(testUI.Out).To(Say(`\n\nTIP: Run 'cf delete some-app' to delete the app and 'cf delete-route' to delete the route\.`)) 111 }) 112 }) 113 }) 114 115 When("checking target fails", func() { 116 BeforeEach(func() { 117 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 118 }) 119 120 It("returns an error", func() { 121 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})) 122 123 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 124 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 125 Expect(checkTargetedOrg).To(BeTrue()) 126 Expect(checkTargetedSpace).To(BeTrue()) 127 }) 128 }) 129 130 When("the user is not logged in", func() { 131 var expectedErr error 132 133 BeforeEach(func() { 134 expectedErr = errors.New("some current user error") 135 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 136 }) 137 138 It("return an error", func() { 139 Expect(executeErr).To(Equal(expectedErr)) 140 }) 141 }) 142 143 When("the -f flag is NOT provided", func() { 144 BeforeEach(func() { 145 cmd.Force = false 146 }) 147 148 When("the user inputs yes", func() { 149 BeforeEach(func() { 150 _, err := input.Write([]byte("y\n")) 151 Expect(err).ToNot(HaveOccurred()) 152 153 fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, nil) 154 }) 155 156 It("delegates to the Actor", func() { 157 actualName, actualSpace, deleteMappedRoutes := fakeActor.DeleteApplicationByNameAndSpaceArgsForCall(0) 158 Expect(actualName).To(Equal(app)) 159 Expect(actualSpace).To(Equal(fakeConfig.TargetedSpace().GUID)) 160 Expect(deleteMappedRoutes).To(BeFalse()) 161 }) 162 163 It("deletes the app", func() { 164 Expect(executeErr).ToNot(HaveOccurred()) 165 166 Expect(testUI.Err).To(Say("some-warning")) 167 Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`)) 168 Expect(testUI.Out).To(Say("OK")) 169 Expect(testUI.Out).NotTo(Say(`App 'some-app' does not exist\.`)) 170 }) 171 }) 172 173 When("the user inputs no", func() { 174 BeforeEach(func() { 175 _, err := input.Write([]byte("n\n")) 176 Expect(err).ToNot(HaveOccurred()) 177 }) 178 179 It("cancels the delete", func() { 180 Expect(executeErr).ToNot(HaveOccurred()) 181 182 Expect(testUI.Out).To(Say(`App 'some-app' has not been deleted\.`)) 183 Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0)) 184 }) 185 }) 186 187 When("the user chooses the default", func() { 188 BeforeEach(func() { 189 _, err := input.Write([]byte("\n")) 190 Expect(err).ToNot(HaveOccurred()) 191 }) 192 193 It("cancels the delete", func() { 194 Expect(executeErr).ToNot(HaveOccurred()) 195 196 Expect(testUI.Out).To(Say(`App 'some-app' has not been deleted\.`)) 197 Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0)) 198 }) 199 }) 200 201 When("the user input is invalid", func() { 202 BeforeEach(func() { 203 _, err := input.Write([]byte("e\n\n")) 204 Expect(err).ToNot(HaveOccurred()) 205 }) 206 207 It("asks the user again", func() { 208 Expect(executeErr).NotTo(HaveOccurred()) 209 210 Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`)) 211 Expect(testUI.Out).To(Say(`invalid input \(not y, n, yes, or no\)`)) 212 Expect(testUI.Out).To(Say(`Really delete the app some-app\? \[yN\]`)) 213 214 Expect(fakeActor.DeleteApplicationByNameAndSpaceCallCount()).To(Equal(0)) 215 }) 216 }) 217 }) 218 219 When("the -f flag is provided", func() { 220 BeforeEach(func() { 221 cmd.Force = true 222 }) 223 224 When("deleting the app errors", func() { 225 Context("generic error", func() { 226 BeforeEach(func() { 227 fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, errors.New("some-error")) 228 }) 229 230 It("displays all warnings, and returns the erorr", func() { 231 Expect(testUI.Err).To(Say("some-warning")) 232 Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`)) 233 Expect(testUI.Out).ToNot(Say("OK")) 234 Expect(executeErr).To(MatchError("some-error")) 235 }) 236 }) 237 }) 238 239 When("the app doesn't exist", func() { 240 BeforeEach(func() { 241 fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, actionerror.ApplicationNotFoundError{Name: "some-app"}) 242 }) 243 244 It("displays all warnings, that the app wasn't found, and does not error", func() { 245 Expect(executeErr).ToNot(HaveOccurred()) 246 247 Expect(testUI.Err).To(Say("some-warning")) 248 Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`)) 249 Expect(testUI.Err).To(Say(`App 'some-app' does not exist\.`)) 250 Expect(testUI.Out).To(Say("OK")) 251 }) 252 }) 253 254 When("the app exists", func() { 255 BeforeEach(func() { 256 fakeActor.DeleteApplicationByNameAndSpaceReturns(v7action.Warnings{"some-warning"}, nil) 257 }) 258 259 It("displays all warnings, and does not error", func() { 260 Expect(executeErr).ToNot(HaveOccurred()) 261 262 Expect(testUI.Err).To(Say("some-warning")) 263 Expect(testUI.Out).To(Say(`Deleting app some-app in org some-org / space some-space as steve\.\.\.`)) 264 Expect(testUI.Out).To(Say("OK")) 265 Expect(testUI.Err).NotTo(Say(`App 'some-app' does not exist\.`)) 266 }) 267 }) 268 }) 269 })