如何在测试中禁用实体隐私设置?

huangapple go评论80阅读模式
英文:

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)))

}

huangapple
  • 本文由 发表于 2022年2月15日 05:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/71118392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定