github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/pluginaction/plugin_repository_test.go (about)

     1  package pluginaction_test
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	. "github.com/liamawhite/cli-with-i18n/actor/pluginaction"
     8  	"github.com/liamawhite/cli-with-i18n/actor/pluginaction/pluginactionfakes"
     9  	"github.com/liamawhite/cli-with-i18n/api/plugin"
    10  	"github.com/liamawhite/cli-with-i18n/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 the repository name is taken", func() {
    52  			BeforeEach(func() {
    53  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
    54  					{
    55  						Name: "repo-1",
    56  						URL:  "https://URL-1",
    57  					},
    58  					{
    59  						Name: "some-repo",
    60  						URL:  "https://www.com",
    61  					},
    62  				})
    63  			})
    64  
    65  			It("returns the RepositoryNameTakenError", func() {
    66  				Expect(err).To(MatchError(RepositoryNameTakenError{Name: "some-repo"}))
    67  			})
    68  		})
    69  
    70  		Context("when the repository name and URL are already taken in the same repo", func() {
    71  			BeforeEach(func() {
    72  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
    73  					{
    74  						Name: "some-repo",
    75  						URL:  "https://some-URL",
    76  					},
    77  				})
    78  			})
    79  
    80  			It("returns a RepositoryAlreadyExistsError", func() {
    81  				Expect(err).To(MatchError(RepositoryAlreadyExistsError{Name: "some-repo", URL: "https://some-URL"}))
    82  
    83  				Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(0))
    84  				Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(0))
    85  			})
    86  		})
    87  
    88  		Context("when the repository name is the same except for case sensitivity", func() {
    89  			BeforeEach(func() {
    90  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
    91  					{
    92  						Name: "sOmE-rEpO",
    93  						URL:  "https://some-URL",
    94  					},
    95  				})
    96  			})
    97  
    98  			It("returns a RepositoryAlreadyExistsError", func() {
    99  				Expect(err).To(MatchError(RepositoryAlreadyExistsError{Name: "sOmE-rEpO", URL: "https://some-URL"}))
   100  
   101  				Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(0))
   102  				Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(0))
   103  			})
   104  		})
   105  
   106  		Context("when the repository name is the same and repostiroy URL is the same except for trailing slash", func() {
   107  			BeforeEach(func() {
   108  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
   109  					{
   110  						Name: "some-repo",
   111  						URL:  "https://some-URL",
   112  					},
   113  				})
   114  			})
   115  
   116  			It("returns a RepositoryAlreadyExistsError", func() {
   117  				err = actor.AddPluginRepository("some-repo", "some-URL/")
   118  				Expect(err).To(MatchError(RepositoryAlreadyExistsError{Name: "some-repo", URL: "https://some-URL"}))
   119  
   120  				Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(0))
   121  				Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(0))
   122  			})
   123  		})
   124  
   125  		Context("when the repository URL is taken", func() {
   126  			BeforeEach(func() {
   127  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
   128  					{
   129  						Name: "repo-1",
   130  						URL:  "https://URL-1",
   131  					},
   132  					{
   133  						Name: "repo-2",
   134  						URL:  "https://some-URL",
   135  					},
   136  				})
   137  			})
   138  
   139  			It("adds the repo to the config and returns nil", func() {
   140  				Expect(err).ToNot(HaveOccurred())
   141  
   142  				Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(1))
   143  				Expect(fakePluginClient.GetPluginRepositoryArgsForCall(0)).To(Equal("https://some-URL"))
   144  
   145  				Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(1))
   146  				repoName, repoURL := fakeConfig.AddPluginRepositoryArgsForCall(0)
   147  				Expect(repoName).To(Equal("some-repo"))
   148  				Expect(repoURL).To(Equal("https://some-URL"))
   149  			})
   150  		})
   151  
   152  		Context("when getting the repository errors", func() {
   153  			BeforeEach(func() {
   154  				fakePluginClient.GetPluginRepositoryReturns(plugin.PluginRepository{}, errors.New("generic-error"))
   155  			})
   156  
   157  			It("returns an AddPluginRepositoryError", func() {
   158  				Expect(err).To(MatchError(AddPluginRepositoryError{
   159  					Name:    "some-repo",
   160  					URL:     "https://some-URL",
   161  					Message: "generic-error",
   162  				}))
   163  			})
   164  		})
   165  
   166  		Context("when no errors occur", func() {
   167  			BeforeEach(func() {
   168  				fakePluginClient.GetPluginRepositoryReturns(plugin.PluginRepository{}, nil)
   169  			})
   170  
   171  			It("adds the repo to the config and returns nil", func() {
   172  				Expect(err).ToNot(HaveOccurred())
   173  
   174  				Expect(fakePluginClient.GetPluginRepositoryCallCount()).To(Equal(1))
   175  				Expect(fakePluginClient.GetPluginRepositoryArgsForCall(0)).To(Equal("https://some-URL"))
   176  
   177  				Expect(fakeConfig.AddPluginRepositoryCallCount()).To(Equal(1))
   178  				repoName, repoURL := fakeConfig.AddPluginRepositoryArgsForCall(0)
   179  				Expect(repoName).To(Equal("some-repo"))
   180  				Expect(repoURL).To(Equal("https://some-URL"))
   181  			})
   182  		})
   183  	})
   184  
   185  	Describe("GetPluginRepository", func() {
   186  		Context("when the repository is registered", func() {
   187  			BeforeEach(func() {
   188  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
   189  					{Name: "some-REPO", URL: "some-url"},
   190  				})
   191  			})
   192  
   193  			It("returns the repository case-insensitively", func() {
   194  				pluginRepo, err := actor.GetPluginRepository("sOmE-rEpO")
   195  				Expect(err).ToNot(HaveOccurred())
   196  				Expect(pluginRepo).To(Equal(configv3.PluginRepository{Name: "some-REPO", URL: "some-url"}))
   197  			})
   198  		})
   199  
   200  		Context("when the repository is not registered", func() {
   201  			It("returns a RepositoryNotRegisteredError", func() {
   202  				_, err := actor.GetPluginRepository("some-rEPO")
   203  				Expect(err).To(MatchError(RepositoryNotRegisteredError{Name: "some-rEPO"}))
   204  			})
   205  		})
   206  	})
   207  
   208  	Describe("IsPluginRepositoryRegistered", func() {
   209  		Context("when the repository is registered", func() {
   210  			BeforeEach(func() {
   211  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{
   212  					{Name: "some-repo"},
   213  				})
   214  			})
   215  
   216  			It("returns true", func() {
   217  				Expect(actor.IsPluginRepositoryRegistered("some-repo")).To(BeTrue())
   218  			})
   219  		})
   220  
   221  		Context("when the repository is not registered", func() {
   222  			BeforeEach(func() {
   223  				fakeConfig.PluginRepositoriesReturns([]configv3.PluginRepository{})
   224  			})
   225  
   226  			It("returns true", func() {
   227  				Expect(actor.IsPluginRepositoryRegistered("some-repo")).To(BeFalse())
   228  			})
   229  		})
   230  	})
   231  })