chore: Allow RecordingAuthorizer to record multiple rbac authz calls (#6024)

* chore: Allow RecordingAuthorizer to record multiple rbac authz calls

Prior iteration only recorded the last call. This is required for
more comprehensive testing
This commit is contained in:
Steven Masley
2023-02-03 19:03:46 +00:00
committed by GitHub
parent 571f5d0e02
commit b359dbbd8b
9 changed files with 720 additions and 53 deletions
+20
View File
@@ -1,5 +1,25 @@
package slice
// New is a convenience method for creating []T.
func New[T any](items ...T) []T {
return items
}
// SameElements returns true if the 2 lists have the same elements in any
// order.
func SameElements[T comparable](a []T, b []T) bool {
if len(a) != len(b) {
return false
}
for _, element := range a {
if !Contains(b, element) {
return false
}
}
return true
}
func ContainsCompare[T any](haystack []T, needle T, equal func(a, b T) bool) bool {
for _, hay := range haystack {
if equal(needle, hay) {
+29
View File
@@ -1,14 +1,43 @@
package slice_test
import (
"math/rand"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/coderd/util/slice"
)
func TestSameElements(t *testing.T) {
t.Parallel()
// True
assertSameElements(t, []int{})
assertSameElements(t, []int{1, 2, 3})
assertSameElements(t, slice.New("a", "b", "c"))
assertSameElements(t, slice.New(uuid.New(), uuid.New(), uuid.New()))
// False
assert.False(t, slice.SameElements([]int{1, 2, 3}, []int{1, 2, 3, 4}))
assert.False(t, slice.SameElements([]int{1, 2, 3}, []int{1, 2}))
assert.False(t, slice.SameElements([]int{1, 2, 3}, []int{}))
assert.False(t, slice.SameElements([]int{}, []int{1, 2, 3}))
assert.False(t, slice.SameElements([]int{1, 2, 3}, []int{1, 2, 4}))
assert.False(t, slice.SameElements([]int{1}, []int{2}))
}
func assertSameElements[T comparable](t *testing.T, elements []T) {
cpy := make([]T, len(elements))
copy(cpy, elements)
rand.Shuffle(len(cpy), func(i, j int) {
cpy[i], cpy[j] = cpy[j], cpy[i]
})
assert.True(t, slice.SameElements(elements, cpy))
}
func TestUnique(t *testing.T) {
t.Parallel()