github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/lang/funcs/redact_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package funcs
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/terramate-io/tf/lang/marks"
    10  	"github.com/zclconf/go-cty/cty"
    11  )
    12  
    13  func TestRedactIfSensitive(t *testing.T) {
    14  	testCases := map[string]struct {
    15  		value interface{}
    16  		marks []cty.ValueMarks
    17  		want  string
    18  	}{
    19  		"sensitive string": {
    20  			value: "foo",
    21  			marks: []cty.ValueMarks{cty.NewValueMarks(marks.Sensitive)},
    22  			want:  "(sensitive value)",
    23  		},
    24  		"marked non-sensitive string": {
    25  			value: "foo",
    26  			marks: []cty.ValueMarks{cty.NewValueMarks("boop")},
    27  			want:  `"foo"`,
    28  		},
    29  		"sensitive string with other marks": {
    30  			value: "foo",
    31  			marks: []cty.ValueMarks{cty.NewValueMarks("boop"), cty.NewValueMarks(marks.Sensitive)},
    32  			want:  "(sensitive value)",
    33  		},
    34  		"sensitive number": {
    35  			value: 12345,
    36  			marks: []cty.ValueMarks{cty.NewValueMarks(marks.Sensitive)},
    37  			want:  "(sensitive value)",
    38  		},
    39  		"non-sensitive number": {
    40  			value: 12345,
    41  			marks: []cty.ValueMarks{},
    42  			want:  "12345",
    43  		},
    44  	}
    45  
    46  	for name, tc := range testCases {
    47  		t.Run(name, func(t *testing.T) {
    48  			got := redactIfSensitive(tc.value, tc.marks...)
    49  			if got != tc.want {
    50  				t.Errorf("wrong result, got %v, want %v", got, tc.want)
    51  			}
    52  		})
    53  	}
    54  }