Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix unit tests sharing slice
  • Loading branch information
Emyrk committed May 23, 2022
commit 35879635b04d53510f1fb0db2035a6af1bd329f5
1 change: 0 additions & 1 deletion coderd/rbac/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rbac
import (
"context"
_ "embed"

"golang.org/x/xerrors"

"github.com/open-policy-agent/opa/rego"
Expand Down
27 changes: 17 additions & 10 deletions coderd/rbac/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func TestFilter(t *testing.T) {
objectList = append(objectList, file)
}

// copyList is to prevent tests from sharing the same slice
copyList := func(list []rbac.Object) []rbac.Object {
tmp := make([]rbac.Object, len(list))
copy(tmp, list)
return tmp
}

testCases := []struct {
Name string
List []rbac.Object
Expand All @@ -49,38 +56,38 @@ func TestFilter(t *testing.T) {
}{
{
Name: "FilterWorkspaceType",
List: objectList,
Expected: workspaceList,
List: copyList(objectList),
Expected: copyList(workspaceList),
Auth: func(o rbac.Object) error {
if o.Type != rbac.ResourceWorkspace.Type {
return xerrors.New("wrong type")
return xerrors.New("only workspace")
}
return nil
},
},
{
Name: "FilterFileType",
List: objectList,
Expected: fileList,
List: copyList(objectList),
Expected: copyList(fileList),
Auth: func(o rbac.Object) error {
if o.Type != rbac.ResourceFile.Type {
return xerrors.New("wrong type")
return xerrors.New("only file")
}
return nil
},
},
{
Name: "FilterAll",
List: objectList,
List: copyList(objectList),
Expected: []rbac.Object{},
Auth: func(o rbac.Object) error {
return xerrors.New("always fail")
},
},
{
Name: "FilterNone",
List: objectList,
Expected: objectList,
List: copyList(objectList),
Expected: copyList(objectList),
Auth: func(o rbac.Object) error {
return nil
},
Expand All @@ -98,7 +105,7 @@ func TestFilter(t *testing.T) {
}

filtered := rbac.Filter(context.Background(), authorizer, "me", []string{}, rbac.ActionRead, c.List)
require.ElementsMatch(t, filtered, c.Expected, "expect same list")
require.ElementsMatch(t, c.Expected, filtered, "expect same list")
require.Equal(t, len(c.Expected), len(filtered), "same length list")
})
}
Expand Down