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