github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/api_test.go (about) 1 package commands_test 2 3 import ( 4 "fmt" 5 6 "github.com/cloudfoundry/cli/cf" 7 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 8 "github.com/cloudfoundry/cli/cf/command_registry" 9 "github.com/cloudfoundry/cli/cf/configuration/core_config" 10 "github.com/cloudfoundry/cli/cf/errors" 11 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 12 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 13 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 14 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 18 . "github.com/cloudfoundry/cli/testhelpers/matchers" 19 ) 20 21 var _ = Describe("api command", func() { 22 var ( 23 config core_config.Repository 24 endpointRepo *testapi.FakeEndpointRepo 25 deps command_registry.Dependency 26 requirementsFactory *testreq.FakeReqFactory 27 ui *testterm.FakeUI 28 ) 29 30 updateCommandDependency := func(pluginCall bool) { 31 deps.Ui = ui 32 deps.Config = config 33 deps.RepoLocator = deps.RepoLocator.SetEndpointRepository(endpointRepo) 34 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("api").SetDependency(deps, pluginCall)) 35 } 36 37 callApi := func(args []string, config core_config.Repository, endpointRepo *testapi.FakeEndpointRepo) { 38 testcmd.RunCliCommand("api", args, requirementsFactory, updateCommandDependency, false) 39 } 40 41 BeforeEach(func() { 42 ui = new(testterm.FakeUI) 43 requirementsFactory = &testreq.FakeReqFactory{} 44 config = testconfig.NewRepository() 45 endpointRepo = &testapi.FakeEndpointRepo{} 46 deps = command_registry.NewDependency() 47 }) 48 49 Context("when the api endpoint's ssl certificate is invalid", func() { 50 It("warns the user and prints out a tip", func() { 51 endpointRepo.UpdateEndpointError = errors.NewInvalidSSLCert("https://buttontomatoes.org", "why? no. go away") 52 callApi([]string{"https://buttontomatoes.org"}, config, endpointRepo) 53 54 Expect(ui.Outputs).To(ContainSubstrings( 55 []string{"FAILED"}, 56 []string{"SSL Cert", "https://buttontomatoes.org"}, 57 []string{"TIP", "--skip-ssl-validation"}, 58 )) 59 }) 60 }) 61 62 Context("when the user does not provide an endpoint", func() { 63 Context("when the endpoint is set in the config", func() { 64 var ( 65 requirementsFactory *testreq.FakeReqFactory 66 ) 67 68 BeforeEach(func() { 69 config.SetApiEndpoint("https://api.run.pivotal.io") 70 config.SetApiVersion("2.0") 71 config.SetSSLDisabled(true) 72 73 requirementsFactory = &testreq.FakeReqFactory{} 74 }) 75 76 It("prints out the api endpoint and appropriately sets the config", func() { 77 callApi([]string{}, config, endpointRepo) 78 79 Expect(ui.Outputs).To(ContainSubstrings([]string{"https://api.run.pivotal.io", "2.0"})) 80 Expect(config.IsSSLDisabled()).To(BeTrue()) 81 }) 82 83 Context("when the --unset flag is passed", func() { 84 It("unsets the ApiEndpoint", func() { 85 callApi([]string{"--unset"}, config, endpointRepo) 86 87 Expect(ui.Outputs).To(ContainSubstrings( 88 []string{"Unsetting api endpoint..."}, 89 []string{"OK"}, 90 []string{"No api endpoint set."}, 91 )) 92 Expect(config.ApiEndpoint()).To(Equal("")) 93 }) 94 }) 95 }) 96 97 Context("when the endpoint is not set in the config", func() { 98 It("prompts the user to set an endpoint", func() { 99 callApi([]string{}, config, endpointRepo) 100 101 Expect(ui.Outputs).To(ContainSubstrings( 102 []string{"No api endpoint set", fmt.Sprintf("Use '%s api' to set an endpoint", cf.Name())}, 103 )) 104 }) 105 }) 106 }) 107 108 Context("when the user provides the --skip-ssl-validation flag", func() { 109 It("updates the SSLDisabled field in config", func() { 110 config.SetSSLDisabled(false) 111 callApi([]string{"--skip-ssl-validation", "https://example.com"}, config, endpointRepo) 112 113 Expect(config.IsSSLDisabled()).To(Equal(true)) 114 }) 115 }) 116 117 Context("the user provides an endpoint", func() { 118 Describe("when the user passed in the skip-ssl-validation flag", func() { 119 It("disables SSL validation in the config", func() { 120 callApi([]string{"--skip-ssl-validation", "https://example.com"}, config, endpointRepo) 121 122 Expect(endpointRepo.UpdateEndpointReceived).To(Equal("https://example.com")) 123 Expect(config.IsSSLDisabled()).To(BeTrue()) 124 }) 125 }) 126 127 Context("when the user passed in the unset flag", func() { 128 Context("when the config.ApiEndpoint is set", func() { 129 BeforeEach(func() { 130 config.SetApiEndpoint("some-silly-thing") 131 ui = new(testterm.FakeUI) 132 }) 133 134 It("unsets the ApiEndpoint", func() { 135 callApi([]string{"--unset", "https://example.com"}, config, endpointRepo) 136 137 Expect(ui.Outputs).To(ContainSubstrings( 138 []string{"Unsetting api endpoint..."}, 139 []string{"OK"}, 140 []string{"No api endpoint set."}, 141 )) 142 Expect(config.ApiEndpoint()).To(Equal("")) 143 }) 144 }) 145 146 Context("when the config.ApiEndpoint is empty", func() { 147 It("unsets the ApiEndpoint", func() { 148 callApi([]string{"--unset", "https://example.com"}, config, endpointRepo) 149 150 Expect(ui.Outputs).To(ContainSubstrings( 151 []string{"Unsetting api endpoint..."}, 152 []string{"OK"}, 153 []string{"No api endpoint set."}, 154 )) 155 Expect(config.ApiEndpoint()).To(Equal("")) 156 }) 157 }) 158 159 }) 160 161 Context("when the ssl certificate is valid", func() { 162 It("updates the api endpoint with the given url", func() { 163 callApi([]string{"https://example.com"}, config, endpointRepo) 164 Expect(endpointRepo.UpdateEndpointReceived).To(Equal("https://example.com")) 165 Expect(ui.Outputs).To(ContainSubstrings( 166 []string{"Setting api endpoint to", "example.com"}, 167 []string{"OK"}, 168 )) 169 }) 170 171 It("trims trailing slashes from the api endpoint", func() { 172 callApi([]string{"https://example.com/"}, config, endpointRepo) 173 Expect(endpointRepo.UpdateEndpointReceived).To(Equal("https://example.com")) 174 Expect(ui.Outputs).To(ContainSubstrings( 175 []string{"Setting api endpoint to", "example.com"}, 176 []string{"OK"}, 177 )) 178 }) 179 }) 180 181 Context("when the ssl certificate is invalid", func() { 182 BeforeEach(func() { 183 endpointRepo.UpdateEndpointError = errors.NewInvalidSSLCert("https://example.com", "it don't work") 184 }) 185 186 It("fails and gives the user a helpful message about skipping", func() { 187 callApi([]string{"https://example.com"}, config, endpointRepo) 188 189 Expect(config.ApiEndpoint()).To(Equal("")) 190 Expect(ui.Outputs).To(ContainSubstrings( 191 []string{"Invalid SSL Cert", "https://example.com"}, 192 []string{"TIP", "api"}, 193 )) 194 }) 195 }) 196 197 Describe("unencrypted http endpoints", func() { 198 It("warns the user", func() { 199 callApi([]string{"http://example.com"}, config, endpointRepo) 200 Expect(ui.Outputs).To(ContainSubstrings([]string{"Warning"})) 201 }) 202 }) 203 }) 204 })