test(invocation): adds schema round-trip test
This commit is contained in:
@@ -139,8 +139,6 @@ func prettyDAGJSON(data []byte) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(jsonData))
|
|
||||||
|
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
if err := json.Indent(&out, jsonData, "", " "); err != nil {
|
if err := json.Indent(&out, jsonData, "", " "); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
50
token/invocation/ipld_test.go
Normal file
50
token/invocation/ipld_test.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package invocation_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ipfs/go-cid"
|
||||||
|
"github.com/ipld/go-ipld-prime/datamodel"
|
||||||
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/command"
|
||||||
|
"github.com/ucan-wg/go-ucan/token/invocation"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSealUnsealRoundtrip(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
privKey, iss, sub, cmd, args, prf, meta, err := setupExampleNew()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
tkn1, err := invocation.New(
|
||||||
|
iss,
|
||||||
|
sub,
|
||||||
|
cmd,
|
||||||
|
prf,
|
||||||
|
invocation.WithArguments(args),
|
||||||
|
invocation.WithMeta("env", "development"),
|
||||||
|
invocation.WithMeta("tags", meta["tags"]),
|
||||||
|
invocation.WithExpirationIn(time.Minute),
|
||||||
|
invocation.WithoutInvokedAt())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
data, cid1, err := tkn1.ToSealed(privKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
tkn2, cid2, err := invocation.FromSealed(data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, cid1, cid2)
|
||||||
|
assert.Equal(t, tkn1, tkn2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupNew(t *testing.T) (privKey crypto.PrivKey, iss, sub did.DID, cmd command.Command, args map[string]datamodel.Node, prf []cid.Cid, meta map[string]datamodel.Node) {
|
||||||
|
privKey, iss, sub, cmd, args, prf, meta, err := setupExampleNew()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return // WARNING: named return values
|
||||||
|
}
|
||||||
89
token/invocation/schema_test.go
Normal file
89
token/invocation/schema_test.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package invocation_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/ucan-wg/go-ucan/token/internal/envelope"
|
||||||
|
"github.com/ucan-wg/go-ucan/token/invocation"
|
||||||
|
"gotest.tools/v3/golden"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
issuerPrivKeyCfg = "CAESQK45xBfqIxRp7ZdRdck3tIJZKocCqvANQc925dCJhFwO7DJNA2j94zkF0TNx5mpXV0s6utfkFdHddWTaPVU6yZc="
|
||||||
|
newCID = "zdpuAqY6Zypg4UnpbSUgDvYGneyFaTKaZevzxgSxV4rmv3Fpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSchemaRoundTrip(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
invocationJson := golden.Get(t, "new.dagjson")
|
||||||
|
privKey := privKey(t, issuerPrivKeyCfg)
|
||||||
|
|
||||||
|
t.Run("via buffers", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
// format: dagJson --> PayloadModel --> dagCbor --> PayloadModel --> dagJson
|
||||||
|
// function: DecodeDagJson() Seal() Unseal() EncodeDagJson()
|
||||||
|
|
||||||
|
p1, err := invocation.FromDagJson(invocationJson)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
cborBytes, id, err := p1.ToSealed(privKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, newCID, envelope.CIDToBase58BTC(id))
|
||||||
|
fmt.Println("cborBytes length", len(cborBytes))
|
||||||
|
fmt.Println("cbor", string(cborBytes))
|
||||||
|
|
||||||
|
p2, c2, err := invocation.FromSealed(cborBytes)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, id, c2)
|
||||||
|
fmt.Println("read Cbor", p2)
|
||||||
|
|
||||||
|
readJson, err := p2.ToDagJson(privKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
fmt.Println("readJson length", len(readJson))
|
||||||
|
fmt.Println("json: ", string(readJson))
|
||||||
|
|
||||||
|
assert.JSONEq(t, string(invocationJson), string(readJson))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("via streaming", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer(invocationJson)
|
||||||
|
|
||||||
|
// format: dagJson --> PayloadModel --> dagCbor --> PayloadModel --> dagJson
|
||||||
|
// function: DecodeDagJson() Seal() Unseal() EncodeDagJson()
|
||||||
|
|
||||||
|
p1, err := invocation.FromDagJsonReader(buf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
cborBytes := &bytes.Buffer{}
|
||||||
|
id, err := p1.ToSealedWriter(cborBytes, privKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, newCID, envelope.CIDToBase58BTC(id))
|
||||||
|
|
||||||
|
p2, c2, err := invocation.FromSealedReader(cborBytes)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, envelope.CIDToBase58BTC(id), envelope.CIDToBase58BTC(c2))
|
||||||
|
|
||||||
|
readJson := &bytes.Buffer{}
|
||||||
|
require.NoError(t, p2.ToDagJsonWriter(readJson, privKey))
|
||||||
|
|
||||||
|
assert.JSONEq(t, string(invocationJson), readJson.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func privKey(t require.TestingT, privKeyCfg string) crypto.PrivKey {
|
||||||
|
privKeyMar, err := crypto.ConfigDecodeKey(privKeyCfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
privKey, err := crypto.UnmarshalPrivateKey(privKeyMar)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return privKey
|
||||||
|
}
|
||||||
1
token/invocation/testdata/new.dagjson
vendored
Normal file
1
token/invocation/testdata/new.dagjson
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
[{"/":{"bytes":"o/vTvTs8SEkD9QL/eNhhW0fAng/SGBouywCbUnOfsF2RFHxaV02KTCyzgDxlJLZ2XN/Vk5igLmlKL3QIXMaeCQ"}},{"h":{"/":{"bytes":"NO0BcQ"}},"ucan/inv@1.0.0-rc.1":{"args":{"headers":{"Content-Type":"application/json"},"payload":{"body":"UCAN is great","draft":true,"title":"UCAN for Fun and Profit","topics":["authz","journal"]},"uri":"https://example.com/blog/posts"},"cmd":"/crud/create","exp":1730812145,"iss":"did:key:z6MkvMGkN5nbUQLBVqJhr13Zdqyh9rR1VuF16PuZbfocBxpv","meta":{"env":"development","tags":["blog","post","pr#123"]},"nonce":{"/":{"bytes":"q1AH6MJrqoTH6av7"}},"prf":[{"/":"bafyreigx3qxd2cndpe66j2mdssj773ecv7tqd7wovcnz5raguw6lj7sjoe"},{"/":"bafyreib34ira254zdqgehz6f2bhwme2ja2re3ltcalejv4x4tkcveujvpa"},{"/":"bafyreibkb66tpo2ixqx3fe5hmekkbuasrod6olt5bwm5u5pi726mduuwlq"}],"sub":"did:key:z6MkuFj35aiTL7YQiVMobuSeUQju92g7wZzufS3HAc6NFFcQ"}}]
|
||||||
Reference in New Issue
Block a user