github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/testutil/subjects_test.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/authzed/spicedb/internal/caveats" 10 v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1" 11 ) 12 13 var ( 14 caveatexpr = caveats.CaveatExprForTesting 15 caveatAnd = caveats.And 16 caveatOr = caveats.Or 17 caveatInvert = caveats.Invert 18 sub = FoundSubject 19 wc = Wildcard 20 csub = CaveatedFoundSubject 21 cwc = CaveatedWildcard 22 ) 23 24 func TestCompareSubjects(t *testing.T) { 25 tcs := []struct { 26 first *v1.FoundSubject 27 second *v1.FoundSubject 28 expectedEquivalent bool 29 }{ 30 { 31 sub("1"), 32 sub("1"), 33 true, 34 }, 35 { 36 wc(), 37 wc(), 38 true, 39 }, 40 { 41 wc("1"), 42 wc("1"), 43 true, 44 }, 45 { 46 wc("1", "2"), 47 wc("2", "1"), 48 true, 49 }, 50 { 51 wc("1", "2", "3"), 52 wc("2", "1"), 53 false, 54 }, 55 { 56 sub("1"), 57 sub("2"), 58 false, 59 }, 60 { 61 csub("1", caveatexpr("first")), 62 csub("1", caveatInvert(caveatexpr("first"))), 63 false, 64 }, 65 { 66 csub("1", caveatInvert(caveatexpr("first"))), 67 csub("1", caveatInvert(caveatexpr("first"))), 68 true, 69 }, 70 { 71 csub("1", caveatInvert(caveatexpr("first"))), 72 csub("1", caveatInvert(caveatexpr("second"))), 73 false, 74 }, 75 { 76 csub("1", caveatexpr("first")), 77 csub("1", caveatexpr("first")), 78 true, 79 }, 80 { 81 csub("1", caveatexpr("first")), 82 csub("1", caveatexpr("second")), 83 false, 84 }, 85 { 86 cwc(caveatexpr("first")), 87 cwc(caveatexpr("first")), 88 true, 89 }, 90 { 91 cwc(caveatexpr("first")), 92 cwc(caveatexpr("second")), 93 false, 94 }, 95 { 96 cwc(caveatexpr("first"), csub("1", caveatexpr("c1"))), 97 cwc(caveatexpr("first"), csub("1", caveatexpr("c1"))), 98 true, 99 }, 100 { 101 cwc(caveatexpr("first"), csub("1", caveatexpr("c1"))), 102 cwc(caveatexpr("first"), csub("1", caveatexpr("c2"))), 103 false, 104 }, 105 { 106 cwc( 107 caveatAnd( 108 caveatexpr("first"), 109 caveatexpr("second"), 110 ), 111 ), 112 cwc( 113 caveatAnd( 114 caveatexpr("second"), 115 caveatexpr("first"), 116 ), 117 ), 118 true, 119 }, 120 { 121 cwc( 122 caveatAnd( 123 caveatexpr("first"), 124 caveatexpr("second"), 125 ), 126 ), 127 cwc( 128 caveatOr( 129 caveatexpr("second"), 130 caveatexpr("first"), 131 ), 132 ), 133 false, 134 }, 135 { 136 cwc( 137 caveatAnd( 138 caveatexpr("first"), 139 caveatexpr("first"), 140 ), 141 ), 142 cwc( 143 caveatexpr("first"), 144 ), 145 true, 146 }, 147 } 148 149 for _, tc := range tcs { 150 tc := tc 151 t.Run(fmt.Sprintf("%s vs %s", FormatSubject(tc.first), FormatSubject(tc.second)), func(t *testing.T) { 152 err := CheckEquivalentSubjects(tc.first, tc.second) 153 if tc.expectedEquivalent { 154 require.NoError(t, err) 155 } else { 156 require.NotNil(t, err) 157 } 158 159 err = CheckEquivalentSets([]*v1.FoundSubject{tc.first}, []*v1.FoundSubject{tc.second}) 160 if tc.expectedEquivalent { 161 require.NoError(t, err) 162 } else { 163 require.NotNil(t, err) 164 } 165 }) 166 } 167 }