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