-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrubber_test.go
76 lines (60 loc) · 1.75 KB
/
scrubber_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
package jsonscrubber_test
import (
"strings"
"testing"
jsonscrubber "github.com/Fyb3roptik/go-json-scrubber"
)
type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Address *Address `json:"address"`
}
type Address struct {
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
}
var user *User
func init() {
user = &User{
FirstName: "Foo",
LastName: "Bar",
Address: &Address{
City: "New York",
State: "NY",
},
}
}
func TestAddOnly(t *testing.T) {
scrubbedUser := jsonscrubber.AddOnly(user, "first_name", "address").(map[string]interface{})
scrubbedUser["address"] = jsonscrubber.AddOnly(user.Address, "city")
expectedScrubbed := [][]string{{"last_name"}, {"address", "address"}, {"address", "state"}, {"address", "zip"}}
for _, path := range expectedScrubbed {
if fieldExists(scrubbedUser, path) {
t.Errorf("unexpected field found %q", strings.Join(path, "."))
}
}
}
func TestRemoveOnly(t *testing.T) {
scrubbedUser := jsonscrubber.RemoveOnly(user, "first_name").(map[string]interface{})
scrubbedUser["address"] = jsonscrubber.RemoveOnly(user.Address, "city")
expectedScrubbed := [][]string{{"first_name"}, {"address", "city"}}
for _, path := range expectedScrubbed {
if fieldExists(scrubbedUser, path) {
t.Errorf("unexpected field found %q", strings.Join(path, "."))
}
}
}
// test helper, depends on the full object tree being map[string]interface{}
func fieldExists(object map[string]interface{}, path []string) bool {
key := path[0]
value, ok := object[key]
if !ok {
return false
}
if len(path) == 1 {
return true
}
return fieldExists(value.(map[string]interface{}), append([]string{}, path[1:]...))
}