Files
ucan/pkg/policy/ipld_test.go

65 lines
1.2 KiB
Go
Raw Normal View History

2024-09-01 20:27:54 +02:00
package policy
import (
"testing"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/stretchr/testify/require"
)
func TestIpldRoundTrip(t *testing.T) {
2024-09-02 02:25:34 +02:00
const illustrativeExample = `
[
2024-09-04 19:08:21 +02:00
["==", ".status", "draft"],
["all", ".reviewer", ["like", ".email", "*@example.com"]],
["any", ".tags",
["or", [
["==", ".", "news"],
2024-09-09 19:32:53 +02:00
["==", ".", "press"]
]]
]
2024-09-01 20:27:54 +02:00
]`
// must contain all the operators
const allOps = `
[
["and", [
["==", ".foo1", ".bar1"],
["!=", ".foo2", ".bar2"]
]],
["or", [
[">", ".foo5", 5.2],
[">=", ".foo6", 6.2]
]],
["not", ["like", ".foo7", "*@example.com"]],
["all", ".foo8",
["<", ".foo3", 3]
],
["any", ".foo9",
["<=", ".foo4", 4]
]
]`
2024-09-01 20:27:54 +02:00
for _, tc := range []struct {
2024-09-02 02:25:34 +02:00
name, dagJsonStr string
2024-09-01 20:27:54 +02:00
}{
{"illustrativeExample", illustrativeExample},
{"allOps", allOps},
2024-09-01 20:27:54 +02:00
} {
2024-09-02 02:25:34 +02:00
nodes, err := ipld.Decode([]byte(tc.dagJsonStr), dagjson.Decode)
2024-09-01 20:27:54 +02:00
require.NoError(t, err)
2024-09-02 02:25:34 +02:00
pol, err := FromIPLD(nodes)
2024-09-01 20:27:54 +02:00
require.NoError(t, err)
wroteIpld, err := pol.ToIPLD()
require.NoError(t, err)
wroteAsDagJson, err := ipld.Encode(wroteIpld, dagjson.Encode)
require.NoError(t, err)
2024-09-02 02:25:34 +02:00
require.JSONEq(t, tc.dagJsonStr, string(wroteAsDagJson))
2024-09-01 20:27:54 +02:00
}
}