|
| 1 | +package access_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/storacha/go-libstoracha/capabilities/access" |
| 8 | + "github.com/storacha/go-libstoracha/testutil" |
| 9 | + "github.com/storacha/go-ucanto/core/delegation" |
| 10 | + "github.com/storacha/go-ucanto/core/invocation" |
| 11 | + "github.com/storacha/go-ucanto/core/message" |
| 12 | + "github.com/storacha/go-ucanto/core/receipt" |
| 13 | + "github.com/storacha/go-ucanto/core/receipt/ran" |
| 14 | + "github.com/storacha/go-ucanto/core/result" |
| 15 | + "github.com/storacha/go-ucanto/transport/car" |
| 16 | + "github.com/storacha/go-ucanto/ucan" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGrant(t *testing.T) { |
| 21 | + grantCaveats := access.GrantCaveats{ |
| 22 | + Att: []access.CapabilityRequest{{Can: "admin/party"}}, |
| 23 | + Cause: testutil.RandomCID(t), |
| 24 | + } |
| 25 | + |
| 26 | + inv, err := access.Grant.Invoke( |
| 27 | + testutil.Alice, |
| 28 | + testutil.Bob, |
| 29 | + testutil.Alice.DID().String(), |
| 30 | + grantCaveats, |
| 31 | + ) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + t.Run("round trip", func(t *testing.T) { |
| 35 | + d0, err := delegation.Delegate( |
| 36 | + testutil.Bob, |
| 37 | + testutil.Alice, |
| 38 | + []ucan.Capability[ucan.NoCaveats]{ |
| 39 | + ucan.NewCapability(grantCaveats.Att[0].Can, testutil.Bob.DID().String(), ucan.NoCaveats{}), |
| 40 | + }, |
| 41 | + ) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + d0Bytes, err := io.ReadAll(d0.Archive()) |
| 45 | + require.NoError(t, err) |
| 46 | + |
| 47 | + grantOk := access.GrantOk{ |
| 48 | + Delegations: access.DelegationsModel{ |
| 49 | + Keys: []string{d0.Link().String()}, |
| 50 | + Values: map[string][]byte{d0.Link().String(): d0Bytes}, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + r0, err := receipt.Issue( |
| 55 | + testutil.Bob, |
| 56 | + result.Ok[access.GrantOk, access.GrantError](grantOk), |
| 57 | + ran.FromInvocation(inv), |
| 58 | + ) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + // round trip the invocation and receipt in an agent message to ensure the |
| 62 | + // invocation can be encoded and the receipt decoded |
| 63 | + msg := roundTripAgentMessage(t, []invocation.Invocation{inv}, []receipt.AnyReceipt{r0}) |
| 64 | + |
| 65 | + rcptLink, ok := msg.Get(inv.Link()) |
| 66 | + require.True(t, ok) |
| 67 | + |
| 68 | + reader, err := access.NewGrantReceiptReader() |
| 69 | + require.NoError(t, err) |
| 70 | + |
| 71 | + r1, err := reader.Read(rcptLink, msg.Blocks()) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + o, x := result.Unwrap(r1.Out()) |
| 75 | + require.Empty(t, x) |
| 76 | + require.Len(t, o.Delegations.Keys, 1) |
| 77 | + require.Equal(t, d0.Link().String(), o.Delegations.Keys[0]) |
| 78 | + |
| 79 | + _, err = delegation.Extract(o.Delegations.Values[d0.Link().String()]) |
| 80 | + require.NoError(t, err) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("round trip with error", func(t *testing.T) { |
| 84 | + grantErr := access.GrantError{ |
| 85 | + ErrorName: "Unauthorized", |
| 86 | + Message: "No, no you may not.", |
| 87 | + } |
| 88 | + |
| 89 | + r0, err := receipt.Issue( |
| 90 | + testutil.Bob, |
| 91 | + result.Error[access.GrantOk](grantErr), |
| 92 | + ran.FromInvocation(inv), |
| 93 | + ) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + msg := roundTripAgentMessage(t, []invocation.Invocation{inv}, []receipt.AnyReceipt{r0}) |
| 97 | + |
| 98 | + rcptLink, ok := msg.Get(inv.Link()) |
| 99 | + require.True(t, ok) |
| 100 | + |
| 101 | + reader, err := access.NewGrantReceiptReader() |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + r1, err := reader.Read(rcptLink, msg.Blocks()) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + o, x := result.Unwrap(r1.Out()) |
| 108 | + require.Empty(t, o) |
| 109 | + require.Equal(t, grantErr.ErrorName, x.ErrorName) |
| 110 | + require.Equal(t, grantErr.Name(), x.Name()) |
| 111 | + require.Equal(t, grantErr.Message, x.Message) |
| 112 | + require.Equal(t, grantErr.Error(), x.Error()) |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +func roundTripAgentMessage(t *testing.T, invs []invocation.Invocation, rcpts []receipt.AnyReceipt) message.AgentMessage { |
| 117 | + t.Helper() |
| 118 | + inMsg, err := message.Build(invs, rcpts) |
| 119 | + require.NoError(t, err) |
| 120 | + |
| 121 | + outCodec := car.NewOutboundCodec() |
| 122 | + req, err := outCodec.Encode(inMsg) |
| 123 | + require.NoError(t, err) |
| 124 | + |
| 125 | + inCodec, err := car.NewInboundCodec().Accept(req) |
| 126 | + require.NoError(t, err) |
| 127 | + |
| 128 | + outMsg, err := inCodec.Decoder().Decode(req) |
| 129 | + require.NoError(t, err) |
| 130 | + |
| 131 | + return outMsg |
| 132 | +} |
0 commit comments