Files
lndhub.go/integration_tests/incoming_payment_test.go
Stefan Kostic 09aee50ec0 Cleanup
2022-02-17 20:17:29 +01:00

117 lines
3.9 KiB
Go

package integration_tests
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"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 IncomingPaymentTestSuite struct {
TestSuite
fundingClient *lnd.LNDWrapper
service *service.LndhubService
userLogin controllers.CreateUserResponseBody
userToken string
invoiceUpdateSubCancelFn context.CancelFunc
}
func (suite *IncomingPaymentTestSuite) 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(nil)
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
users, tokens, err := createUsers(svc, 1)
if err != nil {
log.Fatalf("Error creating test users: %v", err)
}
// Subscribe to LND invoice updates in the background
// store cancel func to be called in tear down suite
ctx, cancel := context.WithCancel(context.Background())
suite.invoiceUpdateSubCancelFn = cancel
go svc.InvoiceUpdateSubscription(ctx)
suite.service = svc
e := echo.New()
e.HTTPErrorHandler = responses.HTTPErrorHandler
e.Validator = &lib.CustomValidator{Validator: validator.New()}
suite.echo = e
assert.Equal(suite.T(), 1, len(users))
assert.Equal(suite.T(), 1, len(tokens))
suite.userLogin = users[0]
suite.userToken = tokens[0]
}
func (suite *IncomingPaymentTestSuite) TearDownSuite() {
suite.invoiceUpdateSubCancelFn()
}
func (suite *IncomingPaymentTestSuite) TestIncomingPayment() {
var buf bytes.Buffer
req := httptest.NewRequest(http.MethodGet, "/balance", &buf)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.userToken))
rec := httptest.NewRecorder()
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.ServeHTTP(rec, req)
balance := &controllers.BalanceResponse{}
assert.Equal(suite.T(), http.StatusOK, rec.Code)
assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(&balance))
//assert the user has no balance to start with
assert.Equal(suite.T(), int64(0), balance.BTC.AvailableBalance)
fundingSatAmt := 10
invoiceResponse := suite.createAddInvoiceReq(fundingSatAmt, "integration test IncomingPaymentTestSuite", suite.userToken)
//try to pay invoice with external node
// Prepare the LNRPC call
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)
//check balance again
req = httptest.NewRequest(http.MethodGet, "/balance", &buf)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.userToken))
suite.echo.ServeHTTP(rec, req)
balance = &controllers.BalanceResponse{}
assert.Equal(suite.T(), http.StatusOK, rec.Code)
assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(&balance))
//assert the balance was added to the user's account
assert.Equal(suite.T(), int64(fundingSatAmt), balance.BTC.AvailableBalance)
}
func TestIncomingPaymentTestSuite(t *testing.T) {
suite.Run(t, new(IncomingPaymentTestSuite))
}