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

     1  package caveats
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/authzed/spicedb/pkg/caveats"
     9  	"github.com/authzed/spicedb/pkg/caveats/types"
    10  	ns "github.com/authzed/spicedb/pkg/namespace"
    11  	core "github.com/authzed/spicedb/pkg/proto/core/v1"
    12  )
    13  
    14  func TestCaveatDiff(t *testing.T) {
    15  	testCases := []struct {
    16  		name           string
    17  		existing       *core.CaveatDefinition
    18  		updated        *core.CaveatDefinition
    19  		expectedDeltas []Delta
    20  	}{
    21  		{
    22  			"added caveat",
    23  			nil,
    24  			ns.MustCaveatDefinition(
    25  				caveats.MustEnvForVariables(map[string]types.VariableType{
    26  					"someparam": types.IntType,
    27  				}),
    28  				"somecaveat",
    29  				"true",
    30  			),
    31  			[]Delta{
    32  				{Type: CaveatAdded},
    33  			},
    34  		},
    35  		{
    36  			"removed caveat",
    37  			ns.MustCaveatDefinition(
    38  				caveats.MustEnvForVariables(map[string]types.VariableType{
    39  					"someparam": types.IntType,
    40  				}),
    41  				"somecaveat",
    42  				"true",
    43  			),
    44  			nil,
    45  			[]Delta{
    46  				{Type: CaveatRemoved},
    47  			},
    48  		},
    49  		{
    50  			"no changes",
    51  			ns.MustCaveatDefinition(
    52  				caveats.MustEnvForVariables(map[string]types.VariableType{
    53  					"someparam": types.IntType,
    54  				}),
    55  				"somecaveat",
    56  				"true",
    57  			),
    58  			ns.MustCaveatDefinition(
    59  				caveats.MustEnvForVariables(map[string]types.VariableType{
    60  					"someparam": types.IntType,
    61  				}),
    62  				"somecaveat",
    63  				"true",
    64  			),
    65  			[]Delta{},
    66  		},
    67  		{
    68  			"changed comments",
    69  			ns.MustCaveatDefinition(
    70  				caveats.MustEnvForVariables(map[string]types.VariableType{
    71  					"someparam": types.IntType,
    72  				}),
    73  				"somecaveat",
    74  				"true",
    75  			),
    76  			ns.MustCaveatDefinitionWithComment(
    77  				caveats.MustEnvForVariables(map[string]types.VariableType{
    78  					"someparam": types.IntType,
    79  				}),
    80  				"somecaveat",
    81  				"some comment",
    82  				"true",
    83  			),
    84  			[]Delta{
    85  				{Type: CaveatCommentsChanged},
    86  			},
    87  		},
    88  		{
    89  			"added parameter",
    90  			ns.MustCaveatDefinition(
    91  				caveats.MustEnvForVariables(map[string]types.VariableType{
    92  					"someparam": types.IntType,
    93  				}),
    94  				"somecaveat",
    95  				"true",
    96  			),
    97  			ns.MustCaveatDefinition(
    98  				caveats.MustEnvForVariables(map[string]types.VariableType{
    99  					"someparam":    types.IntType,
   100  					"anotherparam": types.BooleanType,
   101  				}),
   102  				"somecaveat",
   103  				"true",
   104  			),
   105  			[]Delta{
   106  				{Type: AddedParameter, ParameterName: "anotherparam"},
   107  			},
   108  		},
   109  		{
   110  			"removed parameter",
   111  			ns.MustCaveatDefinition(
   112  				caveats.MustEnvForVariables(map[string]types.VariableType{
   113  					"someparam": types.IntType,
   114  				}),
   115  				"somecaveat",
   116  				"true",
   117  			),
   118  			ns.MustCaveatDefinition(
   119  				caveats.MustEnvForVariables(map[string]types.VariableType{}),
   120  				"somecaveat",
   121  				"true",
   122  			),
   123  			[]Delta{
   124  				{Type: RemovedParameter, ParameterName: "someparam"},
   125  			},
   126  		},
   127  		{
   128  			"changed parameter type",
   129  			ns.MustCaveatDefinition(
   130  				caveats.MustEnvForVariables(map[string]types.VariableType{
   131  					"someparam": types.IntType,
   132  				}),
   133  				"somecaveat",
   134  				"true",
   135  			),
   136  			ns.MustCaveatDefinition(
   137  				caveats.MustEnvForVariables(map[string]types.VariableType{
   138  					"someparam": types.BooleanType,
   139  				}),
   140  				"somecaveat",
   141  				"true",
   142  			),
   143  			[]Delta{
   144  				{
   145  					Type:          ParameterTypeChanged,
   146  					ParameterName: "someparam",
   147  					PreviousType:  types.EncodeParameterType(types.IntType),
   148  					CurrentType:   types.EncodeParameterType(types.BooleanType),
   149  				},
   150  			},
   151  		},
   152  		{
   153  			"rename parameter",
   154  			ns.MustCaveatDefinition(
   155  				caveats.MustEnvForVariables(map[string]types.VariableType{
   156  					"someparam": types.IntType,
   157  				}),
   158  				"somecaveat",
   159  				"true",
   160  			),
   161  			ns.MustCaveatDefinition(
   162  				caveats.MustEnvForVariables(map[string]types.VariableType{
   163  					"anotherparam": types.IntType,
   164  				}),
   165  				"somecaveat",
   166  				"true",
   167  			),
   168  			[]Delta{
   169  				{Type: RemovedParameter, ParameterName: "someparam"},
   170  				{Type: AddedParameter, ParameterName: "anotherparam"},
   171  			},
   172  		},
   173  		{
   174  			"changed expression",
   175  			ns.MustCaveatDefinition(
   176  				caveats.MustEnvForVariables(map[string]types.VariableType{
   177  					"someparam": types.IntType,
   178  				}),
   179  				"somecaveat",
   180  				"true",
   181  			),
   182  			ns.MustCaveatDefinition(
   183  				caveats.MustEnvForVariables(map[string]types.VariableType{
   184  					"someparam": types.IntType,
   185  				}),
   186  				"somecaveat",
   187  				"false",
   188  			),
   189  			[]Delta{
   190  				{Type: CaveatExpressionChanged},
   191  			},
   192  		},
   193  		{
   194  			"another changed expression",
   195  			ns.MustCaveatDefinition(
   196  				caveats.MustEnvForVariables(map[string]types.VariableType{
   197  					"someparam": types.IntType,
   198  				}),
   199  				"somecaveat",
   200  				"someparam == 1",
   201  			),
   202  			ns.MustCaveatDefinition(
   203  				caveats.MustEnvForVariables(map[string]types.VariableType{
   204  					"someparam": types.IntType,
   205  				}),
   206  				"somecaveat",
   207  				"someparam <= 1",
   208  			),
   209  			[]Delta{
   210  				{Type: CaveatExpressionChanged},
   211  			},
   212  		},
   213  		{
   214  			"third changed expression",
   215  			ns.MustCaveatDefinition(
   216  				caveats.MustEnvForVariables(map[string]types.VariableType{
   217  					"someparam": types.IntType,
   218  				}),
   219  				"somecaveat",
   220  				"someparam == 1",
   221  			),
   222  			ns.MustCaveatDefinition(
   223  				caveats.MustEnvForVariables(map[string]types.VariableType{
   224  					"someparam": types.IntType,
   225  				}),
   226  				"somecaveat",
   227  				"someparam == 2",
   228  			),
   229  			[]Delta{
   230  				{Type: CaveatExpressionChanged},
   231  			},
   232  		},
   233  		{
   234  			"unchanged expression",
   235  			ns.MustCaveatDefinition(
   236  				caveats.MustEnvForVariables(map[string]types.VariableType{
   237  					"someparam": types.IntType,
   238  				}),
   239  				"somecaveat",
   240  				"someparam == 1",
   241  			),
   242  			ns.MustCaveatDefinition(
   243  				caveats.MustEnvForVariables(map[string]types.VariableType{
   244  					"someparam": types.IntType,
   245  				}),
   246  				"somecaveat",
   247  				"someparam == 1",
   248  			),
   249  			[]Delta{},
   250  		},
   251  		{
   252  			"unchanged slightly more complex expression",
   253  			ns.MustCaveatDefinition(
   254  				caveats.MustEnvForVariables(map[string]types.VariableType{
   255  					"someparam": types.IntType,
   256  				}),
   257  				"somecaveat",
   258  				"someparam == {\"a\": 42}.a",
   259  			),
   260  			ns.MustCaveatDefinition(
   261  				caveats.MustEnvForVariables(map[string]types.VariableType{
   262  					"someparam": types.IntType,
   263  				}),
   264  				"somecaveat",
   265  				"someparam == {\"a\": 42}.a",
   266  			),
   267  			[]Delta{},
   268  		},
   269  	}
   270  
   271  	for _, tc := range testCases {
   272  		tc := tc
   273  		t.Run(tc.name, func(t *testing.T) {
   274  			require := require.New(t)
   275  			diff, err := DiffCaveats(tc.existing, tc.updated)
   276  			require.Nil(err)
   277  			require.Equal(tc.expectedDeltas, diff.Deltas())
   278  		})
   279  	}
   280  }