github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/util/configv3/json_config_test.go (about) 1 package configv3_test 2 3 import ( 4 "time" 5 6 . "code.cloudfoundry.org/cli/util/configv3" 7 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("JSONConfig", func() { 13 var homeDir string 14 var config *Config 15 16 BeforeEach(func() { 17 homeDir = setup() 18 }) 19 20 AfterEach(func() { 21 teardown(homeDir) 22 }) 23 24 Describe("AccessToken", func() { 25 BeforeEach(func() { 26 rawConfig := `{ "AccessToken":"some-token" }` 27 setConfig(homeDir, rawConfig) 28 29 var err error 30 config, err = LoadConfig() 31 Expect(err).ToNot(HaveOccurred()) 32 Expect(config).ToNot(BeNil()) 33 }) 34 35 It("returns fields directly from config", func() { 36 Expect(config.AccessToken()).To(Equal("some-token")) 37 }) 38 }) 39 40 Describe("APIVersion", func() { 41 It("returns the api version", func() { 42 config = &Config{ 43 ConfigFile: JSONConfig{ 44 APIVersion: "2.59.0", 45 }, 46 } 47 48 Expect(config.APIVersion()).To(Equal("2.59.0")) 49 }) 50 }) 51 52 Describe("CurrentUser", func() { 53 Context("when using client credentials and the user token is set", func() { 54 It("returns the user", func() { 55 config = &Config{ 56 ConfigFile: JSONConfig{ 57 AccessToken: AccessTokenForClientUsers, 58 }, 59 } 60 61 user, err := config.CurrentUser() 62 Expect(err).ToNot(HaveOccurred()) 63 Expect(user).To(Equal(User{ 64 Name: "potato-face", 65 })) 66 }) 67 }) 68 69 Context("when using user/password and the user token is set", func() { 70 It("returns the user", func() { 71 config = &Config{ 72 ConfigFile: JSONConfig{ 73 AccessToken: AccessTokenForHumanUsers, 74 }, 75 } 76 77 user, err := config.CurrentUser() 78 Expect(err).ToNot(HaveOccurred()) 79 Expect(user).To(Equal(User{ 80 Name: "admin", 81 })) 82 }) 83 }) 84 85 Context("when the user token is blank", func() { 86 It("returns the user", func() { 87 config = new(Config) 88 user, err := config.CurrentUser() 89 Expect(err).ToNot(HaveOccurred()) 90 Expect(user).To(Equal(User{})) 91 }) 92 }) 93 }) 94 95 Describe("HasTargetedOrganization", func() { 96 Context("when an organization is targeted", func() { 97 It("returns true", func() { 98 config = new(Config) 99 config.SetOrganizationInformation("guid-value-1", "my-org-name") 100 Expect(config.HasTargetedOrganization()).To(BeTrue()) 101 }) 102 }) 103 104 Context("when an organization is not targeted", func() { 105 It("returns false", func() { 106 config = new(Config) 107 Expect(config.HasTargetedOrganization()).To(BeFalse()) 108 }) 109 }) 110 }) 111 112 Describe("HasTargetedSpace", func() { 113 Context("when an space is targeted", func() { 114 It("returns true", func() { 115 config = new(Config) 116 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 117 Expect(config.HasTargetedSpace()).To(BeTrue()) 118 }) 119 }) 120 121 Context("when an space is not targeted", func() { 122 It("returns false", func() { 123 config = new(Config) 124 Expect(config.HasTargetedSpace()).To(BeFalse()) 125 }) 126 }) 127 }) 128 129 Describe("MinCLIVersion", func() { 130 It("returns the minimum CLI version the CC requires", func() { 131 config = &Config{ 132 ConfigFile: JSONConfig{ 133 MinCLIVersion: "1.0.0", 134 }, 135 } 136 137 Expect(config.MinCLIVersion()).To(Equal("1.0.0")) 138 }) 139 }) 140 141 Describe("OverallPollingTimeout", func() { 142 Context("when AsyncTimeout is set in config", func() { 143 BeforeEach(func() { 144 rawConfig := `{ "AsyncTimeout":5 }` 145 setConfig(homeDir, rawConfig) 146 147 var err error 148 config, err = LoadConfig() 149 Expect(err).ToNot(HaveOccurred()) 150 Expect(config).ToNot(BeNil()) 151 }) 152 153 It("returns the timeout in duration form", func() { 154 Expect(config.OverallPollingTimeout()).To(Equal(5 * time.Minute)) 155 }) 156 }) 157 }) 158 159 Describe("RefreshToken", func() { 160 BeforeEach(func() { 161 rawConfig := `{ "RefreshToken":"some-token" }` 162 setConfig(homeDir, rawConfig) 163 164 var err error 165 config, err = LoadConfig() 166 Expect(err).ToNot(HaveOccurred()) 167 Expect(config).ToNot(BeNil()) 168 }) 169 170 It("returns fields directly from config", func() { 171 Expect(config.RefreshToken()).To(Equal("some-token")) 172 }) 173 }) 174 175 Describe("SetAccessToken", func() { 176 It("sets the authentication token information", func() { 177 config = new(Config) 178 config.SetAccessToken("I am the access token") 179 Expect(config.ConfigFile.AccessToken).To(Equal("I am the access token")) 180 }) 181 }) 182 183 Describe("SetOrganizationInformation", func() { 184 It("sets the organization GUID and name", func() { 185 config = new(Config) 186 config.SetOrganizationInformation("guid-value-1", "my-org-name") 187 188 Expect(config.ConfigFile.TargetedOrganization.GUID).To(Equal("guid-value-1")) 189 Expect(config.ConfigFile.TargetedOrganization.Name).To(Equal("my-org-name")) 190 }) 191 }) 192 193 Describe("SetRefreshToken", func() { 194 It("sets the refresh token information", func() { 195 config = new(Config) 196 config.SetRefreshToken("I am the refresh token") 197 Expect(config.ConfigFile.RefreshToken).To(Equal("I am the refresh token")) 198 }) 199 }) 200 201 Describe("SetSpaceInformation", func() { 202 It("sets the space GUID, name, and AllowSSH", func() { 203 config = new(Config) 204 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 205 206 Expect(config.ConfigFile.TargetedSpace.GUID).To(Equal("guid-value-1")) 207 Expect(config.ConfigFile.TargetedSpace.Name).To(Equal("my-org-name")) 208 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeTrue()) 209 }) 210 }) 211 212 Describe("SetTargetInformation", func() { 213 It("sets the api target and other related endpoints", func() { 214 config = &Config{ 215 ConfigFile: JSONConfig{ 216 TargetedOrganization: Organization{ 217 GUID: "this-is-a-guid", 218 Name: "jo bobo jim boo", 219 }, 220 TargetedSpace: Space{ 221 GUID: "this-is-a-guid", 222 Name: "jo bobo jim boo", 223 AllowSSH: true, 224 }, 225 }, 226 } 227 config.SetTargetInformation( 228 "https://api.foo.com", 229 "2.59.31", 230 "https://login.foo.com", 231 "2.0.0", 232 "wws://doppler.foo.com:443", 233 "https://api.foo.com/routing", 234 true, 235 ) 236 237 Expect(config.ConfigFile.Target).To(Equal("https://api.foo.com")) 238 Expect(config.ConfigFile.APIVersion).To(Equal("2.59.31")) 239 Expect(config.ConfigFile.AuthorizationEndpoint).To(Equal("https://login.foo.com")) 240 Expect(config.ConfigFile.MinCLIVersion).To(Equal("2.0.0")) 241 Expect(config.ConfigFile.DopplerEndpoint).To(Equal("wws://doppler.foo.com:443")) 242 Expect(config.ConfigFile.RoutingEndpoint).To(Equal("https://api.foo.com/routing")) 243 Expect(config.ConfigFile.SkipSSLValidation).To(BeTrue()) 244 245 Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty()) 246 Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty()) 247 Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty()) 248 Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty()) 249 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse()) 250 }) 251 }) 252 253 Describe("SetTokenInformation", func() { 254 It("sets the authentication token information", func() { 255 config = new(Config) 256 config.SetTokenInformation("I am the access token", "I am the refresh token", "I am the SSH OAuth client") 257 258 Expect(config.ConfigFile.AccessToken).To(Equal("I am the access token")) 259 Expect(config.ConfigFile.RefreshToken).To(Equal("I am the refresh token")) 260 Expect(config.ConfigFile.SSHOAuthClient).To(Equal("I am the SSH OAuth client")) 261 }) 262 }) 263 264 Describe("SetUAAClientCredentials", func() { 265 It("sets the UAA client credentials", func() { 266 config = new(Config) 267 config.SetUAAClientCredentials("some-uaa-client", "some-uaa-client-secret") 268 Expect(config.ConfigFile.UAAOAuthClient).To(Equal("some-uaa-client")) 269 Expect(config.ConfigFile.UAAOAuthClientSecret).To(Equal("some-uaa-client-secret")) 270 }) 271 }) 272 273 Describe("SetUAAEndpoint", func() { 274 It("sets the UAA endpoint", func() { 275 config = new(Config) 276 config.SetUAAGrantType("some-uaa-grant-type") 277 Expect(config.ConfigFile.UAAGrantType).To(Equal("some-uaa-grant-type")) 278 }) 279 }) 280 281 Describe("SetUAAEndpoint", func() { 282 It("sets the UAA endpoint", func() { 283 config = new(Config) 284 config.SetUAAEndpoint("some-uaa-endpoint.com") 285 Expect(config.ConfigFile.UAAEndpoint).To(Equal("some-uaa-endpoint.com")) 286 }) 287 }) 288 289 Describe("SkipSSLValidation", func() { 290 BeforeEach(func() { 291 rawConfig := `{ "SSLDisabled":true }` 292 setConfig(homeDir, rawConfig) 293 294 var err error 295 config, err = LoadConfig() 296 Expect(err).ToNot(HaveOccurred()) 297 Expect(config).ToNot(BeNil()) 298 }) 299 300 It("returns fields directly from config", func() { 301 Expect(config.SkipSSLValidation()).To(BeTrue()) 302 }) 303 }) 304 305 Describe("SSHOAuthClient", func() { 306 BeforeEach(func() { 307 rawConfig := `{ "SSHOAuthClient":"some-ssh-client" }` 308 setConfig(homeDir, rawConfig) 309 310 var err error 311 config, err = LoadConfig() 312 Expect(err).ToNot(HaveOccurred()) 313 Expect(config).ToNot(BeNil()) 314 }) 315 316 It("returns the client ID", func() { 317 Expect(config.SSHOAuthClient()).To(Equal("some-ssh-client")) 318 }) 319 }) 320 321 Describe("Target", func() { 322 BeforeEach(func() { 323 rawConfig := `{ "Target":"https://api.foo.com" }` 324 setConfig(homeDir, rawConfig) 325 326 var err error 327 config, err = LoadConfig() 328 Expect(err).ToNot(HaveOccurred()) 329 Expect(config).ToNot(BeNil()) 330 }) 331 332 It("returns the target", func() { 333 Expect(config.Target()).To(Equal("https://api.foo.com")) 334 }) 335 }) 336 337 Describe("TargetedOrganization", func() { 338 It("returns the organization", func() { 339 organization := Organization{ 340 GUID: "some-guid", 341 Name: "some-org", 342 } 343 config = &Config{ 344 ConfigFile: JSONConfig{ 345 TargetedOrganization: organization, 346 }, 347 } 348 349 Expect(config.TargetedOrganization()).To(Equal(organization)) 350 }) 351 }) 352 353 Describe("TargetedSpace", func() { 354 It("returns the space", func() { 355 space := Space{ 356 GUID: "some-guid", 357 Name: "some-space", 358 } 359 config = &Config{ 360 ConfigFile: JSONConfig{ 361 TargetedSpace: space, 362 }, 363 } 364 365 Expect(config.TargetedSpace()).To(Equal(space)) 366 }) 367 }) 368 369 Describe("UAAOAuthClient", func() { 370 BeforeEach(func() { 371 rawConfig := `{ "UAAOAuthClient":"some-client" }` 372 setConfig(homeDir, rawConfig) 373 374 var err error 375 config, err = LoadConfig() 376 Expect(err).ToNot(HaveOccurred()) 377 Expect(config).ToNot(BeNil()) 378 }) 379 380 It("returns the client ID", func() { 381 Expect(config.UAAOAuthClient()).To(Equal("some-client")) 382 }) 383 }) 384 385 Describe("UAAOAuthClientSecret", func() { 386 BeforeEach(func() { 387 rawConfig := ` 388 { 389 "UAAOAuthClient": "some-client-id", 390 "UAAOAuthClientSecret": "some-client-secret" 391 }` 392 setConfig(homeDir, rawConfig) 393 394 var err error 395 config, err = LoadConfig() 396 Expect(err).ToNot(HaveOccurred()) 397 Expect(config).ToNot(BeNil()) 398 }) 399 400 It("returns the client secret", func() { 401 Expect(config.UAAOAuthClientSecret()).To(Equal("some-client-secret")) 402 }) 403 }) 404 405 Describe("UAAGrantType", func() { 406 BeforeEach(func() { 407 rawConfig := ` { "UAAGrantType": "some-grant-type" }` 408 setConfig(homeDir, rawConfig) 409 410 var err error 411 config, err = LoadConfig() 412 Expect(err).ToNot(HaveOccurred()) 413 Expect(config).ToNot(BeNil()) 414 }) 415 416 It("returns the client secret", func() { 417 Expect(config.UAAGrantType()).To(Equal("some-grant-type")) 418 }) 419 }) 420 421 Describe("UnsetUserInformation", func() { 422 BeforeEach(func() { 423 config = new(Config) 424 config.SetAccessToken("some-access-token") 425 config.SetRefreshToken("some-refresh-token") 426 config.SetUAAGrantType("client-credentials") 427 config.SetUAAClientCredentials("some-client", "some-client-secret") 428 config.SetOrganizationInformation("some-org-guid", "some-org") 429 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 430 }) 431 432 It("resets all user information", func() { 433 config.UnsetUserInformation() 434 435 Expect(config.ConfigFile.AccessToken).To(BeEmpty()) 436 Expect(config.ConfigFile.RefreshToken).To(BeEmpty()) 437 Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty()) 438 Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty()) 439 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse()) 440 Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty()) 441 Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty()) 442 Expect(config.ConfigFile.UAAGrantType).To(BeEmpty()) 443 Expect(config.ConfigFile.UAAOAuthClient).To(Equal(DefaultUAAOAuthClient)) 444 Expect(config.ConfigFile.UAAOAuthClientSecret).To(Equal(DefaultUAAOAuthClientSecret)) 445 }) 446 }) 447 448 Describe("UnsetOrganizationAndSpaceInformation", func() { 449 BeforeEach(func() { 450 config = new(Config) 451 config.SetOrganizationInformation("some-org-guid", "some-org") 452 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 453 }) 454 455 It("resets the org GUID and name", func() { 456 config.UnsetOrganizationAndSpaceInformation() 457 458 Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty()) 459 Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty()) 460 Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty()) 461 Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty()) 462 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse()) 463 }) 464 }) 465 466 Describe("UnsetSpaceInformation", func() { 467 BeforeEach(func() { 468 config = new(Config) 469 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 470 }) 471 472 It("resets the space GUID, name, and AllowSSH to default values", func() { 473 config.UnsetSpaceInformation() 474 475 Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty()) 476 Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty()) 477 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse()) 478 }) 479 }) 480 })