one more file to refactor

This commit is contained in:
kiwiidb
2022-06-15 17:54:15 +02:00
parent 8952227bee
commit c9d7ddd9c7
9 changed files with 146 additions and 138 deletions

View File

@@ -27,6 +27,7 @@ import (
type PaymentTestSuite struct { type PaymentTestSuite struct {
TestSuite TestSuite
mlnd *MockLND mlnd *MockLND
externalLND *MockLND
service *service.LndhubService service *service.LndhubService
aliceLogin ExpectedCreateUserResponseBody aliceLogin ExpectedCreateUserResponseBody
aliceToken string aliceToken string
@@ -41,6 +42,11 @@ func (suite *PaymentTestSuite) SetupSuite() {
log.Fatalf("Error initializing test service: %v", err) log.Fatalf("Error initializing test service: %v", err)
} }
suite.mlnd = mlnd suite.mlnd = mlnd
externalLND, err := NewMockLND("1234567890abcdefabcd", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
suite.externalLND = externalLND
svc, err := LndHubTestServiceInit(mlnd) svc, err := LndHubTestServiceInit(mlnd)
if err != nil { if err != nil {
log.Fatalf("Error initializing test service: %v", err) log.Fatalf("Error initializing test service: %v", err)

View File

@@ -12,6 +12,7 @@ import (
"github.com/getAlby/lndhub.go/lib/service" "github.com/getAlby/lndhub.go/lib/service"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@@ -23,7 +24,11 @@ type InvoiceTestSuite struct {
} }
func (suite *InvoiceTestSuite) SetupSuite() { func (suite *InvoiceTestSuite) SetupSuite() {
svc, err := LndHubTestServiceInit(nil) mlnd, err := NewMockLND("1234567890abcdef", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
svc, err := LndHubTestServiceInit(mlnd)
if err != nil { if err != nil {
log.Fatalf("Error initializing test service: %v", err) log.Fatalf("Error initializing test service: %v", err)
} }

View File

@@ -14,6 +14,7 @@ import (
"github.com/getAlby/lndhub.go/lib/tokens" "github.com/getAlby/lndhub.go/lib/tokens"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@@ -21,14 +22,20 @@ import (
type KeySendTestSuite struct { type KeySendTestSuite struct {
TestSuite TestSuite
service *service.LndhubService service *service.LndhubService
mlnd *MockLND
aliceLogin ExpectedCreateUserResponseBody aliceLogin ExpectedCreateUserResponseBody
aliceToken string aliceToken string
invoiceUpdateSubCancelFn context.CancelFunc invoiceUpdateSubCancelFn context.CancelFunc
} }
func (suite *KeySendTestSuite) SetupSuite() { func (suite *KeySendTestSuite) SetupSuite() {
fee := int64(1)
svc, err := LndHubTestServiceInit(nil) mlnd, err := NewMockLND("1234567890abcdef", fee, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
suite.mlnd = mlnd
svc, err := LndHubTestServiceInit(mlnd)
if err != nil { if err != nil {
log.Fatalf("Error initializing test service: %v", err) log.Fatalf("Error initializing test service: %v", err)
} }
@@ -70,23 +77,15 @@ func (suite *KeySendTestSuite) TearDownSuite() {
func (suite *KeySendTestSuite) TestKeysendPayment() { func (suite *KeySendTestSuite) TestKeysendPayment() {
aliceFundingSats := 1000 aliceFundingSats := 1000
externalSatRequested := 500 externalSatRequested := 500
// 1 sat + 1 ppm
fee := 1
//fund alice account //fund alice account
//todo invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken)
// invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken) err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
// sendPaymentRequest := lnrpc.SendRequest{ assert.NoError(suite.T(), err)
// PaymentRequest: invoiceResponse.PayReq,
// FeeLimit: nil,
// }
// _, err := suite.fundingClient.SendPaymentSync(context.Background(), &sendPaymentRequest)
// assert.NoError(suite.T(), err)
//wait a bit for the callback event to hit //wait a bit for the callback event to hit
time.Sleep(100 * time.Millisecond) time.Sleep(10 * time.Millisecond)
//todo suite.createKeySendReq(int64(externalSatRequested), "key send test", "03abcdef123456789a", suite.aliceToken)
//suite.createKeySendReq(int64(externalSatRequested), "key send test", simnetLnd3PubKey, suite.aliceToken)
// check that balance was reduced // check that balance was reduced
userId := getUserIdFromToken(suite.aliceToken) userId := getUserIdFromToken(suite.aliceToken)
@@ -94,21 +93,16 @@ func (suite *KeySendTestSuite) TestKeysendPayment() {
if err != nil { if err != nil {
fmt.Printf("Error when getting balance %v\n", err.Error()) fmt.Printf("Error when getting balance %v\n", err.Error())
} }
assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested+fee), aliceBalance) assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested+int(suite.mlnd.fee)), aliceBalance)
} }
func (suite *KeySendTestSuite) TestKeysendPaymentNonExistentDestination() { func (suite *KeySendTestSuite) TestKeysendPaymentNonExistentDestination() {
//aliceFundingSats := 1000 aliceFundingSats := 1000
externalSatRequested := 500 externalSatRequested := 500
//fund alice account //fund alice account
//todo invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken)
//invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken) err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
//sendPaymentRequest := lnrpc.SendRequest{ assert.NoError(suite.T(), err)
// PaymentRequest: invoiceResponse.PayReq,
// FeeLimit: nil,
//}
//_, err := suite.fundingClient.SendPaymentSync(context.Background(), &sendPaymentRequest)
//assert.NoError(suite.T(), err)
//wait a bit for the callback event to hit //wait a bit for the callback event to hit
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)

View File

@@ -12,17 +12,12 @@ import (
const SendPaymentMockError = "mocked send payment error" const SendPaymentMockError = "mocked send payment error"
type LNDMockWrapper struct { type LNDMockWrapper struct {
*lnd.LNDWrapper lnd.LightningClientWrapper
}
func NewLNDMockWrapper(lndOptions lnd.LNDoptions) (result *LNDMockWrapper, err error) {
lnd, err := lnd.NewLNDclient(lndOptions)
if err != nil {
return nil, err
} }
func NewLNDMockWrapper(lnd lnd.LightningClientWrapper) (result *LNDMockWrapper, err error) {
return &LNDMockWrapper{ return &LNDMockWrapper{
LNDWrapper: lnd, lnd,
}, nil }, nil
} }
@@ -35,17 +30,12 @@ func (wrapper *LNDMockWrapper) SendPaymentSync(ctx context.Context, req *lnrpc.S
var errorMessageChannel = make(chan string, 1) var errorMessageChannel = make(chan string, 1)
type LNDMockWrapperAsync struct { type LNDMockWrapperAsync struct {
*lnd.LNDWrapper lnd.LightningClientWrapper
}
func NewLNDMockWrapperAsync(lndOptions lnd.LNDoptions) (result *LNDMockWrapperAsync, err error) {
lnd, err := lnd.NewLNDclient(lndOptions)
if err != nil {
return nil, err
} }
func NewLNDMockWrapperAsync(lnd lnd.LightningClientWrapper) (result *LNDMockWrapperAsync, err error) {
return &LNDMockWrapperAsync{ return &LNDMockWrapperAsync{
LNDWrapper: lnd, lnd,
}, nil }, nil
} }

View File

@@ -9,35 +9,35 @@ import (
"time" "time"
"github.com/getAlby/lndhub.go/common" "github.com/getAlby/lndhub.go/common"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func (suite *PaymentTestSuite) TestOutGoingPayment() { func (suite *PaymentTestSuite) TestOutGoingPayment() {
aliceFundingSats := 1000 aliceFundingSats := 1000
//todo
externalSatRequested := 500 externalSatRequested := 500
// 1 sat + 1 ppm // 1 sat + 1 ppm
fee := 1 suite.mlnd.fee = 1
//fund alice account //fund alice account
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken) invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken)
err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil) err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
assert.NoError(suite.T(), err) assert.NoError(suite.T(), err)
//wait a bit for the callback event to hit //wait a bit for the callback event to hit
time.Sleep(100 * time.Millisecond) time.Sleep(10 * time.Millisecond)
//create external invoice //create external invoice
//externalInvoice := lnrpc.Invoice{ externalInvoice := lnrpc.Invoice{
// Memo: "integration tests: external pay from alice", Memo: "integration tests: external pay from alice",
// Value: int64(externalSatRequested), Value: int64(externalSatRequested),
//} }
//invoice, err := suite.fundingClient.AddInvoice(context.Background(), &externalInvoice) invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
//assert.NoError(suite.T(), err) assert.NoError(suite.T(), err)
////pay external from alice //pay external from alice
//payResponse := suite.createPayInvoiceReq(&ExpectedPayInvoiceRequestBody{ payResponse := suite.createPayInvoiceReq(&ExpectedPayInvoiceRequestBody{
// Invoice: invoice.PaymentRequest, Invoice: invoice.PaymentRequest,
//}, suite.aliceToken) }, suite.aliceToken)
//assert.NotEmpty(suite.T(), payResponse.PaymentPreimage) assert.NotEmpty(suite.T(), payResponse.PaymentPreimage)
// check that balance was reduced // check that balance was reduced
userId := getUserIdFromToken(suite.aliceToken) userId := getUserIdFromToken(suite.aliceToken)
@@ -45,7 +45,7 @@ func (suite *PaymentTestSuite) TestOutGoingPayment() {
if err != nil { if err != nil {
fmt.Printf("Error when getting balance %v\n", err.Error()) fmt.Printf("Error when getting balance %v\n", err.Error())
} }
assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested+fee), aliceBalance) assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested+int(suite.mlnd.fee)), aliceBalance)
// check that no additional transaction entry was created // check that no additional transaction entry was created
transactonEntries, err := suite.service.TransactionEntriesFor(context.Background(), userId) transactonEntries, err := suite.service.TransactionEntriesFor(context.Background(), userId)
@@ -77,7 +77,7 @@ func (suite *PaymentTestSuite) TestOutGoingPayment() {
assert.Equal(suite.T(), int64(0), transactonEntries[1].ParentID) assert.Equal(suite.T(), int64(0), transactonEntries[1].ParentID)
assert.Equal(suite.T(), outgoingInvoices[0].ID, transactonEntries[1].InvoiceID) assert.Equal(suite.T(), outgoingInvoices[0].ID, transactonEntries[1].InvoiceID)
assert.Equal(suite.T(), int64(fee), transactonEntries[2].Amount) assert.Equal(suite.T(), int64(suite.mlnd.fee), transactonEntries[2].Amount)
assert.Equal(suite.T(), feeAccount.ID, transactonEntries[2].CreditAccountID) assert.Equal(suite.T(), feeAccount.ID, transactonEntries[2].CreditAccountID)
assert.Equal(suite.T(), currentAccount.ID, transactonEntries[2].DebitAccountID) assert.Equal(suite.T(), currentAccount.ID, transactonEntries[2].DebitAccountID)
assert.Equal(suite.T(), outgoingInvoices[0].ID, transactonEntries[2].InvoiceID) assert.Equal(suite.T(), outgoingInvoices[0].ID, transactonEntries[2].InvoiceID)
@@ -94,7 +94,7 @@ func (suite *PaymentTestSuite) TestOutGoingPayment() {
responseBody := &[]ExpectedOutgoingInvoice{} responseBody := &[]ExpectedOutgoingInvoice{}
assert.Equal(suite.T(), http.StatusOK, rec.Code) assert.Equal(suite.T(), http.StatusOK, rec.Code)
assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(&responseBody)) assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(&responseBody))
assert.Equal(suite.T(), int64(fee), (*responseBody)[0].Fee) assert.Equal(suite.T(), int64(suite.mlnd.fee), (*responseBody)[0].Fee)
} }
func (suite *PaymentTestSuite) TestOutGoingPaymentWithNegativeBalance() { func (suite *PaymentTestSuite) TestOutGoingPaymentWithNegativeBalance() {
@@ -102,27 +102,26 @@ func (suite *PaymentTestSuite) TestOutGoingPaymentWithNegativeBalance() {
aliceFundingSats := 1000 aliceFundingSats := 1000
externalSatRequested := 1000 externalSatRequested := 1000
// 1 sat + 1 ppm // 1 sat + 1 ppm
fee := 1 suite.mlnd.fee = 1
//fund alice account //fund alice account
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken) invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken)
err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil) err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
assert.NoError(suite.T(), err) assert.NoError(suite.T(), err)
//wait a bit for the callback event to hit //wait a bit for the callback event to hit
time.Sleep(100 * time.Millisecond) time.Sleep(10 * time.Millisecond)
//create external invoice externalInvoice := lnrpc.Invoice{
//externalInvoice := lnrpc.Invoice{ Memo: "integration tests: external pay from alice",
// Memo: "integration tests: external pay from alice", Value: int64(externalSatRequested),
// Value: int64(externalSatRequested), }
//} invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
//invoice, err := suite.fundingClient.AddInvoice(context.Background(), &externalInvoice) assert.NoError(suite.T(), err)
//assert.NoError(suite.T(), err) //pay external from alice
////pay external from alice payResponse := suite.createPayInvoiceReq(&ExpectedPayInvoiceRequestBody{
//payResponse := suite.createPayInvoiceReq(&ExpectedPayInvoiceRequestBody{ Invoice: invoice.PaymentRequest,
// Invoice: invoice.PaymentRequest, }, suite.aliceToken)
//}, suite.aliceToken) assert.NotEmpty(suite.T(), payResponse.PaymentPreimage)
//assert.NotEmpty(suite.T(), payResponse.PaymentPreimage)
// check that balance was reduced // check that balance was reduced
userId := getUserIdFromToken(suite.aliceToken) userId := getUserIdFromToken(suite.aliceToken)
@@ -131,7 +130,7 @@ func (suite *PaymentTestSuite) TestOutGoingPaymentWithNegativeBalance() {
if err != nil { if err != nil {
fmt.Printf("Error when getting balance %v\n", err.Error()) fmt.Printf("Error when getting balance %v\n", err.Error())
} }
assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested+fee), aliceBalance) assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested)+suite.mlnd.fee, aliceBalance)
assert.Equal(suite.T(), int64(-1), aliceBalance) assert.Equal(suite.T(), int64(-1), aliceBalance)
// check that no additional transaction entry was created // check that no additional transaction entry was created
@@ -164,7 +163,7 @@ func (suite *PaymentTestSuite) TestOutGoingPaymentWithNegativeBalance() {
assert.Equal(suite.T(), int64(0), transactonEntries[1].ParentID) assert.Equal(suite.T(), int64(0), transactonEntries[1].ParentID)
assert.Equal(suite.T(), outgoingInvoices[0].ID, transactonEntries[1].InvoiceID) assert.Equal(suite.T(), outgoingInvoices[0].ID, transactonEntries[1].InvoiceID)
assert.Equal(suite.T(), int64(fee), transactonEntries[2].Amount) assert.Equal(suite.T(), int64(suite.mlnd.fee), transactonEntries[2].Amount)
assert.Equal(suite.T(), feeAccount.ID, transactonEntries[2].CreditAccountID) assert.Equal(suite.T(), feeAccount.ID, transactonEntries[2].CreditAccountID)
assert.Equal(suite.T(), currentAccount.ID, transactonEntries[2].DebitAccountID) assert.Equal(suite.T(), currentAccount.ID, transactonEntries[2].DebitAccountID)
assert.Equal(suite.T(), outgoingInvoices[0].ID, transactonEntries[2].InvoiceID) assert.Equal(suite.T(), outgoingInvoices[0].ID, transactonEntries[2].InvoiceID)
@@ -175,27 +174,27 @@ func (suite *PaymentTestSuite) TestOutGoingPaymentWithNegativeBalance() {
func (suite *PaymentTestSuite) TestZeroAmountInvoice() { func (suite *PaymentTestSuite) TestZeroAmountInvoice() {
aliceFundingSats := 1000 aliceFundingSats := 1000
//amtToPay := 1000 amtToPay := 1000
//fund alice account //fund alice account
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test zero amount payment alice", suite.aliceToken) invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test zero amount payment alice", suite.aliceToken)
err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil) err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
assert.NoError(suite.T(), err) assert.NoError(suite.T(), err)
//wait a bit for the callback event to hit //wait a bit for the callback event to hit
time.Sleep(100 * time.Millisecond) time.Sleep(10 * time.Millisecond)
//create external invoice //create external invoice
//externalInvoice := lnrpc.Invoice{ externalInvoice := lnrpc.Invoice{
// Memo: "integration tests: zero amount pay from alice", Memo: "integration tests: zero amount pay from alice",
// Value: 0, Value: 0,
//} }
//invoice, err := suite.fundingClient.AddInvoice(context.Background(), &externalInvoice) invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
//assert.NoError(suite.T(), err) assert.NoError(suite.T(), err)
////pay external from alice ////pay external from alice
//payResponse := suite.createPayInvoiceReq(&ExpectedPayInvoiceRequestBody{ payResponse := suite.createPayInvoiceReq(&ExpectedPayInvoiceRequestBody{
// Invoice: invoice.PaymentRequest, Invoice: invoice.PaymentRequest,
// Amount: amtToPay, Amount: amtToPay,
//}, suite.aliceToken) }, suite.aliceToken)
//assert.NotEmpty(suite.T(), payResponse.PaymentPreimage) assert.NotEmpty(suite.T(), payResponse.PaymentPreimage)
//assert.Equal(suite.T(), int64(amtToPay), payResponse.Amount) assert.Equal(suite.T(), int64(amtToPay), payResponse.Amount)
} }

View File

@@ -13,15 +13,17 @@ import (
"github.com/getAlby/lndhub.go/lib/responses" "github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service" "github.com/getAlby/lndhub.go/lib/service"
"github.com/getAlby/lndhub.go/lib/tokens" "github.com/getAlby/lndhub.go/lib/tokens"
"github.com/getAlby/lndhub.go/lnd"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
type PaymentTestAsyncErrorsSuite struct { type PaymentTestAsyncErrorsSuite struct {
TestSuite TestSuite
mlnd *MockLND
externalLND *MockLND
service *service.LndhubService service *service.LndhubService
userLogin ExpectedCreateUserResponseBody userLogin ExpectedCreateUserResponseBody
userToken string userToken string
@@ -30,11 +32,18 @@ type PaymentTestAsyncErrorsSuite struct {
} }
func (suite *PaymentTestAsyncErrorsSuite) SetupSuite() { func (suite *PaymentTestAsyncErrorsSuite) SetupSuite() {
mlnd, err := NewMockLND("1234567890abcdef", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
externalLND, err := NewMockLND("1234567890abcdefabcd", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
suite.externalLND = externalLND
suite.mlnd = mlnd
// inject fake lnd client with failing send payment sync into service // inject fake lnd client with failing send payment sync into service
lndClient, err := NewLNDMockWrapperAsync(lnd.LNDoptions{ lndClient, err := NewLNDMockWrapperAsync(mlnd)
Address: mockLNDAddress,
MacaroonHex: mockLNDMacaroonHex,
})
suite.serviceClient = lndClient suite.serviceClient = lndClient
if err != nil { if err != nil {
log.Fatalf("Error setting up test client: %v", err) log.Fatalf("Error setting up test client: %v", err)
@@ -73,26 +82,22 @@ func (suite *PaymentTestAsyncErrorsSuite) TestExternalAsyncFailingInvoice() {
userFundingSats := 1000 userFundingSats := 1000
externalSatRequested := 500 externalSatRequested := 500
// fund user account // fund user account
//invoiceResponse := suite.createAddInvoiceReq(userFundingSats, "integration test external payment user", suite.userToken) invoiceResponse := suite.createAddInvoiceReq(userFundingSats, "integration test external payment user", suite.userToken)
//sendPaymentRequest := lnrpc.SendRequest{ err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
// PaymentRequest: invoiceResponse.PayReq, assert.NoError(suite.T(), err)
// FeeLimit: nil,
//}
//_, err := suite.fundingClient.SendPaymentSync(context.Background(), &sendPaymentRequest)
//assert.NoError(suite.T(), err)
// wait a bit for the callback event to hit // wait a bit for the callback event to hit
time.Sleep(100 * time.Millisecond) time.Sleep(10 * time.Millisecond)
// create external invoice // create external invoice
//externalInvoice := lnrpc.Invoice{ externalInvoice := lnrpc.Invoice{
// Memo: "integration tests: external pay from user", Memo: "integration tests: external pay from user",
// Value: int64(externalSatRequested), Value: int64(externalSatRequested),
//} }
//invoice, err := suite.fundingClient.AddInvoice(context.Background(), &externalInvoice) invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
//assert.NoError(suite.T(), err) assert.NoError(suite.T(), err)
// pay external from user, req will be canceled after 2 sec // pay external from user, req will be canceled after 2 sec
//go suite.createPayInvoiceReqWithCancel(invoice.PaymentRequest, suite.userToken) go suite.createPayInvoiceReqWithCancel(invoice.PaymentRequest, suite.userToken)
// wait for request to fail // wait for request to fail
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)

View File

@@ -13,9 +13,9 @@ import (
"github.com/getAlby/lndhub.go/lib/responses" "github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service" "github.com/getAlby/lndhub.go/lib/service"
"github.com/getAlby/lndhub.go/lib/tokens" "github.com/getAlby/lndhub.go/lib/tokens"
"github.com/getAlby/lndhub.go/lnd"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@@ -23,17 +23,26 @@ import (
type PaymentTestErrorsSuite struct { type PaymentTestErrorsSuite struct {
TestSuite TestSuite
service *service.LndhubService service *service.LndhubService
mlnd *MockLND
externalLND *MockLND
userLogin ExpectedCreateUserResponseBody userLogin ExpectedCreateUserResponseBody
userToken string userToken string
invoiceUpdateSubCancelFn context.CancelFunc invoiceUpdateSubCancelFn context.CancelFunc
} }
func (suite *PaymentTestErrorsSuite) SetupSuite() { func (suite *PaymentTestErrorsSuite) SetupSuite() {
mlnd, err := NewMockLND("1234567890abcdef", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
externalLND, err := NewMockLND("1234567890abcdefabcd", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
// inject fake lnd client with failing send payment sync into service // inject fake lnd client with failing send payment sync into service
lndClient, err := NewLNDMockWrapper(lnd.LNDoptions{ lndClient, err := NewLNDMockWrapper(mlnd)
Address: mockLNDAddress, suite.mlnd = mlnd
MacaroonHex: mockLNDMacaroonHex, suite.externalLND = externalLND
})
if err != nil { if err != nil {
log.Fatalf("Error setting up test client: %v", err) log.Fatalf("Error setting up test client: %v", err)
} }
@@ -71,26 +80,22 @@ func (suite *PaymentTestErrorsSuite) TestExternalFailingInvoice() {
userFundingSats := 1000 userFundingSats := 1000
externalSatRequested := 500 externalSatRequested := 500
//fund user account //fund user account
//invoiceResponse := suite.createAddInvoiceReq(userFundingSats, "integration test external payment user", suite.userToken) invoiceResponse := suite.createAddInvoiceReq(userFundingSats, "integration test external payment user", suite.userToken)
//sendPaymentRequest := lnrpc.SendRequest{ err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
// PaymentRequest: invoiceResponse.PayReq, assert.NoError(suite.T(), err)
// FeeLimit: nil,
//}
//_, err := suite.fundingClient.SendPaymentSync(context.Background(), &sendPaymentRequest)
//assert.NoError(suite.T(), err)
//wait a bit for the callback event to hit //wait a bit for the callback event to hit
time.Sleep(100 * time.Millisecond) time.Sleep(10 * time.Millisecond)
//create external invoice //create external invoice
//externalInvoice := lnrpc.Invoice{ externalInvoice := lnrpc.Invoice{
// Memo: "integration tests: external pay from user", Memo: "integration tests: external pay from user",
// Value: int64(externalSatRequested), Value: int64(externalSatRequested),
//} }
//invoice, err := suite.fundingClient.AddInvoice(context.Background(), &externalInvoice) invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
//assert.NoError(suite.T(), err) assert.NoError(suite.T(), err)
//pay external from user, mock will fail immediately //pay external from user, mock will fail immediately
//_ = suite.createPayInvoiceReqError(invoice.PaymentRequest, suite.userToken) _ = suite.createPayInvoiceReqError(invoice.PaymentRequest, suite.userToken)
userId := getUserIdFromToken(suite.userToken) userId := getUserIdFromToken(suite.userToken)

View File

@@ -39,7 +39,11 @@ func TestSubscriptionStartTestSuite(t *testing.T) {
suite.Run(t, new(SubscriptionStartTestSuite)) suite.Run(t, new(SubscriptionStartTestSuite))
} }
func (suite *SubscriptionStartTestSuite) SetupSuite() { func (suite *SubscriptionStartTestSuite) SetupSuite() {
svc, err := LndHubTestServiceInit(nil) mockLND, err := NewMockLND("1234567890abcdef", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
svc, err := LndHubTestServiceInit(mockLND)
if err != nil { if err != nil {
log.Fatalf("Error initializing test service: %v", err) log.Fatalf("Error initializing test service: %v", err)
} }

View File

@@ -15,7 +15,6 @@ import (
"github.com/getAlby/lndhub.go/lib/responses" "github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service" "github.com/getAlby/lndhub.go/lib/service"
"github.com/getAlby/lndhub.go/lib/tokens" "github.com/getAlby/lndhub.go/lib/tokens"
"github.com/getAlby/lndhub.go/lnd"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
@@ -25,8 +24,8 @@ import (
type WebHookTestSuite struct { type WebHookTestSuite struct {
TestSuite TestSuite
fundingClient *lnd.LNDWrapper
service *service.LndhubService service *service.LndhubService
mlnd *MockLND
userLogin ExpectedCreateUserResponseBody userLogin ExpectedCreateUserResponseBody
userToken string userToken string
webHookServer *httptest.Server webHookServer *httptest.Server
@@ -47,7 +46,12 @@ func (suite *WebHookTestSuite) SetupSuite() {
suite.invoiceChan <- invoice suite.invoiceChan <- invoice
})) }))
suite.webHookServer = webhookServer suite.webHookServer = webhookServer
svc, err := LndHubTestServiceInit(nil) mlnd, err := NewMockLND("1234567890abcdef", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
svc, err := LndHubTestServiceInit(mlnd)
suite.mlnd = mlnd
if err != nil { if err != nil {
log.Fatalf("Error initializing test service: %v", err) log.Fatalf("Error initializing test service: %v", err)
} }
@@ -79,11 +83,7 @@ func (suite *WebHookTestSuite) SetupSuite() {
func (suite *WebHookTestSuite) TestWebHook() { func (suite *WebHookTestSuite) TestWebHook() {
// create incoming invoice and fund account // create incoming invoice and fund account
invoice := suite.createAddInvoiceReq(1000, "integration test webhook", suite.userToken) invoice := suite.createAddInvoiceReq(1000, "integration test webhook", suite.userToken)
sendPaymentRequest := lnrpc.SendRequest{ err := suite.mlnd.mockPaidInvoice(invoice, 0, false, nil)
PaymentRequest: invoice.PayReq,
FeeLimit: nil,
}
_, err := suite.fundingClient.SendPaymentSync(context.Background(), &sendPaymentRequest)
assert.NoError(suite.T(), err) assert.NoError(suite.T(), err)
invoiceFromWebhook := <-suite.invoiceChan invoiceFromWebhook := <-suite.invoiceChan
assert.Equal(suite.T(), "integration test webhook", invoiceFromWebhook.Memo) assert.Equal(suite.T(), "integration test webhook", invoiceFromWebhook.Memo)