github.com/loafoe/cli@v7.1.0+incompatible/cf/configuration/coreconfig/config_data_test.go (about)

     1  package coreconfig_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     5  	"code.cloudfoundry.org/cli/cf/models"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("V3 Config files", func() {
    12  	var exampleV3JSON = `
    13  	{
    14  		"ConfigVersion": 4,
    15  		"Target": "api.example.com",
    16  		"APIVersion": "3",
    17  		"AuthorizationEndpoint": "auth.example.com",
    18  		"DopplerEndPoint": "doppler.example.com",
    19  		"LogCacheEndPoint": "log-cache.example.com",
    20  		"UaaEndpoint": "uaa.example.com",
    21  		"RoutingAPIEndpoint": "routing-api.example.com",
    22  		"AccessToken": "the-access-token",
    23  		"UAAGrantType": "",
    24  		"UAAOAuthClient": "cf-oauth-client-id",
    25  		"UAAOAuthClientSecret": "cf-oauth-client-secret",
    26  		"SSHOAuthClient": "ssh-oauth-client-id",
    27  		"RefreshToken": "the-refresh-token",
    28  		"OrganizationFields": {
    29  			"GUID": "the-org-guid",
    30  			"Name": "the-org",
    31  			"QuotaDefinition": {
    32  				"name":"",
    33  				"memory_limit":0,
    34  				"instance_memory_limit":0,
    35  				"total_routes":0,
    36  				"total_services":0,
    37  				"non_basic_services_allowed": false,
    38  				"app_instance_limit":0
    39  			}
    40  		},
    41  		"SpaceFields": {
    42  			"GUID": "the-space-guid",
    43  			"Name": "the-space",
    44  			"AllowSSH": false
    45  		},
    46  		"SSLDisabled": true,
    47  		"AsyncTimeout": 1000,
    48  		"Trace": "path/to/some/file",
    49  		"ColorEnabled": "true",
    50  		"Locale": "fr_FR",
    51  		"PluginRepos": [
    52  		{
    53  			"Name": "repo1",
    54  			"URL": "http://repo.com"
    55  		}
    56  		],
    57  		"MinCLIVersion": "6.0.0",
    58  		"MinRecommendedCLIVersion": "6.9.0"
    59  	}`
    60  
    61  	// V2 by virtue of ConfigVersion only
    62  	var exampleV2JSON = `
    63  	{
    64  		"ConfigVersion": 2,
    65  		"Target": "api.example.com",
    66  		"APIVersion": "3",
    67  		"AuthorizationEndpoint": "auth.example.com",
    68  		"LoggregatorEndPoint": "loggregator.example.com",
    69  		"DopplerEndPoint": "doppler.example.com",
    70  		"LogCacheEndPoint": "log-cache.example.com",
    71  		"UaaEndpoint": "uaa.example.com",
    72  		"RoutingAPIEndpoint": "routing-api.example.com",
    73  		"AccessToken": "the-access-token",
    74  		"UAAOAuthClient": "cf-oauth-client-id",
    75  		"UAAOAuthClientSecret": "cf-oauth-client-secret",
    76  		"SSHOAuthClient": "ssh-oauth-client-id",
    77  		"RefreshToken": "the-refresh-token",
    78  		"OrganizationFields": {
    79  			"GUID": "the-org-guid",
    80  			"Name": "the-org",
    81  			"QuotaDefinition": {
    82  				"name":"",
    83  				"memory_limit":0,
    84  				"instance_memory_limit":0,
    85  				"total_routes":0,
    86  				"total_services":0,
    87  				"non_basic_services_allowed": false
    88  			}
    89  		},
    90  		"SpaceFields": {
    91  			"GUID": "the-space-guid",
    92  			"Name": "the-space",
    93  			"AllowSSH": false
    94  		},
    95  		"SSLDisabled": true,
    96  		"AsyncTimeout": 1000,
    97  		"Trace": "path/to/some/file",
    98  		"ColorEnabled": "true",
    99  		"Locale": "fr_FR",
   100  		"PluginRepos": [
   101  		{
   102  			"Name": "repo1",
   103  			"URL": "http://repo.com"
   104  		}
   105  		],
   106  		"MinCLIVersion": "6.0.0",
   107  		"MinRecommendedCLIVersion": "6.9.0"
   108  	}`
   109  
   110  	Describe("NewData", func() {
   111  		It("sets default values for UAAOAuthClient and CFOAUthCLientSecret", func() {
   112  			data := coreconfig.NewData()
   113  			Expect(data.UAAOAuthClient).To(Equal("cf"))
   114  			Expect(data.UAAOAuthClientSecret).To(Equal(""))
   115  		})
   116  	})
   117  
   118  	Describe("JSONMarshalV3", func() {
   119  		It("creates a JSON string from the config object", func() {
   120  			data := &coreconfig.Data{
   121  				Target:                   "api.example.com",
   122  				APIVersion:               "3",
   123  				AuthorizationEndpoint:    "auth.example.com",
   124  				RoutingAPIEndpoint:       "routing-api.example.com",
   125  				DopplerEndPoint:          "doppler.example.com",
   126  				LogCacheEndPoint:         "log-cache.example.com",
   127  				UaaEndpoint:              "uaa.example.com",
   128  				AccessToken:              "the-access-token",
   129  				RefreshToken:             "the-refresh-token",
   130  				UAAGrantType:             "",
   131  				UAAOAuthClient:           "cf-oauth-client-id",
   132  				UAAOAuthClientSecret:     "cf-oauth-client-secret",
   133  				SSHOAuthClient:           "ssh-oauth-client-id",
   134  				MinCLIVersion:            "6.0.0",
   135  				MinRecommendedCLIVersion: "6.9.0",
   136  				OrganizationFields: models.OrganizationFields{
   137  					GUID: "the-org-guid",
   138  					Name: "the-org",
   139  				},
   140  				SpaceFields: models.SpaceFields{
   141  					GUID: "the-space-guid",
   142  					Name: "the-space",
   143  				},
   144  				SSLDisabled:  true,
   145  				Trace:        "path/to/some/file",
   146  				AsyncTimeout: 1000,
   147  				ColorEnabled: "true",
   148  				Locale:       "fr_FR",
   149  				PluginRepos: []models.PluginRepo{
   150  					{
   151  						Name: "repo1",
   152  						URL:  "http://repo.com",
   153  					},
   154  				},
   155  			}
   156  
   157  			jsonData, err := data.JSONMarshalV3()
   158  			Expect(err).NotTo(HaveOccurred())
   159  
   160  			Expect(jsonData).To(MatchJSON(exampleV3JSON))
   161  		})
   162  	})
   163  
   164  	Describe("JSONUnmarshalV3", func() {
   165  		It("returns an error when the JSON is invalid", func() {
   166  			configData := coreconfig.NewData()
   167  			err := configData.JSONUnmarshalV3([]byte(`{ "not_valid": ### }`))
   168  			Expect(err).To(HaveOccurred())
   169  		})
   170  
   171  		It("creates a config object from valid V3 JSON", func() {
   172  			expectedData := &coreconfig.Data{
   173  				ConfigVersion:            4,
   174  				Target:                   "api.example.com",
   175  				APIVersion:               "3",
   176  				AuthorizationEndpoint:    "auth.example.com",
   177  				RoutingAPIEndpoint:       "routing-api.example.com",
   178  				DopplerEndPoint:          "doppler.example.com",
   179  				LogCacheEndPoint:         "log-cache.example.com",
   180  				UaaEndpoint:              "uaa.example.com",
   181  				AccessToken:              "the-access-token",
   182  				RefreshToken:             "the-refresh-token",
   183  				UAAOAuthClient:           "cf-oauth-client-id",
   184  				UAAOAuthClientSecret:     "cf-oauth-client-secret",
   185  				SSHOAuthClient:           "ssh-oauth-client-id",
   186  				MinCLIVersion:            "6.0.0",
   187  				MinRecommendedCLIVersion: "6.9.0",
   188  				OrganizationFields: models.OrganizationFields{
   189  					GUID: "the-org-guid",
   190  					Name: "the-org",
   191  				},
   192  				SpaceFields: models.SpaceFields{
   193  					GUID: "the-space-guid",
   194  					Name: "the-space",
   195  				},
   196  				SSLDisabled:  true,
   197  				Trace:        "path/to/some/file",
   198  				AsyncTimeout: 1000,
   199  				ColorEnabled: "true",
   200  				Locale:       "fr_FR",
   201  				PluginRepos: []models.PluginRepo{
   202  					{
   203  						Name: "repo1",
   204  						URL:  "http://repo.com",
   205  					},
   206  				},
   207  			}
   208  
   209  			actualData := coreconfig.NewData()
   210  			err := actualData.JSONUnmarshalV3([]byte(exampleV3JSON))
   211  			Expect(err).NotTo(HaveOccurred())
   212  
   213  			Expect(actualData).To(Equal(expectedData))
   214  		})
   215  
   216  		It("returns an empty Data object for non-V3 JSON", func() {
   217  			actualData := coreconfig.NewData()
   218  			err := actualData.JSONUnmarshalV3([]byte(exampleV2JSON))
   219  			Expect(err).NotTo(HaveOccurred())
   220  
   221  			Expect(*actualData).To(Equal(coreconfig.Data{}))
   222  		})
   223  	})
   224  })