From b4e222f8a0fa35e9af16352a3e088516b0abf382 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Wed, 6 Nov 2024 10:17:18 -0500 Subject: [PATCH] test(policy): refactor matching test from delegation spec --- pkg/policy/match_test.go | 51 ++++++++++++++++++++++ pkg/policy/spec_test.go | 93 ---------------------------------------- 2 files changed, 51 insertions(+), 93 deletions(-) delete mode 100644 pkg/policy/spec_test.go diff --git a/pkg/policy/match_test.go b/pkg/policy/match_test.go index 9e3de4a..9940799 100644 --- a/pkg/policy/match_test.go +++ b/pkg/policy/match_test.go @@ -7,8 +7,11 @@ 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" @@ -512,3 +515,51 @@ func FuzzMatch(f *testing.F) { policy.Match(dataNode) }) } + +// 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) + + assert.True(t, pol.Match(argsNode)) + }) + + 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) + + assert.False(t, pol.Match(argsNode)) + }) +} diff --git a/pkg/policy/spec_test.go b/pkg/policy/spec_test.go deleted file mode 100644 index 507dc4d..0000000 --- a/pkg/policy/spec_test.go +++ /dev/null @@ -1,93 +0,0 @@ -package policy_test - -import ( - "testing" - - "github.com/ipld/go-ipld-prime/datamodel" - "github.com/ipld/go-ipld-prime/fluent/qp" - basicnode "github.com/ipld/go-ipld-prime/node/basic" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/ucan-wg/go-ucan/pkg/policy" -) - -// TestInvocationValidation applies the example policy to the example -// arguments as defined in the [Validation] section of the invocation -// specification. -// -// [Validation]: https://github.com/ucan-wg/delegation/tree/v1_ipld#validation -func TestInvocationValidation(t *testing.T) { - t.Parallel() - - polNode, err := qp.BuildList(basicnode.Prototype.Any, 2, func(la datamodel.ListAssembler) { - qp.ListEntry(la, qp.List(3, func(la datamodel.ListAssembler) { - qp.ListEntry(la, qp.String("==")) - qp.ListEntry(la, qp.String(".from")) - qp.ListEntry(la, qp.String("alice@example.com")) - })) - qp.ListEntry(la, qp.List(3, func(la datamodel.ListAssembler) { - qp.ListEntry(la, qp.String("any")) - qp.ListEntry(la, qp.String(".to")) - qp.ListEntry(la, qp.List(3, func(la datamodel.ListAssembler) { - qp.ListEntry(la, qp.String("like")) - qp.ListEntry(la, qp.String(".")) - qp.ListEntry(la, qp.String("*@example.com")) - })) - })) - }) - require.NoError(t, err) - - pol, err := policy.FromIPLD(polNode) - require.NoError(t, err) - - 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) - - assert.True(t, pol.Match(argsNode)) - }) - - t.Run("fails on sender (first 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("dan@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) - - assert.False(t, pol.Match(argsNode)) - }) - - 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@not.null.com")) - })) - qp.MapEntry(ma, "title", qp.String("Coffee")) - qp.MapEntry(ma, "body", qp.String("Still on for coffee")) - }) - require.NoError(t, err) - - assert.False(t, pol.Match(argsNode)) - }) -}