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