mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
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:
@@ -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) {
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user