Files
lndhub.go/integration_tests/internal_payment_test.go

105 lines
3.4 KiB
Go

package integration_tests
import (
"context"
"log"
"testing"
"time"
"github.com/getAlby/lndhub.go/controllers"
"github.com/getAlby/lndhub.go/lib"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/getAlby/lndhub.go/lib/tokens"
"github.com/getAlby/lndhub.go/lnd"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type PaymentTestSuite struct {
TestSuite
fundingClient *lnd.LNDWrapper
service *service.LndhubService
aliceLogin controllers.CreateUserResponseBody
aliceToken string
bobLogin controllers.CreateUserResponseBody
bobToken string
}
func (suite *PaymentTestSuite) SetupSuite() {
lndClient, err := lnd.NewLNDclient(lnd.LNDoptions{
Address: lnd2RegtestAddress,
MacaroonHex: lnd2RegtestMacaroonHex,
})
if err != nil {
log.Fatalf("Error setting up funding client: %v", err)
}
suite.fundingClient = lndClient
svc, err := LndHubTestServiceInit()
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
users, userTokens, err := createUsers(svc, 2)
if err != nil {
log.Fatalf("Error creating test users: %v", err)
}
// Subscribe to LND invoice updates in the background
go svc.InvoiceUpdateSubscription(context.Background())
suite.service = svc
e := echo.New()
e.HTTPErrorHandler = responses.HTTPErrorHandler
e.Validator = &lib.CustomValidator{Validator: validator.New()}
suite.echo = e
assert.Equal(suite.T(), 2, len(users))
assert.Equal(suite.T(), 2, len(userTokens))
suite.aliceLogin = users[0]
suite.aliceToken = userTokens[0]
suite.bobLogin = users[1]
suite.bobToken = userTokens[1]
suite.echo.Use(tokens.Middleware([]byte(suite.service.Config.JWTSecret)))
suite.echo.GET("/balance", controllers.NewBalanceController(suite.service).Balance)
suite.echo.POST("/addinvoice", controllers.NewAddInvoiceController(suite.service).AddInvoice)
suite.echo.POST("/payinvoice", controllers.NewPayInvoiceController(suite.service).PayInvoice)
}
func (suite *PaymentTestSuite) TearDownSuite() {
}
func (suite *PaymentTestSuite) TestInternalPayment() {
aliceFundingSats := 1000
bobSatRequested := 500
//fund alice account
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test internal payment alice", suite.aliceToken)
sendPaymentRequest := lnrpc.SendRequest{
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
time.Sleep(100 * time.Millisecond)
//create invoice for bob
bobInvoice := suite.createAddInvoiceReq(bobSatRequested, "integration test internal payment bob", suite.bobToken)
//pay bob from alice
payResponse := suite.createPayInvoiceReq(bobInvoice.PayReq, suite.aliceToken)
assert.NotEmpty(suite.T(), payResponse.PaymentPreimage)
//try to pay Bob more than we currently have
//create invoice for bob
tooMuch := suite.createAddInvoiceReq(10000, "integration test internal payment bob", suite.bobToken)
//pay bob from alice
errorResp := suite.createPayInvoiceReqError(tooMuch.PayReq, suite.aliceToken)
assert.Equal(suite.T(), responses.NotEnoughBalanceError.Code, errorResp.Code)
}
func TestInternalPaymentTestSuite(t *testing.T) {
suite.Run(t, new(PaymentTestSuite))
}