github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/util/configv3/config_test.go (about)

     1  package configv3_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"time"
     9  
    10  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    11  	. "github.com/liamawhite/cli-with-i18n/util/configv3"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/ginkgo/extensions/table"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Config", func() {
    19  	var homeDir string
    20  
    21  	BeforeEach(func() {
    22  		homeDir = setup()
    23  	})
    24  
    25  	AfterEach(func() {
    26  		teardown(homeDir)
    27  	})
    28  
    29  	Describe("LoadConfig", func() {
    30  		Context("when there isn't a config set", func() {
    31  			var (
    32  				oldLang  string
    33  				oldLCAll string
    34  			)
    35  
    36  			BeforeEach(func() {
    37  				oldLang = os.Getenv("LANG")
    38  				oldLCAll = os.Getenv("LC_ALL")
    39  				Expect(os.Unsetenv("LANG")).ToNot(HaveOccurred())
    40  				Expect(os.Unsetenv("LC_ALL")).ToNot(HaveOccurred())
    41  			})
    42  
    43  			It("returns a default config", func() {
    44  				defer os.Setenv("LANG", oldLang)
    45  				defer os.Setenv("LC_ALL", oldLCAll)
    46  
    47  				// specifically for when we run unit tests locally
    48  				// we save and unset this variable in case it's present
    49  				// since we want to load a default config
    50  				envVal := os.Getenv("CF_CLI_EXPERIMENTAL")
    51  				Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).ToNot(HaveOccurred())
    52  
    53  				config, err := LoadConfig()
    54  				Expect(err).ToNot(HaveOccurred())
    55  
    56  				// then we reset the env variable
    57  				err = os.Setenv("CF_CLI_EXPERIMENTAL", envVal)
    58  				Expect(err).ToNot(HaveOccurred())
    59  
    60  				Expect(config).ToNot(BeNil())
    61  				Expect(config.Target()).To(Equal(DefaultTarget))
    62  				Expect(config.SkipSSLValidation()).To(BeFalse())
    63  				Expect(config.ColorEnabled()).To(Equal(ColorEnabled))
    64  				Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins")))
    65  				Expect(config.StagingTimeout()).To(Equal(DefaultStagingTimeout))
    66  				Expect(config.StartupTimeout()).To(Equal(DefaultStartupTimeout))
    67  				Expect(config.Locale()).To(BeEmpty())
    68  				Expect(config.SSHOAuthClient()).To(Equal(DefaultSSHOAuthClient))
    69  				Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient))
    70  				Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret))
    71  				Expect(config.OverallPollingTimeout()).To(Equal(DefaultOverallPollingTimeout))
    72  				Expect(config.LogLevel()).To(Equal(0))
    73  				Expect(config.DockerPassword()).To(BeEmpty())
    74  
    75  				Expect(config.PluginRepositories()).To(Equal([]PluginRepository{{
    76  					Name: "CF-Community",
    77  					URL:  "https://plugins.cloudfoundry.org",
    78  				}}))
    79  				Expect(config.Experimental()).To(BeFalse())
    80  
    81  				pluginConfig := config.Plugins()
    82  				Expect(pluginConfig).To(BeEmpty())
    83  
    84  				trace, location := config.Verbose()
    85  				Expect(trace).To(BeFalse())
    86  				Expect(location).To(BeEmpty())
    87  
    88  				// test the plugins map is initialized
    89  				config.AddPlugin(Plugin{})
    90  			})
    91  		})
    92  
    93  		Context("when there is a config set", func() {
    94  			var (
    95  				config *Config
    96  				err    error
    97  			)
    98  
    99  			Context("but it is empty", func() {
   100  				var (
   101  					oldLang  string
   102  					oldLCAll string
   103  				)
   104  
   105  				BeforeEach(func() {
   106  					oldLang = os.Getenv("LANG")
   107  					oldLCAll = os.Getenv("LC_ALL")
   108  					Expect(os.Unsetenv("LANG")).ToNot(HaveOccurred())
   109  					Expect(os.Unsetenv("LC_ALL")).ToNot(HaveOccurred())
   110  
   111  					setConfig(homeDir, "")
   112  				})
   113  
   114  				It("returns the default config with a json error", func() {
   115  					defer os.Setenv("LANG", oldLang)
   116  					defer os.Setenv("LC_ALL", oldLCAll)
   117  
   118  					// specifically for when we run unit tests locally
   119  					// we save and unset this variable in case it's present
   120  					// since we want to load a default config
   121  					envVal := os.Getenv("CF_CLI_EXPERIMENTAL")
   122  					Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).ToNot(HaveOccurred())
   123  
   124  					config, err = LoadConfig()
   125  					Expect(err).To(Equal(translatableerror.EmptyConfigError{FilePath: filepath.Join(homeDir, ".cf", "config.json")}))
   126  
   127  					// then we reset the env variable
   128  					err = os.Setenv("CF_CLI_EXPERIMENTAL", envVal)
   129  					Expect(err).ToNot(HaveOccurred())
   130  
   131  					Expect(config).ToNot(BeNil())
   132  					Expect(config.Target()).To(Equal(DefaultTarget))
   133  					Expect(config.SkipSSLValidation()).To(BeFalse())
   134  					Expect(config.ColorEnabled()).To(Equal(ColorEnabled))
   135  					Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins")))
   136  					Expect(config.StagingTimeout()).To(Equal(DefaultStagingTimeout))
   137  					Expect(config.StartupTimeout()).To(Equal(DefaultStartupTimeout))
   138  					Expect(config.Locale()).To(BeEmpty())
   139  					Expect(config.SSHOAuthClient()).To(Equal(DefaultSSHOAuthClient))
   140  					Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient))
   141  					Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret))
   142  					Expect(config.OverallPollingTimeout()).To(Equal(DefaultOverallPollingTimeout))
   143  					Expect(config.LogLevel()).To(Equal(0))
   144  					Expect(config.DockerPassword()).To(BeEmpty())
   145  
   146  					Expect(config.PluginRepositories()).To(Equal([]PluginRepository{{
   147  						Name: "CF-Community",
   148  						URL:  "https://plugins.cloudfoundry.org",
   149  					}}))
   150  					Expect(config.Experimental()).To(BeFalse())
   151  
   152  					pluginConfig := config.Plugins()
   153  					Expect(pluginConfig).To(BeEmpty())
   154  
   155  					trace, location := config.Verbose()
   156  					Expect(trace).To(BeFalse())
   157  					Expect(location).To(BeEmpty())
   158  
   159  					// test the plugins map is initialized
   160  					config.AddPlugin(Plugin{})
   161  				})
   162  			})
   163  
   164  			Context("and there are old temp-config* files lingering from previous failed attempts to write the config", func() {
   165  				var (
   166  					oldLang  string
   167  					oldLCAll string
   168  				)
   169  
   170  				BeforeEach(func() {
   171  					oldLang = os.Getenv("LANG")
   172  					oldLCAll = os.Getenv("LC_ALL")
   173  					Expect(os.Unsetenv("LANG")).ToNot(HaveOccurred())
   174  					Expect(os.Unsetenv("LC_ALL")).ToNot(HaveOccurred())
   175  
   176  					setConfig(homeDir, `{}`)
   177  					configDir := filepath.Join(homeDir, ".cf")
   178  					for i := 0; i < 3; i++ {
   179  						tmpFile, fileErr := ioutil.TempFile(configDir, "temp-config")
   180  						Expect(fileErr).ToNot(HaveOccurred())
   181  						tmpFile.Close()
   182  					}
   183  				})
   184  
   185  				It("returns the default config and removes the lingering temp-config* files", func() {
   186  					defer os.Setenv("LANG", oldLang)
   187  					defer os.Setenv("LC_ALL", oldLCAll)
   188  
   189  					// specifically for when we run unit tests locally
   190  					// we save and unset this variable in case it's present
   191  					// since we want to load a default config
   192  					envVal := os.Getenv("CF_CLI_EXPERIMENTAL")
   193  					Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).ToNot(HaveOccurred())
   194  
   195  					config, err = LoadConfig()
   196  					Expect(err).ToNot(HaveOccurred())
   197  
   198  					oldTempFileNames, configErr := filepath.Glob(filepath.Join(homeDir, ".cf", "temp-config?*"))
   199  					Expect(configErr).ToNot(HaveOccurred())
   200  					Expect(oldTempFileNames).To(BeEmpty())
   201  
   202  					// then we reset the env variable
   203  					err = os.Setenv("CF_CLI_EXPERIMENTAL", envVal)
   204  					Expect(err).ToNot(HaveOccurred())
   205  
   206  					Expect(config).ToNot(BeNil())
   207  					Expect(config.Target()).To(Equal(DefaultTarget))
   208  					Expect(config.SkipSSLValidation()).To(BeFalse())
   209  					Expect(config.ColorEnabled()).To(Equal(ColorEnabled))
   210  					Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins")))
   211  					Expect(config.StagingTimeout()).To(Equal(DefaultStagingTimeout))
   212  					Expect(config.StartupTimeout()).To(Equal(DefaultStartupTimeout))
   213  					Expect(config.Locale()).To(BeEmpty())
   214  					Expect(config.SSHOAuthClient()).To(Equal(DefaultSSHOAuthClient))
   215  					Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient))
   216  					Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret))
   217  					Expect(config.OverallPollingTimeout()).To(Equal(DefaultOverallPollingTimeout))
   218  					Expect(config.LogLevel()).To(Equal(0))
   219  
   220  					Expect(config.Experimental()).To(BeFalse())
   221  
   222  					pluginConfig := config.Plugins()
   223  					Expect(pluginConfig).To(BeEmpty())
   224  
   225  					trace, location := config.Verbose()
   226  					Expect(trace).To(BeFalse())
   227  					Expect(location).To(BeEmpty())
   228  
   229  					// test the plugins map is initialized
   230  					config.AddPlugin(Plugin{})
   231  				})
   232  			})
   233  
   234  			Context("when UAAOAuthClient is not present", func() {
   235  				BeforeEach(func() {
   236  					setConfig(homeDir, `{}`)
   237  
   238  					config, err = LoadConfig()
   239  					Expect(err).ToNot(HaveOccurred())
   240  				})
   241  
   242  				It("sets UAAOAuthClient to the default", func() {
   243  					Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient))
   244  				})
   245  
   246  				It("sets UAAOAuthClientSecret to the default", func() {
   247  					Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret))
   248  				})
   249  			})
   250  
   251  			Context("when UAAOAuthClient is empty", func() {
   252  				BeforeEach(func() {
   253  					rawConfig := `
   254  					{
   255  						"UAAOAuthClient": ""
   256  					}`
   257  					setConfig(homeDir, rawConfig)
   258  
   259  					config, err = LoadConfig()
   260  					Expect(err).ToNot(HaveOccurred())
   261  				})
   262  
   263  				It("sets UAAOAuthClient to the default", func() {
   264  					Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient))
   265  				})
   266  
   267  				It("sets UAAOAuthClientSecret to the default", func() {
   268  					Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret))
   269  				})
   270  			})
   271  		})
   272  	})
   273  
   274  	Describe("check functions", func() {
   275  		Describe("HasTargetedOrganization", func() {
   276  			Context("when an organization is targeted", func() {
   277  				It("returns true", func() {
   278  					config := Config{}
   279  					config.SetOrganizationInformation("guid-value-1", "my-org-name")
   280  					Expect(config.HasTargetedOrganization()).To(BeTrue())
   281  				})
   282  			})
   283  
   284  			Context("when an organization is not targeted", func() {
   285  				It("returns false", func() {
   286  					config := Config{}
   287  					Expect(config.HasTargetedOrganization()).To(BeFalse())
   288  				})
   289  			})
   290  		})
   291  
   292  		Describe("HasTargetedSpace", func() {
   293  			Context("when an space is targeted", func() {
   294  				It("returns true", func() {
   295  					config := Config{}
   296  					config.SetSpaceInformation("guid-value-1", "my-org-name", true)
   297  					Expect(config.HasTargetedSpace()).To(BeTrue())
   298  				})
   299  			})
   300  
   301  			Context("when an space is not targeted", func() {
   302  				It("returns false", func() {
   303  					config := Config{}
   304  					Expect(config.HasTargetedSpace()).To(BeFalse())
   305  				})
   306  			})
   307  		})
   308  	})
   309  
   310  	Describe("getter functions", func() {
   311  		Describe("Target", func() {
   312  			var config *Config
   313  
   314  			BeforeEach(func() {
   315  				rawConfig := `{ "Target":"https://api.foo.com" }`
   316  				setConfig(homeDir, rawConfig)
   317  
   318  				var err error
   319  				config, err = LoadConfig()
   320  				Expect(err).ToNot(HaveOccurred())
   321  				Expect(config).ToNot(BeNil())
   322  			})
   323  
   324  			It("returns the target", func() {
   325  				Expect(config.Target()).To(Equal("https://api.foo.com"))
   326  			})
   327  		})
   328  
   329  		Describe("OverallPollingTimeout", func() {
   330  			var config *Config
   331  
   332  			Context("when AsyncTimeout is set in config", func() {
   333  				BeforeEach(func() {
   334  					rawConfig := `{ "AsyncTimeout":5 }`
   335  					setConfig(homeDir, rawConfig)
   336  
   337  					var err error
   338  					config, err = LoadConfig()
   339  					Expect(err).ToNot(HaveOccurred())
   340  					Expect(config).ToNot(BeNil())
   341  				})
   342  
   343  				It("returns the timeout in duration form", func() {
   344  					Expect(config.OverallPollingTimeout()).To(Equal(5 * time.Minute))
   345  				})
   346  			})
   347  		})
   348  
   349  		Describe("SkipSSLValidation", func() {
   350  			var config *Config
   351  
   352  			BeforeEach(func() {
   353  				rawConfig := `{ "SSLDisabled":true }`
   354  				setConfig(homeDir, rawConfig)
   355  
   356  				var err error
   357  				config, err = LoadConfig()
   358  				Expect(err).ToNot(HaveOccurred())
   359  				Expect(config).ToNot(BeNil())
   360  			})
   361  
   362  			It("returns fields directly from config", func() {
   363  				Expect(config.SkipSSLValidation()).To(BeTrue())
   364  			})
   365  		})
   366  
   367  		Describe("AccessToken", func() {
   368  			var config *Config
   369  
   370  			BeforeEach(func() {
   371  				rawConfig := `{ "AccessToken":"some-token" }`
   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 fields directly from config", func() {
   381  				Expect(config.AccessToken()).To(Equal("some-token"))
   382  			})
   383  		})
   384  
   385  		Describe("RefreshToken", func() {
   386  			var config *Config
   387  
   388  			BeforeEach(func() {
   389  				rawConfig := `{ "RefreshToken":"some-token" }`
   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 fields directly from config", func() {
   399  				Expect(config.RefreshToken()).To(Equal("some-token"))
   400  			})
   401  		})
   402  
   403  		Describe("SSHOAuthClient", func() {
   404  			var config *Config
   405  
   406  			BeforeEach(func() {
   407  				rawConfig := `{ "SSHOAuthClient":"some-ssh-client" }`
   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 ID", func() {
   417  				Expect(config.SSHOAuthClient()).To(Equal("some-ssh-client"))
   418  			})
   419  		})
   420  
   421  		Describe("UAAOAuthClient", func() {
   422  			var config *Config
   423  
   424  			BeforeEach(func() {
   425  				rawConfig := `{ "UAAOAuthClient":"some-client" }`
   426  				setConfig(homeDir, rawConfig)
   427  
   428  				var err error
   429  				config, err = LoadConfig()
   430  				Expect(err).ToNot(HaveOccurred())
   431  				Expect(config).ToNot(BeNil())
   432  			})
   433  
   434  			It("returns the client ID", func() {
   435  				Expect(config.UAAOAuthClient()).To(Equal("some-client"))
   436  			})
   437  		})
   438  
   439  		Describe("UAAOAuthClientSecret", func() {
   440  			var config *Config
   441  
   442  			BeforeEach(func() {
   443  				rawConfig := `
   444  					{
   445  						"UAAOAuthClient": "some-client-id",
   446  						"UAAOAuthClientSecret": "some-client-secret"
   447  					}`
   448  				setConfig(homeDir, rawConfig)
   449  
   450  				var err error
   451  				config, err = LoadConfig()
   452  				Expect(err).ToNot(HaveOccurred())
   453  				Expect(config).ToNot(BeNil())
   454  			})
   455  
   456  			It("returns the client secret", func() {
   457  				Expect(config.UAAOAuthClientSecret()).To(Equal("some-client-secret"))
   458  			})
   459  		})
   460  
   461  		DescribeTable("Experimental",
   462  			func(envVal string, expected bool) {
   463  				setConfig(homeDir, `{}`)
   464  
   465  				defer os.Unsetenv("CF_CLI_EXPERIMENTAL")
   466  				if envVal == "" {
   467  					Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).ToNot(HaveOccurred())
   468  				} else {
   469  					Expect(os.Setenv("CF_CLI_EXPERIMENTAL", envVal)).ToNot(HaveOccurred())
   470  				}
   471  
   472  				config, err := LoadConfig()
   473  				Expect(err).ToNot(HaveOccurred())
   474  				Expect(config).ToNot(BeNil())
   475  
   476  				Expect(config.Experimental()).To(Equal(expected))
   477  			},
   478  
   479  			Entry("uses default value of false if environment value is not set", "", false),
   480  			Entry("uses environment value if a valid environment value is set", "true", true),
   481  			Entry("uses default value of false if an invalid environment value is set", "something-invalid", false),
   482  		)
   483  
   484  		Describe("BinaryName", func() {
   485  			It("returns the name used to invoke", func() {
   486  				config, err := LoadConfig()
   487  				Expect(err).ToNot(HaveOccurred())
   488  				Expect(config).ToNot(BeNil())
   489  
   490  				// Ginkgo will uses a config file as the first test argument, so that
   491  				// will be considered the binary name
   492  				Expect(config.BinaryName()).To(Equal("configv3.test"))
   493  			})
   494  		})
   495  
   496  		Context("when there are environment variables", func() {
   497  			var (
   498  				originalCFStagingTimeout string
   499  				originalCFStartupTimeout string
   500  				originalHTTPSProxy       string
   501  				originalForceTTY         string
   502  				originalDockerPassword   string
   503  
   504  				config *Config
   505  			)
   506  
   507  			BeforeEach(func() {
   508  				originalCFStagingTimeout = os.Getenv("CF_STAGING_TIMEOUT")
   509  				originalCFStartupTimeout = os.Getenv("CF_STARTUP_TIMEOUT")
   510  				originalHTTPSProxy = os.Getenv("https_proxy")
   511  				originalForceTTY = os.Getenv("FORCE_TTY")
   512  				originalDockerPassword = os.Getenv("CF_DOCKER_PASSWORD")
   513  				Expect(os.Setenv("CF_STAGING_TIMEOUT", "8675")).ToNot(HaveOccurred())
   514  				Expect(os.Setenv("CF_STARTUP_TIMEOUT", "309")).ToNot(HaveOccurred())
   515  				Expect(os.Setenv("https_proxy", "proxy.com")).ToNot(HaveOccurred())
   516  				Expect(os.Setenv("FORCE_TTY", "true")).ToNot(HaveOccurred())
   517  				Expect(os.Setenv("CF_DOCKER_PASSWORD", "banana")).ToNot(HaveOccurred())
   518  
   519  				var err error
   520  				config, err = LoadConfig()
   521  				Expect(err).ToNot(HaveOccurred())
   522  				Expect(config).ToNot(BeNil())
   523  			})
   524  
   525  			AfterEach(func() {
   526  				Expect(os.Setenv("CF_STAGING_TIMEOUT", originalCFStagingTimeout)).ToNot(HaveOccurred())
   527  				Expect(os.Setenv("CF_STARTUP_TIMEOUT", originalCFStartupTimeout)).ToNot(HaveOccurred())
   528  				Expect(os.Setenv("https_proxy", originalHTTPSProxy)).ToNot(HaveOccurred())
   529  				Expect(os.Setenv("FORCE_TTY", originalForceTTY)).ToNot(HaveOccurred())
   530  				Expect(os.Setenv("CF_DOCKER_PASSWORD", originalDockerPassword)).ToNot(HaveOccurred())
   531  			})
   532  
   533  			It("overrides specific config values", func() {
   534  				Expect(config.StagingTimeout()).To(Equal(time.Duration(8675) * time.Minute))
   535  				Expect(config.StartupTimeout()).To(Equal(time.Duration(309) * time.Minute))
   536  				Expect(config.HTTPSProxy()).To(Equal("proxy.com"))
   537  				Expect(config.IsTTY()).To(BeTrue())
   538  				Expect(config.DockerPassword()).To(Equal("banana"))
   539  			})
   540  		})
   541  
   542  		Describe("APIVersion", func() {
   543  			It("returns the api version", func() {
   544  				config := Config{
   545  					ConfigFile: CFConfig{
   546  						APIVersion: "2.59.0",
   547  					},
   548  				}
   549  
   550  				Expect(config.APIVersion()).To(Equal("2.59.0"))
   551  			})
   552  		})
   553  
   554  		Describe("MinCLIVersion", func() {
   555  			It("returns the minimum CLI version the CC requires", func() {
   556  				config := Config{
   557  					ConfigFile: CFConfig{
   558  						MinCLIVersion: "1.0.0",
   559  					},
   560  				}
   561  
   562  				Expect(config.MinCLIVersion()).To(Equal("1.0.0"))
   563  			})
   564  		})
   565  
   566  		Describe("TargetedOrganization", func() {
   567  			It("returns the organization", func() {
   568  				organization := Organization{
   569  					GUID: "some-guid",
   570  					Name: "some-org",
   571  				}
   572  				config := Config{
   573  					ConfigFile: CFConfig{
   574  						TargetedOrganization: organization,
   575  					},
   576  				}
   577  
   578  				Expect(config.TargetedOrganization()).To(Equal(organization))
   579  			})
   580  		})
   581  
   582  		Describe("TargetedSpace", func() {
   583  			It("returns the space", func() {
   584  				space := Space{
   585  					GUID: "some-guid",
   586  					Name: "some-space",
   587  				}
   588  				config := Config{
   589  					ConfigFile: CFConfig{
   590  						TargetedSpace: space,
   591  					},
   592  				}
   593  
   594  				Expect(config.TargetedSpace()).To(Equal(space))
   595  			})
   596  		})
   597  
   598  		Describe("DialTimeout", func() {
   599  			var (
   600  				originalDialTimeout string
   601  
   602  				config *Config
   603  			)
   604  
   605  			BeforeEach(func() {
   606  				originalDialTimeout = os.Getenv("CF_DIAL_TIMEOUT")
   607  				Expect(os.Setenv("CF_DIAL_TIMEOUT", "1234")).ToNot(HaveOccurred())
   608  
   609  				var err error
   610  				config, err = LoadConfig()
   611  				Expect(err).ToNot(HaveOccurred())
   612  				Expect(config).ToNot(BeNil())
   613  			})
   614  
   615  			AfterEach(func() {
   616  				Expect(os.Setenv("CF_DIAL_TIMEOUT", originalDialTimeout)).ToNot(HaveOccurred())
   617  			})
   618  
   619  			It("returns the dial timeout", func() {
   620  				Expect(config.DialTimeout()).To(Equal(1234 * time.Second))
   621  			})
   622  		})
   623  
   624  		Describe("BinaryVersion", func() {
   625  			It("returns back version.BinaryVersion", func() {
   626  				conf := Config{}
   627  				Expect(conf.BinaryVersion()).To(Equal("0.0.0-unknown-version"))
   628  			})
   629  		})
   630  
   631  		DescribeTable("LogLevel",
   632  			func(envVal string, expectedLevel int) {
   633  				config := Config{ENV: EnvOverride{CFLogLevel: envVal}}
   634  				Expect(config.LogLevel()).To(Equal(expectedLevel))
   635  			},
   636  
   637  			Entry("Default to 0", "", 0),
   638  			Entry("panic returns 0", "panic", 0),
   639  			Entry("fatal returns 1", "fatal", 1),
   640  			Entry("error returns 2", "error", 2),
   641  			Entry("warn returns 3", "warn", 3),
   642  			Entry("info returns 4", "info", 4),
   643  			Entry("debug returns 5", "debug", 5),
   644  			Entry("dEbUg returns 5", "dEbUg", 5),
   645  		)
   646  	})
   647  
   648  	Describe("WriteConfig", func() {
   649  		var config *Config
   650  
   651  		BeforeEach(func() {
   652  			config = &Config{
   653  				ConfigFile: CFConfig{
   654  					ConfigVersion: 3,
   655  					Target:        "foo.com",
   656  					ColorEnabled:  "true",
   657  				},
   658  				ENV: EnvOverride{
   659  					CFColor: "false",
   660  				},
   661  			}
   662  		})
   663  
   664  		Context("when no errors are encountered", func() {
   665  			It("writes ConfigFile to homeDir/.cf/config.json", func() {
   666  				err := WriteConfig(config)
   667  				Expect(err).ToNot(HaveOccurred())
   668  
   669  				file, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json"))
   670  				Expect(err).ToNot(HaveOccurred())
   671  
   672  				var writtenCFConfig CFConfig
   673  				err = json.Unmarshal(file, &writtenCFConfig)
   674  				Expect(err).ToNot(HaveOccurred())
   675  
   676  				Expect(writtenCFConfig.ConfigVersion).To(Equal(config.ConfigFile.ConfigVersion))
   677  				Expect(writtenCFConfig.Target).To(Equal(config.ConfigFile.Target))
   678  				Expect(writtenCFConfig.ColorEnabled).To(Equal(config.ConfigFile.ColorEnabled))
   679  			})
   680  		})
   681  	})
   682  
   683  	Describe("setter functions", func() {
   684  		Describe("SetTargetInformation", func() {
   685  			It("sets the api target and other related endpoints", func() {
   686  				config := Config{
   687  					ConfigFile: CFConfig{
   688  						TargetedOrganization: Organization{
   689  							GUID: "this-is-a-guid",
   690  							Name: "jo bobo jim boo",
   691  						},
   692  						TargetedSpace: Space{
   693  							GUID:     "this-is-a-guid",
   694  							Name:     "jo bobo jim boo",
   695  							AllowSSH: true,
   696  						},
   697  					},
   698  				}
   699  				config.SetTargetInformation(
   700  					"https://api.foo.com",
   701  					"2.59.31",
   702  					"https://login.foo.com",
   703  					"2.0.0",
   704  					"wws://doppler.foo.com:443",
   705  					"https://api.foo.com/routing",
   706  					true,
   707  				)
   708  
   709  				Expect(config.ConfigFile.Target).To(Equal("https://api.foo.com"))
   710  				Expect(config.ConfigFile.APIVersion).To(Equal("2.59.31"))
   711  				Expect(config.ConfigFile.AuthorizationEndpoint).To(Equal("https://login.foo.com"))
   712  				Expect(config.ConfigFile.MinCLIVersion).To(Equal("2.0.0"))
   713  				Expect(config.ConfigFile.DopplerEndpoint).To(Equal("wws://doppler.foo.com:443"))
   714  				Expect(config.ConfigFile.RoutingEndpoint).To(Equal("https://api.foo.com/routing"))
   715  				Expect(config.ConfigFile.SkipSSLValidation).To(BeTrue())
   716  
   717  				Expect(config.ConfigFile.TargetedOrganization.GUID).To(BeEmpty())
   718  				Expect(config.ConfigFile.TargetedOrganization.Name).To(BeEmpty())
   719  				Expect(config.ConfigFile.TargetedSpace.GUID).To(BeEmpty())
   720  				Expect(config.ConfigFile.TargetedSpace.Name).To(BeEmpty())
   721  				Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse())
   722  			})
   723  		})
   724  
   725  		Describe("SetTokenInformation", func() {
   726  			It("sets the authentication token information", func() {
   727  				var config Config
   728  				config.SetTokenInformation("I am the access token", "I am the refresh token", "I am the SSH OAuth client")
   729  
   730  				Expect(config.ConfigFile.AccessToken).To(Equal("I am the access token"))
   731  				Expect(config.ConfigFile.RefreshToken).To(Equal("I am the refresh token"))
   732  				Expect(config.ConfigFile.SSHOAuthClient).To(Equal("I am the SSH OAuth client"))
   733  			})
   734  		})
   735  
   736  		Describe("SetAccessToken", func() {
   737  			It("sets the authentication token information", func() {
   738  				var config Config
   739  				config.SetAccessToken("I am the access token")
   740  				Expect(config.ConfigFile.AccessToken).To(Equal("I am the access token"))
   741  			})
   742  		})
   743  
   744  		Describe("SetRefreshToken", func() {
   745  			It("sets the refresh token information", func() {
   746  				var config Config
   747  				config.SetRefreshToken("I am the refresh token")
   748  				Expect(config.ConfigFile.RefreshToken).To(Equal("I am the refresh token"))
   749  			})
   750  		})
   751  
   752  		Describe("SetOrganizationInformation", func() {
   753  			It("sets the organization GUID and name", func() {
   754  				config := Config{}
   755  				config.SetOrganizationInformation("guid-value-1", "my-org-name")
   756  
   757  				Expect(config.ConfigFile.TargetedOrganization.GUID).To(Equal("guid-value-1"))
   758  				Expect(config.ConfigFile.TargetedOrganization.Name).To(Equal("my-org-name"))
   759  			})
   760  		})
   761  
   762  		Describe("SetSpaceInformation", func() {
   763  			It("sets the space GUID, name, and AllowSSH", func() {
   764  				config := Config{}
   765  				config.SetSpaceInformation("guid-value-1", "my-org-name", true)
   766  
   767  				Expect(config.ConfigFile.TargetedSpace.GUID).To(Equal("guid-value-1"))
   768  				Expect(config.ConfigFile.TargetedSpace.Name).To(Equal("my-org-name"))
   769  				Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeTrue())
   770  			})
   771  		})
   772  
   773  		Describe("SetUAAEndpoint", func() {
   774  			It("sets the UAA endpoint", func() {
   775  				var config Config
   776  				config.SetUAAEndpoint("some-uaa-endpoint.com")
   777  				Expect(config.ConfigFile.UAAEndpoint).To(Equal("some-uaa-endpoint.com"))
   778  			})
   779  		})
   780  
   781  		Describe("UnsetOrganizationInformation", func() {
   782  			config := Config{}
   783  			BeforeEach(func() {
   784  				config.SetOrganizationInformation("some-org-guid", "some-org")
   785  			})
   786  
   787  			It("resets the org GUID and name", func() {
   788  				config.UnsetOrganizationInformation()
   789  
   790  				Expect(config.ConfigFile.TargetedOrganization.GUID).To(Equal(""))
   791  				Expect(config.ConfigFile.TargetedOrganization.Name).To(Equal(""))
   792  			})
   793  		})
   794  
   795  		Describe("UnsetSpaceInformation", func() {
   796  			config := Config{}
   797  			BeforeEach(func() {
   798  				config.SetSpaceInformation("guid-value-1", "my-org-name", true)
   799  			})
   800  
   801  			It("resets the space GUID, name, and AllowSSH to default values", func() {
   802  				config.UnsetSpaceInformation()
   803  
   804  				Expect(config.ConfigFile.TargetedSpace.GUID).To(Equal(""))
   805  				Expect(config.ConfigFile.TargetedSpace.Name).To(Equal(""))
   806  				Expect(config.ConfigFile.TargetedSpace.AllowSSH).To(BeFalse())
   807  			})
   808  		})
   809  	})
   810  })