How to disable ent privacy settings for tests?

137 Views Asked by At

I setup a graphql application, which uses ent as ORM and firebase.

With ent, I added privacy rules to authenticate which users can access certain graphql methods.

Now I want to write tests, but I can't reach certain graphql methods because of the following error:

viewer is missing: ent/privacy: deny rule

Is there a way to disable privacy rules in runtime for testing?

1

There are 1 best solutions below

0
On BEST ANSWER

When testing, create your client with following parameters:

// WithContext defines a client.Option to pass context.
func WithContext(ctx context.Context) client.Option {
    return client.Option(func(r *client.Request) {
        r.HTTP = r.HTTP.WithContext(ctx)
    })
}

func TestGraphql(t *testing.T) {
    opts := []enttest.Option{
        enttest.WithOptions(ent.Log(t.Log)),
        enttest.WithMigrateOptions(migrate.WithGlobalUniqueID(true)),
    }
    entClient := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1", opts...)

    handler := handler.NewDefaultServer(gql.NewSchema(entClient, &trustly.Client{}))
    handler.Use(entgql.Transactioner{TxOpener: entClient})

    defer entClient.Close()

    err := c.Post(`query{}....`, &output, WithContext(privacy.DecisionContext(context.Background(), privacy.Allow)))

}