github.com/opentofu/opentofu@v1.7.1/internal/lang/funcs/redact_test.go (about)

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