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