github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/actor/pluginaction/plugin_repository_test.go (about)

     1  package pluginaction_test
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	. "code.cloudfoundry.org/cli/actor/pluginaction"
     8  	"code.cloudfoundry.org/cli/actor/pluginaction/pluginactionfakes"
     9  	"code.cloudfoundry.org/cli/api/plugin"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Plugin Repository Actions", func() {
    16  	var (
    17  		actor            *Actor
    18  		fakeConfig       *pluginactionfakes.FakeConfig
    19  		fakePluginClient *pluginactionfakes.FakePluginClient
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeConfig = new(pluginactionfakes.FakeConfig)
    24  		fakePluginClient = new(pluginactionfakes.FakePluginClient)
    25  		actor = NewActor(fakeConfig, fakePluginClient)
    26  	})
    27  
    28  	Describe("AddPluginRepository", func() {
    29  		var err error
    30  
    31  		JustBeforeEach(func() {
    32  			err = actor.AddPluginRepository("some-repo", "some-URL")
    33  		})
    34  
    35  		Context("when passed a url without a scheme", func() {
    36  			It("prepends https://", func() {
    37  				_ = actor.AddPluginRepository("some-repo2", "some-URL")
    38  				url := fakePluginClient.GetPluginRepositoryArgsForCall(1)
    39  				Expect(strings.HasPrefix(url, "https://")).To(BeTrue())
    40  			})
    41  		})
    42  
    43  		Context("when passed a schemeless IP address with a port", func() {
    44  			It("prepends https://", func() {
    45  				_ = actor.AddPluginRepository("some-repo2", "127.0.0.1:5000")
    46  				url := fakePluginClient.GetPluginRepositoryArgsForCall(1)
    47  				Expect(strings.HasPrefix(url, "https://")).To(BeTrue())
    48  			})
    49  		})
    50  
    51  		Context("when url ends with a trailing slash", func() {
    52  			It("removes the trailing slash", func() {
    53  				_ = actor.AddPluginRepository("some-repo2", "some-URL/")
    54  				url := fakePluginClient.GetPluginRepositoryArgsForCall(1)
    55  				Expect(strings.HasSuffix(url, "/")).To(BeFalse())
    56  			})
    57  		})
    58  
    59  		Context("when the repository name is taken", func() {
    60  			BeforeEach(func() {
    61  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
    62  					{
    63  						Name: "repo-1",
    64  						URL:  "https://URL-1",
    65  					},
    66  					{
    67  						Name: "some-repo",
    68  						URL:  "https://www.com",
    69  					},
    70  				})
    71  			})
    72  
    73  			It("returns the RepositoryNameTakenError", func() {
    74  				Expect(err).To(MatchError(RepositoryNameTakenError{Name: "some-repo"}))
    75  			})
    76  		})
    77  
    78  		Context("when the repository name and URL are already taken in the same repo", func() {
    79  			BeforeEach(func() {
    80  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
    81  					{
    82  						Name: "some-repo",
    83  						URL:  "https://some-URL",
    84  					},
    85  				})
    86  			})
    87  
    88  			It("returns a RepositoryAlreadyExistsError", func() {
    89  				Expect(err).To(MatchError(RepositoryAlreadyExistsError{Name: "some-repo", URL: "https://some-URL"}))
    90  
    91  				Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(0))
    92  				Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(0))
    93  			})
    94  
    95  			Context("when the repository name is the same except for case sensitivity", func() {
    96  				BeforeEach(func() {
    97  					fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
    98  						{
    99  							Name: "sOmE-rEpO",
   100  							URL:  "https://some-URL",
   101  						},
   102  					})
   103  				})
   104  
   105  				It("returns a RepositoryAlreadyExistsError", func() {
   106  					Expect(err).To(MatchError(RepositoryAlreadyExistsError{Name: "sOmE-rEpO", URL: "https://some-URL"}))
   107  
   108  					Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(0))
   109  					Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(0))
   110  				})
   111  			})
   112  		})
   113  
   114  		Context("when the repository URL is taken", func() {
   115  			BeforeEach(func() {
   116  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
   117  					{
   118  						Name: "repo-1",
   119  						URL:  "https://URL-1",
   120  					},
   121  					{
   122  						Name: "repo-2",
   123  						URL:  "https://some-URL",
   124  					},
   125  				})
   126  			})
   127  
   128  			It("adds the repo to the config and returns nil", func() {
   129  				Expect(err).ToNot(HaveOccurred())
   130  
   131  				Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(1))
   132  				Expect(fakePluginClient.GetPluginRepositoryArgsForCall(0)).To(Equal("https://some-URL"))
   133  
   134  				Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(1))
   135  				repoName, repoURL := fakeConfig.AddPluginRepositoryArgsForCall(0)
   136  				Expect(repoName).To(Equal("some-repo"))
   137  				Expect(repoURL).To(Equal("https://some-URL"))
   138  			})
   139  		})
   140  
   141  		Context("when getting the repository errors", func() {
   142  			BeforeEach(func() {
   143  				fakePluginClient.GetPluginRepositoryReturns(plugin.PluginRepository{}, errors.New("generic-error"))
   144  			})
   145  
   146  			It("returns an AddPluginRepositoryError", func() {
   147  				Expect(err).To(MatchError(AddPluginRepositoryError{
   148  					Name:    "some-repo",
   149  					URL:     "https://some-URL",
   150  					Message: "generic-error",
   151  				}))
   152  			})
   153  		})
   154  
   155  		Context("when no errors occur", func() {
   156  			BeforeEach(func() {
   157  				fakePluginClient.GetPluginRepositoryReturns(plugin.PluginRepository{}, nil)
   158  			})
   159  
   160  			It("adds the repo to the config and returns nil", func() {
   161  				Expect(err).ToNot(HaveOccurred())
   162  
   163  				Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(1))
   164  				Expect(fakePluginClient.GetPluginRepositoryArgsForCall(0)).To(Equal("https://some-URL"))
   165  
   166  				Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(1))
   167  				repoName, repoURL := fakeConfig.AddPluginRepositoryArgsForCall(0)
   168  				Expect(repoName).To(Equal("some-repo"))
   169  				Expect(repoURL).To(Equal("https://some-URL"))
   170  			})
   171  		})
   172  	})
   173  
   174  	Describe("GetPluginRepository", func() {
   175  		Context("when the repository is registered", func() {
   176  			BeforeEach(func() {
   177  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
   178  					{Name: "some-REPO", URL: "some-url"},
   179  				})
   180  			})
   181  
   182  			It("returns the repository case-insensitively", func() {
   183  				pluginRepo, err := actor.GetPluginRepository("sOmE-rEpO")
   184  				Expect(err).ToNot(HaveOccurred())
   185  				Expect(pluginRepo).To(Equal(configv3.PluginRepository{Name: "some-REPO", URL: "some-url"}))
   186  			})
   187  		})
   188  
   189  		Context("when the repository is not registered", func() {
   190  			It("returns a RepositoryNotRegisteredError", func() {
   191  				_, err := actor.GetPluginRepository("some-rEPO")
   192  				Expect(err).To(MatchError(RepositoryNotRegisteredError{Name: "some-rEPO"}))
   193  			})
   194  		})
   195  	})
   196  
   197  	Describe("IsPluginRepositoryRegistered", func() {
   198  		Context("when the repository is registered", func() {
   199  			BeforeEach(func() {
   200  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
   201  					{Name: "some-repo"},
   202  				})
   203  			})
   204  
   205  			It("returns true", func() {
   206  				Expect(actor.IsPluginRepositoryRegistered("some-repo")).To(BeTrue())
   207  			})
   208  		})
   209  
   210  		Context("when the repository is not registered", func() {
   211  			BeforeEach(func() {
   212  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{})
   213  			})
   214  
   215  			It("returns true", func() {
   216  				Expect(actor.IsPluginRepositoryRegistered("some-repo")).To(BeFalse())
   217  			})
   218  		})
   219  	})
   220  })