github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/caveats/structure_test.go (about)

     1  package caveats
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	"golang.org/x/exp/maps"
     9  
    10  	"github.com/authzed/spicedb/pkg/caveats/types"
    11  )
    12  
    13  func TestReferencedParameters(t *testing.T) {
    14  	tcs := []struct {
    15  		env                  *Environment
    16  		expr                 string
    17  		referencedParamNames []string
    18  	}{
    19  		{
    20  			MustEnvForVariables(map[string]types.VariableType{
    21  				"hi": types.BooleanType,
    22  			}),
    23  			"hi",
    24  			[]string{
    25  				"hi",
    26  			},
    27  		},
    28  		{
    29  			MustEnvForVariables(map[string]types.VariableType{
    30  				"a": types.IntType,
    31  				"b": types.IntType,
    32  			}),
    33  			"a == 42",
    34  			[]string{
    35  				"a",
    36  			},
    37  		},
    38  		{
    39  			MustEnvForVariables(map[string]types.VariableType{
    40  				"a": types.MustMapType(types.StringType),
    41  				"b": types.StringType,
    42  			}),
    43  			"a[b] == 'hi'",
    44  			[]string{
    45  				"a", "b",
    46  			},
    47  		},
    48  		{
    49  			MustEnvForVariables(map[string]types.VariableType{
    50  				"name":  types.StringType,
    51  				"group": types.StringType,
    52  			}),
    53  			`name.startsWith("/groups/" + group)`,
    54  			[]string{
    55  				"group", "name",
    56  			},
    57  		},
    58  		{
    59  			MustEnvForVariables(map[string]types.VariableType{
    60  				"somemap": types.MustMapType(types.StringType),
    61  			}),
    62  			`somemap.foo == 'hi'`,
    63  			[]string{
    64  				"somemap",
    65  			},
    66  		},
    67  		{
    68  			MustEnvForVariables(map[string]types.VariableType{
    69  				"tweets":    types.MustListType(types.MustMapType(types.IntType)),
    70  				"something": types.IntType,
    71  			}),
    72  			`tweets.all(t, t.size <= 140 && something > 42)`,
    73  			[]string{
    74  				"tweets",
    75  				"something",
    76  			},
    77  		},
    78  		{
    79  			MustEnvForVariables(map[string]types.VariableType{
    80  				"tweets":    types.MustListType(types.MustMapType(types.IntType)),
    81  				"something": types.IntType,
    82  			}),
    83  			`tweets.all(t, t.size <= 140)`,
    84  			[]string{
    85  				"tweets",
    86  			},
    87  		},
    88  	}
    89  
    90  	for _, tc := range tcs {
    91  		tc := tc
    92  		t.Run(tc.expr, func(t *testing.T) {
    93  			compiled, err := compileCaveat(tc.env, tc.expr)
    94  			require.NoError(t, err)
    95  
    96  			sort.Strings(tc.referencedParamNames)
    97  
    98  			found, err := compiled.ReferencedParameters(maps.Keys(tc.env.variables))
    99  			require.NoError(t, err)
   100  
   101  			foundSlice := found.AsSlice()
   102  			sort.Strings(foundSlice)
   103  			require.Equal(t, tc.referencedParamNames, foundSlice)
   104  		})
   105  	}
   106  }