Skip to content

Commit

Permalink
chore: add more tests for limits
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Apr 4, 2024
1 parent 71c578d commit 8590406
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
20 changes: 20 additions & 0 deletions integration_tests/invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ func (suite *InvoiceTestSuite) TestAddInvoiceWithoutToken() {
assert.Equal(suite.T(), 1, len(invoicesAfter))
}

func (suite *InvoiceTestSuite) TestAddInvoiceWithLimits() {
suite.service.Config.MaxReceiveAmount = 200
rec := httptest.NewRecorder()
var buf bytes.Buffer
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&ExpectedV2AddInvoiceRequestBody{
Amount: 300,
Memo: "testing limits",
}))
req := httptest.NewRequest(http.MethodPost, "/v2/invoices", &buf)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken))
suite.echo.ServeHTTP(rec, req)
//should fail because max receive amount check
assert.Equal(suite.T(), http.StatusBadRequest, rec.Code)
resp := &responses.ErrorResponse{}
err := json.NewDecoder(rec.Body).Decode(resp)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), responses.ReceiveExceededError.Message, resp.Message)
}

func (suite *InvoiceTestSuite) TestAddInvoiceForNonExistingUser() {
nonExistingLogin := suite.aliceLogin.Login + "abc"
suite.createInvoiceReqError(10, "test invoice without token", nonExistingLogin)
Expand Down
43 changes: 42 additions & 1 deletion integration_tests/keysend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (suite *KeySendTestSuite) TearDownSuite() {

func (suite *KeySendTestSuite) TestKeysendPayment() {
suite.service.Config.ServiceFee = 1
aliceFundingSats := 1000
aliceFundingSats := 1500
externalSatRequested := 500
expectedServiceFee := 1
//fund alice account
Expand All @@ -99,6 +99,47 @@ func (suite *KeySendTestSuite) TestKeysendPayment() {
}
assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested+int(suite.mlnd.fee)+expectedServiceFee), aliceBalance)
suite.service.Config.ServiceFee = 0

var buf bytes.Buffer
suite.service.Config.MaxSendAmount = 200
rec := httptest.NewRecorder()
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&ExpectedKeySendRequestBody{
Amount: int64(externalSatRequested),
Destination: "123456789012345678901234567890123456789012345678901234567890abcdef",
Memo: "key send test",
}))
req := httptest.NewRequest(http.MethodPost, "/keysend", &buf)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken))
suite.echo.ServeHTTP(rec, req)
//should fail because max send amount check
assert.Equal(suite.T(), http.StatusBadRequest, rec.Code)
resp := &responses.ErrorResponse{}
err = json.NewDecoder(rec.Body).Decode(resp)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), responses.SendExceededError.Message, resp.Message)

// check if setting zero as send amount stops
suite.service.Config.MaxSendAmount = 0
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&ExpectedKeySendRequestBody{
Amount: int64(externalSatRequested),
Destination: "123456789012345678901234567890123456789012345678901234567890abcdef",
Memo: "key send test",
}))
req = httptest.NewRequest(http.MethodPost, "/keysend", &buf)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken))
suite.echo.ServeHTTP(rec, req)
//should fail because max send amount check
assert.Equal(suite.T(), http.StatusBadRequest, rec.Code)
resp = &responses.ErrorResponse{}
err = json.NewDecoder(rec.Body).Decode(resp)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), responses.SendExceededError.Message, resp.Message)

// restore default and try again, should work now
suite.service.Config.MaxSendAmount = -1
suite.createKeySendReq(int64(externalSatRequested), "key send test", "123456789012345678901234567890123456789012345678901234567890abcdef", suite.aliceToken)
}

func (suite *KeySendTestSuite) TestKeysendPaymentNonExistentDestination() {
Expand Down
26 changes: 24 additions & 2 deletions integration_tests/outgoing_payment_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration_tests

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -9,6 +10,8 @@ import (
"time"

"github.com/getAlby/lndhub.go/common"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert"
)
Expand All @@ -35,6 +38,25 @@ func (suite *PaymentTestSuite) TestOutGoingPayment() {
}
invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
assert.NoError(suite.T(), err)

var buf bytes.Buffer
suite.service.Config.MaxSendAmount = 200
rec := httptest.NewRecorder()
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&ExpectedPayInvoiceRequestBody{
Invoice: invoice.PaymentRequest,
}))
req := httptest.NewRequest(http.MethodPost, "/payinvoice", &buf)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken))
suite.echo.ServeHTTP(rec, req)
//should fail because max send amount check
assert.Equal(suite.T(), http.StatusBadRequest, rec.Code)
resp := &responses.ErrorResponse{}
err = json.NewDecoder(rec.Body).Decode(resp)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), responses.SendExceededError.Message, resp.Message)

suite.service.Config.MaxSendAmount = -1
//pay external from alice
payResponse := suite.createPayInvoiceReq(&ExpectedPayInvoiceRequestBody{
Invoice: invoice.PaymentRequest,
Expand Down Expand Up @@ -116,9 +138,9 @@ func (suite *PaymentTestSuite) TestOutGoingPayment() {

// fetch transactions, make sure the fee is there
// check invoices again
req := httptest.NewRequest(http.MethodGet, "/gettxs", nil)
req = httptest.NewRequest(http.MethodGet, "/gettxs", nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken))
rec := httptest.NewRecorder()
rec = httptest.NewRecorder()
suite.echo.ServeHTTP(rec, req)
responseBody := &[]ExpectedOutgoingInvoice{}
assert.Equal(suite.T(), http.StatusOK, rec.Code)
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *service.LndhubService, err error) {
dbUri, ok := os.LookupEnv("DATABASE_URI")
if !ok {
dbUri = "postgresql://user:password@localhost/lndhub?sslmode=disable"
dbUri = "postgresql://im-adithya:password@localhost:5432/lndhub?sslmode=disable"
}
c := &service.Config{
DatabaseUri: dbUri,
Expand Down

0 comments on commit 8590406

Please sign in to comment.