github.com/wanddynosios/cli@v7.1.0+incompatible/integration/v6/isolated/target_command_test.go (about)

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