-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathuser_filter_test.go
172 lines (144 loc) · 5.27 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
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
package ldclient
import (
"encoding/json"
"errors"
"fmt"
"sort"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/launchdarkly/go-sdk-common.v1/ldvalue"
)
func TestScrubUser(t *testing.T) {
t.Run("private built-in attributes per user", func(t *testing.T) {
filter := newUserFilter(DefaultConfig)
user := NewUserBuilder("user-key").
FirstName("sam").
LastName("smith").
Name("sammy").
Country("freedonia").
Avatar("my-avatar").
IP("123.456.789").
Email("me@example.com").
Secondary("abcdef").
Build()
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 := NewUserBuilder("user-key").
FirstName("sam").
LastName("smith").
Name("sammy").
Country("freedonia").
Avatar("my-avatar").
IP("123.456.789").
Email("me@example.com").
Secondary("abcdef").
Build()
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 := NewUserBuilder(userKey).
Custom("my-secret-attr", ldvalue.String("my secret value")).AsPrivateAttribute().
Custom("non-secret-attr", ldvalue.String("OK value")).
Build()
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 := NewUserBuilder(userKey).
FirstName("sam").
LastName("smith").
Name("sammy").
Country("freedonia").
Avatar("my-avatar").
IP("123.456.789").
Email("me@example.com").
Secondary("abcdef").
Custom("my-secret-attr", ldvalue.String("my-secret-value")).
Build()
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, NewUser(userKey), scrubbedUser.User)
})
t.Run("anonymous attribute can't be private", func(t *testing.T) {
filter := newUserFilter(Config{AllAttributesPrivate: true})
user := NewUserBuilder(userKey).Anonymous(true).Build()
scrubbedUser := *filter.scrubUser(user)
assert.Equal(t, user, scrubbedUser.User)
})
}
func TestUserSerialization(t *testing.T) {
var errorMessage = "don't serialize me, bro"
doUserSerializationErrorTest := func(errorValue interface{}, withUserKey bool) {
logger := newMockLogger("")
config := DefaultConfig
config.Loggers.SetBaseLogger(logger)
config.LogUserKeyInErrors = withUserKey
filter := newUserFilter(config)
user := NewUserBuilder("user-key").
FirstName("sam").
Email("test@example.com").
Build()
// To inject our problematic value, we need to access the Custom map directly. In a future version
// this will no longer be possible.
custom := make(map[string]interface{})
custom["problem"] = errorValue
user.Custom = &custom
scrubbedUser := filter.scrubUser(user)
bytes, err := json.Marshal(scrubbedUser)
assert.NoError(t, err)
expectedMessage := "ERROR: " + fmt.Sprintf(userSerializationErrorMessage, describeUserForErrorLog(&user, withUserKey), errorMessage)
assert.Equal(t, []string{expectedMessage}, logger.output)
// Verify that we did marshal all of the user attributes except the custom ones
expectedUser := user
expectedUser.Custom = nil
resultUser := NewUser("")
err = json.Unmarshal(bytes, &resultUser)
assert.NoError(t, err)
assert.Equal(t, expectedUser, resultUser)
}
t.Run("error in serialization of custom attributes is caught", func(t *testing.T) {
doUserSerializationErrorTest(valueThatErrorsWhenMarshalledToJSON(errorMessage), false)
})
t.Run("panic in serialization of custom attributes is caught", func(t *testing.T) {
doUserSerializationErrorTest(valueThatPanicsWhenMarshalledToJSON(errorMessage), false)
})
t.Run("error message includes user key depending on configuration", func(t *testing.T) {
doUserSerializationErrorTest(valueThatErrorsWhenMarshalledToJSON(errorMessage), true)
})
t.Run("panic message includes user key depending on configuration", func(t *testing.T) {
doUserSerializationErrorTest(valueThatPanicsWhenMarshalledToJSON(errorMessage), true)
})
}
func strPtr(s string) *string {
return &s
}
type valueThatErrorsWhenMarshalledToJSON string
type valueThatPanicsWhenMarshalledToJSON string
func (v valueThatErrorsWhenMarshalledToJSON) MarshalJSON() ([]byte, error) {
return nil, errors.New(string(v))
}
func (v valueThatPanicsWhenMarshalledToJSON) MarshalJSON() ([]byte, error) {
panic(errors.New(string(v)))
}