github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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, org, space string
    37  
    38  				BeforeEach(func() {
    39  					target = "https://api.fake.com"
    40  					apiVersion = "2.59.0"
    41  					org = "the-org"
    42  					space = "the-space"
    43  
    44  					userConfig := configv3.Config{
    45  						ConfigFile: configv3.JSONConfig{
    46  							Target:      target,
    47  							APIVersion:  apiVersion,
    48  							AccessToken: "bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiI3YzZkMDA2MjA2OTI0NmViYWI0ZjBmZjY3NGQ3Zjk4OSIsInN1YiI6Ijk1MTliZTNlLTQ0ZDktNDBkMC1hYjlhLWY0YWNlMTFkZjE1OSIsInNjb3BlIjpbIm9wZW5pZCIsInJvdXRpbmcucm91dGVyX2dyb3Vwcy53cml0ZSIsInNjaW0ucmVhZCIsImNsb3VkX2NvbnRyb2xsZXIuYWRtaW4iLCJ1YWEudXNlciIsInJvdXRpbmcucm91dGVyX2dyb3Vwcy5yZWFkIiwiY2xvdWRfY29udHJvbGxlci5yZWFkIiwicGFzc3dvcmQud3JpdGUiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwiZG9wcGxlci5maXJlaG9zZSIsInNjaW0ud3JpdGUiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImF6cCI6ImNmIiwiZ3JhbnRfdHlwZSI6InBhc3N3b3JkIiwidXNlcl9pZCI6Ijk1MTliZTNlLTQ0ZDktNDBkMC1hYjlhLWY0YWNlMTFkZjE1OSIsIm9yaWdpbiI6InVhYSIsInVzZXJfbmFtZSI6ImFkbWluIiwiZW1haWwiOiJhZG1pbiIsImF1dGhfdGltZSI6MTQ3MzI4NDU3NywicmV2X3NpZyI6IjZiMjdkYTZjIiwiaWF0IjoxNDczMjg0NTc3LCJleHAiOjE0NzMyODUxNzcsImlzcyI6Imh0dHBzOi8vdWFhLmJvc2gtbGl0ZS5jb20vb2F1dGgvdG9rZW4iLCJ6aWQiOiJ1YWEiLCJhdWQiOlsiY2YiLCJvcGVuaWQiLCJyb3V0aW5nLnJvdXRlcl9ncm91cHMiLCJzY2ltIiwiY2xvdWRfY29udHJvbGxlciIsInVhYSIsInBhc3N3b3JkIiwiZG9wcGxlciJdfQ.OcH_w9yIKJkEcTZMThIs-qJAHk3G0JwNjG-aomVH9hKye4ciFO6IMQMLKmCBrrAQVc7ST1SZZwq7gv12Dq__6Jp-hai0a2_ADJK-Vc9YXyNZKgYTWIeVNGM1JGdHgFSrBR2Lz7IIrH9HqeN8plrKV5HzU8uI9LL4lyOCjbXJ9cM",
    49  							TargetedOrganization: configv3.Organization{
    50  								Name: org,
    51  							},
    52  							TargetedSpace: configv3.Space{
    53  								Name: space,
    54  							},
    55  						},
    56  					}
    57  					err := configv3.WriteConfig(&userConfig)
    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  		Context("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: 3,
    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 := configv3.WriteConfig(&userConfig)
   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  	Context("when Skip SSL Validation is required", func() {
   136  		Context("api has SSL", func() {
   137  			BeforeEach(func() {
   138  				if skipSSLValidation == "" {
   139  					Skip("SKIP_SSL_VALIDATION is not enabled")
   140  				}
   141  			})
   142  
   143  			It("warns about skip SSL", func() {
   144  				session := helpers.CF("api", apiURL)
   145  				Eventually(session).Should(Say("Setting api endpoint to %s...", apiURL))
   146  				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))
   147  				Eventually(session.Err).Should(Say("TIP: Use 'cf api --skip-ssl-validation' to continue with an insecure API endpoint"))
   148  				Eventually(session).Should(Say("FAILED"))
   149  				Eventually(session).Should(Exit(1))
   150  			})
   151  
   152  			It("sets the API endpoint", func() {
   153  				session := helpers.CF("api", apiURL, "--skip-ssl-validation")
   154  				Eventually(session).Should(Say("Setting api endpoint to %s...", apiURL))
   155  				Eventually(session).Should(Say("OK"))
   156  				Eventually(session).Should(Say("api endpoint:\\s+%s", apiURL))
   157  				Eventually(session).Should(Say("api version:\\s+\\d+\\.\\d+\\.\\d+"))
   158  				Eventually(session).Should(Exit(0))
   159  			})
   160  		})
   161  
   162  		Context("api does not have SSL", func() {
   163  			var server *ghttp.Server
   164  
   165  			BeforeEach(func() {
   166  				server = ghttp.NewServer()
   167  				serverAPIURL := server.URL()[7:]
   168  
   169  				response := `{
   170  					"name":"",
   171  					"build":"",
   172  					"support":"http://support.cloudfoundry.com",
   173  					"version":0,
   174  					"description":"",
   175  					"authorization_endpoint":"https://login.APISERVER",
   176  					"token_endpoint":"https://uaa.APISERVER",
   177  					"min_cli_version":null,
   178  					"min_recommended_cli_version":null,
   179  					"api_version":"2.59.0",
   180  					"app_ssh_endpoint":"ssh.APISERVER",
   181  					"app_ssh_host_key_fingerprint":"a6:d1:08:0b:b0:cb:9b:5f:c4:ba:44:2a:97:26:19:8a",
   182  					"app_ssh_oauth_client":"ssh-proxy",
   183  					"logging_endpoint":"wss://loggregator.APISERVER",
   184  					"doppler_logging_endpoint":"wss://doppler.APISERVER"
   185  				}`
   186  				response = strings.Replace(response, "APISERVER", serverAPIURL, -1)
   187  				server.AppendHandlers(
   188  					ghttp.CombineHandlers(
   189  						ghttp.VerifyRequest("GET", "/v2/info"),
   190  						ghttp.RespondWith(http.StatusOK, response),
   191  					),
   192  				)
   193  			})
   194  
   195  			AfterEach(func() {
   196  				server.Close()
   197  			})
   198  
   199  			It("falls back to http and gives a warning", func() {
   200  				session := helpers.CF("api", server.URL(), "--skip-ssl-validation")
   201  				Eventually(session).Should(Say("Setting api endpoint to %s...", server.URL()))
   202  				Eventually(session).Should(Say("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended"))
   203  				Eventually(session).Should(Say("OK"))
   204  				Eventually(session).Should(Say("Not logged in. Use 'cf login' to log in."))
   205  				Eventually(session).Should(Exit(0))
   206  			})
   207  		})
   208  
   209  		It("sets SSL Disabled in the config file to true", func() {
   210  			command := exec.Command("cf", "api", apiURL, "--skip-ssl-validation")
   211  			session, err := Start(command, GinkgoWriter, GinkgoWriter)
   212  			Expect(err).NotTo(HaveOccurred())
   213  			Eventually(session).Should(Exit(0))
   214  
   215  			rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json"))
   216  			Expect(err).NotTo(HaveOccurred())
   217  
   218  			var configFile configv3.JSONConfig
   219  			err = json.Unmarshal(rawConfig, &configFile)
   220  			Expect(err).NotTo(HaveOccurred())
   221  
   222  			Expect(configFile.SkipSSLValidation).To(BeTrue())
   223  		})
   224  	})
   225  
   226  	Context("when skip-ssl-validation is not required", func() {
   227  		BeforeEach(func() {
   228  			if skipSSLValidation != "" {
   229  				Skip("SKIP_SSL_VALIDATION is enabled")
   230  			}
   231  		})
   232  
   233  		It("logs in without any warnings", func() {
   234  			session := helpers.CF("api", apiURL)
   235  			Eventually(session).Should(Say("Setting api endpoint to %s...", apiURL))
   236  			Consistently(session).ShouldNot(Say("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended"))
   237  			Eventually(session).Should(Say("OK"))
   238  			Eventually(session).Should(Say("Not logged in. Use 'cf login' to log in."))
   239  			Eventually(session).Should(Exit(0))
   240  		})
   241  
   242  		It("sets SSL Disabled in the config file to false", func() {
   243  			session := helpers.CF("api", apiURL, skipSSLValidation)
   244  			Eventually(session).Should(Exit(0))
   245  
   246  			rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json"))
   247  			Expect(err).NotTo(HaveOccurred())
   248  
   249  			var configFile configv3.JSONConfig
   250  			err = json.Unmarshal(rawConfig, &configFile)
   251  			Expect(err).NotTo(HaveOccurred())
   252  
   253  			Expect(configFile.SkipSSLValidation).To(BeTrue())
   254  		})
   255  	})
   256  
   257  	It("sets the config file", func() {
   258  		session := helpers.CF("api", apiURL, skipSSLValidation)
   259  		Eventually(session).Should(Exit(0))
   260  
   261  		rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json"))
   262  		Expect(err).NotTo(HaveOccurred())
   263  
   264  		var configFile configv3.JSONConfig
   265  		err = json.Unmarshal(rawConfig, &configFile)
   266  		Expect(err).NotTo(HaveOccurred())
   267  
   268  		Expect(configFile.ConfigVersion).To(Equal(3))
   269  		Expect(configFile.Target).To(Equal(apiURL))
   270  		Expect(configFile.APIVersion).To(MatchRegexp("\\d+\\.\\d+\\.\\d+"))
   271  		Expect(configFile.AuthorizationEndpoint).ToNot(BeEmpty())
   272  		Expect(configFile.DopplerEndpoint).To(MatchRegexp("^wss://"))
   273  		Expect(configFile.RoutingEndpoint).NotTo(BeEmpty())
   274  		Expect(configFile.UAAEndpoint).To(BeEmpty())
   275  		Expect(configFile.AccessToken).To(BeEmpty())
   276  		Expect(configFile.RefreshToken).To(BeEmpty())
   277  		Expect(configFile.TargetedOrganization.GUID).To(BeEmpty())
   278  		Expect(configFile.TargetedOrganization.Name).To(BeEmpty())
   279  		Expect(configFile.TargetedSpace.GUID).To(BeEmpty())
   280  		Expect(configFile.TargetedSpace.Name).To(BeEmpty())
   281  		Expect(configFile.TargetedSpace.AllowSSH).To(BeFalse())
   282  	})
   283  
   284  	It("handles API endpoints with trailing slash", func() {
   285  		session := helpers.CF("api", apiURL+"/", skipSSLValidation)
   286  		Eventually(session).Should(Exit(0))
   287  
   288  		helpers.LoginCF()
   289  
   290  		session = helpers.CF("orgs")
   291  		Eventually(session).Should(Exit(0))
   292  	})
   293  })