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