github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/configuration/core_config/config_data_test.go (about)

     1  package core_config_test
     2  
     3  import (
     4  	"regexp"
     5  
     6  	. "github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var exampleJSON = `
    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  	"AccessToken": "the-access-token",
    22  	"RefreshToken": "the-refresh-token",
    23  	"OrganizationFields": {
    24  		"Guid": "the-org-guid",
    25  		"Name": "the-org",
    26  		"QuotaDefinition": {
    27  			"name":"",
    28  			"memory_limit":0,
    29  			"instance_memory_limit":0,
    30  			"total_routes":0,
    31  			"total_services":0,
    32  			"non_basic_services_allowed": false
    33  		}
    34  	},
    35  	"SpaceFields": {
    36  		"Guid": "the-space-guid",
    37  		"Name": "the-space"
    38  	},
    39  	"SSLDisabled": true,
    40  	"AsyncTimeout": 1000,
    41  	"Trace": "path/to/some/file",
    42  	"ColorEnabled": "true",
    43  	"Locale": "fr_FR",
    44  	"PluginRepos": [
    45  		{
    46  			"Name": "repo1",
    47  			"Url": "http://repo.com"
    48  		}
    49  	],
    50  	"MinCliVersion": "6.0.0",
    51  	"MinRecommendedCliVersion": "6.9.0"
    52  }`
    53  
    54  var exampleData = &Data{
    55  	Target:                   "api.example.com",
    56  	ApiVersion:               "3",
    57  	AuthorizationEndpoint:    "auth.example.com",
    58  	LoggregatorEndPoint:      "loggregator.example.com",
    59  	DopplerEndPoint:          "doppler.example.com",
    60  	UaaEndpoint:              "uaa.example.com",
    61  	AccessToken:              "the-access-token",
    62  	RefreshToken:             "the-refresh-token",
    63  	MinCliVersion:            "6.0.0",
    64  	MinRecommendedCliVersion: "6.9.0",
    65  	OrganizationFields: models.OrganizationFields{
    66  		Guid: "the-org-guid",
    67  		Name: "the-org",
    68  	},
    69  	SpaceFields: models.SpaceFields{
    70  		Guid: "the-space-guid",
    71  		Name: "the-space",
    72  	},
    73  	SSLDisabled:  true,
    74  	Trace:        "path/to/some/file",
    75  	AsyncTimeout: 1000,
    76  	ColorEnabled: "true",
    77  	Locale:       "fr_FR",
    78  	PluginRepos: []models.PluginRepo{
    79  		models.PluginRepo{
    80  			Name: "repo1",
    81  			Url:  "http://repo.com",
    82  		},
    83  	},
    84  }
    85  
    86  var _ = Describe("V3 Config files", func() {
    87  	Describe("serialization", func() {
    88  		It("creates a JSON string from the config object", func() {
    89  			jsonData, err := exampleData.JsonMarshalV3()
    90  
    91  			Expect(err).NotTo(HaveOccurred())
    92  			Expect(stripWhitespace(string(jsonData))).To(ContainSubstring(stripWhitespace(exampleJSON)))
    93  		})
    94  	})
    95  
    96  	Describe("parsing", func() {
    97  		It("returns an error when the JSON is invalid", func() {
    98  			configData := NewData()
    99  			err := configData.JsonUnmarshalV3([]byte(`{ "not_valid": ### }`))
   100  
   101  			Expect(err).To(HaveOccurred())
   102  		})
   103  
   104  		It("creates a config object from valid JSON", func() {
   105  			configData := NewData()
   106  			err := configData.JsonUnmarshalV3([]byte(exampleJSON))
   107  
   108  			Expect(err).NotTo(HaveOccurred())
   109  			Expect(configData).To(Equal(exampleData))
   110  		})
   111  	})
   112  })
   113  
   114  var whiteSpaceRegex = regexp.MustCompile(`\s+`)
   115  
   116  func stripWhitespace(input string) string {
   117  	return whiteSpaceRegex.ReplaceAllString(input, "")
   118  }