github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/actors/plugin_repo/plugin_repo_test.go (about)

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