github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/development/development_test.go (about) 1 package development 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 "go.uber.org/goleak" 10 11 core "github.com/authzed/spicedb/pkg/proto/core/v1" 12 devinterface "github.com/authzed/spicedb/pkg/proto/developer/v1" 13 "github.com/authzed/spicedb/pkg/tuple" 14 "github.com/authzed/spicedb/pkg/validationfile/blocks" 15 ) 16 17 func TestDevelopment(t *testing.T) { 18 defer goleak.VerifyNone(t, goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreCurrent()) 19 20 devCtx, devErrs, err := NewDevContext(context.Background(), &devinterface.RequestContext{ 21 Schema: `definition user {} 22 23 definition document { 24 relation viewer: user 25 } 26 `, 27 Relationships: []*core.RelationTuple{ 28 tuple.MustParse("document:somedoc#viewer@user:someuser"), 29 }, 30 }) 31 32 require.Nil(t, err) 33 require.Nil(t, devErrs) 34 35 assertions := &blocks.Assertions{ 36 AssertTrue: []blocks.Assertion{ 37 { 38 RelationshipWithContextString: "document:somedoc#viewer@user:someuser", 39 Relationship: tuple.MustToRelationship(tuple.MustParse("document:somedoc#viewer@user:someuser")), 40 }, 41 }, 42 } 43 44 adErrs, err := RunAllAssertions(devCtx, assertions) 45 require.NoError(t, err) 46 require.Nil(t, adErrs) 47 } 48 49 func TestDevelopmentInvalidRelationship(t *testing.T) { 50 defer goleak.VerifyNone(t, goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreCurrent()) 51 52 _, _, err := NewDevContext(context.Background(), &devinterface.RequestContext{ 53 Schema: `definition user {} 54 55 definition document { 56 relation viewer: user 57 } 58 `, 59 Relationships: []*core.RelationTuple{ 60 { 61 ResourceAndRelation: &core.ObjectAndRelation{ 62 Namespace: "document", 63 ObjectId: "*", 64 Relation: "view", 65 }, 66 Subject: &core.ObjectAndRelation{ 67 Namespace: "user", 68 ObjectId: "tom", 69 Relation: "...", 70 }, 71 }, 72 }, 73 }) 74 75 require.Error(t, err) 76 require.Contains(t, err.Error(), "invalid resource id") 77 } 78 79 func TestDevelopmentCaveatedExpectedRels(t *testing.T) { 80 defer goleak.VerifyNone(t, goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreCurrent()) 81 82 devCtx, devErrs, err := NewDevContext(context.Background(), &devinterface.RequestContext{ 83 Schema: `definition user {} 84 85 caveat somecaveat(somecondition int) { 86 somecondition == 42 87 } 88 89 definition document { 90 relation viewer: user with somecaveat 91 } 92 `, 93 Relationships: []*core.RelationTuple{ 94 tuple.MustParse("document:somedoc#viewer@user:someuser[somecaveat]"), 95 }, 96 }) 97 98 require.Nil(t, err) 99 require.Nil(t, devErrs) 100 101 validation, devErr := ParseExpectedRelationsYAML(`document:somedoc#viewer: []`) 102 require.Nil(t, devErr) 103 require.NotNil(t, validation) 104 105 ms, _, err := RunValidation(devCtx, validation) 106 require.Nil(t, err) 107 108 generated, err := GenerateValidation(ms) 109 require.Nil(t, err) 110 111 require.Equal(t, "document:somedoc#viewer:\n- '[user:someuser[...]] is <document:somedoc#viewer>'\n", generated) 112 } 113 114 func TestDevContextV1Service(t *testing.T) { 115 defer goleak.VerifyNone(t, goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreCurrent()) 116 117 devCtx, devErrs, err := NewDevContext(context.Background(), &devinterface.RequestContext{ 118 Schema: `definition user {} 119 120 definition document { 121 relation viewer: user 122 } 123 `, 124 Relationships: []*core.RelationTuple{ 125 tuple.MustParse("document:somedoc#viewer@user:someuser"), 126 }, 127 }) 128 129 require.Nil(t, err) 130 require.Nil(t, devErrs) 131 132 conn, shutdown, err := devCtx.RunV1InMemoryService() 133 require.Nil(t, err) 134 t.Cleanup(shutdown) 135 136 client := v1.NewSchemaServiceClient(conn) 137 resp, err := client.ReadSchema(context.Background(), &v1.ReadSchemaRequest{}) 138 require.Nil(t, err) 139 require.Contains(t, resp.SchemaText, "definition document") 140 141 shutdown() 142 }