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