Skip to content

Commit

Permalink
fix(exists-condition) Fixed exists comparison for leaf condition. (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirfolio3 authored and Michael Ng committed Nov 14, 2019
1 parent 8f5043e commit fbaeab1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
8 changes: 1 addition & 7 deletions pkg/decision/evaluator/matchers/exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,5 @@ type ExistsMatcher struct {

// Match returns true if the user's attribute is in the condition
func (m ExistsMatcher) Match(user entities.UserContext) (bool, error) {

_, err := user.GetStringAttribute(m.Condition.Name)
if err != nil {
return false, nil
}

return true, nil
return user.CheckAttributeExists(m.Condition.Name), nil
}
9 changes: 9 additions & 0 deletions pkg/entities/user_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ type UserContext struct {
Attributes map[string]interface{}
}

// CheckAttributeExists returns whether the specified attribute name exists in the attributes map.
func (u UserContext) CheckAttributeExists(attrName string) bool {
if value, ok := u.Attributes[attrName]; ok && value != nil {
return true
}

return false
}

// GetStringAttribute returns the string value for the specified attribute name in the attributes map. Returns error if not found.
func (u UserContext) GetStringAttribute(attrName string) (string, error) {
if value, ok := u.Attributes[attrName]; ok {
Expand Down
22 changes: 22 additions & 0 deletions pkg/entities/user_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ import (
"github.com/stretchr/testify/assert"
)

func TestUserAttributeExists(t *testing.T) {
userContext := UserContext{
Attributes: map[string]interface{}{
"string_foo": "foo",
"bool_true": true,
"bool_false": false,
"null_value": nil,
},
}

// Test happy path
assert.Equal(t, true, userContext.CheckAttributeExists("string_foo"))
assert.Equal(t, true, userContext.CheckAttributeExists("bool_true"))
assert.Equal(t, true, userContext.CheckAttributeExists("bool_false"))

// Test non-existent attr name
assert.Equal(t, false, userContext.CheckAttributeExists("invalid"))

// Test null value
assert.Equal(t, false, userContext.CheckAttributeExists("null_value"))
}

func TestUserAttributesGetStringAttribute(t *testing.T) {
userContext := UserContext{
Attributes: map[string]interface{}{
Expand Down

0 comments on commit fbaeab1

Please sign in to comment.