From 0b9875340701f51e270b1912696cd44bca36ca72 Mon Sep 17 00:00:00 2001 From: kiwiidb Date: Mon, 25 Sep 2023 14:39:38 +0200 Subject: [PATCH] add integration test max volume --- integration_tests/internal_payment_test.go | 61 ++++++++++++++++++++++ integration_tests/util.go | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/integration_tests/internal_payment_test.go b/integration_tests/internal_payment_test.go index 3d117fa..ec68810 100644 --- a/integration_tests/internal_payment_test.go +++ b/integration_tests/internal_payment_test.go @@ -133,6 +133,67 @@ func (suite *PaymentTestSuite) TestPaymentFeeReserve() { //reset fee reserve so it's not used in other tests suite.service.Config.FeeReserve = false } +func (suite *PaymentTestSuite) TestVolumeExceeded() { + //this will cause the payment to fail as the account was already funded + //with 1000 sats + suite.service.Config.MaxVolume = 999 + suite.service.Config.MaxVolumePeriod = 2592000 + aliceFundingSats := 1000 + //fund alice account + invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test internal payment alice", suite.aliceToken) + err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil) + assert.NoError(suite.T(), err) + + //wait a bit for the payment to be processed + time.Sleep(10 * time.Millisecond) + + //try to make external payment + //which should fail + //create external invoice + externalSatRequested := 1000 + externalInvoice := lnrpc.Invoice{ + Memo: "integration tests: external pay from user", + Value: int64(externalSatRequested), + } + invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice) + assert.NoError(suite.T(), err) + //pay external invoice + rec := httptest.NewRecorder() + var buf bytes.Buffer + 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 volume 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.TooMuchVolumeError.Message, resp.Message) + + //change the period to be 1 second, sleep for 2 seconds, try to make another payment, this should work + suite.service.Config.MaxVolumePeriod = 1 + time.Sleep(2 * time.Second) + rec = httptest.NewRecorder() + externalInvoice = lnrpc.Invoice{ + Memo: "integration tests: external pay from user", + Value: int64(externalSatRequested), + } + invoice, err = suite.externalLND.AddInvoice(context.Background(), &externalInvoice) + assert.NoError(suite.T(), err) + assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&ExpectedPayInvoiceRequestBody{ + Invoice: invoice.PaymentRequest, + })) + suite.echo.ServeHTTP(rec, req) + assert.Equal(suite.T(), http.StatusOK, rec.Code) + + //change the config back + suite.service.Config.MaxVolumePeriod = 0 + suite.service.Config.MaxVolume = 1e6 +} func (suite *PaymentTestSuite) TestInternalPayment() { aliceFundingSats := 1000 bobSatRequested := 500 diff --git a/integration_tests/util.go b/integration_tests/util.go index 1c303bb..959feeb 100644 --- a/integration_tests/util.go +++ b/integration_tests/util.go @@ -51,7 +51,7 @@ func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *servi DatabaseMaxConns: 1, DatabaseMaxIdleConns: 1, DatabaseConnMaxLifetime: 10, - MaxFeeAmount: 1000000, //todo: add max fee test + MaxFeeAmount: 1e6, //todo: add max fee test JWTSecret: []byte("SECRET"), JWTAccessTokenExpiry: 3600, JWTRefreshTokenExpiry: 3600,