pomerium/pkg/storage/index_test.go
Caleb Doxsey 1a5b8b606f
core/lint: upgrade golangci-lint, replace interface{} with any (#5099)
* core/lint: upgrade golangci-lint, replace interface{} with any

* regen proto
2024-05-02 14:33:52 -06:00

62 lines
1.4 KiB
Go

package storage
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/structpb"
"github.com/pomerium/pomerium/pkg/protoutil"
)
func TestGetRecordIndex(t *testing.T) {
type M = map[string]any
t.Run("missing", func(t *testing.T) {
v, err := structpb.NewStruct(M{
"notindex": "value",
})
require.NoError(t, err)
assert.Nil(t, GetRecordIndex(v))
})
t.Run("struct", func(t *testing.T) {
v, err := structpb.NewStruct(M{
"$index": M{
"cidr": "192.168.0.0/16",
},
})
require.NoError(t, err)
assert.Equal(t, &structpb.Struct{
Fields: map[string]*structpb.Value{
"cidr": structpb.NewStringValue("192.168.0.0/16"),
},
}, GetRecordIndex(v))
})
t.Run("value", func(t *testing.T) {
v, err := structpb.NewValue(M{
"$index": M{
"cidr": "192.168.0.0/16",
},
})
require.NoError(t, err)
assert.Equal(t, &structpb.Struct{
Fields: map[string]*structpb.Value{
"cidr": structpb.NewStringValue("192.168.0.0/16"),
},
}, GetRecordIndex(v))
})
t.Run("any", func(t *testing.T) {
v, err := structpb.NewValue(M{
"$index": M{
"cidr": "192.168.0.0/16",
},
})
require.NoError(t, err)
data := protoutil.NewAny(v)
assert.Equal(t, &structpb.Struct{
Fields: map[string]*structpb.Value{
"cidr": structpb.NewStringValue("192.168.0.0/16"),
},
}, GetRecordIndex(data))
})
}