code.cloudfoundry.org/cli@v7.1.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  			"testtokentest": "CrAzY_PaSSw0rd",
    32  			"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"
    33  			}
    34  		]`),
    35  
    36  		Entry("when the top level is an array", `{
    37  			"mytoken": "CrAzY_PaSSw0rd",
    38  			"next_level": {
    39  				"again": {
    40  					"real password ": "CrAzY_PaSSw0rd",
    41  					"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",
    42  					"some other url":"jdbc:mysql://hostname/db-name?password=CrAzY_PaSSw0rd&user=username",
    43  					"some url":"jdbc:mysql://hostname/db-name?user=username&password=CrAzY_PaSSw0rd",
    44  					"testtokentest": "CrAzY_PaSSw0rd",
    45  					"uri":"postgres://some-user-name:CrAzY_PaSSw0rd@10.0.0.1:5432/some-other-data"
    46  				},
    47  				"ary": [
    48  					"jdbc:mysql://hostname/db-name?user=username&password=CrAzY_PaSSw0rd",
    49  					"postgres://some-user-name:CrAzY_PaSSw0rd@10.0.0.1:5432/some-other-data"
    50  				],
    51  				"ary2": [
    52  					{
    53  						"some other url":"jdbc:mysql://hostname/db-name?password=CrAzY_PaSSw0rd&user=username",
    54  						"some url":"jdbc:mysql://hostname/db-name?user=username&password=CrAzY_PaSSw0rd",
    55  						"uri":"postgres://some-user-name:CrAzY_PaSSw0rd@10.0.0.1:5432/some-other-data"
    56  					}
    57  				],
    58  				"next_pAssword_all": "CrAzY_PaSSw0rd"
    59  			}
    60  		}`),
    61  	)
    62  
    63  	It("formats spacing", func() {
    64  		original := `{"a":"b"}`
    65  		expected := `{
    66    "a": "b"
    67  }
    68  ` // Extra new line is required due to encoder
    69  		redacted, err := SanitizeJSON([]byte(original))
    70  		Expect(err).ToNot(HaveOccurred())
    71  		Expect(redacted).To(Equal([]byte(expected)))
    72  	})
    73  
    74  	It("does not escape characters", func() {
    75  		original := `{"a":"&<foo#>"}`
    76  		expected := `{
    77    "a": "&<foo#>"
    78  }
    79  ` // Extra new line is required due to encoder
    80  		redacted, err := SanitizeJSON([]byte(original))
    81  		Expect(err).ToNot(HaveOccurred())
    82  		Expect(redacted).To(Equal([]byte(expected)))
    83  	})
    84  })