Files
ent/entc/integration/privacy/privacy_test.go
Ariel Mashraki 7988d3084d entc/hooks: initial work for mutations and hooks (#377)
* entc/hooks: initial work for mutations and hooks

* ent/schema: adding policy to schema

* ent: change op string to uint

* entc: move entschema to runtime and enable smooth transition

* entc/privacy: adding privacy template

* all: goimports

* intg/hooks: mutation client/tx and basic schema tests

* ent/privacy: adding more verbose decisions

* entc/gen: edge-ids getter and additional tests

* all: regen assets

* entc/gen: fix client hookd propagation

* intg: add deletion example

* intg/privacy: remove old entschema package

* typed privacy

* ent/privacy: hooks shouldn't be called on privacy deny

* entc/gen: fix schema hooks invocation order

* remove read policy from public api

* update circleci go orb

Co-authored-by: Ariel Mashraki <ariel@mashraki.co.il>
2020-03-16 00:19:27 +02:00

42 lines
1.4 KiB
Go

package privacy
import (
"context"
"errors"
"testing"
"github.com/facebookincubator/ent/entc/integration/privacy/ent"
"github.com/facebookincubator/ent/entc/integration/privacy/ent/planet"
"github.com/facebookincubator/ent/entc/integration/privacy/ent/privacy"
_ "github.com/facebookincubator/ent/entc/integration/privacy/ent/runtime"
"github.com/facebookincubator/ent/entc/integration/privacy/rule"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/require"
)
func TestPrivacyRules(t *testing.T) {
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
require.NoError(t, err)
defer client.Close()
ctx := context.Background()
err = client.Schema.Create(ctx)
require.NoError(t, err)
logf := rule.SetMutationLogFunc(t.Logf)
defer rule.SetMutationLogFunc(logf)
earth, err := client.Planet.Create().SetName("Earth").SetAge(4_540_000_000).Save(ctx)
require.NoError(t, err)
mars := client.Planet.Create().SetName("Mars").SaveX(ctx)
err = earth.Update().AddNeighbors(mars).Exec(ctx)
require.NoError(t, err)
logf = rule.SetMutationLogFunc(func(string, ...interface{}) {
require.FailNow(t, "hook called on privacy deny")
})
defer rule.SetMutationLogFunc(logf)
err = client.Planet.Update().Where(planet.ID(earth.ID)).SetAge(4_600_000_000).Exec(ctx)
require.True(t, errors.Is(err, privacy.Deny))
err = earth.Update().AddNeighbors(earth).Exec(ctx)
require.True(t, errors.Is(err, privacy.Deny))
}