Skip to content

Commit b44697f

Browse files
committed
updating tests for first release
1 parent 7eb72c9 commit b44697f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+19120
-2
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/samkreter/redact
2+
3+
go 1.16
4+
5+
require github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

redact.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
)
77

88
const (
9-
tagName = "redact"
9+
tagName = "redact"
10+
RedactStrConst = "REDACTED"
1011
)
1112

1213
type redactor func(string) string
@@ -125,7 +126,7 @@ func transformString(input, tagVal string) string {
125126
default:
126127
redactor, ok := redactors[tagVal]
127128
if !ok {
128-
return "REDACTED"
129+
return RedactStrConst
129130
}
130131

131132
return redactor(input)

redact_test.go

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

vendor/github.com/davecgh/go-spew/LICENSE

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypass.go

Lines changed: 152 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)