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