-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_payments_test.go
121 lines (95 loc) · 2.3 KB
/
api_payments_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
package lightning
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"testing"
"time"
entities "github.com/bolt-observer/go_common/entities"
"github.com/stretchr/testify/assert"
)
func getAPI(t *testing.T, name string, typ APIType) LightingAPICalls {
var data entities.Data
if _, err := os.Stat(name); errors.Is(err, os.ErrNotExist) {
// If file with credentials does not exist succeed
return nil
}
content, err := os.ReadFile(name)
if err != nil {
t.Fatalf("Error when opening file: %v", err)
return nil
}
if _, err := os.Stat(FixtureDir); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(FixtureDir, os.ModePerm)
if err != nil {
t.Fatalf("Could not create directory: %v", err)
return nil
}
}
err = json.Unmarshal(content, &data)
if err != nil {
t.Fatalf("Error during Unmarshal(): %v", err)
return nil
}
api, err := NewAPI(typ, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
return api
}
func TestGetInvoicesRest(t *testing.T) {
api := getAPI(t, "fixture.secret", LndRest)
if api == nil {
return
}
resp, err := api.GetInvoices(context.Background(), false, Pagination{Reversed: true, BatchSize: 10})
if err != nil {
t.Fatalf("Error received %v\n", err)
}
fmt.Printf("%+v\n", resp)
//t.Fail()
}
func TestGetInvoicesGrpc(t *testing.T) {
api := getAPI(t, "fixture-grpc.secret", LndGrpc)
if api == nil {
return
}
resp, err := api.GetInvoices(context.Background(), false, Pagination{Reversed: true, BatchSize: 10})
if err != nil {
t.Fatalf("Error received %v\n", err)
}
fmt.Printf("%+v\n", resp)
//t.Fail()
}
func TestSubscribeFailedForwards(t *testing.T) {
var err error
rest := getAPI(t, "fixture.secret", LndRest)
if rest == nil {
return
}
grpc := getAPI(t, "fixture-grpc.secret", LndGrpc)
if grpc == nil {
return
}
a := rest.(*LndRestLightningAPI)
b := grpc.(*LndGrpcLightningAPI)
assert.NotNil(t, a)
assert.NotNil(t, b)
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
outchan := make(chan RawMessage)
err = a.SubscribeFailedForwards(ctx, outchan)
assert.NoError(t, err)
err = b.SubscribeFailedForwards(ctx, outchan)
assert.NoError(t, err)
for {
select {
case <-ctx.Done():
return
case data := <-outchan:
fmt.Printf("Received %v\n", data)
}
}
}