test(invocation): verify arguments versus aggregated policies
This commit is contained in:
@@ -7,11 +7,8 @@ import (
|
||||
"github.com/ipfs/go-cid"
|
||||
"github.com/ipld/go-ipld-prime"
|
||||
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
||||
"github.com/ipld/go-ipld-prime/datamodel"
|
||||
"github.com/ipld/go-ipld-prime/fluent/qp"
|
||||
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
|
||||
"github.com/ipld/go-ipld-prime/node/basicnode"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
|
||||
@@ -904,55 +901,3 @@ func TestPartialMatch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestInvocationValidation applies the example policy to the second
|
||||
// example arguments as defined in the [Validation] section of the
|
||||
// invocation specification.
|
||||
//
|
||||
// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation
|
||||
func TestInvocationValidationSpecExamples(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pol := MustConstruct(
|
||||
Equal(".from", literal.String("alice@example.com")),
|
||||
Any(".to", Like(".", "*@example.com")),
|
||||
)
|
||||
|
||||
t.Run("with passing args", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
argsNode, err := qp.BuildMap(basicnode.Prototype.Any, 2, func(ma datamodel.MapAssembler) {
|
||||
qp.MapEntry(ma, "from", qp.String("alice@example.com"))
|
||||
qp.MapEntry(ma, "to", qp.List(2, func(la datamodel.ListAssembler) {
|
||||
qp.ListEntry(la, qp.String("bob@example.com"))
|
||||
qp.ListEntry(la, qp.String("carol@not.example.com"))
|
||||
}))
|
||||
qp.MapEntry(ma, "title", qp.String("Coffee"))
|
||||
qp.MapEntry(ma, "body", qp.String("Still on for coffee"))
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
exec, stmt := pol.Match(argsNode)
|
||||
assert.True(t, exec)
|
||||
assert.Nil(t, stmt)
|
||||
})
|
||||
|
||||
t.Run("fails on recipients (second statement)", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
argsNode, err := qp.BuildMap(basicnode.Prototype.Any, 2, func(ma datamodel.MapAssembler) {
|
||||
qp.MapEntry(ma, "from", qp.String("alice@example.com"))
|
||||
qp.MapEntry(ma, "to", qp.List(2, func(la datamodel.ListAssembler) {
|
||||
qp.ListEntry(la, qp.String("bob@null.com"))
|
||||
qp.ListEntry(la, qp.String("carol@elsewhere.example.com"))
|
||||
}))
|
||||
qp.MapEntry(ma, "title", qp.String("Coffee"))
|
||||
qp.MapEntry(ma, "body", qp.String("Still on for coffee"))
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
exec, stmt := pol.Match(argsNode)
|
||||
assert.False(t, exec)
|
||||
assert.NotNil(t, stmt)
|
||||
})
|
||||
}
|
||||
|
||||
98
pkg/policy/policytest/example.go
Normal file
98
pkg/policy/policytest/example.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package policytest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/ipld/go-ipld-prime"
|
||||
"github.com/ucan-wg/go-ucan/pkg/args"
|
||||
"github.com/ucan-wg/go-ucan/pkg/policy"
|
||||
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
|
||||
)
|
||||
|
||||
// EmptyPolicy provides a Policy with no statements.
|
||||
var EmptyPolicy = policy.Policy{}
|
||||
|
||||
// ExampleValidationPolicy provides a instantiated Policy containing the
|
||||
// statements that are included in the second code block of the [Validation]
|
||||
// section of the delegation specification.
|
||||
//
|
||||
// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation
|
||||
var ExamplePolicy = policy.MustConstruct(
|
||||
policy.Equal(".from", literal.String("alice@example.com")),
|
||||
policy.Any(".to", policy.Like(".", "*@example.com")),
|
||||
)
|
||||
|
||||
// TODO: Replace the URL for [Validation] above when the delegation
|
||||
// specification has been finished/merged.
|
||||
|
||||
// ExampleValidArguments provides valid, instantiated Arguments containing
|
||||
// the key/value pairs that are included in portion of the the second code
|
||||
// block of the [Validation] section of the delegation specification.
|
||||
//
|
||||
// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation
|
||||
var ExampleValidArguments = newBuilder(nil).
|
||||
add("from", "alice@example.com").
|
||||
add("to", []string{
|
||||
"bob@example.com",
|
||||
"carol@not.example.com",
|
||||
}).
|
||||
add("title", "Coffee").
|
||||
add("body", "Still on for coffee").
|
||||
mustBuild()
|
||||
|
||||
var exampleValidArgumentsIPLD = mustIPLD(ExampleValidArguments)
|
||||
|
||||
// ExampleInvalidArguments provides invalid, instantiated Arguments containing
|
||||
// the key/value pairs that are included in portion of the the second code
|
||||
// block of the [Validation] section of the delegation specification.
|
||||
//
|
||||
// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation
|
||||
var ExampleInvalidArguments = newBuilder(nil).
|
||||
add("from", "alice@example.com").
|
||||
add("to", []string{
|
||||
"bob@null.com",
|
||||
"carol@elsewhere.example.com",
|
||||
}).
|
||||
add("title", "Coffee").
|
||||
add("body", "Still on for coffee").
|
||||
mustBuild()
|
||||
|
||||
var exampleInvalidArgumentsIPLD = mustIPLD(ExampleInvalidArguments)
|
||||
|
||||
type builder struct {
|
||||
args *args.Args
|
||||
errs error
|
||||
}
|
||||
|
||||
func newBuilder(a *args.Args) *builder {
|
||||
if a == nil {
|
||||
a = args.New()
|
||||
}
|
||||
|
||||
return &builder{
|
||||
args: a,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *builder) add(key string, val any) *builder {
|
||||
b.errs = errors.Join(b.errs, b.args.Add(key, val))
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *builder) mustBuild() *args.Args {
|
||||
if b.errs != nil {
|
||||
panic(b.errs)
|
||||
}
|
||||
|
||||
return b.args
|
||||
}
|
||||
|
||||
func mustIPLD(args *args.Args) ipld.Node {
|
||||
node, err := args.ToIPLD()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return node
|
||||
}
|
||||
32
pkg/policy/policytest/example_test.go
Normal file
32
pkg/policy/policytest/example_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package policytest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestInvocationValidation applies the example policy to the second
|
||||
// example arguments as defined in the [Validation] section of the
|
||||
// invocation specification.
|
||||
//
|
||||
// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation
|
||||
func TestInvocationValidationSpecExamples(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("with passing args", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
exec, stmt := ExamplePolicy.Match(exampleValidArgumentsIPLD)
|
||||
assert.True(t, exec)
|
||||
assert.Nil(t, stmt)
|
||||
})
|
||||
|
||||
t.Run("fails on recipients (second statement)", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
exec, stmt := ExamplePolicy.Match(exampleInvalidArgumentsIPLD)
|
||||
assert.False(t, exec)
|
||||
assert.NotNil(t, stmt)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user