英文:
How to disable ent privacy settings for tests?
问题
我设置了一个使用ent作为ORM和Firebase的GraphQL应用程序。
使用ent,我添加了隐私规则来验证哪些用户可以访问特定的GraphQL方法。
现在我想编写测试,但由于以下错误,我无法访问某些GraphQL方法:
viewer is missing: ent/privacy: deny rule
有没有办法在运行时禁用隐私规则进行测试?
英文:
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
得分: 0
在进行测试时,使用以下参数创建你的客户端:
// WithContext 定义了一个 client.Option,用于传递上下文。
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)))
}
英文:
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)))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论