forked from lightninglabs/lndclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightning_client_test.go
174 lines (149 loc) · 4.22 KB
/
lightning_client_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package lndclient
import (
"context"
"errors"
"testing"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
// addInvoiceArg records the args used in a call to mockRPCClient.AddInvoice.
type addInvoiceArg struct {
in *lnrpc.Invoice
opts []grpc.CallOption
}
// mockRPCClient implements lnrpc.LightningClient with dynamic method
// implementations and call spying.
type mockRPCClient struct {
lnrpc.LightningClient
addInvoice func(in *lnrpc.Invoice, opts ...grpc.CallOption) (
*lnrpc.AddInvoiceResponse, error)
addInvoiceArgs []addInvoiceArg
}
func (m *mockRPCClient) AddInvoice(ctx context.Context, in *lnrpc.Invoice,
opts ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) {
m.addInvoiceArgs = append(m.addInvoiceArgs, addInvoiceArg{
in: in,
opts: opts,
})
return m.addInvoice(in, opts...)
}
// TestLightningClientAddInvoice ensures that adding an invoice via
// lightningClient is completed as expected.
func TestLightningClientAddInvoice(t *testing.T) {
// Define constants / fixtures.
var validPreimage lntypes.Preimage
copy(validPreimage[:], "valid preimage")
var validRHash lntypes.Hash
copy(validRHash[:], "valid hash")
validAddInvoiceData := &invoicesrpc.AddInvoiceData{
Memo: "fake memo",
Preimage: &validPreimage,
Hash: &validRHash,
Value: lnwire.MilliSatoshi(500000),
DescriptionHash: []byte("fake 32 byte hash"),
Expiry: 123,
CltvExpiry: 456,
}
validInvoice := &lnrpc.Invoice{
Memo: validAddInvoiceData.Memo,
RPreimage: validAddInvoiceData.Preimage[:],
RHash: validAddInvoiceData.Hash[:],
ValueMsat: int64(validAddInvoiceData.Value),
DescriptionHash: validAddInvoiceData.DescriptionHash,
Expiry: validAddInvoiceData.Expiry,
CltvExpiry: validAddInvoiceData.CltvExpiry,
Private: true,
}
validPayReq := "a valid pay req"
validResp := &lnrpc.AddInvoiceResponse{
RHash: validRHash[:],
PaymentRequest: validPayReq,
}
validAddInvoiceArgs := []addInvoiceArg{
{in: validInvoice},
}
validAddInvoice := func(in *lnrpc.Invoice, opts ...grpc.CallOption) (
*lnrpc.AddInvoiceResponse, error) {
return validResp, nil
}
errorAddInvoice := func(in *lnrpc.Invoice, opts ...grpc.CallOption) (
*lnrpc.AddInvoiceResponse, error) {
return nil, errors.New("error")
}
// Set up the test structure.
type expect struct {
addInvoiceArgs []addInvoiceArg
hash lntypes.Hash
payRequest string
wantErr bool
}
type testCase struct {
name string
client mockRPCClient
invoice *invoicesrpc.AddInvoiceData
expect expect
}
// Run through the test cases.
tests := []testCase{
{
name: "happy path",
client: mockRPCClient{
addInvoice: validAddInvoice,
},
invoice: validAddInvoiceData,
expect: expect{
addInvoiceArgs: validAddInvoiceArgs,
hash: validRHash,
payRequest: validPayReq,
},
}, {
name: "rpc client error",
client: mockRPCClient{
addInvoice: errorAddInvoice,
},
invoice: validAddInvoiceData,
expect: expect{
addInvoiceArgs: validAddInvoiceArgs,
wantErr: true,
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
ln := lightningClient{
client: &test.client,
}
hash, payRequest, err := ln.AddInvoice(
context.Background(), test.invoice,
)
// Check if an error (or no error) was received as
// expected.
if test.expect.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
// Check if the expected hash was returned.
require.Equal(
t, hash, test.expect.hash,
"received unexpected hash",
)
// Check if the expected invoice was returned.
require.Equal(
t, payRequest, test.expect.payRequest,
"received unexpected payment request",
)
// Check if the expected args were passed to the RPC
// client call.
require.Equal(t, test.client.addInvoiceArgs,
test.expect.addInvoiceArgs,
"rpc client call was not made as expected",
)
})
}
}