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

     1  package caveats
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	core "github.com/authzed/spicedb/pkg/proto/core/v1"
     8  	"github.com/authzed/spicedb/pkg/testutil"
     9  )
    10  
    11  func TestShortcircuitedOr(t *testing.T) {
    12  	tcs := []struct {
    13  		first    *core.CaveatExpression
    14  		second   *core.CaveatExpression
    15  		expected *core.CaveatExpression
    16  	}{
    17  		{
    18  			nil,
    19  			nil,
    20  			nil,
    21  		},
    22  		{
    23  			CaveatExprForTesting("first"),
    24  			nil,
    25  			nil,
    26  		},
    27  		{
    28  			nil,
    29  			CaveatExprForTesting("first"),
    30  			nil,
    31  		},
    32  		{
    33  			CaveatExprForTesting("first"),
    34  			CaveatExprForTesting("second"),
    35  			Or(CaveatExprForTesting("first"), CaveatExprForTesting("second")),
    36  		},
    37  	}
    38  
    39  	for _, tc := range tcs {
    40  		tc := tc
    41  		t.Run(fmt.Sprintf("%v-%v", tc.first, tc.second), func(t *testing.T) {
    42  			testutil.RequireProtoEqual(t, tc.expected, ShortcircuitedOr(tc.first, tc.second), "mismatch")
    43  		})
    44  	}
    45  }
    46  
    47  func TestOr(t *testing.T) {
    48  	tcs := []struct {
    49  		first    *core.CaveatExpression
    50  		second   *core.CaveatExpression
    51  		expected *core.CaveatExpression
    52  	}{
    53  		{
    54  			nil,
    55  			nil,
    56  			nil,
    57  		},
    58  		{
    59  			CaveatExprForTesting("first"),
    60  			nil,
    61  			CaveatExprForTesting("first"),
    62  		},
    63  		{
    64  			nil,
    65  			CaveatExprForTesting("first"),
    66  			CaveatExprForTesting("first"),
    67  		},
    68  		{
    69  			CaveatExprForTesting("first"),
    70  			CaveatExprForTesting("first"),
    71  			CaveatExprForTesting("first"),
    72  		},
    73  		{
    74  			CaveatExprForTesting("first"),
    75  			CaveatExprForTesting("second"),
    76  			&core.CaveatExpression{
    77  				OperationOrCaveat: &core.CaveatExpression_Operation{
    78  					Operation: &core.CaveatOperation{
    79  						Op:       core.CaveatOperation_OR,
    80  						Children: []*core.CaveatExpression{CaveatExprForTesting("first"), CaveatExprForTesting("second")},
    81  					},
    82  				},
    83  			},
    84  		},
    85  	}
    86  
    87  	for _, tc := range tcs {
    88  		tc := tc
    89  		t.Run(fmt.Sprintf("%v-%v", tc.first, tc.second), func(t *testing.T) {
    90  			testutil.RequireProtoEqual(t, tc.expected, Or(tc.first, tc.second), "mismatch")
    91  		})
    92  	}
    93  }
    94  
    95  func TestAnd(t *testing.T) {
    96  	tcs := []struct {
    97  		first    *core.CaveatExpression
    98  		second   *core.CaveatExpression
    99  		expected *core.CaveatExpression
   100  	}{
   101  		{
   102  			nil,
   103  			nil,
   104  			nil,
   105  		},
   106  		{
   107  			CaveatExprForTesting("first"),
   108  			nil,
   109  			CaveatExprForTesting("first"),
   110  		},
   111  		{
   112  			nil,
   113  			CaveatExprForTesting("first"),
   114  			CaveatExprForTesting("first"),
   115  		},
   116  		{
   117  			CaveatExprForTesting("first"),
   118  			CaveatExprForTesting("first"),
   119  			CaveatExprForTesting("first"),
   120  		},
   121  		{
   122  			CaveatExprForTesting("first"),
   123  			CaveatExprForTesting("second"),
   124  			&core.CaveatExpression{
   125  				OperationOrCaveat: &core.CaveatExpression_Operation{
   126  					Operation: &core.CaveatOperation{
   127  						Op:       core.CaveatOperation_AND,
   128  						Children: []*core.CaveatExpression{CaveatExprForTesting("first"), CaveatExprForTesting("second")},
   129  					},
   130  				},
   131  			},
   132  		},
   133  	}
   134  
   135  	for _, tc := range tcs {
   136  		tc := tc
   137  		t.Run(fmt.Sprintf("%v-%v", tc.first, tc.second), func(t *testing.T) {
   138  			testutil.RequireProtoEqual(t, tc.expected, And(tc.first, tc.second), "mismatch")
   139  		})
   140  	}
   141  }
   142  
   143  func TestInvert(t *testing.T) {
   144  	tcs := []struct {
   145  		first    *core.CaveatExpression
   146  		expected *core.CaveatExpression
   147  	}{
   148  		{
   149  			nil,
   150  			nil,
   151  		},
   152  		{
   153  			CaveatExprForTesting("first"),
   154  			&core.CaveatExpression{
   155  				OperationOrCaveat: &core.CaveatExpression_Operation{
   156  					Operation: &core.CaveatOperation{
   157  						Op:       core.CaveatOperation_NOT,
   158  						Children: []*core.CaveatExpression{CaveatExprForTesting("first")},
   159  					},
   160  				},
   161  			},
   162  		},
   163  	}
   164  
   165  	for _, tc := range tcs {
   166  		tc := tc
   167  		t.Run(fmt.Sprintf("%v", tc.first), func(t *testing.T) {
   168  			testutil.RequireProtoEqual(t, tc.expected, Invert(tc.first), "mismatch")
   169  		})
   170  	}
   171  }