diff --git a/integration_tests/invoice_test.go b/integration_tests/invoice_test.go index 083671c0..a0d23929 100644 --- a/integration_tests/invoice_test.go +++ b/integration_tests/invoice_test.go @@ -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) diff --git a/integration_tests/keysend_test.go b/integration_tests/keysend_test.go index 58da0f85..102b4c6a 100644 --- a/integration_tests/keysend_test.go +++ b/integration_tests/keysend_test.go @@ -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 @@ -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() { diff --git a/integration_tests/outgoing_payment_test.go b/integration_tests/outgoing_payment_test.go index efa7d8b4..891bd73a 100644 --- a/integration_tests/outgoing_payment_test.go +++ b/integration_tests/outgoing_payment_test.go @@ -1,6 +1,7 @@ package integration_tests import ( + "bytes" "context" "encoding/json" "fmt" @@ -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" ) @@ -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, @@ -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)