2024-12-05 17:11:18 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"iter"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2025-12-08 19:01:51 +01:00
|
|
|
"github.com/MetaMask/go-did-it"
|
2025-08-05 12:11:20 +02:00
|
|
|
"github.com/MetaMask/go-did-it/didtest"
|
2025-12-08 19:01:51 +01:00
|
|
|
"github.com/ipfs/go-cid"
|
2024-12-05 17:11:18 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-08-05 12:11:20 +02:00
|
|
|
|
2024-12-05 17:11:18 +01:00
|
|
|
"github.com/ucan-wg/go-ucan/pkg/command"
|
|
|
|
|
"github.com/ucan-wg/go-ucan/token/delegation"
|
|
|
|
|
"github.com/ucan-wg/go-ucan/token/delegation/delegationtest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestFindProof(t *testing.T) {
|
|
|
|
|
dlgs := func() iter.Seq[*delegation.Bundle] {
|
2025-01-29 16:22:41 +01:00
|
|
|
return func(yield func(*delegation.Bundle) bool) {
|
|
|
|
|
for _, bundle := range delegationtest.AllBundles {
|
|
|
|
|
if !yield(&bundle) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-05 17:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-08 19:01:51 +01:00
|
|
|
for _, tc := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
issuer did.DID
|
|
|
|
|
command command.Command
|
|
|
|
|
subject did.DID
|
|
|
|
|
expected []cid.Cid
|
|
|
|
|
}{
|
|
|
|
|
// Passes
|
|
|
|
|
{
|
|
|
|
|
name: "Alice --> Alice (self-delegation)",
|
|
|
|
|
issuer: didtest.PersonaAlice.DID(),
|
|
|
|
|
command: delegationtest.NominalCommand,
|
|
|
|
|
subject: didtest.PersonaAlice.DID(),
|
|
|
|
|
expected: []cid.Cid{delegationtest.TokenAliceAliceCID},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Alice --> Bob",
|
|
|
|
|
issuer: didtest.PersonaBob.DID(),
|
|
|
|
|
command: delegationtest.NominalCommand,
|
|
|
|
|
subject: didtest.PersonaAlice.DID(),
|
|
|
|
|
expected: delegationtest.ProofAliceBob,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Alice --> Bob --> Carol",
|
|
|
|
|
issuer: didtest.PersonaCarol.DID(),
|
|
|
|
|
command: delegationtest.NominalCommand,
|
|
|
|
|
subject: didtest.PersonaAlice.DID(),
|
|
|
|
|
expected: delegationtest.ProofAliceBobCarol,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Alice --> Bob --> Carol --> Dan",
|
|
|
|
|
issuer: didtest.PersonaDan.DID(),
|
|
|
|
|
command: delegationtest.NominalCommand,
|
|
|
|
|
subject: didtest.PersonaAlice.DID(),
|
|
|
|
|
expected: delegationtest.ProofAliceBobCarolDan,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Alice --> Bob --> Carol --> Dan --> Erin",
|
|
|
|
|
issuer: didtest.PersonaErin.DID(),
|
|
|
|
|
command: delegationtest.NominalCommand,
|
|
|
|
|
subject: didtest.PersonaAlice.DID(),
|
|
|
|
|
expected: delegationtest.ProofAliceBobCarolDanErin,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Alice --> Bob --> Carol --> Dan --> Erin --> Frank",
|
|
|
|
|
issuer: didtest.PersonaFrank.DID(),
|
|
|
|
|
command: delegationtest.NominalCommand,
|
|
|
|
|
subject: didtest.PersonaAlice.DID(),
|
|
|
|
|
expected: delegationtest.ProofAliceBobCarolDanErinFrank,
|
|
|
|
|
},
|
2024-12-05 17:11:18 +01:00
|
|
|
|
2025-12-08 19:01:51 +01:00
|
|
|
// Fails
|
|
|
|
|
{
|
|
|
|
|
name: "wrong command",
|
|
|
|
|
issuer: didtest.PersonaBob.DID(),
|
|
|
|
|
command: command.New("foo"),
|
|
|
|
|
subject: didtest.PersonaAlice.DID(),
|
|
|
|
|
expected: nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "wrong subject",
|
|
|
|
|
issuer: didtest.PersonaBob.DID(),
|
|
|
|
|
command: delegationtest.NominalCommand,
|
|
|
|
|
subject: didtest.PersonaDan.DID(),
|
|
|
|
|
expected: nil,
|
|
|
|
|
},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
res := FindProof(dlgs, tc.issuer, tc.command, tc.subject)
|
|
|
|
|
require.Equal(t, tc.expected, res)
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-12-05 17:11:18 +01:00
|
|
|
}
|