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