github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/pluginrepo/repo_plugins_test.go (about)

     1  package pluginrepo_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/actors/pluginrepo/pluginrepofakes"
     5  	"code.cloudfoundry.org/cli/cf/commands/pluginrepo"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    11  
    12  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    13  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    14  
    15  	clipr "code.cloudfoundry.org/cli-plugin-repo/web"
    16  
    17  	"code.cloudfoundry.org/cli/cf/flags"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("repo-plugins", func() {
    23  	var (
    24  		ui             *testterm.FakeUI
    25  		config         coreconfig.Repository
    26  		fakePluginRepo *pluginrepofakes.FakePluginRepo
    27  		deps           commandregistry.Dependency
    28  		cmd            *pluginrepo.RepoPlugins
    29  		flagContext    flags.FlagContext
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		fakePluginRepo = new(pluginrepofakes.FakePluginRepo)
    34  		ui = &testterm.FakeUI{}
    35  		config = testconfig.NewRepositoryWithDefaults()
    36  
    37  		deps = commandregistry.Dependency{
    38  			UI:         ui,
    39  			Config:     config,
    40  			PluginRepo: fakePluginRepo,
    41  		}
    42  
    43  		cmd = new(pluginrepo.RepoPlugins)
    44  		cmd = cmd.SetDependency(deps, false).(*pluginrepo.RepoPlugins)
    45  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    46  	})
    47  
    48  	var callRepoPlugins = func(args ...string) error {
    49  		err := flagContext.Parse(args...)
    50  		if err != nil {
    51  			return err
    52  		}
    53  
    54  		cmd.Execute(flagContext)
    55  		return nil
    56  	}
    57  
    58  	Context("when using the default CF-Community repo", func() {
    59  		BeforeEach(func() {
    60  			config.SetPluginRepo(models.PluginRepo{
    61  				Name: "cf",
    62  				URL:  "http://plugins.cloudfoundry.org",
    63  			})
    64  		})
    65  
    66  		It("uses https when pointing to plugins.cloudfoundry.org", func() {
    67  			err := callRepoPlugins()
    68  			Expect(err).NotTo(HaveOccurred())
    69  
    70  			Expect(fakePluginRepo.GetPluginsCallCount()).To(Equal(1))
    71  			Expect(fakePluginRepo.GetPluginsArgsForCall(0)[0].Name).To(Equal("cf"))
    72  			Expect(fakePluginRepo.GetPluginsArgsForCall(0)[0].URL).To(Equal("https://plugins.cloudfoundry.org"))
    73  			Expect(len(fakePluginRepo.GetPluginsArgsForCall(0))).To(Equal(1))
    74  		})
    75  	})
    76  
    77  	Context("when using other plugin repos", func() {
    78  		BeforeEach(func() {
    79  			config.SetPluginRepo(models.PluginRepo{
    80  				Name: "repo1",
    81  				URL:  "",
    82  			})
    83  
    84  			config.SetPluginRepo(models.PluginRepo{
    85  				Name: "repo2",
    86  				URL:  "",
    87  			})
    88  		})
    89  
    90  		Context("If repo name is provided by '-r'", func() {
    91  			It("list plugins from just the named repo", func() {
    92  				err := callRepoPlugins("-r", "repo2")
    93  				Expect(err).NotTo(HaveOccurred())
    94  
    95  				Expect(fakePluginRepo.GetPluginsArgsForCall(0)[0].Name).To(Equal("repo2"))
    96  				Expect(len(fakePluginRepo.GetPluginsArgsForCall(0))).To(Equal(1))
    97  
    98  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"Getting plugins from repository 'repo2'"}))
    99  			})
   100  		})
   101  
   102  		Context("If no repo name is provided", func() {
   103  			It("list plugins from just the named repo", func() {
   104  				err := callRepoPlugins()
   105  				Expect(err).NotTo(HaveOccurred())
   106  
   107  				Expect(fakePluginRepo.GetPluginsArgsForCall(0)[0].Name).To(Equal("repo1"))
   108  				Expect(len(fakePluginRepo.GetPluginsArgsForCall(0))).To(Equal(2))
   109  				Expect(fakePluginRepo.GetPluginsArgsForCall(0)[1].Name).To(Equal("repo2"))
   110  
   111  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"Getting plugins from all repositories"}))
   112  			})
   113  		})
   114  
   115  		Context("when GetPlugins returns a list of plugin meta data", func() {
   116  			It("lists all plugin data", func() {
   117  				result := make(map[string][]clipr.Plugin)
   118  				result["repo1"] = []clipr.Plugin{
   119  					{
   120  						Name:        "plugin1",
   121  						Description: "none1",
   122  					},
   123  				}
   124  				result["repo2"] = []clipr.Plugin{
   125  					{
   126  						Name:        "plugin2",
   127  						Description: "none2",
   128  					},
   129  				}
   130  				fakePluginRepo.GetPluginsReturns(result, []string{})
   131  
   132  				err := callRepoPlugins()
   133  				Expect(err).NotTo(HaveOccurred())
   134  
   135  				Expect(ui.Outputs()).NotTo(ContainSubstrings([]string{"Logged errors:"}))
   136  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"repo1"}))
   137  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"plugin1"}))
   138  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"repo2"}))
   139  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"plugin2"}))
   140  			})
   141  		})
   142  
   143  		Context("If errors are reported back from GetPlugins()", func() {
   144  			It("informs user about the errors", func() {
   145  				fakePluginRepo.GetPluginsReturns(nil, []string{
   146  					"error from repo1",
   147  					"error from repo2",
   148  				})
   149  
   150  				err := callRepoPlugins()
   151  				Expect(err).NotTo(HaveOccurred())
   152  
   153  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"Logged errors:"}))
   154  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"error from repo1"}))
   155  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"error from repo2"}))
   156  			})
   157  		})
   158  	})
   159  })