lightning-terminal/accounts/interceptor_test.go
Elle Mouton 03712e6067
accounts: add an IDFromCaveates helper
And use that from the existing accountFromMacaroon helper (which will
then test the new helper by proxy).

We add this helper so that we can use it later on from the sessions
package where we want to extract an account ID from a caveat (we wont
have a full macaroon available).
2025-03-04 13:43:13 +02:00

78 lines
1.8 KiB
Go

package accounts
import (
"fmt"
"testing"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/stretchr/testify/require"
"gopkg.in/macaroon-bakery.v2/bakery/checkers"
"gopkg.in/macaroon.v2"
)
// TestAccountIDCaveatEmbedding tests that the account ID can be embedded in a
// macaroon caveat and extracted from it.
func TestAccountIDCaveatEmbedding(t *testing.T) {
badCondition := checkers.Condition(macaroons.CondLndCustom, fmt.Sprintf(
"%s %s", CondAccount, "invalid hex",
))
tests := []struct {
name string
caveats []macaroon.Caveat
expectedErr string
expectedAcct fn.Option[AccountID]
}{
{
name: "valid account ID, single caveat",
caveats: []macaroon.Caveat{
CaveatFromID(AccountID{1, 2, 3, 4, 5}),
},
expectedAcct: fn.Some(AccountID{1, 2, 3, 4, 5}),
},
{
name: "valid account ID, single multiple caveats",
caveats: []macaroon.Caveat{
{Id: []byte("some other caveat")},
CaveatFromID(AccountID{1, 2, 3, 4, 5}),
{Id: []byte("another one")},
},
expectedAcct: fn.Some(AccountID{1, 2, 3, 4, 5}),
},
{
name: "invalid account ID",
caveats: []macaroon.Caveat{
{Id: []byte(badCondition)},
},
expectedErr: "encoding/hex: invalid",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
acct, err := IDFromCaveats(test.caveats)
if test.expectedErr != "" {
require.ErrorContains(t, err, test.expectedErr)
return
}
require.NoError(t, err)
if test.expectedAcct.IsNone() {
require.True(t, acct.IsNone())
return
}
require.True(t, acct.IsSome())
test.expectedAcct.WhenSome(func(id AccountID) {
acct.WhenSome(func(acct AccountID) {
require.Equal(t, id, acct)
})
})
})
}
}