github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/target_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/translatableerror"
    10  	. "code.cloudfoundry.org/cli/command/v6"
    11  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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("target Command", func() {
    20  	var (
    21  		cmd             TargetCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v6fakes.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(v6fakes.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  		fakeActor.CloudControllerAPIVersionReturns(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  	When("a cloud controller API endpoint is set", func() {
    59  		BeforeEach(func() {
    60  			fakeConfig.TargetReturns("some-api-target")
    61  		})
    62  
    63  		When("checking the cloud controller minimum version warning", func() {
    64  			var binaryVersion string
    65  
    66  			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  			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  			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  			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  		When("checking target fails", func() {
   110  			BeforeEach(func() {
   111  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
   112  			})
   113  
   114  			It("returns an error", func() {
   115  				Expect(executeErr).To(MatchError(actionerror.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.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0))
   123  				Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(0))
   124  			})
   125  		})
   126  
   127  		When("the user is logged in", func() {
   128  			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.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0))
   140  					Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(0))
   141  				})
   142  			})
   143  
   144  			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  				When("no arguments are provided", func() {
   152  					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  					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  					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  				When("space is provided", func() {
   210  					BeforeEach(func() {
   211  						cmd.Space = "some-space"
   212  					})
   213  
   214  					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  						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  						When("the space does not exist", func() {
   244  							BeforeEach(func() {
   245  								fakeActor.GetSpaceByOrganizationAndNameReturns(
   246  									v2action.Space{},
   247  									v2action.Warnings{},
   248  									actionerror.SpaceNotFoundError{Name: "some-space"})
   249  							})
   250  
   251  							It("returns a SpaceNotFoundError and clears existing space", func() {
   252  								Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"}))
   253  
   254  								Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0))
   255  								Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0))
   256  							})
   257  						})
   258  					})
   259  
   260  					When("no org is targeted", func() {
   261  						It("returns NoOrgTargeted error and clears existing space", func() {
   262  							Expect(executeErr).To(MatchError(translatableerror.NoOrganizationTargetedError{BinaryName: "faceman"}))
   263  
   264  							Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0))
   265  							Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0))
   266  						})
   267  					})
   268  				})
   269  
   270  				When("org is provided", func() {
   271  					BeforeEach(func() {
   272  						cmd.Organization = "some-org"
   273  					})
   274  
   275  					When("the org does not exist", func() {
   276  						BeforeEach(func() {
   277  							fakeActor.GetOrganizationByNameReturns(
   278  								v2action.Organization{},
   279  								nil,
   280  								actionerror.OrganizationNotFoundError{Name: "some-org"})
   281  						})
   282  
   283  						It("displays all warnings,returns an org target error, and clears existing targets", func() {
   284  							Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   285  
   286  							Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(0))
   287  							Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1))
   288  						})
   289  					})
   290  
   291  					When("the org exists", func() {
   292  						BeforeEach(func() {
   293  							fakeConfig.HasTargetedOrganizationReturns(true)
   294  							fakeConfig.TargetedOrganizationReturns(configv3.Organization{
   295  								GUID: "some-org-guid",
   296  								Name: "some-org",
   297  							})
   298  							fakeActor.GetOrganizationByNameReturns(
   299  								v2action.Organization{GUID: "some-org-guid"},
   300  								v2action.Warnings{"warning-1", "warning-2"},
   301  								nil)
   302  						})
   303  
   304  						When("getting the organization's spaces returns an error", func() {
   305  							var err error
   306  
   307  							BeforeEach(func() {
   308  								err = errors.New("get-org-spaces-error")
   309  								fakeActor.GetOrganizationSpacesReturns(
   310  									[]v2action.Space{},
   311  									v2action.Warnings{
   312  										"warning-3",
   313  									},
   314  									err)
   315  							})
   316  
   317  							It("displays all warnings, returns a get org spaces error and clears existing targets", func() {
   318  								Expect(executeErr).To(MatchError(err))
   319  
   320  								Expect(fakeActor.GetOrganizationSpacesCallCount()).To(Equal(1))
   321  								orgGUID := fakeActor.GetOrganizationSpacesArgsForCall(0)
   322  								Expect(orgGUID).To(Equal("some-org-guid"))
   323  
   324  								Expect(testUI.Err).To(Say("warning-1"))
   325  								Expect(testUI.Err).To(Say("warning-2"))
   326  								Expect(testUI.Err).To(Say("warning-3"))
   327  
   328  								Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1))
   329  								orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0)
   330  								Expect(orgGUID).To(Equal("some-org-guid"))
   331  								Expect(orgName).To(Equal("some-org"))
   332  								Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0))
   333  
   334  								Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1))
   335  							})
   336  						})
   337  
   338  						When("there are no spaces in the targeted org", func() {
   339  							It("displays all warnings", func() {
   340  								Expect(executeErr).ToNot(HaveOccurred())
   341  
   342  								Expect(testUI.Err).To(Say("warning-1"))
   343  								Expect(testUI.Err).To(Say("warning-2"))
   344  							})
   345  
   346  							It("sets the org and unsets the space in the config", func() {
   347  								Expect(executeErr).ToNot(HaveOccurred())
   348  
   349  								Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1))
   350  								orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0)
   351  								Expect(orgGUID).To(Equal("some-org-guid"))
   352  								Expect(orgName).To(Equal("some-org"))
   353  
   354  								Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1))
   355  								Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0))
   356  							})
   357  						})
   358  
   359  						When("there is only 1 space in the targeted org", func() {
   360  							BeforeEach(func() {
   361  								fakeActor.GetOrganizationSpacesReturns(
   362  									[]v2action.Space{{
   363  										GUID:     "some-space-guid",
   364  										Name:     "some-space",
   365  										AllowSSH: true,
   366  									}},
   367  									v2action.Warnings{"warning-3"},
   368  									nil,
   369  								)
   370  							})
   371  
   372  							It("displays all warnings", func() {
   373  								Expect(executeErr).ToNot(HaveOccurred())
   374  
   375  								Expect(testUI.Err).To(Say("warning-1"))
   376  								Expect(testUI.Err).To(Say("warning-2"))
   377  								Expect(testUI.Err).To(Say("warning-3"))
   378  							})
   379  
   380  							It("targets the org and space", func() {
   381  								Expect(executeErr).ToNot(HaveOccurred())
   382  
   383  								Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1))
   384  								orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0)
   385  								Expect(orgGUID).To(Equal("some-org-guid"))
   386  								Expect(orgName).To(Equal("some-org"))
   387  
   388  								Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1))
   389  
   390  								Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(1))
   391  								spaceGUID, spaceName, spaceAllowSSH := fakeConfig.SetSpaceInformationArgsForCall(0)
   392  								Expect(spaceGUID).To(Equal("some-space-guid"))
   393  								Expect(spaceName).To(Equal("some-space"))
   394  								Expect(spaceAllowSSH).To(Equal(true))
   395  							})
   396  						})
   397  
   398  						When("there are multiple spaces in the targeted org", func() {
   399  							BeforeEach(func() {
   400  								fakeActor.GetOrganizationSpacesReturns(
   401  									[]v2action.Space{
   402  										{
   403  											GUID:     "some-space-guid",
   404  											Name:     "some-space",
   405  											AllowSSH: true,
   406  										},
   407  										{
   408  											GUID:     "another-space-space-guid",
   409  											Name:     "another-space",
   410  											AllowSSH: true,
   411  										},
   412  									},
   413  									v2action.Warnings{"warning-3"},
   414  									nil,
   415  								)
   416  							})
   417  
   418  							It("displays all warnings, sets the org, and clears the existing targetted space from the config", func() {
   419  								Expect(executeErr).ToNot(HaveOccurred())
   420  
   421  								Expect(testUI.Err).To(Say("warning-1"))
   422  								Expect(testUI.Err).To(Say("warning-2"))
   423  
   424  								Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1))
   425  								orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0)
   426  								Expect(orgGUID).To(Equal("some-org-guid"))
   427  								Expect(orgName).To(Equal("some-org"))
   428  
   429  								Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1))
   430  								Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0))
   431  							})
   432  						})
   433  
   434  						When("getting the spaces in org returns an error", func() {
   435  							var err error
   436  
   437  							BeforeEach(func() {
   438  								err = errors.New("get-org-spaces-error")
   439  								fakeActor.GetOrganizationSpacesReturns(
   440  									[]v2action.Space{},
   441  									v2action.Warnings{
   442  										"warning-3",
   443  									},
   444  									err)
   445  							})
   446  
   447  							It("displays all warnings, returns the error, and clears existing targets", func() {
   448  								Expect(executeErr).To(MatchError(err))
   449  
   450  								Expect(testUI.Err).To(Say("warning-1"))
   451  								Expect(testUI.Err).To(Say("warning-2"))
   452  								Expect(testUI.Err).To(Say("warning-3"))
   453  
   454  								Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1))
   455  							})
   456  						})
   457  					})
   458  				})
   459  
   460  				When("org and space arguments are provided", func() {
   461  					BeforeEach(func() {
   462  						cmd.Space = "some-space"
   463  						cmd.Organization = "some-org"
   464  					})
   465  
   466  					When("the org exists", func() {
   467  						BeforeEach(func() {
   468  							fakeActor.GetOrganizationByNameReturns(
   469  								v2action.Organization{
   470  									GUID: "some-org-guid",
   471  									Name: "some-org",
   472  								},
   473  								v2action.Warnings{
   474  									"warning-1",
   475  								},
   476  								nil)
   477  						})
   478  
   479  						When("the space exists", func() {
   480  							BeforeEach(func() {
   481  								fakeActor.GetSpaceByOrganizationAndNameReturns(
   482  									v2action.Space{
   483  										GUID: "some-space-guid",
   484  										Name: "some-space",
   485  									},
   486  									v2action.Warnings{
   487  										"warning-2",
   488  									},
   489  									nil)
   490  							})
   491  
   492  							It("sets the target org and space", func() {
   493  								Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1))
   494  								orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0)
   495  								Expect(orgGUID).To(Equal("some-org-guid"))
   496  								Expect(orgName).To(Equal("some-org"))
   497  
   498  								Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(1))
   499  								spaceGUID, spaceName, allowSSH := fakeConfig.SetSpaceInformationArgsForCall(0)
   500  								Expect(spaceGUID).To(Equal("some-space-guid"))
   501  								Expect(spaceName).To(Equal("some-space"))
   502  								Expect(allowSSH).To(BeFalse())
   503  							})
   504  
   505  							It("displays all warnings", func() {
   506  								Expect(testUI.Err).To(Say("warning-1"))
   507  								Expect(testUI.Err).To(Say("warning-2"))
   508  							})
   509  						})
   510  
   511  						When("the space does not exist", func() {
   512  							BeforeEach(func() {
   513  								fakeActor.GetSpaceByOrganizationAndNameReturns(
   514  									v2action.Space{},
   515  									nil,
   516  									actionerror.SpaceNotFoundError{Name: "some-space"})
   517  							})
   518  
   519  							It("returns an error and clears existing targets", func() {
   520  								Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"}))
   521  
   522  								Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(0))
   523  								Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0))
   524  
   525  								Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1))
   526  							})
   527  						})
   528  					})
   529  
   530  					When("the org does not exist", func() {
   531  						BeforeEach(func() {
   532  							fakeActor.GetOrganizationByNameReturns(
   533  								v2action.Organization{},
   534  								nil,
   535  								actionerror.OrganizationNotFoundError{Name: "some-org"})
   536  						})
   537  
   538  						It("returns an error and clears existing targets", func() {
   539  							Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   540  
   541  							Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(0))
   542  							Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0))
   543  
   544  							Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1))
   545  						})
   546  					})
   547  				})
   548  			})
   549  		})
   550  	})
   551  })