github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/developmentmembership/foundsubject_test.go (about) 1 package developmentmembership 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/authzed/spicedb/internal/caveats" 10 "github.com/authzed/spicedb/pkg/tuple" 11 "github.com/authzed/spicedb/pkg/validationfile/blocks" 12 ) 13 14 func cfs(subjectType string, subjectID string, subjectRel string, excludedSubjectIDs []string, caveatName string) FoundSubject { 15 excludedSubjects := make([]FoundSubject, 0, len(excludedSubjectIDs)) 16 for _, excludedSubjectID := range excludedSubjectIDs { 17 excludedSubjects = append(excludedSubjects, FoundSubject{subject: ONR(subjectType, excludedSubjectID, subjectRel)}) 18 } 19 20 return FoundSubject{ 21 subject: ONR(subjectType, subjectID, subjectRel), 22 excludedSubjects: excludedSubjects, 23 relationships: tuple.NewONRSet(), 24 caveatExpression: caveats.CaveatExprForTesting(caveatName), 25 } 26 } 27 28 func TestToValidationString(t *testing.T) { 29 testCases := []struct { 30 name string 31 fs FoundSubject 32 expected string 33 }{ 34 { 35 "basic", 36 fs("user", "user1", "..."), 37 "user:user1", 38 }, 39 { 40 "with exclusion", 41 fs("user", "*", "...", "user1"), 42 "user:* - {user:user1}", 43 }, 44 { 45 "with some exclusion", 46 fs("user", "*", "...", 47 "user1", "user2", "user3", "user4", "user5", 48 ), 49 "user:* - {user:user1, user:user2, user:user3, user:user4, user:user5}", 50 }, 51 { 52 "with many exclusion", 53 fs("user", "*", "...", 54 "user1", "user2", "user3", "user4", "user5", "user6", 55 ), 56 "user:* - {user:user1, user:user2, user:user3, user:user4, user:user5, user:user6}", 57 }, 58 { 59 "caveated", 60 cfs("user", "tom", "...", nil, "somecaveat"), 61 "user:tom[...]", 62 }, 63 { 64 "caveated wildcard", 65 cfs("user", "*", "...", []string{"foo", "bar"}, "somecaveat"), 66 "user:*[...] - {user:bar, user:foo}", 67 }, 68 } 69 70 for _, tc := range testCases { 71 tc := tc 72 t.Run(tc.name, func(t *testing.T) { 73 require := require.New(t) 74 require.Equal(tc.expected, tc.fs.ToValidationString()) 75 76 sub, err := blocks.ValidationString(fmt.Sprintf("[%s]", tc.expected)).Subject() 77 require.Nil(err) 78 require.NotNil(sub) 79 }) 80 } 81 }