github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/services/v1/preconditions_test.go (about) 1 package v1 2 3 import ( 4 "context" 5 "testing" 6 7 v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" 8 "github.com/stretchr/testify/require" 9 10 "github.com/authzed/spicedb/internal/datastore/memdb" 11 "github.com/authzed/spicedb/internal/testfixtures" 12 "github.com/authzed/spicedb/pkg/datastore" 13 ) 14 15 var companyPlanFolder = &v1.RelationshipFilter{ 16 ResourceType: "document", 17 OptionalResourceId: "companyplan", 18 OptionalRelation: "parent", 19 OptionalSubjectFilter: &v1.SubjectFilter{ 20 SubjectType: "folder", 21 OptionalSubjectId: "company", 22 }, 23 } 24 25 var prefixMatch = &v1.RelationshipFilter{ 26 OptionalResourceIdPrefix: "c", 27 } 28 29 var prefixNoMatch = &v1.RelationshipFilter{ 30 OptionalResourceIdPrefix: "zzz", 31 } 32 33 func TestPreconditions(t *testing.T) { 34 require := require.New(t) 35 uninitialized, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC) 36 require.NoError(err) 37 38 ds, _ := testfixtures.StandardDatastoreWithData(uninitialized, require) 39 40 ctx := context.Background() 41 _, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error { 42 require.NoError(checkPreconditions(ctx, rwt, []*v1.Precondition{ 43 { 44 Operation: v1.Precondition_OPERATION_MUST_MATCH, 45 Filter: companyPlanFolder, 46 }, 47 })) 48 require.Error(checkPreconditions(ctx, rwt, []*v1.Precondition{ 49 { 50 Operation: v1.Precondition_OPERATION_MUST_NOT_MATCH, 51 Filter: companyPlanFolder, 52 }, 53 })) 54 require.NoError(checkPreconditions(ctx, rwt, []*v1.Precondition{ 55 { 56 Operation: v1.Precondition_OPERATION_MUST_MATCH, 57 Filter: prefixMatch, 58 }, 59 })) 60 require.Error(checkPreconditions(ctx, rwt, []*v1.Precondition{ 61 { 62 Operation: v1.Precondition_OPERATION_MUST_MATCH, 63 Filter: prefixNoMatch, 64 }, 65 })) 66 return nil 67 }) 68 require.NoError(err) 69 }