github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/command/v2/target_command_test.go (about)

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