-
Notifications
You must be signed in to change notification settings - Fork 0
/
obscure_test.go
119 lines (102 loc) · 2.45 KB
/
obscure_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
package gopii
import (
"strconv"
"testing"
"github.com/matryer/is"
)
type obscureFuncTestCase struct {
s string
want string
}
func TestPlain(t *testing.T) {
testObscureFunc(t, Plain(), []obscureFuncTestCase{
{"", ""},
{"a", "a"},
{"ab", "ab"},
{"abc", "abc"},
{"abcde", "abcde"},
{"abcdefghijklm", "abcdefghijklm"},
})
}
func TestObscured(t *testing.T) {
testObscureFunc(t, Obscured(), []obscureFuncTestCase{
{"", "***"},
{"a", "***"},
{"ab", "***"},
{"abc", "***"},
{"abcde", "***"},
{"abcdefghijklm", "***"},
})
}
func TestKeepFirst(t *testing.T) {
testObscureFunc(t, KeepFirst(3), []obscureFuncTestCase{
{"", ""},
{"a", "a"},
{"ab", "ab"},
{"abc", "abc"},
{"abcde", "abc***"},
{"abcdefghijklm", "abc***"},
})
}
func TestKeepLast(t *testing.T) {
testObscureFunc(t, KeepLast(3), []obscureFuncTestCase{
{"", ""},
{"a", "a"},
{"ab", "ab"},
{"abc", "abc"},
{"abcde", "***cde"},
{"abcdefghijklm", "***klm"},
})
}
func TestKeepFirstLast(t *testing.T) {
testObscureFunc(t, KeepFirstLast(2, 3), []obscureFuncTestCase{
{"", ""},
{"a", "a"},
{"ab", "ab"},
{"abc", "abc"},
{"abcde", "abcde"},
{"abcdefg", "ab***efg"},
{"abcdefghijklm", "ab***klm"},
})
}
func testObscureFunc(t *testing.T, obscureFunc ObscureFunc, tests []obscureFuncTestCase) {
t.Helper()
for _, test := range tests {
t.Run(test.s, func(t *testing.T) {
is := is.New(t)
is.Equal(obscureFunc(test.s), test.want)
})
}
}
func TestObscure(t *testing.T) {
obscureFuncs := map[Level]ObscureFunc{
levelDevelopment: Plain(), // user123456789 -> user123456789
levelLog: KeepFirstLast(4, 3), // user123456789 -> user***789
levelAuditLog: KeepFirst(3), // user123456789 -> use***
levelInternet: KeepFirst(1), // user123456789 -> u***
}
tests := []struct {
outLevel Level
want string
}{
{0, "user123456789"},
{levelDevelopment, "user123456789"},
{levelLog - 2, "user***789"},
{levelLog, "user***789"},
{levelAuditLog - 2, "use***"},
{levelAuditLog, "use***"},
{levelInternet - 2, "u***"},
{levelInternet, "u***"},
{100, "***"},
}
for _, test := range tests {
t.Run("level "+strconv.Itoa(int(test.outLevel)), func(t *testing.T) {
is := is.New(t)
is.Equal(obscure("user123456789", test.outLevel, obscureFuncs), test.want)
})
}
}
func TestObscure_NoFunc(t *testing.T) {
is := is.New(t)
is.Equal(obscure("user123456789", levelLog, map[Level]ObscureFunc{}), "***")
}