github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/cf/actors/pluginrepo/plugin_repo_test.go (about) 1 package pluginrepo_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 8 . "code.cloudfoundry.org/cli/cf/actors/pluginrepo" 9 "code.cloudfoundry.org/cli/cf/models" 10 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("PluginRepo", func() { 17 var ( 18 repoActor PluginRepo 19 testServer1CallCount int 20 testServer2CallCount int 21 testServer1 *httptest.Server 22 testServer2 *httptest.Server 23 ) 24 25 BeforeEach(func() { 26 repoActor = NewPluginRepo() 27 }) 28 29 Context("request data from all repos", func() { 30 BeforeEach(func() { 31 testServer1CallCount = 0 32 h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 33 defer GinkgoRecover() 34 testServer1CallCount++ 35 fmt.Fprintln(w, `{"plugins":[]}`) 36 Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) 37 }) 38 testServer1 = httptest.NewServer(h1) 39 40 testServer2CallCount = 0 41 h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 42 defer GinkgoRecover() 43 testServer2CallCount++ 44 fmt.Fprintln(w, `{"plugins":[]}`) 45 Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) 46 }) 47 testServer2 = httptest.NewServer(h2) 48 49 }) 50 51 AfterEach(func() { 52 testServer1.Close() 53 testServer2.Close() 54 }) 55 56 It("make query to all repos listed in config.json", func() { 57 repoActor.GetPlugins([]models.PluginRepo{ 58 { 59 Name: "repo1", 60 URL: testServer1.URL, 61 }, 62 { 63 Name: "repo2", 64 URL: testServer2.URL, 65 }, 66 }) 67 68 Expect(testServer1CallCount).To(Equal(1)) 69 Expect(testServer2CallCount).To(Equal(1)) 70 }) 71 72 It("lists each of the repos in config.json", func() { 73 list, _ := repoActor.GetPlugins([]models.PluginRepo{ 74 { 75 Name: "repo1", 76 URL: testServer1.URL, 77 }, 78 { 79 Name: "repo2", 80 URL: testServer2.URL, 81 }, 82 }) 83 84 Expect(list["repo1"]).NotTo(BeNil()) 85 Expect(list["repo2"]).NotTo(BeNil()) 86 }) 87 88 }) 89 90 Context("Getting data from repos", func() { 91 Context("When data is valid", func() { 92 BeforeEach(func() { 93 h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 94 defer GinkgoRecover() 95 fmt.Fprintln(w, `{"plugins":[ 96 { 97 "name":"plugin1", 98 "description":"none", 99 "version":"1.3.4", 100 "binaries":[ 101 { 102 "platform":"osx", 103 "url":"https://github.com/simonleung8/cli-plugin-echo/raw/master/bin/osx/echo", 104 "checksum":"2a087d5cddcfb057fbda91e611c33f46" 105 } 106 ] 107 }, 108 { 109 "name":"plugin2", 110 "binaries":[ 111 { 112 "platform":"windows", 113 "url":"http://going.no.where", 114 "checksum":"abcdefg" 115 } 116 ] 117 }] 118 }`) 119 Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) 120 }) 121 testServer1 = httptest.NewServer(h1) 122 123 }) 124 125 AfterEach(func() { 126 testServer1.Close() 127 }) 128 129 It("lists the info for each plugin", func() { 130 list, _ := repoActor.GetPlugins([]models.PluginRepo{ 131 { 132 Name: "repo1", 133 URL: testServer1.URL, 134 }, 135 }) 136 137 Expect(list["repo1"]).NotTo(BeNil()) 138 Expect(len(list["repo1"])).To(Equal(2)) 139 140 Expect(list["repo1"][0].Name).To(Equal("plugin1")) 141 Expect(list["repo1"][0].Description).To(Equal("none")) 142 Expect(list["repo1"][0].Version).To(Equal("1.3.4")) 143 Expect(list["repo1"][0].Binaries[0].Platform).To(Equal("osx")) 144 Expect(list["repo1"][1].Name).To(Equal("plugin2")) 145 Expect(list["repo1"][1].Binaries[0].Platform).To(Equal("windows")) 146 }) 147 148 }) 149 }) 150 151 Context("When data is invalid", func() { 152 Context("json is invalid", func() { 153 BeforeEach(func() { 154 h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 155 defer GinkgoRecover() 156 fmt.Fprintln(w, `"plugins":[]}`) 157 Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) 158 }) 159 testServer1 = httptest.NewServer(h1) 160 }) 161 162 AfterEach(func() { 163 testServer1.Close() 164 }) 165 166 It("informs user of invalid json", func() { 167 _, err := repoActor.GetPlugins([]models.PluginRepo{ 168 { 169 Name: "repo1", 170 URL: testServer1.URL, 171 }, 172 }) 173 174 Expect(err).To(ContainSubstrings( 175 []string{"Invalid json data"}, 176 )) 177 178 }) 179 180 }) 181 182 Context("when data is valid json, but not valid plugin repo data", func() { 183 BeforeEach(func() { 184 h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 185 defer GinkgoRecover() 186 fmt.Fprintln(w, `{"bad_plugin_tag":[]}`) 187 Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1")) 188 }) 189 testServer1 = httptest.NewServer(h1) 190 }) 191 192 AfterEach(func() { 193 testServer1.Close() 194 }) 195 196 It("informs user of invalid repo data", func() { 197 _, err := repoActor.GetPlugins([]models.PluginRepo{ 198 { 199 Name: "repo1", 200 URL: testServer1.URL, 201 }, 202 }) 203 204 Expect(err).To(ContainSubstrings( 205 []string{"Invalid data", "plugin data does not exist"}, 206 )) 207 208 }) 209 210 }) 211 }) 212 })