-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredact_test.go
178 lines (174 loc) · 3.36 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package sensitive
import (
"errors"
"reflect"
"strconv"
"testing"
)
func TestRedact(t *testing.T) {
type tc struct {
val any
want any
option func(*RedactConfig)
ok bool
err error
}
tcs := []tc{
{
val: nil,
ok: false,
err: ErrUnsupportedType,
},
{
val: "070 a",
ok: false,
err: ErrUnsupportedType,
},
{
val: Address{Street: "07024 Quigley Trace"},
ok: false,
err: ErrUnsupportedType,
},
{
val: &Address{Street: "07024 Quigley Trace"},
want: &Address{Street: "*******************"},
ok: true,
},
{
val: &Address{Street: "070 a"},
want: &Address{Street: "*****"},
ok: true,
},
func() tc {
testErr := errors.New("test replace error")
return tc{
val: &Address{Street: "070 a"},
option: func(rc *RedactConfig) {
rc.RedactFunc = func(fr FieldReplace, val string) (string, error) {
return "", testErr
}
},
ok: false,
err: testErr,
}
}(),
{
val: &Address{Street: "070 a"},
want: &Address{Street: "*"},
option: func(rc *RedactConfig) {
rc.RedactFunc = func(fr FieldReplace, val string) (string, error) {
return "*", nil
}
},
ok: true,
},
{
val: &Address{Street: "070 a"},
want: &Address{Street: "*"},
option: func(rc *RedactConfig) {
rc.RedactFunc = nil
},
ok: false,
err: ErrRedactFuncNotFound,
},
{
val: &Profile{
Email: "[email protected]",
Phone: ptr("250-308-0529"),
},
want: &Profile{
ID: "",
Email: "*************************",
Phone: ptr("************"),
},
ok: true,
},
func() tc {
type T struct {
Profile `sensitive:"dive"`
Address *Address `sensitive:"dive"`
}
return tc{
val: &T{
Profile: Profile{
ID: "abc",
Email: "[email protected]",
},
Address: &Address{Street: "07024 Quigley Trace"},
},
want: &T{
Profile: Profile{
ID: "abc",
Email: "*****************",
},
Address: &Address{Street: "*******************"},
},
ok: true,
}
}(),
func() tc {
type T struct {
Value string
}
return tc{
val: &T{
Value: "abc",
},
want: &T{
Value: "abc",
},
ok: true,
}
}(),
func() tc {
type T struct {
Email Email `sensitive:"data"`
Fullname string `sensitive:"data"`
}
return tc{
val: &T{
Email: "[email protected]",
Fullname: "Sarah Turcotte",
},
want: &T{
Email: "****@****.com",
Fullname: "**************",
},
option: func(rc *RedactConfig) {
rc.RedactFunc = func(fr FieldReplace, val string) (string, error) {
switch {
case fr.RType == reflect.TypeOf(Email("")):
return "****@****.com", nil
default:
return RedactDefaultFunc(fr, val)
}
}
},
ok: true,
}
}(),
}
for i, tc := range tcs {
t.Run("tc: "+strconv.Itoa(i+1), func(t *testing.T) {
err := Redact(tc.val, tc.option)
if !tc.ok {
if tc.err == nil {
if err == nil {
t.Fatal("expect err not to be nil")
}
return
}
if !errors.Is(err, tc.err) {
t.Fatalf("expect err is %v, got %v", tc.err, err)
}
return
}
if err != nil {
t.Fatal("expect err be nil, got", err)
}
if !reflect.DeepEqual(tc.want, tc.val) {
t.Fatalf("expect %s, %s be equals", tc.want, tc.val)
}
})
}
}