Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(exists-condition) Fixed exists comparison for leaf condition. #185

Merged
merged 2 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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