github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/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  	Context("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  	Context("when the environment is not setup correctly", func() {
    62  		Context("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  		Context("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  	Context("when no arguments are provided", func() {
    95  		Context("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+[\\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  		Context("when targeted to an org and space", func() {
   108  			BeforeEach(func() {
   109  				setupCF(ReadOnlyOrg, ReadOnlySpace)
   110  			})
   111  
   112  			It("displays current target information", func() {
   113  				username, _ := helpers.GetCredentials()
   114  				session := helpers.CF("target")
   115  				Eventually(session).Should(Say("api endpoint:\\s+%s", apiURL))
   116  				Eventually(session).Should(Say("api version:\\s+[\\d.]+"))
   117  				Eventually(session).Should(Say("user:\\s+%s", username))
   118  				Eventually(session).Should(Say("org:\\s+%s", ReadOnlyOrg))
   119  				Eventually(session).Should(Say("space:\\s+%s", ReadOnlySpace))
   120  				Eventually(session).Should(Exit(0))
   121  			})
   122  		})
   123  	})
   124  
   125  	Context("when only an org argument is provided", func() {
   126  		Context("when the org does not exist", func() {
   127  			// We set targets to verify that the target command
   128  			// preserves existing targets in failure
   129  			BeforeEach(func() {
   130  				setupCF(ReadOnlyOrg, ReadOnlySpace)
   131  			})
   132  
   133  			It("displays org not found, exits 1, and clears existing targets", func() {
   134  				session := helpers.CF("target", "-o", orgName)
   135  				Eventually(session.Err).Should(Say("Organization '%s' not found", orgName))
   136  				Eventually(session).Should(Say("FAILED"))
   137  				Eventually(session).Should(Exit(1))
   138  
   139  				session = helpers.CF("target")
   140  				Eventually(session).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   141  				Eventually(session).Should(Exit(0))
   142  			})
   143  		})
   144  
   145  		Context("when the org exists", func() {
   146  			BeforeEach(func() {
   147  				helpers.CreateOrg(orgName)
   148  				helpers.TargetOrg(orgName)
   149  			})
   150  
   151  			AfterEach(func() {
   152  				helpers.QuickDeleteOrg(orgName)
   153  			})
   154  
   155  			Context("when there are no spaces in the org", func() {
   156  				BeforeEach(func() {
   157  					helpers.ClearTarget()
   158  				})
   159  
   160  				It("only targets the org and exits 0", func() {
   161  					username, _ := helpers.GetCredentials()
   162  					session := helpers.CF("target", "-o", orgName)
   163  					Eventually(session).Should(Say("api endpoint:\\s+%s", apiURL))
   164  					Eventually(session).Should(Say("api version:\\s+[\\d.]+"))
   165  					Eventually(session).Should(Say("user:\\s+%s", username))
   166  					Eventually(session).Should(Say("org:\\s+%s", orgName))
   167  					Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE"))
   168  					Eventually(session).Should(Exit(0))
   169  				})
   170  			})
   171  
   172  			Context("when there is only one space in the org", func() {
   173  				BeforeEach(func() {
   174  					helpers.CreateSpace(spaceName)
   175  					helpers.ClearTarget()
   176  				})
   177  
   178  				It("targets the org and space and exits 0", func() {
   179  					username, _ := helpers.GetCredentials()
   180  					session := helpers.CF("target", "-o", orgName)
   181  					Eventually(session).Should(Say("api endpoint:\\s+%s", apiURL))
   182  					Eventually(session).Should(Say("api version:\\s+[\\d.]+"))
   183  					Eventually(session).Should(Say("user:\\s+%s", username))
   184  					Eventually(session).Should(Say("org:\\s+%s", orgName))
   185  					Eventually(session).Should(Say("space:\\s+%s", spaceName))
   186  					Eventually(session).Should(Exit(0))
   187  				})
   188  			})
   189  
   190  			Context("when there are multiple spaces in the org", func() {
   191  				BeforeEach(func() {
   192  					helpers.CreateSpace(spaceName)
   193  					helpers.CreateSpace(helpers.NewSpaceName())
   194  					helpers.ClearTarget()
   195  				})
   196  
   197  				It("targets the org only and exits 0", func() {
   198  					username, _ := helpers.GetCredentials()
   199  					session := helpers.CF("target", "-o", orgName)
   200  					Eventually(session).Should(Say("api endpoint:\\s+%s", apiURL))
   201  					Eventually(session).Should(Say("api version:\\s+[\\d.]+"))
   202  					Eventually(session).Should(Say("user:\\s+%s", username))
   203  					Eventually(session).Should(Say("org:\\s+%s", orgName))
   204  					Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE"))
   205  					Eventually(session).Should(Exit(0))
   206  				})
   207  
   208  				Context("when there is an existing targeted space", func() {
   209  					BeforeEach(func() {
   210  						session := helpers.CF("target", "-o", orgName, "-s", spaceName)
   211  						Eventually(session).Should(Exit(0))
   212  					})
   213  
   214  					It("unsets the targeted space", func() {
   215  						session := helpers.CF("target", "-o", orgName)
   216  						Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE"))
   217  						Eventually(session).Should(Exit(0))
   218  					})
   219  				})
   220  			})
   221  		})
   222  	})
   223  
   224  	Context("when only a space argument is provided", func() {
   225  		Context("when there is an existing targeted org", func() {
   226  			BeforeEach(func() {
   227  				helpers.LoginCF()
   228  				Eventually(helpers.CF("target", "-o", ReadOnlyOrg)).Should(Exit(0))
   229  			})
   230  
   231  			Context("when the space exists", func() {
   232  				It("targets the space and exits 0", func() {
   233  					username, _ := helpers.GetCredentials()
   234  					session := helpers.CF("target", "-s", ReadOnlySpace)
   235  					Eventually(session).Should(Say("api endpoint:\\s+%s", apiURL))
   236  					Eventually(session).Should(Say("api version:\\s+[\\d.]+"))
   237  					Eventually(session).Should(Say("user:\\s+%s", username))
   238  					Eventually(session).Should(Say("org:\\s+%s", ReadOnlyOrg))
   239  					Eventually(session).Should(Say("space:\\s+%s", ReadOnlySpace))
   240  					Eventually(session).Should(Exit(0))
   241  				})
   242  			})
   243  
   244  			Context("when the space does not exist", func() {
   245  				It("displays space not found, exits 1, and clears existing targeted space", func() {
   246  					session := helpers.CF("target", "-s", spaceName)
   247  					Eventually(session.Err).Should(Say("Space '%s' not found.", spaceName))
   248  					Eventually(session).Should(Say("FAILED"))
   249  					Eventually(session).Should(Exit(1))
   250  
   251  					session = helpers.CF("target")
   252  					Eventually(session).Should(Say("org:\\s+%s", ReadOnlyOrg))
   253  					Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE'"))
   254  					Eventually(session).Should(Exit(0))
   255  				})
   256  			})
   257  		})
   258  
   259  		Context("when there is not an existing targeted org", func() {
   260  			It("displays org must be targeted first and exits 1", func() {
   261  				session := helpers.CF("target", "-s", spaceName)
   262  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
   263  				Eventually(session).Should(Say("FAILED"))
   264  				Eventually(session).Should(Exit(1))
   265  			})
   266  		})
   267  	})
   268  
   269  	Context("when both org and space arguments are provided", func() {
   270  		// We set the targets to verify that the target command preserves existing targets
   271  		// in failure
   272  		BeforeEach(func() {
   273  			setupCF(ReadOnlyOrg, ReadOnlySpace)
   274  		})
   275  
   276  		Context("when the org does not exist", func() {
   277  			It("displays org not found, exits 1, and clears existing targets", func() {
   278  				session := helpers.CF("target", "-o", orgName, "-s", spaceName)
   279  				Eventually(session.Err).Should(Say("Organization '%s' not found", orgName))
   280  				Eventually(session).Should(Say("FAILED"))
   281  				Eventually(session).Should(Exit(1))
   282  
   283  				session = helpers.CF("target")
   284  				Eventually(session).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   285  				Eventually(session).Should(Exit(0))
   286  			})
   287  		})
   288  
   289  		Context("when the org exists", func() {
   290  			BeforeEach(func() {
   291  				helpers.CreateOrg(orgName)
   292  			})
   293  
   294  			AfterEach(func() {
   295  				helpers.QuickDeleteOrg(orgName)
   296  			})
   297  
   298  			Context("when the space exists", func() {
   299  				BeforeEach(func() {
   300  					helpers.TargetOrg(orgName)
   301  					helpers.CreateSpace(spaceName)
   302  					helpers.ClearTarget()
   303  				})
   304  
   305  				It("targets the org and space and exits 0", func() {
   306  					username, _ := helpers.GetCredentials()
   307  					session := helpers.CF("target", "-o", orgName, "-s", spaceName)
   308  					Eventually(session).Should(Say("api endpoint:\\s+%s", apiURL))
   309  					Eventually(session).Should(Say("api version:\\s+[\\d.]+"))
   310  					Eventually(session).Should(Say("user:\\s+%s", username))
   311  					Eventually(session).Should(Say("org:\\s+%s", orgName))
   312  					Eventually(session).Should(Say("space:\\s+%s", spaceName))
   313  					Eventually(session).Should(Exit(0))
   314  				})
   315  			})
   316  
   317  			Context("when the space does not exist", func() {
   318  				It("displays space not found, exits 1, and clears the existing targets", func() {
   319  					session := helpers.CF("target", "-o", orgName, "-s", spaceName)
   320  					Eventually(session.Err).Should(Say("Space '%s' not found.", spaceName))
   321  					Eventually(session).Should(Say("FAILED"))
   322  					Eventually(session).Should(Exit(1))
   323  
   324  					session = helpers.CF("target")
   325  					Eventually(session).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   326  					Eventually(session).Should(Exit(0))
   327  				})
   328  			})
   329  		})
   330  	})
   331  })