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