forked from Azure/redact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredact_test.go
105 lines (81 loc) · 2.74 KB
/
redact_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package redact_test
import (
"testing"
"github.com/samkreter/redact"
"github.com/stretchr/testify/assert"
)
const (
secretVal = "thisIsASecret"
nonSecretVal = "thisIsAStandardVal"
)
var (
secretPtrVal = "thisIsAPtrSecret"
)
type TestStruct struct {
Secret string
SecretPtr *string
NonSecret string `redact:"nonsecret"`
}
type TestStructList struct {
Data []*TestStruct
}
func TestStringTestStruct(t *testing.T) {
t.Run("Basic Secret Redaction", func(t *testing.T) {
tStruct := &TestStruct{
NonSecret: nonSecretVal,
Secret: secretVal,
SecretPtr: &secretPtrVal,
}
err := redact.Redact(tStruct)
assert.NoError(t, err, "should not fail to redact struct")
assert.Equal(t, nonSecretVal, tStruct.NonSecret, "should contain non secret value")
assert.Equal(t, redact.RedactStrConst, tStruct.Secret, "should redact secret value")
assert.Equal(t, redact.RedactStrConst, *tStruct.SecretPtr, "should redact secret value")
})
t.Run("Should still redact empty strings", func(t *testing.T) {
emptyStrVal := ""
tStruct := &TestStruct{
NonSecret: nonSecretVal,
Secret: "",
SecretPtr: &emptyStrVal,
}
err := redact.Redact(tStruct)
assert.NoError(t, err, "should not fail to redact struct")
assert.Equal(t, nonSecretVal, tStruct.NonSecret, "should contain non secret value")
assert.Equal(t, redact.RedactStrConst, tStruct.Secret, "should redact secret value")
assert.Equal(t, redact.RedactStrConst, *tStruct.SecretPtr, "should redact secret value")
})
}
func TestStringTestStructList(t *testing.T) {
t.Run("Basic Secret Redaction", func(t *testing.T) {
tStruct := &TestStruct{
NonSecret: nonSecretVal,
Secret: secretVal,
SecretPtr: &secretPtrVal,
}
list := &TestStructList{
Data: []*TestStruct{tStruct},
}
err := redact.Redact(list)
assert.NoError(t, err, "should not fail to redact struct")
assert.Equal(t, nonSecretVal, list.Data[0].NonSecret, "should contain non secret value")
assert.Equal(t, redact.RedactStrConst, list.Data[0].Secret, "should redact secret value")
assert.Equal(t, redact.RedactStrConst, *list.Data[0].SecretPtr, "should redact secret value")
})
t.Run("Should still redact empty strings", func(t *testing.T) {
emptyStrVal := ""
tStruct := &TestStruct{
NonSecret: nonSecretVal,
Secret: "",
SecretPtr: &emptyStrVal,
}
list := &TestStructList{
Data: []*TestStruct{tStruct},
}
err := redact.Redact(list)
assert.NoError(t, err, "should not fail to redact struct")
assert.Equal(t, nonSecretVal, list.Data[0].NonSecret, "should contain non secret value")
assert.Equal(t, redact.RedactStrConst, list.Data[0].Secret, "should redact secret value")
assert.Equal(t, redact.RedactStrConst, *list.Data[0].SecretPtr, "should redact secret value")
})
}