snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
30
internal/testhelpers/driver.go
Normal file
30
internal/testhelpers/driver.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package testhelpers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
keysetpagination "github.com/ory/x/pagination/keysetpagination_v2"
|
||||
|
||||
"github.com/ory/keto/internal/driver"
|
||||
"github.com/ory/keto/internal/relationtuple"
|
||||
)
|
||||
|
||||
func CustomMapperNamespace(ctx context.Context) uuid.UUID {
|
||||
return uuid.Nil
|
||||
}
|
||||
|
||||
func RegistryWithManagerWrapper(t *testing.T, reg driver.Registry, pageOpts ...keysetpagination.Option) (*Registry, *relationtuple.ManagerWrapper) {
|
||||
mw := relationtuple.NewManagerWrapper(t, reg, pageOpts...)
|
||||
return &Registry{mw: mw, Registry: reg}, mw
|
||||
}
|
||||
|
||||
type Registry struct {
|
||||
mw *relationtuple.ManagerWrapper // managerProvider
|
||||
driver.Registry
|
||||
}
|
||||
|
||||
func (d *Registry) RelationTupleManager() relationtuple.Manager {
|
||||
return d.mw
|
||||
}
|
||||
54
internal/testhelpers/scenarios.go
Normal file
54
internal/testhelpers/scenarios.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package testhelpers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/ory/keto/internal/driver"
|
||||
"github.com/ory/keto/internal/driver/config"
|
||||
"github.com/ory/keto/schema"
|
||||
)
|
||||
|
||||
type Scenario struct {
|
||||
Name string
|
||||
Strict bool
|
||||
OrderedShardID bool
|
||||
Opl string
|
||||
InputTuples []string
|
||||
}
|
||||
|
||||
func (s Scenario) WithStrict(v bool) Scenario {
|
||||
s.Strict = v
|
||||
return s
|
||||
}
|
||||
|
||||
func (s Scenario) WithOrderedShardID() Scenario {
|
||||
s.OrderedShardID = true
|
||||
return s
|
||||
}
|
||||
|
||||
func (s Scenario) Run(t *testing.T, f func(t *testing.T, reg driver.Registry)) {
|
||||
name := s.Name
|
||||
if s.Strict {
|
||||
name += " (strict)"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if s.Opl != "" {
|
||||
_, errs := schema.Parse(s.Opl)
|
||||
require.Len(t, errs, 0)
|
||||
}
|
||||
opts := []driver.TestRegistryOption{
|
||||
driver.WithOPL(s.Opl),
|
||||
driver.WithMapperNamespace(CustomMapperNamespace),
|
||||
driver.WithConfig(config.KeyFeatureFlagStrictMode, s.Strict),
|
||||
}
|
||||
if s.OrderedShardID {
|
||||
opts = append(opts, driver.WithOrderedShardID())
|
||||
}
|
||||
reg := driver.NewSqliteTestRegistry(t, opts...)
|
||||
|
||||
MapAndInsertTuplesFromString(t, reg, s.InputTuples)
|
||||
f(t, reg)
|
||||
})
|
||||
}
|
||||
136
internal/testhelpers/tuples.go
Normal file
136
internal/testhelpers/tuples.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package testhelpers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/ory/keto/internal/relationtuple"
|
||||
"github.com/ory/keto/ketoapi"
|
||||
)
|
||||
|
||||
// TupleFactory parses human-readable tuples like "File:x#view@User:u" into
|
||||
// internal relation tuples. It adds a randomized suffix to namespaces and maps
|
||||
// object/subject IDs to UUIDs unique per factory.
|
||||
type TupleFactory struct {
|
||||
suffix string
|
||||
seed uuid.UUID
|
||||
}
|
||||
|
||||
func NewTupleFactory() *TupleFactory {
|
||||
seed := uuid.Must(uuid.NewV4())
|
||||
return &TupleFactory{suffix: "_" + seed.String(), seed: seed}
|
||||
}
|
||||
|
||||
// NS returns the namespace name with the factory's isolation suffix applied.
|
||||
func (f *TupleFactory) NS(namespace string) string { return namespace + f.suffix }
|
||||
|
||||
// UUID maps an object or subject ID to a UUID unique to this factory.
|
||||
func (f *TupleFactory) UUID(id string) uuid.UUID { return uuid.NewV5(f.seed, id) }
|
||||
|
||||
func (f *TupleFactory) Tuple(t testing.TB, s string) *relationtuple.RelationTuple {
|
||||
t.Helper()
|
||||
rt := APITupleFromString(t, s)
|
||||
result := &relationtuple.RelationTuple{
|
||||
Namespace: f.NS(rt.Namespace),
|
||||
Object: f.UUID(rt.Object),
|
||||
Relation: rt.Relation,
|
||||
}
|
||||
switch {
|
||||
case rt.SubjectID != nil:
|
||||
result.Subject = &relationtuple.SubjectID{ID: f.UUID(*rt.SubjectID)}
|
||||
case rt.SubjectSet != nil:
|
||||
result.Subject = &relationtuple.SubjectSet{
|
||||
Namespace: f.NS(rt.SubjectSet.Namespace),
|
||||
Object: f.UUID(rt.SubjectSet.Object),
|
||||
Relation: rt.SubjectSet.Relation,
|
||||
}
|
||||
default:
|
||||
t.Fatalf("tuple %q has no subject", s)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func APITupleFromString(t testing.TB, s string) *ketoapi.RelationTuple {
|
||||
t.Helper()
|
||||
rt, err := (&ketoapi.RelationTuple{}).FromString(s)
|
||||
require.NoError(t, err)
|
||||
return rt
|
||||
}
|
||||
|
||||
func TupleFromString(t testing.TB, s string) *relationtuple.RelationTuple {
|
||||
t.Helper()
|
||||
rt := APITupleFromString(t, s)
|
||||
result := &relationtuple.RelationTuple{
|
||||
Namespace: rt.Namespace,
|
||||
Object: toUUID(rt.Object),
|
||||
Relation: rt.Relation,
|
||||
}
|
||||
switch {
|
||||
case rt.SubjectID != nil:
|
||||
result.Subject = &relationtuple.SubjectID{ID: toUUID(*rt.SubjectID)}
|
||||
case rt.SubjectSet != nil:
|
||||
result.Subject = &relationtuple.SubjectSet{
|
||||
Namespace: rt.SubjectSet.Namespace,
|
||||
Object: toUUID(rt.SubjectSet.Object),
|
||||
Relation: rt.SubjectSet.Relation,
|
||||
}
|
||||
default:
|
||||
t.Fatal("invalid tuple")
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func SubjectFromString(t testing.TB, s string) relationtuple.Subject {
|
||||
t.Helper()
|
||||
rt, err := (&ketoapi.RelationTuple{}).FromString(" :object#relation@" + s)
|
||||
require.NoError(t, err)
|
||||
|
||||
switch {
|
||||
case rt.SubjectID != nil:
|
||||
return &relationtuple.SubjectID{ID: toUUID(*rt.SubjectID)}
|
||||
case rt.SubjectSet != nil:
|
||||
return &relationtuple.SubjectSet{
|
||||
Namespace: rt.SubjectSet.Namespace,
|
||||
Object: toUUID(rt.SubjectSet.Object),
|
||||
Relation: rt.SubjectSet.Relation,
|
||||
}
|
||||
default:
|
||||
t.Fatal("invalid subject")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func SubjectSetFromString(t testing.TB, s string) *relationtuple.SubjectSet {
|
||||
t.Helper()
|
||||
subject := SubjectFromString(t, s)
|
||||
subjectSet, ok := subject.(*relationtuple.SubjectSet)
|
||||
require.True(t, ok, "expected subject to be a SubjectSet")
|
||||
return subjectSet
|
||||
}
|
||||
|
||||
type deps interface {
|
||||
relationtuple.MapperProvider
|
||||
relationtuple.ManagerProvider
|
||||
}
|
||||
|
||||
func MapAndInsertTuplesFromString(t testing.TB, d deps, tuples []string) {
|
||||
t.Helper()
|
||||
relationTuples := make([]*relationtuple.RelationTuple, len(tuples))
|
||||
for i, tuple := range tuples {
|
||||
relationTuples[i] = TupleFromString(t, tuple)
|
||||
}
|
||||
require.NoError(t, d.RelationTupleManager().WriteRelationTuples(t.Context(), relationTuples...))
|
||||
}
|
||||
|
||||
func MapAndInsertTuples(t testing.TB, d deps, tuples ...*ketoapi.RelationTuple) {
|
||||
t.Helper()
|
||||
its, err := d.Mapper().FromTuple(t.Context(), tuples...)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, d.RelationTupleManager().WriteRelationTuples(t.Context(), its...))
|
||||
}
|
||||
|
||||
func toUUID(s string) uuid.UUID {
|
||||
return uuid.NewV5(uuid.Nil, s)
|
||||
}
|
||||
Reference in New Issue
Block a user