diff --git a/integration_tests/gettxs_test.go b/integration_tests/gettxs_test.go index db209de..fc345ad 100644 --- a/integration_tests/gettxs_test.go +++ b/integration_tests/gettxs_test.go @@ -1,7 +1,6 @@ package integration_tests import ( - "bytes" "context" "encoding/json" "fmt" @@ -120,15 +119,7 @@ func (suite *GetTxTestSuite) TestGetOutgoingInvoices() { // create invoice invoice = suite.createAddInvoiceReq(500, "integration test internal payment alice", suite.userToken) // pay invoice, this will create outgoing invoice and settle it - rec = httptest.NewRecorder() - var buf bytes.Buffer - assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&controllers.PayInvoiceRequestBody{ - Invoice: invoice.PayReq, - })) - req = httptest.NewRequest(http.MethodPost, "/payinvoice", &buf) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.userToken)) - suite.echo.ServeHTTP(rec, req) + suite.createPayInvoiceReq(invoice.PayReq, suite.userToken) // check invoices again req = httptest.NewRequest(http.MethodGet, "/gettxs", nil) req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.userToken)) diff --git a/integration_tests/internal_payment_test.go b/integration_tests/internal_payment_test.go index dc75130..4105504 100644 --- a/integration_tests/internal_payment_test.go +++ b/integration_tests/internal_payment_test.go @@ -1,13 +1,8 @@ package integration_tests import ( - "bytes" "context" - "encoding/json" - "fmt" "log" - "net/http" - "net/http/httptest" "testing" "time" @@ -94,36 +89,13 @@ func (suite *PaymentTestSuite) TestInternalPayment() { //create invoice for bob bobInvoice := suite.createAddInvoiceReq(bobSatRequested, "integration test internal payment bob", suite.bobToken) //pay bob from alice - rec := httptest.NewRecorder() - var buf bytes.Buffer - assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&controllers.PayInvoiceRequestBody{ - Invoice: bobInvoice.PayReq, - })) - 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) - payResponse := &controllers.PayInvoiceResponseBody{} - assert.Equal(suite.T(), http.StatusOK, rec.Code) - assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(payResponse)) + 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 - var buf2 bytes.Buffer - assert.NoError(suite.T(), json.NewEncoder(&buf2).Encode(&controllers.PayInvoiceRequestBody{ - Invoice: tooMuch.PayReq, - })) - rec = httptest.NewRecorder() - req = httptest.NewRequest(http.MethodPost, "/payinvoice", &buf2) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken)) - suite.echo.ServeHTTP(rec, req) - assert.NotEqual(suite.T(), http.StatusOK, rec.Code) - errorResp := &responses.ErrorResponse{} - assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(errorResp)) + errorResp := suite.createPayInvoiceReqError(tooMuch.PayReq, suite.aliceToken) assert.Equal(suite.T(), responses.NotEnoughBalanceError.Code, errorResp.Code) } diff --git a/integration_tests/outgoing_payment_test.go b/integration_tests/outgoing_payment_test.go index 596063b..6f24cb6 100644 --- a/integration_tests/outgoing_payment_test.go +++ b/integration_tests/outgoing_payment_test.go @@ -1,16 +1,9 @@ package integration_tests import ( - "bytes" "context" - "encoding/json" - "fmt" - "net/http" - "net/http/httptest" "time" - "github.com/getAlby/lndhub.go/controllers" - "github.com/labstack/echo/v4" "github.com/lightningnetwork/lnd/lnrpc" "github.com/stretchr/testify/assert" ) @@ -38,17 +31,6 @@ func (suite *PaymentTestSuite) TestOutGoingPayment() { invoice, err := suite.fundingClient.AddInvoice(context.Background(), &externalInvoice) assert.NoError(suite.T(), err) //pay external from alice - rec := httptest.NewRecorder() - var buf bytes.Buffer - assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&controllers.PayInvoiceRequestBody{ - 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) - payResponse := &controllers.PayInvoiceResponseBody{} - assert.Equal(suite.T(), http.StatusOK, rec.Code) - assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(payResponse)) + payResponse := suite.createPayInvoiceReq(invoice.PaymentRequest, suite.aliceToken) assert.NotEmpty(suite.T(), payResponse.PaymentPreimage) } diff --git a/integration_tests/util.go b/integration_tests/util.go index b422edb..0d726a3 100644 --- a/integration_tests/util.go +++ b/integration_tests/util.go @@ -14,6 +14,7 @@ import ( "github.com/getAlby/lndhub.go/db" "github.com/getAlby/lndhub.go/db/migrations" "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/lnd" "github.com/labstack/echo/v4" @@ -125,3 +126,37 @@ func (suite *TestSuite) createAddInvoiceReq(amt int, memo, token string) *contro assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(invoiceResponse)) return invoiceResponse } + +func (suite *TestSuite) createPayInvoiceReq(payReq string, token string) *controllers.PayInvoiceResponseBody { + rec := httptest.NewRecorder() + var buf bytes.Buffer + assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&controllers.PayInvoiceRequestBody{ + Invoice: payReq, + })) + req := httptest.NewRequest(http.MethodPost, "/payinvoice", &buf) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) + suite.echo.ServeHTTP(rec, req) + + payInvoiceResponse := &controllers.PayInvoiceResponseBody{} + assert.Equal(suite.T(), http.StatusOK, rec.Code) + assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(payInvoiceResponse)) + return payInvoiceResponse +} + +func (suite *TestSuite) createPayInvoiceReqError(payReq string, token string) *responses.ErrorResponse { + rec := httptest.NewRecorder() + var buf bytes.Buffer + assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&controllers.PayInvoiceRequestBody{ + Invoice: payReq, + })) + req := httptest.NewRequest(http.MethodPost, "/payinvoice", &buf) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) + suite.echo.ServeHTTP(rec, req) + + errorResponse := &responses.ErrorResponse{} + assert.Equal(suite.T(), http.StatusBadRequest, rec.Code) + assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(errorResponse)) + return errorResponse +}