-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CCXDEV-12885] Fix bug in RBAC logic + simplify (#1379)
* fix bug in RBAC logic + simplify * prevent nil pointer issues * fix lint * fix lint
- Loading branch information
Showing
2 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package auth | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/RedHatInsights/insights-results-smart-proxy/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAggregatePermissions(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
permissions []string | ||
want map[string][]string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "no permissions", | ||
permissions: []string{}, | ||
want: map[string][]string{}, | ||
}, | ||
{ | ||
name: "all permissions", | ||
permissions: []string{"ocp-advisor:*:*"}, | ||
want: map[string][]string{"*": {}}, | ||
}, | ||
{ | ||
name: "all permissions for recommendations", | ||
permissions: []string{"ocp-advisor:recommendation-results:*"}, | ||
want: map[string][]string{"recommendation-results": {"*"}}, | ||
}, | ||
{ | ||
name: "read permissions for recommendations", | ||
permissions: []string{"ocp-advisor:recommendation-results:read"}, | ||
want: map[string][]string{"recommendation-results": {"read"}}, | ||
}, | ||
{ | ||
name: "recommendations permissions but not for ocp-advisor", | ||
permissions: []string{"other:recommendation-results:read"}, | ||
want: map[string][]string{}, | ||
}, | ||
{ | ||
name: "all permissions but not for ocp-advisor", | ||
permissions: []string{"other:recommendation-results:*"}, | ||
want: map[string][]string{}, | ||
}, | ||
{ | ||
name: "permissions on ocp-advisor but not for recommendations", | ||
permissions: []string{"ocp-advisor:other:*"}, | ||
want: map[string][]string{}, | ||
}, | ||
{ | ||
name: "all permissions for recommendations and other resources", | ||
permissions: []string{"other:other:*", "ocp-advisor:recommendation-results:*"}, | ||
want: map[string][]string{"recommendation-results": {"*"}}, | ||
}, | ||
{ | ||
name: "bad RBAC response (not enough elements)", | ||
permissions: []string{"ocp-advisor:recommendation-results"}, | ||
want: map[string][]string{}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
acls := []types.RbacData{} | ||
for _, permission := range tc.permissions { | ||
acls = append(acls, types.RbacData{Permission: permission}) | ||
} | ||
got := aggregatePermissions(acls) | ||
assert.Equal(t, tc.want, got) | ||
}) | ||
} | ||
} |