package extargs import ( "fmt" "testing" "github.com/ipld/go-ipld-prime/datamodel" "github.com/ipld/go-ipld-prime/fluent/qp" "github.com/stretchr/testify/require" "github.com/ucan-wg/go-ucan/pkg/policy" "github.com/ucan-wg/go-ucan/pkg/policy/literal" ) func ExampleInfuraExtArgs() { // Note: this is an example for how to build arguments, but you likely want to use InfuraExtArgs // through UcanCtx. pol := policy.Policy{} // policies from the delegations // We will construct the following args: // { // "ntwk":"eth-mainnet", // "quota":{ // "ur":1234, // "udc":1234, // "arch":1234, // "down":1234, // "store":1234, // "up":1234 // } // } infArgs := NewInfuraExtArgs(pol, func(ma datamodel.MapAssembler) { qp.MapEntry(ma, "ntwk", qp.String("eth-mainnet")) qp.MapEntry(ma, "quota", qp.Map(6, func(ma datamodel.MapAssembler) { qp.MapEntry(ma, "ur", qp.Int(1234)) qp.MapEntry(ma, "udc", qp.Int(1234)) qp.MapEntry(ma, "arch", qp.Int(1234)) qp.MapEntry(ma, "down", qp.Int(1234)) qp.MapEntry(ma, "store", qp.Int(1234)) qp.MapEntry(ma, "up", qp.Int(1234)) })) }) err := infArgs.Verify() fmt.Println(err) // Output: // } func TestInfura(t *testing.T) { assembler := func(ma datamodel.MapAssembler) { qp.MapEntry(ma, "ntwk", qp.String("eth-mainnet")) qp.MapEntry(ma, "quota", qp.Map(6, func(ma datamodel.MapAssembler) { qp.MapEntry(ma, "ur", qp.Int(1234)) qp.MapEntry(ma, "udc", qp.Int(1234)) qp.MapEntry(ma, "arch", qp.Int(1234)) qp.MapEntry(ma, "down", qp.Int(1234)) qp.MapEntry(ma, "store", qp.Int(1234)) qp.MapEntry(ma, "up", qp.Int(1234)) })) } tests := []struct { name string pol policy.Policy expected bool }{ { name: "no policies", pol: policy.Policy{}, expected: true, }, { name: "matching args", pol: policy.MustConstruct( policy.Equal(".inf.ntwk", literal.String("eth-mainnet")), policy.LessThanOrEqual(".inf.quota.ur", literal.Int(1234)), ), expected: true, }, { name: "wrong network", pol: policy.MustConstruct( policy.Equal(".inf.ntwk", literal.String("avalanche-fuji")), policy.LessThanOrEqual(".inf.quota.ur", literal.Int(1234)), ), expected: false, }, { name: "unrespected quota", pol: policy.MustConstruct( policy.Equal(".inf.ntwk", literal.String("eth-mainnet")), policy.LessThanOrEqual(".inf.quota.ur", literal.Int(100)), ), expected: false, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { extArgs := NewInfuraExtArgs(tc.pol, assembler) _, err := extArgs.Args() require.NoError(t, err) if tc.expected { require.NoError(t, extArgs.Verify()) } else { require.Error(t, extArgs.Verify()) } }) } }