github.com/arunkumar7540/cli@v6.45.0+incompatible/util/ui/sanitize_json_test.go (about)

     1  package ui_test
     2  
     3  import (
     4  	"strings"
     5  
     6  	. "code.cloudfoundry.org/cli/util/ui"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/ginkgo/extensions/table"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("SanitizeJSON", func() {
    14  	DescribeTable("sanitizes the json input",
    15  		func(original string) {
    16  			expected := strings.Replace(original, "CrAzY_PaSSw0rd", RedactedValue, -1)
    17  
    18  			redacted, err := SanitizeJSON([]byte(original))
    19  			Expect(err).ToNot(HaveOccurred())
    20  			Expect(redacted).To(MatchJSON(expected))
    21  		},
    22  
    23  		Entry("when the top level is an array", `[
    24  			{
    25  				"some url":"jdbc:mysql://hostname/db-name?user=username&password=CrAzY_PaSSw0rd",
    26  				"some other url":"jdbc:mysql://hostname/db-name?password=CrAzY_PaSSw0rd&user=username",
    27  				"uri":"postgres://some-user-name:CrAzY_PaSSw0rd@10.0.0.1:5432/some-other-data"
    28  			},
    29  			{
    30  			"real password ": "CrAzY_PaSSw0rd",
    31  			"token_endpoint": "some url",
    32  			"testtokentest": "CrAzY_PaSSw0rd",
    33  			"simple": "https://www.google.com/search?q=i+am+a+potato&oq=I+am+a+potato&aqs=chrome.0.0l6.2383j0j8&client=ubuntu&sourceid=chrome&ie=UTF-8"
    34  			}
    35  		]`),
    36  
    37  		Entry("when the top level is an array", `{
    38  			"mytoken": "CrAzY_PaSSw0rd",
    39  			"next_level": {
    40  				"again": {
    41  					"real password ": "CrAzY_PaSSw0rd",
    42  					"simple": "https://www.google.com/search?q=i+am+a+potato&oq=I+am+a+potato&aqs=chrome.0.0l6.2383j0j8&client=ubuntu&sourceid=chrome&ie=UTF-8",
    43  					"some other url":"jdbc:mysql://hostname/db-name?password=CrAzY_PaSSw0rd&user=username",
    44  					"some url":"jdbc:mysql://hostname/db-name?user=username&password=CrAzY_PaSSw0rd",
    45  					"testtokentest": "CrAzY_PaSSw0rd",
    46  					"token_endpoint": "some url",
    47  					"uri":"postgres://some-user-name:CrAzY_PaSSw0rd@10.0.0.1:5432/some-other-data"
    48  				},
    49  				"ary": [
    50  					"jdbc:mysql://hostname/db-name?user=username&password=CrAzY_PaSSw0rd",
    51  					"postgres://some-user-name:CrAzY_PaSSw0rd@10.0.0.1:5432/some-other-data"
    52  				],
    53  				"ary2": [
    54  					{
    55  						"some other url":"jdbc:mysql://hostname/db-name?password=CrAzY_PaSSw0rd&user=username",
    56  						"some url":"jdbc:mysql://hostname/db-name?user=username&password=CrAzY_PaSSw0rd",
    57  						"uri":"postgres://some-user-name:CrAzY_PaSSw0rd@10.0.0.1:5432/some-other-data"
    58  					}
    59  				],
    60  				"next_pAssword_all": "CrAzY_PaSSw0rd"
    61  			}
    62  		}`),
    63  	)
    64  
    65  	It("formats spacing", func() {
    66  		original := `{"a":"b"}`
    67  		expected := `{
    68    "a": "b"
    69  }
    70  ` // Extra new line is required due to encoder
    71  		redacted, err := SanitizeJSON([]byte(original))
    72  		Expect(err).ToNot(HaveOccurred())
    73  		Expect(redacted).To(Equal([]byte(expected)))
    74  	})
    75  
    76  	It("does not escape characters", func() {
    77  		original := `{"a":"&<foo#>"}`
    78  		expected := `{
    79    "a": "&<foo#>"
    80  }
    81  ` // Extra new line is required due to encoder
    82  		redacted, err := SanitizeJSON([]byte(original))
    83  		Expect(err).ToNot(HaveOccurred())
    84  		Expect(redacted).To(Equal([]byte(expected)))
    85  	})
    86  })