code.cloudfoundry.org/cli@v7.1.0+incompatible/cf/commands/pluginrepo/add_plugin_repo_test.go (about) 1 package pluginrepo_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 8 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 9 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 10 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 11 12 "code.cloudfoundry.org/cli/cf/commandregistry" 13 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 14 "code.cloudfoundry.org/cli/cf/models" 15 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 16 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 17 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 ) 21 22 var _ = Describe("add-plugin-repo", func() { 23 var ( 24 ui *testterm.FakeUI 25 config coreconfig.Repository 26 requirementsFactory *requirementsfakes.FakeFactory 27 testServer *httptest.Server 28 deps commandregistry.Dependency 29 ) 30 31 updateCommandDependency := func(pluginCall bool) { 32 deps.UI = ui 33 deps.Config = config 34 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("add-plugin-repo").SetDependency(deps, pluginCall)) 35 } 36 37 BeforeEach(func() { 38 ui = &testterm.FakeUI{} 39 requirementsFactory = new(requirementsfakes.FakeFactory) 40 config = testconfig.NewRepositoryWithDefaults() 41 }) 42 43 var callAddPluginRepo = func(args []string) bool { 44 return testcmd.RunCLICommand("add-plugin-repo", args, requirementsFactory, updateCommandDependency, false, ui) 45 } 46 47 Context("When repo server is valid", func() { 48 BeforeEach(func() { 49 50 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 51 fmt.Fprintln(w, `{"plugins":[ 52 { 53 "name":"echo", 54 "description":"none", 55 "version":"4", 56 "binaries":[ 57 { 58 "platform":"osx", 59 "url":"https://github.com/simonleung8/cli-plugin-echo/raw/master/bin/osx/echo", 60 "checksum":"2a087d5cddcfb057fbda91e611c33f46" 61 } 62 ] 63 }] 64 }`) 65 }) 66 testServer = httptest.NewServer(h) 67 }) 68 69 AfterEach(func() { 70 testServer.Close() 71 }) 72 73 It("saves the repo url into config", func() { 74 callAddPluginRepo([]string{"repo", testServer.URL}) 75 76 Expect(config.PluginRepos()[0].Name).To(Equal("repo")) 77 Expect(config.PluginRepos()[0].URL).To(Equal(testServer.URL)) 78 }) 79 }) 80 81 Context("repo name already existing", func() { 82 BeforeEach(func() { 83 config.SetPluginRepo(models.PluginRepo{Name: "repo", URL: "http://repo.com"}) 84 }) 85 86 It("informs user of the already existing repo", func() { 87 88 callAddPluginRepo([]string{"repo", "http://repo2.com"}) 89 90 Expect(ui.Outputs()).To(ContainSubstrings( 91 []string{"Plugin repo named \"repo\"", " already exists"}, 92 )) 93 }) 94 }) 95 96 Context("repo address already existing", func() { 97 BeforeEach(func() { 98 config.SetPluginRepo(models.PluginRepo{Name: "repo1", URL: "http://repo.com"}) 99 }) 100 101 It("informs user of the already existing repo", func() { 102 103 callAddPluginRepo([]string{"repo2", "http://repo.com"}) 104 105 Expect(ui.Outputs()).To(ContainSubstrings( 106 []string{"http://repo.com (repo1)", " already exists."}, 107 )) 108 }) 109 }) 110 111 Context("When repo server is not valid", func() { 112 113 Context("server url is invalid", func() { 114 It("informs user of invalid url which does not has prefix http", func() { 115 116 callAddPluginRepo([]string{"repo", "msn.com"}) 117 118 Expect(ui.Outputs()).To(ContainSubstrings( 119 []string{"msn.com", "is not a valid url"}, 120 )) 121 }) 122 123 It("should not contain the tip", func() { 124 callAddPluginRepo([]string{"repo", "msn.com"}) 125 126 Expect(ui.Outputs()).NotTo(ContainSubstrings( 127 []string{"TIP: If you are behind a firewall and require an HTTP proxy, verify the https_proxy environment variable is correctly set. Else, check your network connection."}, 128 )) 129 }) 130 }) 131 132 Context("server does not has a '/list' endpoint", func() { 133 It("informs user of invalid repo server", func() { 134 135 callAddPluginRepo([]string{"repo", "https://google.com"}) 136 137 Expect(ui.Outputs()).To(ContainSubstrings( 138 []string{"https://google.com/list", "is not responding."}, 139 )) 140 }) 141 }) 142 143 Context("server responses with invalid json", func() { 144 BeforeEach(func() { 145 146 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 147 fmt.Fprintln(w, `"plugins":[]}`) 148 }) 149 testServer = httptest.NewServer(h) 150 }) 151 152 AfterEach(func() { 153 testServer.Close() 154 }) 155 156 It("informs user of invalid repo server", func() { 157 callAddPluginRepo([]string{"repo", testServer.URL}) 158 159 Expect(ui.Outputs()).To(ContainSubstrings( 160 []string{"Error processing data from server"}, 161 )) 162 }) 163 }) 164 165 Context("server responses with json without 'plugins' object", func() { 166 BeforeEach(func() { 167 168 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 169 fmt.Fprintln(w, `{"bad_plugins":[ 170 { 171 "name": "plugin1", 172 "description": "none" 173 } 174 ]}`) 175 }) 176 testServer = httptest.NewServer(h) 177 }) 178 179 AfterEach(func() { 180 testServer.Close() 181 }) 182 183 It("informs user of invalid repo server", func() { 184 callAddPluginRepo([]string{"repo", testServer.URL}) 185 186 Expect(ui.Outputs()).To(ContainSubstrings( 187 []string{"\"Plugins\" object not found in the responded data"}, 188 )) 189 }) 190 }) 191 192 Context("When connection could not be established", func() { 193 It("prints a tip", func() { 194 callAddPluginRepo([]string{"repo", "https://broccoli.nonexistanttld:"}) 195 196 Expect(ui.Outputs()).To(ContainSubstrings( 197 []string{"TIP: If you are behind a firewall and require an HTTP proxy, verify the https_proxy environment variable is correctly set. Else, check your network connection."}, 198 )) 199 }) 200 }) 201 202 Context("server responds with an http error", func() { 203 BeforeEach(func() { 204 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 205 w.WriteHeader(http.StatusInternalServerError) 206 }) 207 testServer = httptest.NewServer(h) 208 }) 209 210 AfterEach(func() { 211 testServer.Close() 212 }) 213 214 It("does not print a tip", func() { 215 callAddPluginRepo([]string{"repo", testServer.URL}) 216 217 Expect(ui.Outputs()).NotTo(ContainSubstrings( 218 []string{"TIP: If you are behind a firewall and require an HTTP proxy, verify the https_proxy environment variable is correctly set. Else, check your network connection."}, 219 )) 220 }) 221 }) 222 }) 223 })