github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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.RandomName() 24 }) 25 26 Context("help", func() { 27 It("displays help", func() { 28 session := helpers.CF("target", "--help") 29 Eventually(session.Out).Should(Say("NAME:")) 30 Eventually(session.Out).Should(Say(" target - Set or view the targeted org or space")) 31 Eventually(session.Out).Should(Say("USAGE:")) 32 Eventually(session.Out).Should(Say(" cf target \\[-o ORG\\] \\[-s SPACE\\]")) 33 Eventually(session.Out).Should(Say("ALIAS:")) 34 Eventually(session.Out).Should(Say(" t")) 35 Eventually(session.Out).Should(Say("OPTIONS:")) 36 Eventually(session.Out).Should(Say(" -o Organization")) 37 Eventually(session.Out).Should(Say(" -s Space")) 38 Eventually(session.Out).Should(Say("SEE ALSO:")) 39 Eventually(session.Out).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.Out).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.Out).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 // TODO: Decide if we want to uncomment this or delete them 82 // Eventually(session.Out).Should(Say("API endpoint: %s", apiURL)) 83 // Eventually(session.Out).Should(Say(`API version: [\d.]+`)) 84 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in.")) 85 Eventually(session.Out).Should(Say("FAILED")) 86 Eventually(session).Should(Exit(1)) 87 }, 88 89 Entry("when trying to target an org", "-o", "some-org"), 90 Entry("when trying to target a space", "-s", "some-space"), 91 Entry("when trying to target an org and space", "-o", "some-org", "-s", "some-space"), 92 Entry("when trying to get the target"), 93 ) 94 }) 95 }) 96 97 Context("when no arguments are provided", func() { 98 Context("when *no* org and space are targeted", func() { 99 It("displays current target information", func() { 100 username, _ := helpers.GetCredentials() 101 session := helpers.CF("target") 102 Eventually(session.Out).Should(Say("API endpoint: %s", apiURL)) 103 Eventually(session.Out).Should(Say(`API version: [\d.]+`)) 104 Eventually(session.Out).Should(Say("User: %s", username)) 105 Eventually(session.Out).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'")) 106 Eventually(session).Should(Exit(0)) 107 }) 108 }) 109 110 Context("when targeted to an org and space", func() { 111 BeforeEach(func() { 112 setupCF(ReadOnlyOrg, ReadOnlySpace) 113 }) 114 115 It("displays current target information", func() { 116 username, _ := helpers.GetCredentials() 117 session := helpers.CF("target") 118 Eventually(session.Out).Should(Say("API endpoint: %s", apiURL)) 119 Eventually(session.Out).Should(Say(`API version: [\d.]+`)) 120 Eventually(session.Out).Should(Say("User: %s", username)) 121 Eventually(session.Out).Should(Say("Org: %s", ReadOnlyOrg)) 122 Eventually(session.Out).Should(Say("Space: %s", ReadOnlySpace)) 123 Eventually(session).Should(Exit(0)) 124 }) 125 }) 126 }) 127 128 Context("when only an org argument is provided", func() { 129 Context("when the org does not exist", func() { 130 // We set targets to verify that the target command 131 // preserves existing targets in failure 132 BeforeEach(func() { 133 setupCF(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.Out).Should(Say("FAILED")) 140 Eventually(session).Should(Exit(1)) 141 142 session = helpers.CF("target") 143 Eventually(session.Out).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'")) 144 Eventually(session).Should(Exit(0)) 145 }) 146 }) 147 148 Context("when the org exists", func() { 149 BeforeEach(func() { 150 helpers.CreateOrg(orgName) 151 helpers.TargetOrg(orgName) 152 }) 153 154 Context("when there are no spaces in the org", func() { 155 BeforeEach(func() { 156 helpers.ClearTarget() 157 }) 158 159 It("only targets the org and exits 0", func() { 160 username, _ := helpers.GetCredentials() 161 session := helpers.CF("target", "-o", orgName) 162 Eventually(session.Out).Should(Say("API endpoint: %s", apiURL)) 163 Eventually(session.Out).Should(Say(`API version: [\d.]+`)) 164 Eventually(session.Out).Should(Say("User: %s", username)) 165 Eventually(session.Out).Should(Say("Org: %s", orgName)) 166 Eventually(session.Out).Should(Say("No space targeted, use 'cf target -s SPACE")) 167 Eventually(session).Should(Exit(0)) 168 }) 169 }) 170 171 Context("when there is only one space in the org", func() { 172 BeforeEach(func() { 173 helpers.CreateSpace(spaceName) 174 helpers.ClearTarget() 175 }) 176 177 It("targets the org and space and exits 0", func() { 178 username, _ := helpers.GetCredentials() 179 session := helpers.CF("target", "-o", orgName) 180 Eventually(session.Out).Should(Say("API endpoint: %s", apiURL)) 181 Eventually(session.Out).Should(Say(`API version: [\d.]+`)) 182 Eventually(session.Out).Should(Say("User: %s", username)) 183 Eventually(session.Out).Should(Say("Org: %s", orgName)) 184 Eventually(session.Out).Should(Say("Space: %s", spaceName)) 185 Eventually(session).Should(Exit(0)) 186 }) 187 }) 188 189 Context("when there are multiple spaces in the org", func() { 190 BeforeEach(func() { 191 helpers.CreateSpace(spaceName) 192 helpers.CreateSpace(helpers.RandomName()) 193 helpers.ClearTarget() 194 }) 195 196 It("targets the org only and exits 0", func() { 197 username, _ := helpers.GetCredentials() 198 session := helpers.CF("target", "-o", orgName) 199 Eventually(session.Out).Should(Say("API endpoint: %s", apiURL)) 200 Eventually(session.Out).Should(Say(`API version: [\d.]+`)) 201 Eventually(session.Out).Should(Say("User: %s", username)) 202 Eventually(session.Out).Should(Say("Org: %s", orgName)) 203 Eventually(session.Out).Should(Say("No space targeted, use 'cf target -s SPACE")) 204 Eventually(session).Should(Exit(0)) 205 }) 206 207 Context("when there is an existing targeted space", func() { 208 BeforeEach(func() { 209 session := helpers.CF("target", "-o", orgName, "-s", spaceName) 210 Eventually(session).Should(Exit(0)) 211 }) 212 213 It("unsets the targeted space", func() { 214 session := helpers.CF("target", "-o", orgName) 215 Eventually(session.Out).Should(Say("No space targeted, use 'cf target -s SPACE")) 216 Eventually(session).Should(Exit(0)) 217 }) 218 }) 219 }) 220 }) 221 }) 222 223 Context("when only a space argument is provided", func() { 224 Context("when there is an existing targeted org", func() { 225 existingOrg := helpers.RandomName() 226 existingSpace := helpers.RandomName() 227 228 BeforeEach(func() { 229 helpers.LoginCF() 230 // We create and set a space to verify that the target command 231 // preserves existing targets in failure 232 helpers.CreateOrgAndSpace(existingOrg, existingSpace) 233 helpers.TargetOrgAndSpace(existingOrg, existingSpace) 234 }) 235 236 Context("when the space exists", func() { 237 BeforeEach(func() { 238 helpers.CreateSpace(spaceName) 239 }) 240 241 It("targets the space and exits 0", func() { 242 username, _ := helpers.GetCredentials() 243 session := helpers.CF("target", "-s", spaceName) 244 Eventually(session.Out).Should(Say("API endpoint: %s", apiURL)) 245 Eventually(session.Out).Should(Say(`API version: [\d.]+`)) 246 Eventually(session.Out).Should(Say("User: %s", username)) 247 Eventually(session.Out).Should(Say("Org: %s", existingOrg)) 248 Eventually(session.Out).Should(Say("Space: %s", spaceName)) 249 Eventually(session).Should(Exit(0)) 250 }) 251 }) 252 253 Context("when the space does not exist", func() { 254 It("displays space not found, exits 1, and clears existing targeted space", func() { 255 session := helpers.CF("target", "-s", spaceName) 256 Eventually(session.Err).Should(Say("Space '%s' not found.", spaceName)) 257 Eventually(session.Out).Should(Say("FAILED")) 258 Eventually(session).Should(Exit(1)) 259 260 session = helpers.CF("target") 261 Eventually(session.Out).Should(Say("Org: %s", existingOrg)) 262 Eventually(session.Out).Should(Say("No space targeted, use 'cf target -s SPACE'")) 263 Eventually(session).Should(Exit(0)) 264 }) 265 }) 266 }) 267 268 Context("when there is not an existing targeted org", func() { 269 It("displays org must be targeted first and exits 1", func() { 270 session := helpers.CF("target", "-s", spaceName) 271 Eventually(session.Err).Should(Say("An org must be targeted before targeting a space")) 272 Eventually(session.Out).Should(Say("FAILED")) 273 Eventually(session).Should(Exit(1)) 274 }) 275 }) 276 }) 277 278 Context("when both org and space arguments are provided", func() { 279 // We set the targets to verify that the target command preserves existing targets 280 // in failure 281 BeforeEach(func() { 282 setupCF(ReadOnlyOrg, ReadOnlySpace) 283 }) 284 285 Context("when the org does not exist", func() { 286 It("displays org not found, exits 1, and clears existing targets", func() { 287 session := helpers.CF("target", "-o", orgName, "-s", spaceName) 288 Eventually(session.Err).Should(Say("Organization '%s' not found", orgName)) 289 Eventually(session.Out).Should(Say("FAILED")) 290 Eventually(session).Should(Exit(1)) 291 292 session = helpers.CF("target") 293 Eventually(session.Out).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'")) 294 Eventually(session).Should(Exit(0)) 295 }) 296 }) 297 298 Context("when the org exists", func() { 299 BeforeEach(func() { 300 helpers.CreateOrg(orgName) 301 }) 302 303 Context("when the space exists", func() { 304 BeforeEach(func() { 305 helpers.TargetOrg(orgName) 306 helpers.CreateSpace(spaceName) 307 helpers.ClearTarget() 308 }) 309 310 It("targets the org and space and exits 0", func() { 311 username, _ := helpers.GetCredentials() 312 session := helpers.CF("target", "-o", orgName, "-s", spaceName) 313 Eventually(session.Out).Should(Say("API endpoint: %s", apiURL)) 314 Eventually(session.Out).Should(Say(`API version: [\d.]+`)) 315 Eventually(session.Out).Should(Say("User: %s", username)) 316 Eventually(session.Out).Should(Say("Org: %s", orgName)) 317 Eventually(session.Out).Should(Say("Space: %s", spaceName)) 318 Eventually(session).Should(Exit(0)) 319 }) 320 }) 321 322 Context("when the space does not exist", func() { 323 It("displays space not found, exits 1, and clears the existing targets", func() { 324 session := helpers.CF("target", "-o", orgName, "-s", spaceName) 325 Eventually(session.Err).Should(Say("Space '%s' not found.", spaceName)) 326 Eventually(session.Out).Should(Say("FAILED")) 327 Eventually(session).Should(Exit(1)) 328 329 session = helpers.CF("target") 330 Eventually(session.Out).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'")) 331 Eventually(session).Should(Exit(0)) 332 }) 333 }) 334 }) 335 }) 336 })