github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/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 testServer1CallCount++ 34 fmt.Fprintln(w, `{"plugins":[]}`) 35 }) 36 testServer1 = httptest.NewServer(h1) 37 38 testServer2CallCount = 0 39 h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 40 testServer2CallCount++ 41 fmt.Fprintln(w, `{"plugins":[]}`) 42 }) 43 testServer2 = httptest.NewServer(h2) 44 45 }) 46 47 AfterEach(func() { 48 testServer1.Close() 49 testServer2.Close() 50 }) 51 52 It("make query to all repos listed in config.json", func() { 53 repoActor.GetPlugins([]models.PluginRepo{ 54 { 55 Name: "repo1", 56 URL: testServer1.URL, 57 }, 58 { 59 Name: "repo2", 60 URL: testServer2.URL, 61 }, 62 }) 63 64 Expect(testServer1CallCount).To(Equal(1)) 65 Expect(testServer2CallCount).To(Equal(1)) 66 }) 67 68 It("lists each of the repos in config.json", func() { 69 list, _ := repoActor.GetPlugins([]models.PluginRepo{ 70 { 71 Name: "repo1", 72 URL: testServer1.URL, 73 }, 74 { 75 Name: "repo2", 76 URL: testServer2.URL, 77 }, 78 }) 79 80 Expect(list["repo1"]).NotTo(BeNil()) 81 Expect(list["repo2"]).NotTo(BeNil()) 82 }) 83 84 }) 85 86 Context("Getting data from repos", func() { 87 Context("When data is valid", func() { 88 BeforeEach(func() { 89 h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 90 fmt.Fprintln(w, `{"plugins":[ 91 { 92 "name":"plugin1", 93 "description":"none", 94 "version":"1.3.4", 95 "binaries":[ 96 { 97 "platform":"osx", 98 "url":"https://github.com/simonleung8/cli-plugin-echo/raw/master/bin/osx/echo", 99 "checksum":"2a087d5cddcfb057fbda91e611c33f46" 100 } 101 ] 102 }, 103 { 104 "name":"plugin2", 105 "binaries":[ 106 { 107 "platform":"windows", 108 "url":"http://going.no.where", 109 "checksum":"abcdefg" 110 } 111 ] 112 }] 113 }`) 114 }) 115 testServer1 = httptest.NewServer(h1) 116 117 }) 118 119 AfterEach(func() { 120 testServer1.Close() 121 }) 122 123 It("lists the info for each plugin", func() { 124 list, _ := repoActor.GetPlugins([]models.PluginRepo{ 125 { 126 Name: "repo1", 127 URL: testServer1.URL, 128 }, 129 }) 130 131 Expect(list["repo1"]).NotTo(BeNil()) 132 Expect(len(list["repo1"])).To(Equal(2)) 133 134 Expect(list["repo1"][0].Name).To(Equal("plugin1")) 135 Expect(list["repo1"][0].Description).To(Equal("none")) 136 Expect(list["repo1"][0].Version).To(Equal("1.3.4")) 137 Expect(list["repo1"][0].Binaries[0].Platform).To(Equal("osx")) 138 Expect(list["repo1"][1].Name).To(Equal("plugin2")) 139 Expect(list["repo1"][1].Binaries[0].Platform).To(Equal("windows")) 140 }) 141 142 }) 143 }) 144 145 Context("When data is invalid", func() { 146 Context("json is invalid", func() { 147 BeforeEach(func() { 148 h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 149 fmt.Fprintln(w, `"plugins":[]}`) 150 }) 151 testServer1 = httptest.NewServer(h1) 152 }) 153 154 AfterEach(func() { 155 testServer1.Close() 156 }) 157 158 It("informs user of invalid json", func() { 159 _, err := repoActor.GetPlugins([]models.PluginRepo{ 160 { 161 Name: "repo1", 162 URL: testServer1.URL, 163 }, 164 }) 165 166 Expect(err).To(ContainSubstrings( 167 []string{"Invalid json data"}, 168 )) 169 170 }) 171 172 }) 173 174 Context("when data is valid json, but not valid plugin repo data", func() { 175 BeforeEach(func() { 176 h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 177 fmt.Fprintln(w, `{"bad_plugin_tag":[]}`) 178 }) 179 testServer1 = httptest.NewServer(h1) 180 }) 181 182 AfterEach(func() { 183 testServer1.Close() 184 }) 185 186 It("informs user of invalid repo data", func() { 187 _, err := repoActor.GetPlugins([]models.PluginRepo{ 188 { 189 Name: "repo1", 190 URL: testServer1.URL, 191 }, 192 }) 193 194 Expect(err).To(ContainSubstrings( 195 []string{"Invalid data", "plugin data does not exist"}, 196 )) 197 198 }) 199 200 }) 201 }) 202 })