github.com/loafoe/cli@v7.1.0+incompatible/util/configv3/json_config_test.go (about)

     1  package configv3_test
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	. "code.cloudfoundry.org/cli/util/configv3"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("JSONConfig", func() {
    14  	var homeDir string
    15  	var config *Config
    16  
    17  	BeforeEach(func() {
    18  		homeDir = setup()
    19  	})
    20  
    21  	AfterEach(func() {
    22  		teardown(homeDir)
    23  	})
    24  
    25  	Describe("AccessToken", func() {
    26  		BeforeEach(func() {
    27  			rawConfig := fmt.Sprintf(`{ "AccessToken":"some-token", "ConfigVersion": %d }`, CurrentConfigVersion)
    28  			setConfig(homeDir, rawConfig)
    29  
    30  			var err error
    31  			config, err = LoadConfig()
    32  			Expect(err).ToNot(HaveOccurred())
    33  			Expect(config).ToNot(BeNil())
    34  		})
    35  
    36  		It("returns fields directly from config", func() {
    37  			Expect(config.AccessToken()).To(Equal("some-token"))
    38  		})
    39  	})
    40  
    41  	Describe("APIVersion", func() {
    42  		It("returns the api version", func() {
    43  			config = &Config{
    44  				ConfigFile: JSONConfig{
    45  					APIVersion: "2.59.0",
    46  				},
    47  			}
    48  
    49  			Expect(config.APIVersion()).To(Equal("2.59.0"))
    50  		})
    51  	})
    52  
    53  	Describe("CurrentUser", func() {
    54  		When("using client credentials and the user token is set", func() {
    55  			It("returns the user", func() {
    56  				config = &Config{
    57  					ConfigFile: JSONConfig{
    58  						AccessToken: AccessTokenForClientUsers,
    59  					},
    60  				}
    61  				user, err := config.CurrentUser()
    62  				Expect(err).ToNot(HaveOccurred())
    63  				Expect(user).To(Equal(User{
    64  					Name:     "potato-face",
    65  					GUID:     "potato-face",
    66  					IsClient: true,
    67  				}))
    68  			})
    69  		})
    70  
    71  		When("using user/password and the user token is set", func() {
    72  			It("returns the user", func() {
    73  				config = &Config{
    74  					ConfigFile: JSONConfig{
    75  						AccessToken: AccessTokenForHumanUsers,
    76  					},
    77  				}
    78  
    79  				user, err := config.CurrentUser()
    80  				Expect(err).ToNot(HaveOccurred())
    81  				Expect(user).To(Equal(User{
    82  					Name:     "admin",
    83  					GUID:     "9519be3e-44d9-40d0-ab9a-f4ace11df159",
    84  					Origin:   "uaa",
    85  					IsClient: false,
    86  				}))
    87  			})
    88  		})
    89  
    90  		When("the user token is blank", func() {
    91  			It("returns the user", func() {
    92  				config = new(Config)
    93  				user, err := config.CurrentUser()
    94  				Expect(err).ToNot(HaveOccurred())
    95  				Expect(user).To(Equal(User{}))
    96  			})
    97  		})
    98  	})
    99  
   100  	Describe("CurrentUserName", func() {
   101  		When("using client credentials and the user token is set", func() {
   102  			It("returns the username", func() {
   103  				config = &Config{
   104  					ConfigFile: JSONConfig{
   105  						AccessToken: AccessTokenForClientUsers,
   106  					},
   107  				}
   108  
   109  				username, err := config.CurrentUserName()
   110  				Expect(err).ToNot(HaveOccurred())
   111  				Expect(username).To(Equal("potato-face"))
   112  			})
   113  		})
   114  
   115  		When("using user/password and the user token is set", func() {
   116  			It("returns the username", func() {
   117  				config = &Config{
   118  					ConfigFile: JSONConfig{
   119  						AccessToken: AccessTokenForHumanUsers,
   120  					},
   121  				}
   122  
   123  				username, err := config.CurrentUserName()
   124  				Expect(err).ToNot(HaveOccurred())
   125  				Expect(username).To(Equal("admin"))
   126  			})
   127  		})
   128  
   129  		When("the user token is blank", func() {
   130  			It("returns an empty string", func() {
   131  				config = new(Config)
   132  				username, err := config.CurrentUserName()
   133  				Expect(err).ToNot(HaveOccurred())
   134  				Expect(username).To(BeEmpty())
   135  			})
   136  		})
   137  	})
   138  
   139  	Describe("HasTargetedOrganization", func() {
   140  		When("an organization is targeted", func() {
   141  			It("returns true", func() {
   142  				config = new(Config)
   143  				config.SetOrganizationInformation("guid-value-1", "my-org-name")
   144  				Expect(config.HasTargetedOrganization()).To(BeTrue())
   145  			})
   146  		})
   147  
   148  		When("an organization is not targeted", func() {
   149  			It("returns false", func() {
   150  				config = new(Config)
   151  				Expect(config.HasTargetedOrganization()).To(BeFalse())
   152  			})
   153  		})
   154  	})
   155  
   156  	Describe("HasTargetedSpace", func() {
   157  		When("an space is targeted", func() {
   158  			It("returns true", func() {
   159  				config = new(Config)
   160  				config.SetSpaceInformation("guid-value-1", "my-org-name", true)
   161  				Expect(config.HasTargetedSpace()).To(BeTrue())
   162  			})
   163  		})
   164  
   165  		When("an space is not targeted", func() {
   166  			It("returns false", func() {
   167  				config = new(Config)
   168  				Expect(config.HasTargetedSpace()).To(BeFalse())
   169  			})
   170  		})
   171  	})
   172  
   173  	Describe("MinCLIVersion", func() {
   174  		It("returns the minimum CLI version the CC requires", func() {
   175  			config = &Config{
   176  				ConfigFile: JSONConfig{
   177  					MinCLIVersion: "1.0.0",
   178  				},
   179  			}
   180  
   181  			Expect(config.MinCLIVersion()).To(Equal("1.0.0"))
   182  		})
   183  	})
   184  
   185  	Describe("OverallPollingTimeout", func() {
   186  		When("AsyncTimeout is set in config", func() {
   187  			BeforeEach(func() {
   188  				rawConfig := fmt.Sprintf(`{ "AsyncTimeout":5, "ConfigVersion": %d }`, CurrentConfigVersion)
   189  				setConfig(homeDir, rawConfig)
   190  
   191  				var err error
   192  				config, err = LoadConfig()
   193  				Expect(err).ToNot(HaveOccurred())
   194  				Expect(config).ToNot(BeNil())
   195  			})
   196  
   197  			It("returns the timeout in duration form", func() {
   198  				Expect(config.OverallPollingTimeout()).To(Equal(5 * time.Minute))
   199  			})
   200  		})
   201  	})
   202  
   203  	Describe("RefreshToken", func() {
   204  		BeforeEach(func() {
   205  			rawConfig := fmt.Sprintf(`{ "RefreshToken":"some-token", "ConfigVersion": %d }`, CurrentConfigVersion)
   206  			setConfig(homeDir, rawConfig)
   207  
   208  			var err error
   209  			config, err = LoadConfig()
   210  			Expect(err).ToNot(HaveOccurred())
   211  			Expect(config).ToNot(BeNil())
   212  		})
   213  
   214  		It("returns fields directly from config", func() {
   215  			Expect(config.RefreshToken()).To(Equal("some-token"))
   216  		})
   217  	})
   218  
   219  	Describe("SetAsyncTimeout", func() {
   220  		It("sets the async timeout", func() {
   221  			config = new(Config)
   222  			config.SetAsyncTimeout(2)
   223  			Expect(config.ConfigFile.AsyncTimeout).To(Equal(2))
   224  		})
   225  	})
   226  
   227  	Describe("SetColorEnabled", func() {
   228  		It("sets the color enabled field", func() {
   229  			config = new(Config)
   230  			config.SetColorEnabled("true")
   231  			Expect(config.ConfigFile.ColorEnabled).To(Equal("true"))
   232  		})
   233  	})
   234  
   235  	Describe("SetAccessToken", func() {
   236  		It("sets the authentication token information", func() {
   237  			config = new(Config)
   238  			config.SetAccessToken("I am the access token")
   239  			Expect(config.ConfigFile.AccessToken).To(Equal("I am the access token"))
   240  		})
   241  	})
   242  
   243  	Describe("SetLocale", func() {
   244  		It("sets the locale field", func() {
   245  			config = new(Config)
   246  			config.SetLocale("en-US")
   247  			Expect(config.ConfigFile.Locale).To(Equal("en-US"))
   248  		})
   249  
   250  		It("clears the locale field if requested", func() {
   251  			config = new(Config)
   252  			config.SetLocale("CLEAR")
   253  			Expect(config.ConfigFile.Locale).To(Equal(""))
   254  		})
   255  	})
   256  
   257  	Describe("SetOrganizationInformation", func() {
   258  		It("sets the organization GUID and name", func() {
   259  			config = new(Config)
   260  			config.SetOrganizationInformation("guid-value-1", "my-org-name")
   261  
   262  			Expect(config.ConfigFile.TargetedOrganization.GUID).To(Equal("guid-value-1"))
   263  			Expect(config.ConfigFile.TargetedOrganization.Name).To(Equal("my-org-name"))
   264  		})
   265  	})
   266  
   267  	Describe("SetRefreshToken", func() {
   268  		It("sets the refresh token information", func() {
   269  			config = new(Config)
   270  			config.SetRefreshToken("I am the refresh token")
   271  			Expect(config.ConfigFile.RefreshToken).To(Equal("I am the refresh token"))
   272  		})
   273  	})
   274  
   275  	Describe("SetSpaceInformation", func() {
   276  		It("sets the space GUID, name, and AllowSSH", func() {
   277  			config = new(Config)
   278  			config.SetSpaceInformation("guid-value-1", "my-org-name", true)
   279  
   280  			Expect(config.ConfigFile.TargetedSpace.GUID).To(Equal("guid-value-1"))
   281  			Expect(config.ConfigFile.TargetedSpace.Name).To(Equal("my-org-name"))
   282  			Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeTrue())
   283  		})
   284  	})
   285  
   286  	Describe("SetTargetInformation", func() {
   287  		It("sets the api target and other related endpoints", func() {
   288  			config = &Config{
   289  				ConfigFile: JSONConfig{
   290  					TargetedOrganization: Organization{
   291  						GUID: "this-is-a-guid",
   292  						Name: "jo bobo jim boo",
   293  					},
   294  					TargetedSpace: Space{
   295  						GUID:     "this-is-a-guid",
   296  						Name:     "jo bobo jim boo",
   297  						AllowSSH: true,
   298  					},
   299  				},
   300  			}
   301  			config.SetTargetInformation(TargetInformationArgs{
   302  				Api:               "https://api.foo.com",
   303  				ApiVersion:        "2.59.31",
   304  				Auth:              "https://login.foo.com",
   305  				MinCLIVersion:     "2.0.0",
   306  				Doppler:           "wws://doppler.foo.com:443",
   307  				LogCache:          "https://log-cache.foo.com",
   308  				Routing:           "https://api.foo.com/routing",
   309  				SkipSSLValidation: true,
   310  			})
   311  
   312  			Expect(config.ConfigFile.Target).To(Equal("https://api.foo.com"))
   313  			Expect(config.ConfigFile.APIVersion).To(Equal("2.59.31"))
   314  			Expect(config.ConfigFile.AuthorizationEndpoint).To(Equal("https://login.foo.com"))
   315  			Expect(config.ConfigFile.MinCLIVersion).To(Equal("2.0.0"))
   316  			Expect(config.ConfigFile.DopplerEndpoint).To(Equal("wws://doppler.foo.com:443"))
   317  			Expect(config.ConfigFile.LogCacheEndpoint).To(Equal("https://log-cache.foo.com"))
   318  			Expect(config.ConfigFile.RoutingEndpoint).To(Equal("https://api.foo.com/routing"))
   319  			Expect(config.ConfigFile.SkipSSLValidation).To(BeTrue())
   320  
   321  			Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty())
   322  			Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty())
   323  			Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty())
   324  			Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty())
   325  			Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse())
   326  		})
   327  	})
   328  
   329  	Describe("SetTokenInformation", func() {
   330  		It("sets the authentication token information", func() {
   331  			config = new(Config)
   332  			config.SetTokenInformation("I am the access token", "I am the refresh token", "I am the SSH OAuth client")
   333  
   334  			Expect(config.ConfigFile.AccessToken).To(Equal("I am the access token"))
   335  			Expect(config.ConfigFile.RefreshToken).To(Equal("I am the refresh token"))
   336  			Expect(config.ConfigFile.SSHOAuthClient).To(Equal("I am the SSH OAuth client"))
   337  		})
   338  	})
   339  
   340  	Describe("SetTrace", func() {
   341  		It("sets the trace field", func() {
   342  			config = new(Config)
   343  			config.SetTrace("true")
   344  			Expect(config.ConfigFile.Trace).To(Equal("true"))
   345  		})
   346  	})
   347  
   348  	Describe("SetUAAClientCredentials", func() {
   349  		It("sets the UAA client credentials", func() {
   350  			config = new(Config)
   351  			config.SetUAAClientCredentials("some-uaa-client", "some-uaa-client-secret")
   352  			Expect(config.ConfigFile.UAAOAuthClient).To(Equal("some-uaa-client"))
   353  			Expect(config.ConfigFile.UAAOAuthClientSecret).To(Equal("some-uaa-client-secret"))
   354  		})
   355  	})
   356  
   357  	Describe("SetUAAEndpoint", func() {
   358  		It("sets the UAA endpoint", func() {
   359  			config = new(Config)
   360  			config.SetUAAEndpoint("some-uaa-endpoint.com")
   361  			Expect(config.ConfigFile.UAAEndpoint).To(Equal("some-uaa-endpoint.com"))
   362  		})
   363  	})
   364  
   365  	Describe("SetUAAGrantType", func() {
   366  		It("sets the UAA endpoint", func() {
   367  			config = new(Config)
   368  			config.SetUAAGrantType("some-uaa-grant-type")
   369  			Expect(config.ConfigFile.UAAGrantType).To(Equal("some-uaa-grant-type"))
   370  		})
   371  	})
   372  
   373  	Describe("SkipSSLValidation", func() {
   374  		BeforeEach(func() {
   375  			rawConfig := fmt.Sprintf(`{ "SSLDisabled":true, "ConfigVersion": %d }`, CurrentConfigVersion)
   376  			setConfig(homeDir, rawConfig)
   377  
   378  			var err error
   379  			config, err = LoadConfig()
   380  			Expect(err).ToNot(HaveOccurred())
   381  			Expect(config).ToNot(BeNil())
   382  		})
   383  
   384  		It("returns fields directly from config", func() {
   385  			Expect(config.SkipSSLValidation()).To(BeTrue())
   386  		})
   387  	})
   388  
   389  	Describe("SSHOAuthClient", func() {
   390  		BeforeEach(func() {
   391  			rawConfig := fmt.Sprintf(`{ "SSHOAuthClient":"some-ssh-client", "ConfigVersion": %d }`, CurrentConfigVersion)
   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 ID", func() {
   401  			Expect(config.SSHOAuthClient()).To(Equal("some-ssh-client"))
   402  		})
   403  	})
   404  
   405  	Describe("Target", func() {
   406  		BeforeEach(func() {
   407  			rawConfig := fmt.Sprintf(`{ "Target":"https://api.foo.com", "ConfigVersion": %d }`, CurrentConfigVersion)
   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 target", func() {
   417  			Expect(config.Target()).To(Equal("https://api.foo.com"))
   418  		})
   419  	})
   420  
   421  	Describe("TargetedOrganization", func() {
   422  		It("returns the organization", func() {
   423  			organization := Organization{
   424  				GUID: "some-guid",
   425  				Name: "some-org",
   426  			}
   427  			config = &Config{
   428  				ConfigFile: JSONConfig{
   429  					TargetedOrganization: organization,
   430  				},
   431  			}
   432  
   433  			Expect(config.TargetedOrganization()).To(Equal(organization))
   434  		})
   435  	})
   436  
   437  	Describe("TargetedOrganizationName", func() {
   438  		It("returns the name of targeted organization", func() {
   439  			organization := Organization{
   440  				GUID: "some-guid",
   441  				Name: "some-org",
   442  			}
   443  			config = &Config{
   444  				ConfigFile: JSONConfig{
   445  					TargetedOrganization: organization,
   446  				},
   447  			}
   448  
   449  			Expect(config.TargetedOrganizationName()).To(Equal(organization.Name))
   450  		})
   451  	})
   452  
   453  	Describe("TargetedSpace", func() {
   454  		It("returns the space", func() {
   455  			space := Space{
   456  				GUID: "some-guid",
   457  				Name: "some-space",
   458  			}
   459  			config = &Config{
   460  				ConfigFile: JSONConfig{
   461  					TargetedSpace: space,
   462  				},
   463  			}
   464  
   465  			Expect(config.TargetedSpace()).To(Equal(space))
   466  		})
   467  	})
   468  
   469  	Describe("UAAGrantType", func() {
   470  		BeforeEach(func() {
   471  			rawConfig := fmt.Sprintf(`{ "UAAGrantType":"some-grant-type", "ConfigVersion": %d }`, CurrentConfigVersion)
   472  			setConfig(homeDir, rawConfig)
   473  
   474  			var err error
   475  			config, err = LoadConfig()
   476  			Expect(err).ToNot(HaveOccurred())
   477  			Expect(config).ToNot(BeNil())
   478  		})
   479  
   480  		It("returns the client secret", func() {
   481  			Expect(config.UAAGrantType()).To(Equal("some-grant-type"))
   482  		})
   483  	})
   484  
   485  	Describe("UAAOAuthClient", func() {
   486  		BeforeEach(func() {
   487  			rawConfig := fmt.Sprintf(`{ "UAAOAuthClient":"some-client", "ConfigVersion": %d }`, CurrentConfigVersion)
   488  			setConfig(homeDir, rawConfig)
   489  
   490  			var err error
   491  			config, err = LoadConfig()
   492  			Expect(err).ToNot(HaveOccurred())
   493  			Expect(config).ToNot(BeNil())
   494  		})
   495  
   496  		It("returns the client ID", func() {
   497  			Expect(config.UAAOAuthClient()).To(Equal("some-client"))
   498  		})
   499  	})
   500  
   501  	Describe("UAAOAuthClientSecret", func() {
   502  		BeforeEach(func() {
   503  			rawConfig := fmt.Sprintf(`
   504  					{
   505  						"UAAOAuthClient": "some-client-id",
   506  						"UAAOAuthClientSecret": "some-client-secret",
   507  						"ConfigVersion": %d
   508  					}`, CurrentConfigVersion)
   509  			setConfig(homeDir, rawConfig)
   510  
   511  			var err error
   512  			config, err = LoadConfig()
   513  			Expect(err).ToNot(HaveOccurred())
   514  			Expect(config).ToNot(BeNil())
   515  		})
   516  
   517  		It("returns the client secret", func() {
   518  			Expect(config.UAAOAuthClientSecret()).To(Equal("some-client-secret"))
   519  		})
   520  	})
   521  
   522  	Describe("UnsetOrganizationAndSpaceInformation", func() {
   523  		BeforeEach(func() {
   524  			config = new(Config)
   525  			config.SetOrganizationInformation("some-org-guid", "some-org")
   526  			config.SetSpaceInformation("guid-value-1", "my-org-name", true)
   527  		})
   528  
   529  		It("resets the org GUID and name", func() {
   530  			config.UnsetOrganizationAndSpaceInformation()
   531  
   532  			Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty())
   533  			Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty())
   534  			Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty())
   535  			Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty())
   536  			Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse())
   537  		})
   538  	})
   539  
   540  	Describe("UnsetSpaceInformation", func() {
   541  		BeforeEach(func() {
   542  			config = new(Config)
   543  			config.SetSpaceInformation("guid-value-1", "my-org-name", true)
   544  		})
   545  
   546  		It("resets the space GUID, name, and AllowSSH to default values", func() {
   547  			config.UnsetSpaceInformation()
   548  
   549  			Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty())
   550  			Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty())
   551  			Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse())
   552  		})
   553  	})
   554  
   555  	Describe("UnsetUserInformation", func() {
   556  		BeforeEach(func() {
   557  			config = new(Config)
   558  			config.SetAccessToken("some-access-token")
   559  			config.SetRefreshToken("some-refresh-token")
   560  			config.SetUAAGrantType("client-credentials")
   561  			config.SetUAAClientCredentials("some-client", "some-client-secret")
   562  			config.SetOrganizationInformation("some-org-guid", "some-org")
   563  			config.SetSpaceInformation("guid-value-1", "my-org-name", true)
   564  		})
   565  
   566  		It("resets all user information", func() {
   567  			config.UnsetUserInformation()
   568  
   569  			Expect(config.ConfigFile.AccessToken).To(BeEmpty())
   570  			Expect(config.ConfigFile.RefreshToken).To(BeEmpty())
   571  			Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty())
   572  			Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty())
   573  			Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse())
   574  			Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty())
   575  			Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty())
   576  			Expect(config.ConfigFile.UAAGrantType).To(BeEmpty())
   577  			Expect(config.ConfigFile.UAAOAuthClient).To(Equal(DefaultUAAOAuthClient))
   578  			Expect(config.ConfigFile.UAAOAuthClientSecret).To(Equal(DefaultUAAOAuthClientSecret))
   579  		})
   580  	})
   581  })