Skip to content

Commit

Permalink
Handle nil in restricter (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostcar committed Sep 21, 2020
1 parent a68c180 commit 4af78b8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions internal/datastore/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func newCache() *cache {
// first call fetches the result, the other calles get blocked until it the
// answer was fetched.
//
// A non existing value is returned as nil.
//
// All values get returned together. If only one key is missing, this function
// blocks, until all values are retrieved.
//
Expand Down
2 changes: 2 additions & 0 deletions internal/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func New(url string, closed <-chan struct{}, errHandler func(error), keychanger
}

// Get returns the value for one or many keys.
//
// If a key does not exist, the value nil is returned for that key.
func (d *Datastore) Get(ctx context.Context, keys ...string) ([]json.RawMessage, error) {
values, err := d.cache.GetOrSet(ctx, keys, func(keys []string) (map[string]json.RawMessage, error) {
return d.requestKeys(keys)
Expand Down
4 changes: 4 additions & 0 deletions internal/restrict/restrict.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (r *Restricter) Restrict(uid int, data map[string]json.RawMessage) error {
}

for k, v := range data {
if v == nil {
continue
}

if !allowed[k] {
data[k] = nil
continue
Expand Down
18 changes: 14 additions & 4 deletions internal/restrict/restrict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func TestRestrict(t *testing.T) {
func TestChecker(t *testing.T) {
perms := new(test.MockPermission)
perms.Data = map[string]bool{
"user/1/name": true,
"user/1/password": false,
"user/1/name": true,
"user/1/password": false,
"user/1/first_name": true,
}

called := make(map[string]bool)
Expand All @@ -49,12 +50,17 @@ func TestChecker(t *testing.T) {
called[key] = true
return []byte("touched"), nil
}),
"user/first_name": restrict.CheckerFunc(func(uid int, key string, value json.RawMessage) (json.RawMessage, error) {
called[key] = true
return []byte("touched"), nil
}),
}

r := restrict.New(perms, checker)
data := map[string]json.RawMessage{
"user/1/name": []byte("uwe"),
"user/1/password": []byte("easy"),
"user/1/name": []byte("uwe"),
"user/1/password": []byte("easy"),
"user/1/first_name": nil,
}
if err := r.Restrict(1, data); err != nil {
t.Errorf("Restrict returned unexpected error: %v", err)
Expand All @@ -75,4 +81,8 @@ func TestChecker(t *testing.T) {
if called["user/1/password"] {
t.Errorf("checker for key user/1/password was called")
}

if called["user/1/first_name"] {
t.Errorf("checker for key user/1/first_name was called")
}
}

0 comments on commit 4af78b8

Please sign in to comment.