【问题标题】:How to disable ent privacy settings for tests?如何禁用测试的耳鼻喉隐私设置?
【发布时间】:2022-02-18 07:55:15
【问题描述】:

我设置了一个 graphql 应用程序,它使用 ent 作为 ORM 和 firebase。

使用 ent,我添加了隐私规则来验证哪些用户可以访问某些 graphql 方法。

现在我想编写测试,但由于以下错误,我无法访问某些 graphql 方法:

viewer is missing: ent/privacy: deny rule

有没有办法在运行时禁用隐私规则进行测试?

【问题讨论】:

    标签: go orm graphql ent


    【解决方案1】:

    测试时,使用以下参数创建客户端:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 2021-11-17
      相关资源
      最近更新 更多