github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/delete_space_command_test.go (about) 1 package v2_test 2 3 import ( 4 "errors" 5 6 "github.com/liamawhite/cli-with-i18n/actor/sharedaction" 7 "github.com/liamawhite/cli-with-i18n/actor/v2action" 8 "github.com/liamawhite/cli-with-i18n/command/commandfakes" 9 "github.com/liamawhite/cli-with-i18n/command/translatableerror" 10 . "github.com/liamawhite/cli-with-i18n/command/v2" 11 "github.com/liamawhite/cli-with-i18n/command/v2/v2fakes" 12 "github.com/liamawhite/cli-with-i18n/util/configv3" 13 "github.com/liamawhite/cli-with-i18n/util/ui" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 ) 18 19 var _ = Describe("delete-space Command", func() { 20 var ( 21 cmd DeleteSpaceCommand 22 testUI *ui.UI 23 fakeConfig *commandfakes.FakeConfig 24 fakeSharedActor *commandfakes.FakeSharedActor 25 fakeActor *v2fakes.FakeDeleteSpaceActor 26 input *Buffer 27 binaryName string 28 executeErr error 29 ) 30 31 BeforeEach(func() { 32 input = NewBuffer() 33 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 34 fakeConfig = new(commandfakes.FakeConfig) 35 fakeSharedActor = new(commandfakes.FakeSharedActor) 36 fakeActor = new(v2fakes.FakeDeleteSpaceActor) 37 38 cmd = DeleteSpaceCommand{ 39 UI: testUI, 40 Config: fakeConfig, 41 SharedActor: fakeSharedActor, 42 Actor: fakeActor, 43 } 44 45 cmd.RequiredArgs.Space = "some-space" 46 47 binaryName = "faceman" 48 fakeConfig.BinaryNameReturns(binaryName) 49 fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil) 50 }) 51 52 JustBeforeEach(func() { 53 executeErr = cmd.Execute(nil) 54 }) 55 56 Context("when a cloud controller API endpoint is set", func() { 57 BeforeEach(func() { 58 fakeConfig.TargetReturns("some-url") 59 }) 60 61 Context("when checking target fails", func() { 62 Context("when an org is provided", func() { 63 BeforeEach(func() { 64 cmd.Org = "some-org" 65 fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName}) 66 }) 67 68 It("returns the NotLoggedInError", func() { 69 Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName})) 70 71 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 72 Expect(checkTargetedOrg).To(BeFalse()) 73 Expect(checkTargetedSpace).To(BeFalse()) 74 }) 75 }) 76 77 Context("when an org is NOT provided", func() { 78 BeforeEach(func() { 79 fakeSharedActor.CheckTargetReturns(sharedaction.NoOrganizationTargetedError{}) 80 }) 81 82 It("returns the NoOrganizationTargetedError", func() { 83 Expect(executeErr).To(MatchError(translatableerror.NoOrganizationTargetedError{})) 84 85 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 86 Expect(checkTargetedOrg).To(BeTrue()) 87 Expect(checkTargetedSpace).To(BeFalse()) 88 }) 89 }) 90 }) 91 92 Context("when the user is logged in", func() { 93 Context("when getting the current user returns an error", func() { 94 var returnedErr error 95 96 BeforeEach(func() { 97 returnedErr = errors.New("some error") 98 fakeConfig.CurrentUserReturns(configv3.User{}, returnedErr) 99 }) 100 101 It("returns the error", func() { 102 Expect(executeErr).To(MatchError(returnedErr)) 103 }) 104 }) 105 106 Context("when the -o flag is provided", func() { 107 BeforeEach(func() { 108 cmd.Org = "some-org" 109 }) 110 111 Context("when the -f flag is provided", func() { 112 BeforeEach(func() { 113 cmd.Force = true 114 }) 115 116 Context("when the deleting the space errors", func() { 117 BeforeEach(func() { 118 fakeActor.DeleteSpaceByNameAndOrganizationNameReturns(v2action.Warnings{"warning-1", "warning-2"}, v2action.SpaceNotFoundError{Name: "some-space"}) 119 }) 120 121 It("returns the translatable error", func() { 122 Expect(executeErr).To(MatchError(translatableerror.SpaceNotFoundError{Name: "some-space"})) 123 Expect(testUI.Out).To(Say("Deleting space some-space in org some-org as some-user\\.\\.\\.")) 124 125 Expect(testUI.Err).To(Say("warning-1")) 126 Expect(testUI.Err).To(Say("warning-2")) 127 }) 128 }) 129 130 Context("when the deleting the space succeeds", func() { 131 BeforeEach(func() { 132 fakeActor.DeleteSpaceByNameAndOrganizationNameReturns(v2action.Warnings{"warning-1", "warning-2"}, nil) 133 }) 134 135 Context("when the user was targeted to the space", func() { 136 BeforeEach(func() { 137 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space"}) 138 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 139 }) 140 141 It("untargets the space, displays all warnings and does not error", func() { 142 Expect(executeErr).ToNot(HaveOccurred()) 143 144 Expect(testUI.Out).To(Say("Deleting space some-space in org some-org as some-user\\.\\.\\.")) 145 Expect(testUI.Out).To(Say("OK")) 146 Expect(testUI.Out).To(Say("TIP: No space targeted, use 'faceman target -s' to target a space.")) 147 148 Expect(testUI.Err).To(Say("warning-1")) 149 Expect(testUI.Err).To(Say("warning-2")) 150 151 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 152 153 spaceArg, orgArg := fakeActor.DeleteSpaceByNameAndOrganizationNameArgsForCall(0) 154 Expect(spaceArg).To(Equal("some-space")) 155 Expect(orgArg).To(Equal("some-org")) 156 }) 157 }) 158 159 Context("when the user was NOT targeted to the space", func() { 160 BeforeEach(func() { 161 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space"}) 162 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-other-org"}) 163 }) 164 165 It("displays all warnings and does not error", func() { 166 Expect(executeErr).ToNot(HaveOccurred()) 167 168 Expect(testUI.Out).To(Say("Deleting space some-space in org some-org as some-user\\.\\.\\.")) 169 Expect(testUI.Out).To(Say("OK")) 170 Expect(testUI.Out).ToNot(Say("TIP: No space targeted, use 'faceman target -s' to target a space.")) 171 172 Expect(testUI.Err).To(Say("warning-1")) 173 Expect(testUI.Err).To(Say("warning-2")) 174 175 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(0)) 176 }) 177 }) 178 }) 179 }) 180 181 Context("when the -f flag is NOT provided", func() { 182 BeforeEach(func() { 183 cmd.Force = false 184 }) 185 186 Context("when the user inputs yes", func() { 187 BeforeEach(func() { 188 _, err := input.Write([]byte("y\n")) 189 Expect(err).ToNot(HaveOccurred()) 190 191 fakeActor.DeleteSpaceByNameAndOrganizationNameReturns(v2action.Warnings{"warning-1", "warning-2"}, nil) 192 }) 193 194 It("deletes the space", func() { 195 Expect(executeErr).ToNot(HaveOccurred()) 196 197 Expect(testUI.Out).To(Say("Really delete the space some-space\\? \\[yN\\]")) 198 Expect(testUI.Out).To(Say("Deleting space some-space in org some-org as some-user\\.\\.\\.")) 199 Expect(testUI.Out).To(Say("OK")) 200 Expect(testUI.Out).ToNot(Say("TIP: No space targeted, use 'faceman target -s' to target a space.")) 201 202 Expect(testUI.Err).To(Say("warning-1")) 203 Expect(testUI.Err).To(Say("warning-2")) 204 }) 205 }) 206 207 Context("when the user inputs no", func() { 208 BeforeEach(func() { 209 _, err := input.Write([]byte("n\n")) 210 Expect(err).ToNot(HaveOccurred()) 211 }) 212 213 It("cancels the delete", func() { 214 Expect(executeErr).ToNot(HaveOccurred()) 215 216 Expect(testUI.Out).To(Say("Delete cancelled")) 217 Expect(fakeActor.DeleteSpaceByNameAndOrganizationNameCallCount()).To(Equal(0)) 218 }) 219 }) 220 221 Context("when the user chooses the default", func() { 222 BeforeEach(func() { 223 _, err := input.Write([]byte("\n")) 224 Expect(err).ToNot(HaveOccurred()) 225 }) 226 227 It("cancels the delete", func() { 228 Expect(executeErr).ToNot(HaveOccurred()) 229 230 Expect(testUI.Out).To(Say("Delete cancelled")) 231 Expect(fakeActor.DeleteSpaceByNameAndOrganizationNameCallCount()).To(Equal(0)) 232 }) 233 }) 234 235 Context("when the user input is invalid", func() { 236 BeforeEach(func() { 237 _, err := input.Write([]byte("e\n\n")) 238 Expect(err).ToNot(HaveOccurred()) 239 }) 240 241 It("asks the user again", func() { 242 Expect(executeErr).NotTo(HaveOccurred()) 243 244 Expect(testUI.Out).To(Say("Really delete the space some-space\\? \\[yN\\]")) 245 Expect(testUI.Out).To(Say("invalid input \\(not y, n, yes, or no\\)")) 246 Expect(testUI.Out).To(Say("Really delete the space some-space\\? \\[yN\\]")) 247 248 Expect(fakeActor.DeleteSpaceByNameAndOrganizationNameCallCount()).To(Equal(0)) 249 }) 250 }) 251 }) 252 }) 253 254 Context("when the -o flag is NOT provided", func() { 255 BeforeEach(func() { 256 cmd.Org = "" 257 cmd.Force = true 258 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-targeted-org"}) 259 fakeActor.DeleteSpaceByNameAndOrganizationNameReturns(v2action.Warnings{"warning-1", "warning-2"}, nil) 260 }) 261 262 It("deletes the space in the targeted org", func() { 263 Expect(executeErr).NotTo(HaveOccurred()) 264 265 Expect(testUI.Out).To(Say("Deleting space some-space in org some-targeted-org as some-user\\.\\.\\.")) 266 Expect(testUI.Out).To(Say("OK")) 267 Expect(testUI.Out).ToNot(Say("TIP: No space targeted, use 'faceman target -s' to target a space\\.")) 268 269 Expect(testUI.Err).To(Say("warning-1")) 270 Expect(testUI.Err).To(Say("warning-2")) 271 272 spaceArg, orgArg := fakeActor.DeleteSpaceByNameAndOrganizationNameArgsForCall(0) 273 Expect(spaceArg).To(Equal("some-space")) 274 Expect(orgArg).To(Equal("some-targeted-org")) 275 }) 276 277 Context("when deleting a targeted space", func() { 278 BeforeEach(func() { 279 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space"}) 280 }) 281 282 It("deletes the space and untargets the org", func() { 283 Expect(executeErr).ToNot(HaveOccurred()) 284 285 Expect(testUI.Out).To(Say("Deleting space some-space in org some-targeted-org as some-user\\.\\.\\.")) 286 Expect(testUI.Out).To(Say("OK")) 287 Expect(testUI.Out).To(Say("TIP: No space targeted, use 'faceman target -s' to target a space.")) 288 289 Expect(testUI.Err).To(Say("warning-1")) 290 Expect(testUI.Err).To(Say("warning-2")) 291 292 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 293 }) 294 }) 295 }) 296 }) 297 }) 298 })