forked from launchdarkly/go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_filter_test.go
116 lines (101 loc) · 3.35 KB
/
user_filter_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
package ldclient
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestScrubUser(t *testing.T) {
t.Run("private built-in attributes per user", func(t *testing.T) {
filter := newUserFilter(DefaultConfig)
user := User{
Key: strPtr("user-key"),
FirstName: strPtr("sam"),
LastName: strPtr("smith"),
Name: strPtr("sammy"),
Country: strPtr("freedonia"),
Avatar: strPtr("my-avatar"),
Ip: strPtr("123.456.789"),
Email: strPtr("[email protected]"),
Secondary: strPtr("abcdef"),
}
for _, attr := range BuiltinAttributes {
user.PrivateAttributeNames = []string{attr}
scrubbedUser := *filter.scrubUser(user)
assert.Equal(t, []string{attr}, scrubbedUser.PrivateAttributes)
scrubbedUser.PrivateAttributes = nil
assert.NotEqual(t, user, scrubbedUser)
}
})
t.Run("global private built-in attributes", func(t *testing.T) {
user := User{
Key: strPtr("user-key"),
FirstName: strPtr("sam"),
LastName: strPtr("smith"),
Name: strPtr("sammy"),
Country: strPtr("freedonia"),
Avatar: strPtr("my-avatar"),
Ip: strPtr("123.456.789"),
Email: strPtr("[email protected]"),
Secondary: strPtr("abcdef"),
}
for _, attr := range BuiltinAttributes {
filter := newUserFilter(Config{PrivateAttributeNames: []string{attr}})
scrubbedUser := *filter.scrubUser(user)
assert.Equal(t, []string{attr}, scrubbedUser.PrivateAttributes)
scrubbedUser.PrivateAttributes = nil
assert.NotEqual(t, user, scrubbedUser)
}
})
t.Run("private custom attribute", func(t *testing.T) {
filter := newUserFilter(DefaultConfig)
userKey := "userKey"
user := User{
Key: &userKey,
PrivateAttributeNames: []string{"my-secret-attr"},
Custom: &map[string]interface{}{
"my-secret-attr": "my secret value",
}}
scrubbedUser := *filter.scrubUser(user)
assert.Equal(t, []string{"my-secret-attr"}, scrubbedUser.PrivateAttributes)
assert.NotContains(t, *scrubbedUser.Custom, "my-secret-attr")
})
t.Run("all attributes private", func(t *testing.T) {
filter := newUserFilter(Config{AllAttributesPrivate: true})
userKey := "userKey"
user := User{
Key: &userKey,
FirstName: strPtr("sam"),
LastName: strPtr("smith"),
Name: strPtr("sammy"),
Country: strPtr("freedonia"),
Avatar: strPtr("my-avatar"),
Ip: strPtr("123.456.789"),
Email: strPtr("[email protected]"),
Secondary: strPtr("abcdef"),
Custom: &map[string]interface{}{
"my-secret-attr": "my secret value",
}}
scrubbedUser := *filter.scrubUser(user)
sort.Strings(scrubbedUser.PrivateAttributes)
expectedAttributes := append(BuiltinAttributes, "my-secret-attr")
sort.Strings(expectedAttributes)
assert.Equal(t, expectedAttributes, scrubbedUser.PrivateAttributes)
scrubbedUser.PrivateAttributes = nil
assert.Equal(t, User{Key: &userKey, Custom: &map[string]interface{}{}}, scrubbedUser)
assert.NotContains(t, *scrubbedUser.Custom, "my-secret-attr")
assert.Nil(t, scrubbedUser.Name)
})
t.Run("anonymous attribute can't be private", func(t *testing.T) {
filter := newUserFilter(Config{AllAttributesPrivate: true})
userKey := "userKey"
anon := true
user := User{
Key: &userKey,
Anonymous: &anon}
scrubbedUser := *filter.scrubUser(user)
assert.Equal(t, scrubbedUser, user)
})
}
func strPtr(s string) *string {
return &s
}