github.com/loafoe/cli@v7.1.0+incompatible/command/v7/target_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/v7action" 7 v7 "code.cloudfoundry.org/cli/command/v7" 8 "code.cloudfoundry.org/cli/command/v7/v7fakes" 9 "code.cloudfoundry.org/cli/resources" 10 11 "code.cloudfoundry.org/cli/actor/actionerror" 12 "code.cloudfoundry.org/cli/command/commandfakes" 13 "code.cloudfoundry.org/cli/command/translatableerror" 14 "code.cloudfoundry.org/cli/util/configv3" 15 "code.cloudfoundry.org/cli/util/ui" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("target Command", func() { 22 var ( 23 cmd v7.TargetCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v7fakes.FakeActor 28 binaryName string 29 apiVersion string 30 minCLIVersion string 31 executeErr error 32 ) 33 34 BeforeEach(func() { 35 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v7fakes.FakeActor) 39 40 cmd = v7.TargetCommand{ 41 BaseCommand: v7.BaseCommand{ 42 UI: testUI, 43 Config: fakeConfig, 44 SharedActor: fakeSharedActor, 45 Actor: fakeActor, 46 }, 47 } 48 49 binaryName = "faceman" 50 fakeConfig.BinaryNameReturns(binaryName) 51 apiVersion = "1.2.3" 52 fakeActor.CloudControllerAPIVersionReturns(apiVersion) 53 minCLIVersion = "1.0.0" 54 fakeConfig.MinCLIVersionReturns(minCLIVersion) 55 fakeConfig.BinaryVersionReturns("1.0.0") 56 }) 57 58 JustBeforeEach(func() { 59 executeErr = cmd.Execute(nil) 60 }) 61 62 When("a cloud controller API endpoint is set", func() { 63 BeforeEach(func() { 64 fakeConfig.TargetReturns("some-api-target") 65 }) 66 67 When("checking target fails", func() { 68 BeforeEach(func() { 69 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 70 }) 71 72 It("returns an error", func() { 73 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 74 75 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 76 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 77 Expect(checkTargetedOrg).To(BeFalse()) 78 Expect(checkTargetedSpace).To(BeFalse()) 79 80 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 81 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(0)) 82 }) 83 }) 84 85 When("the user is logged in", func() { 86 When("getting the current user returns an error", func() { 87 var someErr error 88 89 BeforeEach(func() { 90 someErr = errors.New("some-current-user-error") 91 fakeConfig.CurrentUserReturns(configv3.User{}, someErr) 92 }) 93 94 It("returns the same error", func() { 95 Expect(executeErr).To(MatchError(someErr)) 96 97 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 98 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(0)) 99 }) 100 }) 101 102 When("getting the current user does not return an error", func() { 103 BeforeEach(func() { 104 fakeConfig.CurrentUserReturns( 105 configv3.User{Name: "some-user"}, 106 nil) 107 }) 108 109 When("no arguments are provided", func() { 110 When("no org or space are targeted", func() { 111 It("displays how to target an org and space", func() { 112 Expect(executeErr).ToNot(HaveOccurred()) 113 114 Expect(testUI.Out).To(Say("API endpoint: some-api-target")) 115 Expect(testUI.Out).To(Say("API version: 1.2.3")) 116 Expect(testUI.Out).To(Say("user: some-user")) 117 Expect(testUI.Out).To(Say("No org or space targeted, use '%s target -o ORG -s SPACE'", binaryName)) 118 }) 119 }) 120 121 When("an org but no space is targeted", func() { 122 BeforeEach(func() { 123 fakeConfig.HasTargetedOrganizationReturns(true) 124 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 125 GUID: "some-org-guid", 126 Name: "some-org", 127 }) 128 }) 129 130 It("displays the org and tip to target space", func() { 131 Expect(executeErr).ToNot(HaveOccurred()) 132 133 Expect(testUI.Out).To(Say("API endpoint: some-api-target")) 134 Expect(testUI.Out).To(Say("API version: 1.2.3")) 135 Expect(testUI.Out).To(Say("user: some-user")) 136 Expect(testUI.Out).To(Say("org: some-org")) 137 Expect(testUI.Out).To(Say("No space targeted, use '%s target -s SPACE'", binaryName)) 138 }) 139 }) 140 141 When("an org and space are targeted", func() { 142 BeforeEach(func() { 143 fakeConfig.HasTargetedOrganizationReturns(true) 144 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 145 GUID: "some-org-guid", 146 Name: "some-org", 147 }) 148 fakeConfig.HasTargetedSpaceReturns(true) 149 fakeConfig.TargetedSpaceReturns(configv3.Space{ 150 GUID: "some-space-guid", 151 Name: "some-space", 152 }) 153 }) 154 155 It("displays the org and space targeted ", func() { 156 Expect(executeErr).ToNot(HaveOccurred()) 157 158 Expect(testUI.Out).To(Say("API endpoint: some-api-target")) 159 Expect(testUI.Out).To(Say("API version: 1.2.3")) 160 Expect(testUI.Out).To(Say("user: some-user")) 161 Expect(testUI.Out).To(Say("org: some-org")) 162 Expect(testUI.Out).To(Say("space: some-space")) 163 }) 164 }) 165 }) 166 167 When("space is provided", func() { 168 BeforeEach(func() { 169 cmd.Space = "some-space" 170 }) 171 172 When("an org is already targeted", func() { 173 BeforeEach(func() { 174 fakeConfig.HasTargetedOrganizationReturns(true) 175 fakeConfig.TargetedOrganizationReturns(configv3.Organization{GUID: "some-org-guid"}) 176 }) 177 178 When("the space exists", func() { 179 BeforeEach(func() { 180 fakeActor.GetSpaceByNameAndOrganizationReturns( 181 resources.Space{ 182 GUID: "some-space-guid", 183 Name: "some-space", 184 }, 185 v7action.Warnings{}, 186 nil) 187 }) 188 189 It("targets the space", func() { 190 Expect(executeErr).ToNot(HaveOccurred()) 191 192 Expect(fakeConfig.V7SetSpaceInformationCallCount()).To(Equal(1)) 193 spaceGUID, spaceName := fakeConfig.V7SetSpaceInformationArgsForCall(0) 194 Expect(spaceGUID).To(Equal("some-space-guid")) 195 Expect(spaceName).To(Equal("some-space")) 196 }) 197 }) 198 199 When("the space does not exist", func() { 200 BeforeEach(func() { 201 fakeActor.GetSpaceByNameAndOrganizationReturns( 202 resources.Space{}, 203 v7action.Warnings{}, 204 actionerror.SpaceNotFoundError{Name: "some-space"}) 205 }) 206 207 It("returns a SpaceNotFoundError and clears existing space", func() { 208 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"})) 209 210 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 211 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 212 }) 213 }) 214 }) 215 216 When("no org is targeted", func() { 217 It("returns NoOrgTargeted error and clears existing space", func() { 218 Expect(executeErr).To(MatchError(translatableerror.NoOrganizationTargetedError{BinaryName: "faceman"})) 219 220 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 221 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 222 }) 223 }) 224 }) 225 226 When("org is provided", func() { 227 BeforeEach(func() { 228 cmd.Organization = "some-org" 229 }) 230 231 When("the org does not exist", func() { 232 BeforeEach(func() { 233 fakeActor.GetOrganizationByNameReturns( 234 resources.Organization{}, 235 nil, 236 actionerror.OrganizationNotFoundError{Name: "some-org"}) 237 }) 238 239 It("displays all warnings,returns an org target error, and clears existing targets", func() { 240 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"})) 241 242 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(0)) 243 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 244 }) 245 }) 246 247 When("the org exists", func() { 248 BeforeEach(func() { 249 fakeConfig.HasTargetedOrganizationReturns(true) 250 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 251 GUID: "some-org-guid", 252 Name: "some-org", 253 }) 254 fakeActor.GetOrganizationByNameReturns( 255 resources.Organization{GUID: "some-org-guid"}, 256 v7action.Warnings{"warning-1", "warning-2"}, 257 nil) 258 }) 259 260 When("getting the organization's spaces returns an error", func() { 261 var err error 262 263 BeforeEach(func() { 264 err = errors.New("get-org-spaces-error") 265 fakeActor.GetOrganizationSpacesReturns( 266 []resources.Space{}, 267 v7action.Warnings{ 268 "warning-3", 269 }, 270 err) 271 }) 272 273 It("displays all warnings, returns a get org spaces error and clears existing targets", func() { 274 Expect(executeErr).To(MatchError(err)) 275 276 Expect(fakeActor.GetOrganizationSpacesCallCount()).To(Equal(1)) 277 orgGUID := fakeActor.GetOrganizationSpacesArgsForCall(0) 278 Expect(orgGUID).To(Equal("some-org-guid")) 279 280 Expect(testUI.Err).To(Say("warning-1")) 281 Expect(testUI.Err).To(Say("warning-2")) 282 Expect(testUI.Err).To(Say("warning-3")) 283 284 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 285 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 286 Expect(orgGUID).To(Equal("some-org-guid")) 287 Expect(orgName).To(Equal("some-org")) 288 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 289 290 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 291 }) 292 }) 293 294 When("there are no spaces in the targeted org", func() { 295 It("displays all warnings", func() { 296 Expect(executeErr).ToNot(HaveOccurred()) 297 298 Expect(testUI.Err).To(Say("warning-1")) 299 Expect(testUI.Err).To(Say("warning-2")) 300 }) 301 302 It("sets the org and unsets the space in the config", func() { 303 Expect(executeErr).ToNot(HaveOccurred()) 304 305 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 306 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 307 Expect(orgGUID).To(Equal("some-org-guid")) 308 Expect(orgName).To(Equal("some-org")) 309 310 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 311 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 312 }) 313 }) 314 315 When("there is only 1 space in the targeted org", func() { 316 BeforeEach(func() { 317 fakeActor.GetOrganizationSpacesReturns( 318 []resources.Space{{ 319 GUID: "some-space-guid", 320 Name: "some-space", 321 }}, 322 v7action.Warnings{"warning-3"}, 323 nil, 324 ) 325 }) 326 327 It("displays all warnings", func() { 328 Expect(executeErr).ToNot(HaveOccurred()) 329 330 Expect(testUI.Err).To(Say("warning-1")) 331 Expect(testUI.Err).To(Say("warning-2")) 332 Expect(testUI.Err).To(Say("warning-3")) 333 }) 334 335 It("targets the org and space", func() { 336 Expect(executeErr).ToNot(HaveOccurred()) 337 338 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 339 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 340 Expect(orgGUID).To(Equal("some-org-guid")) 341 Expect(orgName).To(Equal("some-org")) 342 343 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 344 345 Expect(fakeConfig.V7SetSpaceInformationCallCount()).To(Equal(1)) 346 spaceGUID, spaceName := fakeConfig.V7SetSpaceInformationArgsForCall(0) 347 Expect(spaceGUID).To(Equal("some-space-guid")) 348 Expect(spaceName).To(Equal("some-space")) 349 }) 350 }) 351 352 When("there are multiple spaces in the targeted org", func() { 353 BeforeEach(func() { 354 fakeActor.GetOrganizationSpacesReturns( 355 []resources.Space{ 356 { 357 GUID: "some-space-guid", 358 Name: "some-space", 359 }, 360 { 361 GUID: "another-space-space-guid", 362 Name: "another-space", 363 }, 364 }, 365 v7action.Warnings{"warning-3"}, 366 nil, 367 ) 368 }) 369 370 It("displays all warnings, sets the org, and clears the existing targetted space from the config", func() { 371 Expect(executeErr).ToNot(HaveOccurred()) 372 373 Expect(testUI.Err).To(Say("warning-1")) 374 Expect(testUI.Err).To(Say("warning-2")) 375 376 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 377 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 378 Expect(orgGUID).To(Equal("some-org-guid")) 379 Expect(orgName).To(Equal("some-org")) 380 381 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 382 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 383 }) 384 }) 385 386 When("getting the spaces in org returns an error", func() { 387 var err error 388 389 BeforeEach(func() { 390 err = errors.New("get-org-spaces-error") 391 fakeActor.GetOrganizationSpacesReturns( 392 []resources.Space{}, 393 v7action.Warnings{ 394 "warning-3", 395 }, 396 err) 397 }) 398 399 It("displays all warnings, returns the error, and clears existing targets", func() { 400 Expect(executeErr).To(MatchError(err)) 401 402 Expect(testUI.Err).To(Say("warning-1")) 403 Expect(testUI.Err).To(Say("warning-2")) 404 Expect(testUI.Err).To(Say("warning-3")) 405 406 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 407 }) 408 }) 409 }) 410 }) 411 412 When("org and space arguments are provided", func() { 413 BeforeEach(func() { 414 cmd.Space = "some-space" 415 cmd.Organization = "some-org" 416 }) 417 418 When("the org exists", func() { 419 BeforeEach(func() { 420 fakeActor.GetOrganizationByNameReturns( 421 resources.Organization{ 422 GUID: "some-org-guid", 423 Name: "some-org", 424 }, 425 v7action.Warnings{ 426 "warning-1", 427 }, 428 nil) 429 430 fakeConfig.HasTargetedOrganizationReturns(true) 431 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 432 GUID: "some-org-guid", 433 Name: "some-org", 434 }) 435 436 }) 437 438 When("the space exists", func() { 439 BeforeEach(func() { 440 fakeActor.GetSpaceByNameAndOrganizationReturns( 441 resources.Space{ 442 GUID: "some-space-guid", 443 Name: "some-space", 444 }, 445 v7action.Warnings{ 446 "warning-2", 447 }, 448 nil) 449 }) 450 451 It("sets the target org and space", func() { 452 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 453 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 454 Expect(orgGUID).To(Equal("some-org-guid")) 455 Expect(orgName).To(Equal("some-org")) 456 457 Expect(fakeConfig.V7SetSpaceInformationCallCount()).To(Equal(1)) 458 spaceGUID, spaceName := fakeConfig.V7SetSpaceInformationArgsForCall(0) 459 Expect(spaceGUID).To(Equal("some-space-guid")) 460 Expect(spaceName).To(Equal("some-space")) 461 }) 462 463 It("displays all warnings", func() { 464 Expect(testUI.Err).To(Say("warning-1")) 465 Expect(testUI.Err).To(Say("warning-2")) 466 }) 467 }) 468 469 When("the space does not exist", func() { 470 BeforeEach(func() { 471 fakeActor.GetSpaceByNameAndOrganizationReturns( 472 resources.Space{}, 473 nil, 474 actionerror.SpaceNotFoundError{Name: "some-space"}) 475 }) 476 477 It("returns an error and clears existing targets", func() { 478 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"})) 479 480 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 481 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 482 483 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 484 }) 485 }) 486 }) 487 488 When("the org does not exist", func() { 489 BeforeEach(func() { 490 fakeActor.GetOrganizationByNameReturns( 491 resources.Organization{}, 492 nil, 493 actionerror.OrganizationNotFoundError{Name: "some-org"}) 494 }) 495 496 It("returns an error and clears existing targets", func() { 497 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"})) 498 499 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(0)) 500 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 501 502 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 503 }) 504 }) 505 }) 506 }) 507 }) 508 }) 509 })