github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/configuration/coreconfig/config_repository_test.go (about)

     1  package coreconfig_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"code.cloudfoundry.org/cli/cf/configuration"
     9  	"code.cloudfoundry.org/cli/cf/configuration/configurationfakes"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/version"
    13  	"github.com/blang/semver"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("Configuration Repository", func() {
    20  	var (
    21  		config    coreconfig.Repository
    22  		persistor *configurationfakes.FakePersistor
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		persistor = new(configurationfakes.FakePersistor)
    27  		persistor.ExistsReturns(true)
    28  		config = coreconfig.NewRepositoryFromPersistor(persistor, func(err error) { panic(err) })
    29  	})
    30  
    31  	It("is threadsafe", func() {
    32  		performSaveCh := make(chan struct{})
    33  		beginSaveCh := make(chan struct{})
    34  		finishSaveCh := make(chan struct{})
    35  		finishReadCh := make(chan struct{})
    36  
    37  		persistor.SaveStub = func(configuration.DataInterface) error {
    38  			close(beginSaveCh)
    39  			<-performSaveCh
    40  			close(finishSaveCh)
    41  
    42  			return nil
    43  		}
    44  
    45  		go func() {
    46  			config.SetAPIEndpoint("foo")
    47  		}()
    48  
    49  		<-beginSaveCh
    50  
    51  		go func() {
    52  			config.APIEndpoint()
    53  			close(finishReadCh)
    54  		}()
    55  
    56  		Consistently(finishSaveCh).ShouldNot(BeClosed())
    57  		Consistently(finishReadCh).ShouldNot(BeClosed())
    58  
    59  		performSaveCh <- struct{}{}
    60  
    61  		Eventually(finishReadCh).Should(BeClosed())
    62  	})
    63  
    64  	It("has acccessor methods for all config fields", func() {
    65  		config.SetAPIEndpoint("http://api.the-endpoint")
    66  		Expect(config.APIEndpoint()).To(Equal("http://api.the-endpoint"))
    67  
    68  		config.SetAPIVersion("3")
    69  		Expect(config.APIVersion()).To(Equal("3"))
    70  
    71  		config.SetAuthenticationEndpoint("http://auth.the-endpoint")
    72  		Expect(config.AuthenticationEndpoint()).To(Equal("http://auth.the-endpoint"))
    73  
    74  		config.SetUaaEndpoint("http://uaa.the-endpoint")
    75  		Expect(config.UaaEndpoint()).To(Equal("http://uaa.the-endpoint"))
    76  
    77  		config.SetAccessToken("the-token")
    78  		Expect(config.AccessToken()).To(Equal("the-token"))
    79  
    80  		config.SetUAAOAuthClient("cf-oauth-client-id")
    81  		Expect(config.UAAOAuthClient()).To(Equal("cf-oauth-client-id"))
    82  
    83  		config.SetUAAOAuthClientSecret("cf-oauth-client-secret")
    84  		Expect(config.UAAOAuthClientSecret()).To(Equal("cf-oauth-client-secret"))
    85  
    86  		config.SetSSHOAuthClient("oauth-client-id")
    87  		Expect(config.SSHOAuthClient()).To(Equal("oauth-client-id"))
    88  
    89  		config.SetDopplerEndpoint("doppler.the-endpoint")
    90  		Expect(config.DopplerEndpoint()).To(Equal("doppler.the-endpoint"))
    91  
    92  		config.SetRefreshToken("the-token")
    93  		Expect(config.RefreshToken()).To(Equal("the-token"))
    94  
    95  		organization := models.OrganizationFields{Name: "the-org"}
    96  		config.SetOrganizationFields(organization)
    97  		Expect(config.OrganizationFields()).To(Equal(organization))
    98  
    99  		space := models.SpaceFields{Name: "the-space"}
   100  		config.SetSpaceFields(space)
   101  		Expect(config.SpaceFields()).To(Equal(space))
   102  
   103  		config.SetSSLDisabled(false)
   104  		Expect(config.IsSSLDisabled()).To(BeFalse())
   105  
   106  		config.SetLocale("en_US")
   107  		Expect(config.Locale()).To(Equal("en_US"))
   108  
   109  		config.SetPluginRepo(models.PluginRepo{Name: "repo", URL: "nowhere.com"})
   110  		Expect(config.PluginRepos()[0].Name).To(Equal("repo"))
   111  		Expect(config.PluginRepos()[0].URL).To(Equal("nowhere.com"))
   112  
   113  		s, _ := semver.Make("3.1")
   114  		Expect(config.IsMinAPIVersion(s)).To(Equal(false))
   115  
   116  		config.SetMinCLIVersion("6.5.0")
   117  		Expect(config.MinCLIVersion()).To(Equal("6.5.0"))
   118  
   119  		config.SetMinRecommendedCLIVersion("6.9.0")
   120  		Expect(config.MinRecommendedCLIVersion()).To(Equal("6.9.0"))
   121  	})
   122  
   123  	Describe("APIEndpoint", func() {
   124  		It("sanitizes the target URL", func() {
   125  			config.SetAPIEndpoint("http://api.the-endpoint/")
   126  			Expect(config.APIEndpoint()).To(Equal("http://api.the-endpoint"))
   127  		})
   128  	})
   129  
   130  	Describe("HasAPIEndpoint", func() {
   131  		Context("when both endpoint and version are set", func() {
   132  			BeforeEach(func() {
   133  				config.SetAPIEndpoint("http://example.org")
   134  				config.SetAPIVersion("42.1.2.3")
   135  			})
   136  
   137  			It("returns true", func() {
   138  				Expect(config.HasAPIEndpoint()).To(BeTrue())
   139  			})
   140  		})
   141  
   142  		Context("when endpoint is not set", func() {
   143  			BeforeEach(func() {
   144  				config.SetAPIVersion("42.1.2.3")
   145  			})
   146  
   147  			It("returns false", func() {
   148  				Expect(config.HasAPIEndpoint()).To(BeFalse())
   149  			})
   150  		})
   151  
   152  		Context("when version is not set", func() {
   153  			BeforeEach(func() {
   154  				config.SetAPIEndpoint("http://example.org")
   155  			})
   156  
   157  			It("returns false", func() {
   158  				Expect(config.HasAPIEndpoint()).To(BeFalse())
   159  			})
   160  		})
   161  	})
   162  
   163  	Describe("UserGUID", func() {
   164  		Context("with a valid access token", func() {
   165  			BeforeEach(func() {
   166  				config.SetAccessToken("bearer eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJjNDE4OTllNS1kZTE1LTQ5NGQtYWFiNC04ZmNlYzUxN2UwMDUiLCJzdWIiOiI3NzJkZGEzZi02NjlmLTQyNzYtYjJiZC05MDQ4NmFiZTFmNmYiLCJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwib3BlbmlkIiwicGFzc3dvcmQud3JpdGUiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInVzZXJfaWQiOiI3NzJkZGEzZi02NjlmLTQyNzYtYjJiZC05MDQ4NmFiZTFmNmYiLCJ1c2VyX25hbWUiOiJ1c2VyMUBleGFtcGxlLmNvbSIsImVtYWlsIjoidXNlcjFAZXhhbXBsZS5jb20iLCJpYXQiOjEzNzcwMjgzNTYsImV4cCI6MTM3NzAzNTU1NiwiaXNzIjoiaHR0cHM6Ly91YWEuYXJib3JnbGVuLmNmLWFwcC5jb20vb2F1dGgvdG9rZW4iLCJhdWQiOlsib3BlbmlkIiwiY2xvdWRfY29udHJvbGxlciIsInBhc3N3b3JkIl19.kjFJHi0Qir9kfqi2eyhHy6kdewhicAFu8hrPR1a5AxFvxGB45slKEjuP0_72cM_vEYICgZn3PcUUkHU9wghJO9wjZ6kiIKK1h5f2K9g-Iprv9BbTOWUODu1HoLIvg2TtGsINxcRYy_8LW1RtvQc1b4dBPoopaEH4no-BIzp0E5E")
   167  			})
   168  
   169  			It("returns the guid", func() {
   170  				Expect(config.UserGUID()).To(Equal("772dda3f-669f-4276-b2bd-90486abe1f6f"))
   171  			})
   172  		})
   173  
   174  		Context("with an invalid access token", func() {
   175  			BeforeEach(func() {
   176  				config.SetAccessToken("bearer eyJhbGciOiJSUzI1NiJ9")
   177  			})
   178  
   179  			It("returns an empty string", func() {
   180  				Expect(config.UserGUID()).To(BeEmpty())
   181  			})
   182  		})
   183  	})
   184  
   185  	Describe("UserEmail", func() {
   186  		Context("with a valid access token", func() {
   187  			BeforeEach(func() {
   188  				config.SetAccessToken("bearer eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJjNDE4OTllNS1kZTE1LTQ5NGQtYWFiNC04ZmNlYzUxN2UwMDUiLCJzdWIiOiI3NzJkZGEzZi02NjlmLTQyNzYtYjJiZC05MDQ4NmFiZTFmNmYiLCJzY29wZSI6WyJjbG91ZF9jb250cm9sbGVyLnJlYWQiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIiwib3BlbmlkIiwicGFzc3dvcmQud3JpdGUiXSwiY2xpZW50X2lkIjoiY2YiLCJjaWQiOiJjZiIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInVzZXJfaWQiOiI3NzJkZGEzZi02NjlmLTQyNzYtYjJiZC05MDQ4NmFiZTFmNmYiLCJ1c2VyX25hbWUiOiJ1c2VyMUBleGFtcGxlLmNvbSIsImVtYWlsIjoidXNlcjFAZXhhbXBsZS5jb20iLCJpYXQiOjEzNzcwMjgzNTYsImV4cCI6MTM3NzAzNTU1NiwiaXNzIjoiaHR0cHM6Ly91YWEuYXJib3JnbGVuLmNmLWFwcC5jb20vb2F1dGgvdG9rZW4iLCJhdWQiOlsib3BlbmlkIiwiY2xvdWRfY29udHJvbGxlciIsInBhc3N3b3JkIl19.kjFJHi0Qir9kfqi2eyhHy6kdewhicAFu8hrPR1a5AxFvxGB45slKEjuP0_72cM_vEYICgZn3PcUUkHU9wghJO9wjZ6kiIKK1h5f2K9g-Iprv9BbTOWUODu1HoLIvg2TtGsINxcRYy_8LW1RtvQc1b4dBPoopaEH4no-BIzp0E5E")
   189  			})
   190  
   191  			It("returns the email", func() {
   192  				Expect(config.UserEmail()).To(Equal("user1@example.com"))
   193  			})
   194  		})
   195  
   196  		Context("with an invalid access token", func() {
   197  			BeforeEach(func() {
   198  				config.SetAccessToken("bearer eyJhbGciOiJSUzI1NiJ9")
   199  			})
   200  
   201  			It("returns an empty string", func() {
   202  				Expect(config.UserEmail()).To(BeEmpty())
   203  			})
   204  		})
   205  	})
   206  
   207  	Describe("NewRepositoryFromFilepath", func() {
   208  		var configPath string
   209  
   210  		It("returns nil repository if no error handler provided", func() {
   211  			config = coreconfig.NewRepositoryFromFilepath(configPath, nil)
   212  			Expect(config).To(BeNil())
   213  		})
   214  
   215  		Context("when the configuration file doesn't exist", func() {
   216  			var tmpDir string
   217  
   218  			BeforeEach(func() {
   219  				tmpDir, err := ioutil.TempDir("", "test-config")
   220  				if err != nil {
   221  					Fail("Couldn't create tmp file")
   222  				}
   223  
   224  				configPath = filepath.Join(tmpDir, ".cf", "config.json")
   225  			})
   226  
   227  			AfterEach(func() {
   228  				if tmpDir != "" {
   229  					os.RemoveAll(tmpDir)
   230  				}
   231  			})
   232  
   233  			It("has sane defaults when there is no config to read", func() {
   234  				config = coreconfig.NewRepositoryFromFilepath(configPath, func(err error) {
   235  					panic(err)
   236  				})
   237  
   238  				Expect(config.APIEndpoint()).To(Equal(""))
   239  				Expect(config.APIVersion()).To(Equal(""))
   240  				Expect(config.AuthenticationEndpoint()).To(Equal(""))
   241  				Expect(config.AccessToken()).To(Equal(""))
   242  			})
   243  		})
   244  
   245  		Context("when the configuration version is older than the current version", func() {
   246  			BeforeEach(func() {
   247  				cwd, err := os.Getwd()
   248  				Expect(err).NotTo(HaveOccurred())
   249  				configPath = filepath.Join(cwd, "..", "..", "..", "fixtures", "config", "outdated-config", ".cf", "config.json")
   250  			})
   251  
   252  			It("returns a new empty config", func() {
   253  				config = coreconfig.NewRepositoryFromFilepath(configPath, func(err error) {
   254  					panic(err)
   255  				})
   256  
   257  				Expect(config.APIEndpoint()).To(Equal(""))
   258  			})
   259  		})
   260  	})
   261  
   262  	Describe("IsMinCLIVersion", func() {
   263  		It("returns true when the actual version is the default version string", func() {
   264  			Expect(config.IsMinCLIVersion(version.DefaultVersion)).To(BeTrue())
   265  		})
   266  
   267  		It("returns true when the MinCLIVersion is empty", func() {
   268  			config.SetMinCLIVersion("")
   269  			Expect(config.IsMinCLIVersion("1.2.3")).To(BeTrue())
   270  		})
   271  
   272  		It("returns false when the actual version is less than the MinCLIVersion", func() {
   273  			actualVersion := "1.2.3+abc123"
   274  			minCLIVersion := "1.2.4"
   275  			config.SetMinCLIVersion(minCLIVersion)
   276  			Expect(config.IsMinCLIVersion(actualVersion)).To(BeFalse())
   277  		})
   278  
   279  		It("returns true when the actual version is equal to the MinCLIVersion", func() {
   280  			actualVersion := "1.2.3+abc123"
   281  			minCLIVersion := "1.2.3"
   282  			config.SetMinCLIVersion(minCLIVersion)
   283  			Expect(config.IsMinCLIVersion(actualVersion)).To(BeTrue())
   284  		})
   285  
   286  		It("returns true when the actual version is greater than the MinCLIVersion", func() {
   287  			actualVersion := "1.2.3+abc123"
   288  			minCLIVersion := "1.2.2"
   289  			config.SetMinCLIVersion(minCLIVersion)
   290  			Expect(config.IsMinCLIVersion(actualVersion)).To(BeTrue())
   291  		})
   292  	})
   293  })