-
Notifications
You must be signed in to change notification settings - Fork 43
/
macaroon_recipes_test.go
87 lines (74 loc) · 2.04 KB
/
macaroon_recipes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package lndclient_test
import (
"context"
"encoding/json"
"os"
"testing"
"github.com/lightninglabs/lndclient"
"github.com/stretchr/testify/require"
)
var (
expectedPermissions = map[string]int{
"lnrpc": 13,
"chainrpc": 1,
"invoicesrpc": 2,
"routerrpc": 2,
"signrpc": 2,
"verrpc": 1,
"walletrpc": 3,
}
)
type permissionJSONData struct {
Permissions map[string]struct {
Permissions []struct {
Entity string `json:"entity"`
Action string `json:"action"`
} `json:"permissions"`
} `json:"method_permissions"`
}
type lightningMock struct {
lndclient.LightningClient
mockPermissions map[string][]lndclient.MacaroonPermission
}
func (m *lightningMock) ListPermissions(
_ context.Context) (map[string][]lndclient.MacaroonPermission, error) {
return m.mockPermissions, nil
}
// TestMacaroonRecipe makes sure the macaroon recipe for all supported packages
// can be generated.
func TestMacaroonRecipe(t *testing.T) {
// Load our static permissions exported from lnd by calling
// `lncli listpermissions > permissions.json`.
content, err := os.ReadFile("testdata/permissions.json")
require.NoError(t, err)
data := &permissionJSONData{}
err = json.Unmarshal(content, data)
require.NoError(t, err)
mockPermissions := make(map[string][]lndclient.MacaroonPermission)
for uri, perms := range data.Permissions {
mockPermissions[uri] = make(
[]lndclient.MacaroonPermission, len(perms.Permissions),
)
for idx, perm := range perms.Permissions {
mockPermissions[uri][idx] = lndclient.MacaroonPermission{
Entity: perm.Entity,
Action: perm.Action,
}
}
}
clientMock := &lightningMock{
mockPermissions: mockPermissions,
}
// Run the test for all supported RPC packages.
for pkg, numPermissions := range expectedPermissions {
pkg, numPermissions := pkg, numPermissions
t.Run(pkg, func(t *testing.T) {
t.Parallel()
requiredPermissions, err := lndclient.MacaroonRecipe(
clientMock, []string{pkg},
)
require.NoError(t, err)
require.Len(t, requiredPermissions, numPermissions)
})
}
}