github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/shared/isolated/api_command_test.go (about) 1 package isolated 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "net/http" 7 "os" 8 "os/exec" 9 "path/filepath" 10 "strings" 11 12 "code.cloudfoundry.org/cli/integration/helpers" 13 "code.cloudfoundry.org/cli/util/configv3" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 . "github.com/onsi/gomega/gbytes" 17 . "github.com/onsi/gomega/gexec" 18 "github.com/onsi/gomega/ghttp" 19 ) 20 21 var _ = Describe("api command", func() { 22 Context("no arguments", func() { 23 When("the api is set", func() { 24 When("the user is not logged in", func() { 25 It("outputs the current api", func() { 26 session := helpers.CF("api") 27 28 Eventually(session).Should(Say(`api endpoint:\s+%s`, apiURL)) 29 Eventually(session).Should(Say(`api version:\s+\d+\.\d+\.\d+`)) 30 Eventually(session).Should(Exit(0)) 31 }) 32 }) 33 34 When("the user is logged in", func() { 35 var target, apiVersion, org, space string 36 37 BeforeEach(func() { 38 target = "https://api.fake.com" 39 apiVersion = "2.59.0" 40 org = "the-org" 41 space = "the-space" 42 43 userConfig := configv3.Config{ 44 ConfigFile: configv3.JSONConfig{ 45 Target: target, 46 APIVersion: apiVersion, 47 AccessToken: "bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiI3YzZkMDA2MjA2OTI0NmViYWI0ZjBmZjY3NGQ3Zjk4OSIsInN1YiI6Ijk1MTliZTNlLTQ0ZDktNDBkMC1hYjlhLWY0YWNlMTFkZjE1OSIsInNjb3BlIjpbIm9wZW5pZCIsInJvdXRpbmcucm91dGVyX2dyb3Vwcy53cml0ZSIsInNjaW0ucmVhZCIsImNsb3VkX2NvbnRyb2xsZXIuYWRtaW4iLCJ1YWEudXNlciIsInJvdXRpbmcucm91dGVyX2dyb3Vwcy5yZWFkIiwiY2xvdWRfY29udHJvbGxlci5yZWFkIiwicGFzc3dvcmQud3JpdGUiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwiZG9wcGxlci5maXJlaG9zZSIsInNjaW0ud3JpdGUiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImF6cCI6ImNmIiwiZ3JhbnRfdHlwZSI6InBhc3N3b3JkIiwidXNlcl9pZCI6Ijk1MTliZTNlLTQ0ZDktNDBkMC1hYjlhLWY0YWNlMTFkZjE1OSIsIm9yaWdpbiI6InVhYSIsInVzZXJfbmFtZSI6ImFkbWluIiwiZW1haWwiOiJhZG1pbiIsImF1dGhfdGltZSI6MTQ3MzI4NDU3NywicmV2X3NpZyI6IjZiMjdkYTZjIiwiaWF0IjoxNDczMjg0NTc3LCJleHAiOjE0NzMyODUxNzcsImlzcyI6Imh0dHBzOi8vdWFhLmJvc2gtbGl0ZS5jb20vb2F1dGgvdG9rZW4iLCJ6aWQiOiJ1YWEiLCJhdWQiOlsiY2YiLCJvcGVuaWQiLCJyb3V0aW5nLnJvdXRlcl9ncm91cHMiLCJzY2ltIiwiY2xvdWRfY29udHJvbGxlciIsInVhYSIsInBhc3N3b3JkIiwiZG9wcGxlciJdfQ.OcH_w9yIKJkEcTZMThIs-qJAHk3G0JwNjG-aomVH9hKye4ciFO6IMQMLKmCBrrAQVc7ST1SZZwq7gv12Dq__6Jp-hai0a2_ADJK-Vc9YXyNZKgYTWIeVNGM1JGdHgFSrBR2Lz7IIrH9HqeN8plrKV5HzU8uI9LL4lyOCjbXJ9cM", 48 TargetedOrganization: configv3.Organization{ 49 Name: org, 50 }, 51 TargetedSpace: configv3.Space{ 52 Name: space, 53 }, 54 ConfigVersion: configv3.CurrentConfigVersion, 55 }, 56 } 57 err := userConfig.WriteConfig() 58 Expect(err).ToNot(HaveOccurred()) 59 }) 60 61 It("outputs the user's target information", func() { 62 session := helpers.CF("api") 63 Eventually(session).Should(Say(`api endpoint:\s+%s`, target)) 64 Eventually(session).Should(Say(`api version:\s+%s`, apiVersion)) 65 Eventually(session).Should(Exit(0)) 66 }) 67 }) 68 }) 69 70 When("the api is not set", func() { 71 BeforeEach(func() { 72 os.RemoveAll(filepath.Join(homeDir, ".cf")) 73 }) 74 75 It("outputs that nothing is set", func() { 76 session := helpers.CF("api") 77 Eventually(session).Should(Say("No api endpoint set. Use 'cf api' to set an endpoint")) 78 Eventually(session).Should(Exit(0)) 79 }) 80 }) 81 82 Context("--unset is passed", func() { 83 BeforeEach(func() { 84 85 userConfig := configv3.Config{ 86 ConfigFile: configv3.JSONConfig{ 87 ConfigVersion: configv3.CurrentConfigVersion, 88 Target: "https://api.fake.com", 89 APIVersion: "2.59.0", 90 AccessToken: "bearer tokenstuff", 91 TargetedOrganization: configv3.Organization{ 92 Name: "the-org", 93 }, 94 TargetedSpace: configv3.Space{ 95 Name: "the-space", 96 }, 97 }, 98 } 99 err := userConfig.WriteConfig() 100 Expect(err).ToNot(HaveOccurred()) 101 }) 102 103 It("clears the targetted context", func() { 104 session := helpers.CF("api", "--unset") 105 106 Eventually(session).Should(Say("Unsetting api endpoint...")) 107 Eventually(session).Should(Say("OK")) 108 Eventually(session).Should(Exit(0)) 109 110 rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json")) 111 Expect(err).NotTo(HaveOccurred()) 112 113 var configFile configv3.JSONConfig 114 err = json.Unmarshal(rawConfig, &configFile) 115 Expect(err).NotTo(HaveOccurred()) 116 117 Expect(configFile.ConfigVersion).To(Equal(3)) 118 Expect(configFile.Target).To(BeEmpty()) 119 Expect(configFile.APIVersion).To(BeEmpty()) 120 Expect(configFile.AuthorizationEndpoint).To(BeEmpty()) 121 Expect(configFile.DopplerEndpoint).To(BeEmpty()) 122 Expect(configFile.UAAEndpoint).To(BeEmpty()) 123 Expect(configFile.AccessToken).To(BeEmpty()) 124 Expect(configFile.RefreshToken).To(BeEmpty()) 125 Expect(configFile.TargetedOrganization.GUID).To(BeEmpty()) 126 Expect(configFile.TargetedOrganization.Name).To(BeEmpty()) 127 Expect(configFile.TargetedSpace.GUID).To(BeEmpty()) 128 Expect(configFile.TargetedSpace.Name).To(BeEmpty()) 129 Expect(configFile.TargetedSpace.AllowSSH).To(BeFalse()) 130 Expect(configFile.SkipSSLValidation).To(BeFalse()) 131 }) 132 }) 133 }) 134 135 When("Skip SSL Validation is required", func() { 136 Context("api has SSL", func() { 137 var server *ghttp.Server 138 139 BeforeEach(func() { 140 cliVersion := "1.0.0" 141 server = helpers.StartMockServerWithMinimumCLIVersion(cliVersion) 142 apiURL = server.URL() 143 }) 144 145 AfterEach(func() { 146 server.Close() 147 }) 148 149 It("warns about skip SSL", func() { 150 session := helpers.CF("api", apiURL) 151 Eventually(session).Should(Say("Setting api endpoint to %s...", apiURL)) 152 Eventually(session.Err).Should(Say("x509: certificate has expired or is not yet valid|SSL Certificate Error x509: certificate is valid for|Invalid SSL Cert for %s", apiURL)) 153 Eventually(session.Err).Should(Say("TIP: Use 'cf api --skip-ssl-validation' to continue with an insecure API endpoint")) 154 Eventually(session).Should(Say("FAILED")) 155 Eventually(session).Should(Exit(1)) 156 }) 157 158 It("sets the API endpoint", func() { 159 session := helpers.CF("api", apiURL, "--skip-ssl-validation") 160 Eventually(session).Should(Say("Setting api endpoint to %s...", apiURL)) 161 Eventually(session).Should(Say("OK")) 162 Eventually(session).Should(Say(`api endpoint:\s+%s`, apiURL)) 163 Eventually(session).Should(Say(`api version:\s+\d+\.\d+\.\d+`)) 164 Eventually(session).Should(Exit(0)) 165 }) 166 }) 167 168 Context("api does not have SSL", func() { 169 var server *ghttp.Server 170 171 BeforeEach(func() { 172 server = ghttp.NewServer() 173 serverAPIURL := server.URL()[7:] 174 175 response := `{ 176 "name":"", 177 "build":"", 178 "support":"http://support.cloudfoundry.com", 179 "version":0, 180 "description":"", 181 "authorization_endpoint":"https://login.APISERVER", 182 "min_cli_version":null, 183 "min_recommended_cli_version":null, 184 "api_version":"2.59.0", 185 "app_ssh_endpoint":"ssh.APISERVER", 186 "app_ssh_host_key_fingerprint":"a6:d1:08:0b:b0:cb:9b:5f:c4:ba:44:2a:97:26:19:8a", 187 "app_ssh_oauth_client":"ssh-proxy", 188 "logging_endpoint":"wss://loggregator.APISERVER", 189 "doppler_logging_endpoint":"wss://doppler.APISERVER" 190 }` 191 response = strings.Replace(response, "APISERVER", serverAPIURL, -1) 192 server.AppendHandlers( 193 ghttp.CombineHandlers( 194 ghttp.VerifyRequest("GET", "/v2/info"), 195 ghttp.RespondWith(http.StatusOK, response), 196 ), 197 ) 198 }) 199 200 AfterEach(func() { 201 server.Close() 202 }) 203 204 It("falls back to http and gives a warning", func() { 205 session := helpers.CF("api", server.URL(), "--skip-ssl-validation") 206 Eventually(session).Should(Say("Setting api endpoint to %s...", server.URL())) 207 Eventually(session).Should(Say("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended")) 208 Eventually(session).Should(Say("OK")) 209 Eventually(session).Should(Say("Not logged in. Use 'cf login' or 'cf login --sso' to log in.")) 210 Eventually(session).Should(Exit(0)) 211 }) 212 }) 213 214 It("sets SSL Disabled in the config file to true", func() { 215 command := exec.Command("cf", "api", apiURL, "--skip-ssl-validation") 216 session, err := Start(command, GinkgoWriter, GinkgoWriter) 217 Expect(err).NotTo(HaveOccurred()) 218 Eventually(session).Should(Exit(0)) 219 220 rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json")) 221 Expect(err).NotTo(HaveOccurred()) 222 223 var configFile configv3.JSONConfig 224 err = json.Unmarshal(rawConfig, &configFile) 225 Expect(err).NotTo(HaveOccurred()) 226 227 Expect(configFile.SkipSSLValidation).To(BeTrue()) 228 }) 229 }) 230 231 When("skip-ssl-validation is not required", func() { 232 BeforeEach(func() { 233 if skipSSLValidation { 234 Skip("SKIP_SSL_VALIDATION is enabled") 235 } 236 }) 237 238 It("logs in without any warnings", func() { 239 session := helpers.CF("api", apiURL) 240 Eventually(session).Should(Say("Setting api endpoint to %s...", apiURL)) 241 Consistently(session).ShouldNot(Say("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended")) 242 Eventually(session).Should(Say("OK")) 243 Eventually(session).Should(Say("Not logged in. Use 'cf login' or 'cf login --sso' to log in.")) 244 Eventually(session).Should(Exit(0)) 245 }) 246 247 It("sets SSL Disabled in the config file to false", func() { 248 session := helpers.CF("api", apiURL) 249 Eventually(session).Should(Exit(0)) 250 251 rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json")) 252 Expect(err).NotTo(HaveOccurred()) 253 254 var configFile configv3.JSONConfig 255 err = json.Unmarshal(rawConfig, &configFile) 256 Expect(err).NotTo(HaveOccurred()) 257 258 Expect(configFile.SkipSSLValidation).To(BeFalse()) 259 }) 260 }) 261 262 When("the v3 api supports routing", func() { 263 It("sets the routing endpoing in the config file", func() { 264 var session *Session 265 if skipSSLValidation { 266 session = helpers.CF("api", apiURL, "--skip-ssl-validation") 267 } else { 268 session = helpers.CF("api", apiURL) 269 } 270 271 Eventually(session).Should(Exit(0)) 272 273 rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json")) 274 Expect(err).NotTo(HaveOccurred()) 275 276 var configFile configv3.JSONConfig 277 err = json.Unmarshal(rawConfig, &configFile) 278 Expect(err).NotTo(HaveOccurred()) 279 280 Expect(configFile.RoutingEndpoint).NotTo(BeEmpty()) 281 }) 282 }) 283 284 It("sets the config file", func() { 285 var session *Session 286 if skipSSLValidation { 287 session = helpers.CF("api", apiURL, "--skip-ssl-validation") 288 } else { 289 session = helpers.CF("api", apiURL) 290 } 291 Eventually(session).Should(Exit(0)) 292 293 rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json")) 294 Expect(err).NotTo(HaveOccurred()) 295 296 var configFile configv3.JSONConfig 297 err = json.Unmarshal(rawConfig, &configFile) 298 Expect(err).NotTo(HaveOccurred()) 299 300 Expect(configFile.ConfigVersion).To(Equal(3)) 301 Expect(configFile.Target).To(Equal(apiURL)) 302 Expect(configFile.APIVersion).To(MatchRegexp(`\d+\.\d+\.\d+`)) 303 Expect(configFile.AuthorizationEndpoint).ToNot(BeEmpty()) 304 Expect(configFile.DopplerEndpoint).To(MatchRegexp("^wss://")) 305 Expect(configFile.UAAEndpoint).To(BeEmpty()) 306 Expect(configFile.AccessToken).To(BeEmpty()) 307 Expect(configFile.RefreshToken).To(BeEmpty()) 308 Expect(configFile.TargetedOrganization.GUID).To(BeEmpty()) 309 Expect(configFile.TargetedOrganization.Name).To(BeEmpty()) 310 Expect(configFile.TargetedSpace.GUID).To(BeEmpty()) 311 Expect(configFile.TargetedSpace.Name).To(BeEmpty()) 312 Expect(configFile.TargetedSpace.AllowSSH).To(BeFalse()) 313 }) 314 315 It("handles API endpoints with trailing slash", func() { 316 var session *Session 317 318 if skipSSLValidation { 319 session = helpers.CF("api", apiURL+"/", "--skip-ssl-validation") 320 } else { 321 session = helpers.CF("api", apiURL+"/") 322 } 323 324 Eventually(session).Should(Exit(0)) 325 326 helpers.LoginCF() 327 328 session = helpers.CF("orgs") 329 Eventually(session).Should(Exit(0)) 330 }) 331 })