github.com/arunkumar7540/cli@v6.45.0+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 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 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 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("CurrentUserName", func() { 96 When("using client credentials and the user token is set", func() { 97 It("returns the username", func() { 98 config = &Config{ 99 ConfigFile: JSONConfig{ 100 AccessToken: AccessTokenForClientUsers, 101 }, 102 } 103 104 username, err := config.CurrentUserName() 105 Expect(err).ToNot(HaveOccurred()) 106 Expect(username).To(Equal("potato-face")) 107 }) 108 }) 109 110 When("using user/password and the user token is set", func() { 111 It("returns the username", func() { 112 config = &Config{ 113 ConfigFile: JSONConfig{ 114 AccessToken: AccessTokenForHumanUsers, 115 }, 116 } 117 118 username, err := config.CurrentUserName() 119 Expect(err).ToNot(HaveOccurred()) 120 Expect(username).To(Equal("admin")) 121 }) 122 }) 123 124 When("the user token is blank", func() { 125 It("returns an empty string", func() { 126 config = new(Config) 127 username, err := config.CurrentUserName() 128 Expect(err).ToNot(HaveOccurred()) 129 Expect(username).To(BeEmpty()) 130 }) 131 }) 132 }) 133 134 Describe("HasTargetedOrganization", func() { 135 When("an organization is targeted", func() { 136 It("returns true", func() { 137 config = new(Config) 138 config.SetOrganizationInformation("guid-value-1", "my-org-name") 139 Expect(config.HasTargetedOrganization()).To(BeTrue()) 140 }) 141 }) 142 143 When("an organization is not targeted", func() { 144 It("returns false", func() { 145 config = new(Config) 146 Expect(config.HasTargetedOrganization()).To(BeFalse()) 147 }) 148 }) 149 }) 150 151 Describe("HasTargetedSpace", func() { 152 When("an space is targeted", func() { 153 It("returns true", func() { 154 config = new(Config) 155 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 156 Expect(config.HasTargetedSpace()).To(BeTrue()) 157 }) 158 }) 159 160 When("an space is not targeted", func() { 161 It("returns false", func() { 162 config = new(Config) 163 Expect(config.HasTargetedSpace()).To(BeFalse()) 164 }) 165 }) 166 }) 167 168 Describe("MinCLIVersion", func() { 169 It("returns the minimum CLI version the CC requires", func() { 170 config = &Config{ 171 ConfigFile: JSONConfig{ 172 MinCLIVersion: "1.0.0", 173 }, 174 } 175 176 Expect(config.MinCLIVersion()).To(Equal("1.0.0")) 177 }) 178 }) 179 180 Describe("OverallPollingTimeout", func() { 181 When("AsyncTimeout is set in config", func() { 182 BeforeEach(func() { 183 rawConfig := `{ "AsyncTimeout":5 }` 184 setConfig(homeDir, rawConfig) 185 186 var err error 187 config, err = LoadConfig() 188 Expect(err).ToNot(HaveOccurred()) 189 Expect(config).ToNot(BeNil()) 190 }) 191 192 It("returns the timeout in duration form", func() { 193 Expect(config.OverallPollingTimeout()).To(Equal(5 * time.Minute)) 194 }) 195 }) 196 }) 197 198 Describe("RefreshToken", func() { 199 BeforeEach(func() { 200 rawConfig := `{ "RefreshToken":"some-token" }` 201 setConfig(homeDir, rawConfig) 202 203 var err error 204 config, err = LoadConfig() 205 Expect(err).ToNot(HaveOccurred()) 206 Expect(config).ToNot(BeNil()) 207 }) 208 209 It("returns fields directly from config", func() { 210 Expect(config.RefreshToken()).To(Equal("some-token")) 211 }) 212 }) 213 214 Describe("SetAccessToken", func() { 215 It("sets the authentication token information", func() { 216 config = new(Config) 217 config.SetAccessToken("I am the access token") 218 Expect(config.ConfigFile.AccessToken).To(Equal("I am the access token")) 219 }) 220 }) 221 222 Describe("SetOrganizationInformation", func() { 223 It("sets the organization GUID and name", func() { 224 config = new(Config) 225 config.SetOrganizationInformation("guid-value-1", "my-org-name") 226 227 Expect(config.ConfigFile.TargetedOrganization.GUID).To(Equal("guid-value-1")) 228 Expect(config.ConfigFile.TargetedOrganization.Name).To(Equal("my-org-name")) 229 }) 230 }) 231 232 Describe("SetRefreshToken", func() { 233 It("sets the refresh token information", func() { 234 config = new(Config) 235 config.SetRefreshToken("I am the refresh token") 236 Expect(config.ConfigFile.RefreshToken).To(Equal("I am the refresh token")) 237 }) 238 }) 239 240 Describe("SetSpaceInformation", func() { 241 It("sets the space GUID, name, and AllowSSH", func() { 242 config = new(Config) 243 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 244 245 Expect(config.ConfigFile.TargetedSpace.GUID).To(Equal("guid-value-1")) 246 Expect(config.ConfigFile.TargetedSpace.Name).To(Equal("my-org-name")) 247 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeTrue()) 248 }) 249 }) 250 251 Describe("SetTargetInformation", func() { 252 It("sets the api target and other related endpoints", func() { 253 config = &Config{ 254 ConfigFile: JSONConfig{ 255 TargetedOrganization: Organization{ 256 GUID: "this-is-a-guid", 257 Name: "jo bobo jim boo", 258 }, 259 TargetedSpace: Space{ 260 GUID: "this-is-a-guid", 261 Name: "jo bobo jim boo", 262 AllowSSH: true, 263 }, 264 }, 265 } 266 config.SetTargetInformation( 267 "https://api.foo.com", 268 "2.59.31", 269 "https://login.foo.com", 270 "2.0.0", 271 "wws://doppler.foo.com:443", 272 "https://api.foo.com/routing", 273 true, 274 ) 275 276 Expect(config.ConfigFile.Target).To(Equal("https://api.foo.com")) 277 Expect(config.ConfigFile.APIVersion).To(Equal("2.59.31")) 278 Expect(config.ConfigFile.AuthorizationEndpoint).To(Equal("https://login.foo.com")) 279 Expect(config.ConfigFile.MinCLIVersion).To(Equal("2.0.0")) 280 Expect(config.ConfigFile.DopplerEndpoint).To(Equal("wws://doppler.foo.com:443")) 281 Expect(config.ConfigFile.RoutingEndpoint).To(Equal("https://api.foo.com/routing")) 282 Expect(config.ConfigFile.SkipSSLValidation).To(BeTrue()) 283 284 Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty()) 285 Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty()) 286 Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty()) 287 Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty()) 288 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse()) 289 }) 290 }) 291 292 Describe("SetTokenInformation", func() { 293 It("sets the authentication token information", func() { 294 config = new(Config) 295 config.SetTokenInformation("I am the access token", "I am the refresh token", "I am the SSH OAuth client") 296 297 Expect(config.ConfigFile.AccessToken).To(Equal("I am the access token")) 298 Expect(config.ConfigFile.RefreshToken).To(Equal("I am the refresh token")) 299 Expect(config.ConfigFile.SSHOAuthClient).To(Equal("I am the SSH OAuth client")) 300 }) 301 }) 302 303 Describe("SetUAAClientCredentials", func() { 304 It("sets the UAA client credentials", func() { 305 config = new(Config) 306 config.SetUAAClientCredentials("some-uaa-client", "some-uaa-client-secret") 307 Expect(config.ConfigFile.UAAOAuthClient).To(Equal("some-uaa-client")) 308 Expect(config.ConfigFile.UAAOAuthClientSecret).To(Equal("some-uaa-client-secret")) 309 }) 310 }) 311 312 Describe("SetUAAEndpoint", func() { 313 It("sets the UAA endpoint", func() { 314 config = new(Config) 315 config.SetUAAEndpoint("some-uaa-endpoint.com") 316 Expect(config.ConfigFile.UAAEndpoint).To(Equal("some-uaa-endpoint.com")) 317 }) 318 }) 319 320 Describe("SetUAAGrantType", func() { 321 It("sets the UAA endpoint", func() { 322 config = new(Config) 323 config.SetUAAGrantType("some-uaa-grant-type") 324 Expect(config.ConfigFile.UAAGrantType).To(Equal("some-uaa-grant-type")) 325 }) 326 }) 327 328 Describe("SkipSSLValidation", func() { 329 BeforeEach(func() { 330 rawConfig := `{ "SSLDisabled":true }` 331 setConfig(homeDir, rawConfig) 332 333 var err error 334 config, err = LoadConfig() 335 Expect(err).ToNot(HaveOccurred()) 336 Expect(config).ToNot(BeNil()) 337 }) 338 339 It("returns fields directly from config", func() { 340 Expect(config.SkipSSLValidation()).To(BeTrue()) 341 }) 342 }) 343 344 Describe("SSHOAuthClient", func() { 345 BeforeEach(func() { 346 rawConfig := `{ "SSHOAuthClient":"some-ssh-client" }` 347 setConfig(homeDir, rawConfig) 348 349 var err error 350 config, err = LoadConfig() 351 Expect(err).ToNot(HaveOccurred()) 352 Expect(config).ToNot(BeNil()) 353 }) 354 355 It("returns the client ID", func() { 356 Expect(config.SSHOAuthClient()).To(Equal("some-ssh-client")) 357 }) 358 }) 359 360 Describe("Target", func() { 361 BeforeEach(func() { 362 rawConfig := `{ "Target":"https://api.foo.com" }` 363 setConfig(homeDir, rawConfig) 364 365 var err error 366 config, err = LoadConfig() 367 Expect(err).ToNot(HaveOccurred()) 368 Expect(config).ToNot(BeNil()) 369 }) 370 371 It("returns the target", func() { 372 Expect(config.Target()).To(Equal("https://api.foo.com")) 373 }) 374 }) 375 376 Describe("TargetedOrganization", func() { 377 It("returns the organization", func() { 378 organization := Organization{ 379 GUID: "some-guid", 380 Name: "some-org", 381 } 382 config = &Config{ 383 ConfigFile: JSONConfig{ 384 TargetedOrganization: organization, 385 }, 386 } 387 388 Expect(config.TargetedOrganization()).To(Equal(organization)) 389 }) 390 }) 391 392 Describe("TargetedOrganizationName", func() { 393 It("returns the name of targeted organization", func() { 394 organization := Organization{ 395 GUID: "some-guid", 396 Name: "some-org", 397 } 398 config = &Config{ 399 ConfigFile: JSONConfig{ 400 TargetedOrganization: organization, 401 }, 402 } 403 404 Expect(config.TargetedOrganizationName()).To(Equal(organization.Name)) 405 }) 406 }) 407 408 Describe("TargetedSpace", func() { 409 It("returns the space", func() { 410 space := Space{ 411 GUID: "some-guid", 412 Name: "some-space", 413 } 414 config = &Config{ 415 ConfigFile: JSONConfig{ 416 TargetedSpace: space, 417 }, 418 } 419 420 Expect(config.TargetedSpace()).To(Equal(space)) 421 }) 422 }) 423 424 Describe("UAAGrantType", func() { 425 BeforeEach(func() { 426 rawConfig := ` { "UAAGrantType": "some-grant-type" }` 427 setConfig(homeDir, rawConfig) 428 429 var err error 430 config, err = LoadConfig() 431 Expect(err).ToNot(HaveOccurred()) 432 Expect(config).ToNot(BeNil()) 433 }) 434 435 It("returns the client secret", func() { 436 Expect(config.UAAGrantType()).To(Equal("some-grant-type")) 437 }) 438 }) 439 440 Describe("UAAOAuthClient", func() { 441 BeforeEach(func() { 442 rawConfig := `{ "UAAOAuthClient":"some-client" }` 443 setConfig(homeDir, rawConfig) 444 445 var err error 446 config, err = LoadConfig() 447 Expect(err).ToNot(HaveOccurred()) 448 Expect(config).ToNot(BeNil()) 449 }) 450 451 It("returns the client ID", func() { 452 Expect(config.UAAOAuthClient()).To(Equal("some-client")) 453 }) 454 }) 455 456 Describe("UAAOAuthClientSecret", func() { 457 BeforeEach(func() { 458 rawConfig := ` 459 { 460 "UAAOAuthClient": "some-client-id", 461 "UAAOAuthClientSecret": "some-client-secret" 462 }` 463 setConfig(homeDir, rawConfig) 464 465 var err error 466 config, err = LoadConfig() 467 Expect(err).ToNot(HaveOccurred()) 468 Expect(config).ToNot(BeNil()) 469 }) 470 471 It("returns the client secret", func() { 472 Expect(config.UAAOAuthClientSecret()).To(Equal("some-client-secret")) 473 }) 474 }) 475 476 Describe("UnsetOrganizationAndSpaceInformation", func() { 477 BeforeEach(func() { 478 config = new(Config) 479 config.SetOrganizationInformation("some-org-guid", "some-org") 480 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 481 }) 482 483 It("resets the org GUID and name", func() { 484 config.UnsetOrganizationAndSpaceInformation() 485 486 Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty()) 487 Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty()) 488 Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty()) 489 Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty()) 490 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse()) 491 }) 492 }) 493 494 Describe("UnsetSpaceInformation", func() { 495 BeforeEach(func() { 496 config = new(Config) 497 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 498 }) 499 500 It("resets the space GUID, name, and AllowSSH to default values", func() { 501 config.UnsetSpaceInformation() 502 503 Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty()) 504 Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty()) 505 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse()) 506 }) 507 }) 508 509 Describe("UnsetUserInformation", func() { 510 BeforeEach(func() { 511 config = new(Config) 512 config.SetAccessToken("some-access-token") 513 config.SetRefreshToken("some-refresh-token") 514 config.SetUAAGrantType("client-credentials") 515 config.SetUAAClientCredentials("some-client", "some-client-secret") 516 config.SetOrganizationInformation("some-org-guid", "some-org") 517 config.SetSpaceInformation("guid-value-1", "my-org-name", true) 518 }) 519 520 It("resets all user information", func() { 521 config.UnsetUserInformation() 522 523 Expect(config.ConfigFile.AccessToken).To(BeEmpty()) 524 Expect(config.ConfigFile.RefreshToken).To(BeEmpty()) 525 Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty()) 526 Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty()) 527 Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse()) 528 Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty()) 529 Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty()) 530 Expect(config.ConfigFile.UAAGrantType).To(BeEmpty()) 531 Expect(config.ConfigFile.UAAOAuthClient).To(Equal(DefaultUAAOAuthClient)) 532 Expect(config.ConfigFile.UAAOAuthClientSecret).To(Equal(DefaultUAAOAuthClientSecret)) 533 }) 534 }) 535 })