github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/target_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     5  	"code.cloudfoundry.org/cli/integration/helpers"
     6  	"code.cloudfoundry.org/cli/util/configv3"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/ginkgo/extensions/table"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("target command", func() {
    15  	var (
    16  		orgName   string
    17  		spaceName string
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		helpers.LoginCF()
    22  
    23  		orgName = helpers.NewOrgName()
    24  		spaceName = helpers.NewSpaceName()
    25  	})
    26  
    27  	Context("help", func() {
    28  		When("--help flag is set", func() {
    29  			It("appears in cf help -a", func() {
    30  				session := helpers.CF("help", "-a")
    31  				Eventually(session).Should(Exit(0))
    32  				Expect(session).To(HaveCommandInCategoryWithDescription("target", "GETTING STARTED", "Set or view the targeted org or space"))
    33  			})
    34  
    35  			It("displays help", func() {
    36  				session := helpers.CF("target", "--help")
    37  				Eventually(session).Should(Say("NAME:"))
    38  				Eventually(session).Should(Say("   target - Set or view the targeted org or space"))
    39  				Eventually(session).Should(Say("USAGE:"))
    40  				Eventually(session).Should(Say(`   cf target \[-o ORG\] \[-s SPACE\]`))
    41  				Eventually(session).Should(Say("ALIAS:"))
    42  				Eventually(session).Should(Say("   t"))
    43  				Eventually(session).Should(Say("OPTIONS:"))
    44  				Eventually(session).Should(Say("   -o      Organization"))
    45  				Eventually(session).Should(Say("   -s      Space"))
    46  				Eventually(session).Should(Say("SEE ALSO:"))
    47  				Eventually(session).Should(Say("   create-org, create-space, login, orgs, spaces"))
    48  				Eventually(session).Should(Exit(0))
    49  			})
    50  		})
    51  	})
    52  
    53  	When("both the access and refresh tokens are invalid", func() {
    54  		BeforeEach(func() {
    55  			helpers.SkipIfClientCredentialsTestMode()
    56  			helpers.SetConfig(func(conf *configv3.Config) {
    57  				conf.SetAccessToken("bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImtleS0xIiwidHlwIjoiSldUIn0.eyJqdGkiOiJlNzQyMjg1NjNjZjc0ZGQ0YTU5YTA1NTUyMWVlYzlhNCIsInN1YiI6IjhkN2IxZjRlLTJhNGQtNGQwNy1hYWE0LTdjOTVlZDFhN2YzNCIsInNjb3BlIjpbInJvdXRpbmcucm91dGVyX2dyb3Vwcy5yZWFkIiwiY2xvdWRfY29udHJvbGxlci5yZWFkIiwicGFzc3dvcmQud3JpdGUiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwib3BlbmlkIiwicm91dGluZy5yb3V0ZXJfZ3JvdXBzLndyaXRlIiwiZG9wcGxlci5maXJlaG9zZSIsInNjaW0ud3JpdGUiLCJzY2ltLnJlYWQiLCJjbG91ZF9jb250cm9sbGVyLmFkbWluIiwidWFhLnVzZXIiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImF6cCI6ImNmIiwiZ3JhbnRfdHlwZSI6InBhc3N3b3JkIiwidXNlcl9pZCI6IjhkN2IxZjRlLTJhNGQtNGQwNy1hYWE0LTdjOTVlZDFhN2YzNCIsIm9yaWdpbiI6InVhYSIsInVzZXJfbmFtZSI6ImFkbWluIiwiZW1haWwiOiJhZG1pbiIsInJldl9zaWciOiI2ZjZkM2Y1YyIsImlhdCI6MTQ4Njc2NDQxNywiZXhwIjoxNDg2NzY1MDE3LCJpc3MiOiJodHRwczovL3VhYS5ib3NoLWxpdGUuY29tL29hdXRoL3Rva2VuIiwiemlkIjoidWFhIiwiYXVkIjpbImNsb3VkX2NvbnRyb2xsZXIiLCJzY2ltIiwicGFzc3dvcmQiLCJjZiIsInVhYSIsIm9wZW5pZCIsImRvcHBsZXIiLCJyb3V0aW5nLnJvdXRlcl9ncm91cHMiXX0.AhQI_-u9VzkQ1Z7yzibq7dBWbb5ucTDtwaXjeCf4rakl7hJvQYWI1meO9PSUI8oVbArBgOu0aOU6mfzDE8dSyZ1qAD0mhL5_c2iLGXdqUaPlXrX9vxuJZh_8vMTlxAnJ02c6ixbWaPWujvEIuiLb-QWa0NTbR9RDNyw1MbOQkdQ")
    58  
    59  				conf.SetRefreshToken("bb8f7b209ff74409877974bce5752412-r")
    60  			})
    61  		})
    62  
    63  		It("tells the user to login and exits with 1", func() {
    64  			session := helpers.CF("target", "-o", "some-org", "-s", "some-space")
    65  			Eventually(session.Err).Should(Say("The token expired, was revoked, or the token ID is incorrect. Please log back in to re-authenticate."))
    66  			Eventually(session).Should(Say("FAILED"))
    67  			Eventually(session).Should(Exit(1))
    68  		})
    69  	})
    70  
    71  	When("the environment is not setup correctly", func() {
    72  		When("no API endpoint is set", func() {
    73  			BeforeEach(func() {
    74  				helpers.UnsetAPI()
    75  			})
    76  
    77  			It("fails with no API endpoint set message", func() {
    78  				session := helpers.CF("target", "-o", "some-org", "-s", "some-space")
    79  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    80  				Eventually(session).Should(Say("FAILED"))
    81  				Eventually(session).Should(Exit(1))
    82  			})
    83  		})
    84  
    85  		When("not logged in", func() {
    86  			DescribeTable("fails with not logged in message",
    87  				func(args ...string) {
    88  					helpers.LogoutCF()
    89  					cmd := append([]string{"target"}, args...)
    90  					session := helpers.CF(cmd...)
    91  					Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' or 'cf login --sso' to log in."))
    92  					Eventually(session).Should(Say("FAILED"))
    93  					Eventually(session).Should(Exit(1))
    94  				},
    95  
    96  				Entry("when trying to target an org", "-o", "some-org"),
    97  				Entry("when trying to target a space", "-s", "some-space"),
    98  				Entry("when trying to target an org and space", "-o", "some-org", "-s", "some-space"),
    99  				Entry("when trying to get the target"),
   100  			)
   101  		})
   102  	})
   103  
   104  	When("no arguments are provided", func() {
   105  		When("*no* org and space are targeted", func() {
   106  			It("displays current target information", func() {
   107  				username, _ := helpers.GetCredentials()
   108  				session := helpers.CF("target")
   109  				Eventually(session).Should(Say(`API endpoint:\s+%s`, apiURL))
   110  				Eventually(session).Should(Say(`API version:\s+3.[\d.]+`))
   111  				Eventually(session).Should(Say(`user:\s+%s`, username))
   112  				Eventually(session).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   113  				Eventually(session).Should(Exit(0))
   114  			})
   115  		})
   116  
   117  		When("targeted to an org and space", func() {
   118  			BeforeEach(func() {
   119  				helpers.LoginCF()
   120  				helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
   121  			})
   122  
   123  			It("displays current target information", func() {
   124  				username, _ := helpers.GetCredentials()
   125  				session := helpers.CF("target")
   126  				Eventually(session).Should(Say(`API endpoint:\s+%s`, apiURL))
   127  				Eventually(session).Should(Say(`API version:\s+3.[\d.]+`))
   128  				Eventually(session).Should(Say(`user:\s+%s`, username))
   129  				Eventually(session).Should(Say(`org:\s+%s`, ReadOnlyOrg))
   130  				Eventually(session).Should(Say(`space:\s+%s`, ReadOnlySpace))
   131  				Eventually(session).Should(Exit(0))
   132  			})
   133  		})
   134  	})
   135  
   136  	When("only an org argument is provided", func() {
   137  		When("the org does not exist", func() {
   138  			// We set targets to verify that the target command
   139  			// preserves existing targets in failure
   140  			BeforeEach(func() {
   141  				helpers.LoginCF()
   142  				helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
   143  			})
   144  
   145  			It("displays org not found, exits 1, and clears existing targets", func() {
   146  				session := helpers.CF("target", "-o", orgName)
   147  				Eventually(session.Err).Should(Say("Organization '%s' not found", orgName))
   148  				Eventually(session).Should(Say("FAILED"))
   149  				Eventually(session).Should(Exit(1))
   150  
   151  				session = helpers.CF("target")
   152  				Eventually(session).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   153  				Eventually(session).Should(Exit(0))
   154  			})
   155  		})
   156  
   157  		When("the org exists", func() {
   158  			BeforeEach(func() {
   159  				helpers.CreateOrg(orgName)
   160  				helpers.TargetOrg(orgName)
   161  			})
   162  
   163  			AfterEach(func() {
   164  				helpers.QuickDeleteOrg(orgName)
   165  			})
   166  
   167  			When("there are no spaces in the org", func() {
   168  				BeforeEach(func() {
   169  					helpers.ClearTarget()
   170  				})
   171  
   172  				It("only targets the org and exits 0", func() {
   173  					username, _ := helpers.GetCredentials()
   174  					session := helpers.CF("target", "-o", orgName)
   175  					Eventually(session).Should(Say(`API endpoint:\s+%s`, apiURL))
   176  					Eventually(session).Should(Say(`API version:\s+3.[\d.]+`))
   177  					Eventually(session).Should(Say(`user:\s+%s`, username))
   178  					Eventually(session).Should(Say(`org:\s+%s`, orgName))
   179  					Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE"))
   180  					Eventually(session).Should(Exit(0))
   181  				})
   182  			})
   183  
   184  			When("there is only one space in the org", func() {
   185  				BeforeEach(func() {
   186  					helpers.CreateSpace(spaceName)
   187  					helpers.ClearTarget()
   188  				})
   189  
   190  				It("targets the org and space and exits 0", func() {
   191  					username, _ := helpers.GetCredentials()
   192  					session := helpers.CF("target", "-o", orgName)
   193  					Eventually(session).Should(Say(`API endpoint:\s+%s`, apiURL))
   194  					Eventually(session).Should(Say(`API version:\s+3.[\d.]+`))
   195  					Eventually(session).Should(Say(`user:\s+%s`, username))
   196  					Eventually(session).Should(Say(`org:\s+%s`, orgName))
   197  					Eventually(session).Should(Say(`space:\s+%s`, spaceName))
   198  					Eventually(session).Should(Exit(0))
   199  				})
   200  			})
   201  
   202  			When("there are multiple spaces in the org", func() {
   203  				BeforeEach(func() {
   204  					helpers.CreateSpace(spaceName)
   205  					helpers.CreateSpace(helpers.NewSpaceName())
   206  					helpers.ClearTarget()
   207  				})
   208  
   209  				It("targets the org only and exits 0", func() {
   210  					username, _ := helpers.GetCredentials()
   211  					session := helpers.CF("target", "-o", orgName)
   212  					Eventually(session).Should(Say(`API endpoint:\s+%s`, apiURL))
   213  					Eventually(session).Should(Say(`API version:\s+3.[\d.]+`))
   214  					Eventually(session).Should(Say(`user:\s+%s`, username))
   215  					Eventually(session).Should(Say(`org:\s+%s`, orgName))
   216  					Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE"))
   217  					Eventually(session).Should(Exit(0))
   218  				})
   219  
   220  				When("there is an existing targeted space", func() {
   221  					BeforeEach(func() {
   222  						session := helpers.CF("target", "-o", orgName, "-s", spaceName)
   223  						Eventually(session).Should(Exit(0))
   224  					})
   225  
   226  					It("unsets the targeted space", func() {
   227  						session := helpers.CF("target", "-o", orgName)
   228  						Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE"))
   229  						Eventually(session).Should(Exit(0))
   230  					})
   231  				})
   232  			})
   233  		})
   234  	})
   235  
   236  	When("only a space argument is provided", func() {
   237  		When("there is an existing targeted org", func() {
   238  			BeforeEach(func() {
   239  				helpers.LoginCF()
   240  				Eventually(helpers.CF("target", "-o", ReadOnlyOrg)).Should(Exit(0))
   241  			})
   242  
   243  			When("the space exists", func() {
   244  				It("targets the space and exits 0", func() {
   245  					username, _ := helpers.GetCredentials()
   246  					session := helpers.CF("target", "-s", ReadOnlySpace)
   247  					Eventually(session).Should(Say(`API endpoint:\s+%s`, apiURL))
   248  					Eventually(session).Should(Say(`API version:\s+3.[\d.]+`))
   249  					Eventually(session).Should(Say(`user:\s+%s`, username))
   250  					Eventually(session).Should(Say(`org:\s+%s`, ReadOnlyOrg))
   251  					Eventually(session).Should(Say(`space:\s+%s`, ReadOnlySpace))
   252  					Eventually(session).Should(Exit(0))
   253  				})
   254  			})
   255  
   256  			When("the space does not exist", func() {
   257  				It("displays space not found, exits 1, and clears existing targeted space", func() {
   258  					session := helpers.CF("target", "-s", spaceName)
   259  					Eventually(session.Err).Should(Say("Space '%s' not found.", spaceName))
   260  					Eventually(session).Should(Say("FAILED"))
   261  					Eventually(session).Should(Exit(1))
   262  
   263  					session = helpers.CF("target")
   264  					Eventually(session).Should(Say(`org:\s+%s`, ReadOnlyOrg))
   265  					Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE'"))
   266  					Eventually(session).Should(Exit(0))
   267  				})
   268  			})
   269  		})
   270  
   271  		When("there is not an existing targeted org", func() {
   272  			It("displays org must be targeted first and exits 1", func() {
   273  				session := helpers.CF("target", "-s", spaceName)
   274  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
   275  				Eventually(session).Should(Say("FAILED"))
   276  				Eventually(session).Should(Exit(1))
   277  			})
   278  		})
   279  	})
   280  
   281  	When("both org and space arguments are provided", func() {
   282  		// We set the targets to verify that the target command preserves existing targets
   283  		// in failure
   284  		BeforeEach(func() {
   285  			helpers.LoginCF()
   286  			helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
   287  		})
   288  
   289  		When("the org does not exist", func() {
   290  			It("displays org not found, exits 1, and clears existing targets", func() {
   291  				session := helpers.CF("target", "-o", orgName, "-s", spaceName)
   292  				Eventually(session.Err).Should(Say("Organization '%s' not found", orgName))
   293  				Eventually(session).Should(Say("FAILED"))
   294  				Eventually(session).Should(Exit(1))
   295  
   296  				session = helpers.CF("target")
   297  				Eventually(session).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   298  				Eventually(session).Should(Exit(0))
   299  			})
   300  		})
   301  
   302  		When("the org exists", func() {
   303  			BeforeEach(func() {
   304  				helpers.CreateOrg(orgName)
   305  			})
   306  
   307  			AfterEach(func() {
   308  				helpers.QuickDeleteOrg(orgName)
   309  			})
   310  
   311  			When("the space exists", func() {
   312  				BeforeEach(func() {
   313  					helpers.TargetOrg(orgName)
   314  					helpers.CreateSpace(spaceName)
   315  					helpers.ClearTarget()
   316  				})
   317  
   318  				It("targets the org and space and exits 0", func() {
   319  					username, _ := helpers.GetCredentials()
   320  					session := helpers.CF("target", "-o", orgName, "-s", spaceName)
   321  					Eventually(session).Should(Say(`API endpoint:\s+%s`, apiURL))
   322  					Eventually(session).Should(Say(`API version:\s+3.[\d.]+`))
   323  					Eventually(session).Should(Say(`user:\s+%s`, username))
   324  					Eventually(session).Should(Say(`org:\s+%s`, orgName))
   325  					Eventually(session).Should(Say(`space:\s+%s`, spaceName))
   326  					Eventually(session).Should(Exit(0))
   327  				})
   328  			})
   329  
   330  			When("the space does not exist", func() {
   331  				It("displays space not found, exits 1, and clears the existing targets", func() {
   332  					session := helpers.CF("target", "-o", orgName, "-s", spaceName)
   333  					Eventually(session.Err).Should(Say("Space '%s' not found.", spaceName))
   334  					Eventually(session).Should(Say("FAILED"))
   335  					Eventually(session).Should(Exit(1))
   336  
   337  					session = helpers.CF("target")
   338  					Eventually(session).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   339  					Eventually(session).Should(Exit(0))
   340  				})
   341  			})
   342  		})
   343  	})
   344  })