-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from getAlby/feature/webhooks
Feature/webhooks
- Loading branch information
Showing
10 changed files
with
225 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package integration_tests | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/getAlby/lndhub.go/common" | ||
"github.com/getAlby/lndhub.go/controllers" | ||
"github.com/getAlby/lndhub.go/db/models" | ||
"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 WebHookTestSuite struct { | ||
TestSuite | ||
fundingClient *lnd.LNDWrapper | ||
service *service.LndhubService | ||
userLogin ExpectedCreateUserResponseBody | ||
userToken string | ||
webHookServer *httptest.Server | ||
invoiceChan chan (models.Invoice) | ||
invoiceUpdateSubCancelFn context.CancelFunc | ||
} | ||
|
||
func (suite *WebHookTestSuite) 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 | ||
|
||
suite.invoiceChan = make(chan models.Invoice) | ||
webhookServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
invoice := models.Invoice{} | ||
err = json.NewDecoder(r.Body).Decode(&invoice) | ||
if err != nil { | ||
suite.echo.Logger.Error(err) | ||
close(suite.invoiceChan) | ||
return | ||
} | ||
suite.invoiceChan <- invoice | ||
})) | ||
suite.webHookServer = webhookServer | ||
svc, err := LndHubTestServiceInit(nil) | ||
if err != nil { | ||
log.Fatalf("Error initializing test service: %v", err) | ||
} | ||
svc.Config.WebhookUrl = suite.webHookServer.URL | ||
|
||
users, userTokens, 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) | ||
|
||
go svc.StartWebhookSubscribtion(ctx, svc.Config.WebhookUrl) | ||
|
||
suite.service = svc | ||
e := echo.New() | ||
|
||
e.HTTPErrorHandler = responses.HTTPErrorHandler | ||
e.Validator = &lib.CustomValidator{Validator: validator.New()} | ||
suite.echo = e | ||
suite.userLogin = users[0] | ||
suite.userToken = userTokens[0] | ||
suite.echo.Use(tokens.Middleware([]byte(suite.service.Config.JWTSecret))) | ||
suite.echo.POST("/addinvoice", controllers.NewAddInvoiceController(suite.service).AddInvoice) | ||
} | ||
func (suite *WebHookTestSuite) TestWebHook() { | ||
// create incoming invoice and fund account | ||
invoice := suite.createAddInvoiceReq(1000, "integration test webhook", suite.userToken) | ||
sendPaymentRequest := lnrpc.SendRequest{ | ||
PaymentRequest: invoice.PayReq, | ||
FeeLimit: nil, | ||
} | ||
_, err := suite.fundingClient.SendPaymentSync(context.Background(), &sendPaymentRequest) | ||
assert.NoError(suite.T(), err) | ||
invoiceFromWebhook := <-suite.invoiceChan | ||
assert.Equal(suite.T(), "integration test webhook", invoiceFromWebhook.Memo) | ||
assert.Equal(suite.T(), common.InvoiceTypeIncoming, invoiceFromWebhook.Type) | ||
} | ||
func (suite *WebHookTestSuite) TearDownSuite() { | ||
suite.invoiceUpdateSubCancelFn() | ||
suite.webHookServer.Close() | ||
clearTable(suite.service, "invoices") | ||
} | ||
|
||
func TestWebHookSuite(t *testing.T) { | ||
suite.Run(t, new(WebHookTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package service | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/getAlby/lndhub.go/common" | ||
"github.com/getAlby/lndhub.go/db/models" | ||
) | ||
|
||
func (svc *LndhubService) StartWebhookSubscribtion(ctx context.Context, url string) { | ||
|
||
svc.Logger.Infof("Starting webhook subscription with webhook url %s", svc.Config.WebhookUrl) | ||
incomingInvoices := make(chan models.Invoice) | ||
outgoingInvoices := make(chan models.Invoice) | ||
svc.InvoicePubSub.Subscribe(common.InvoiceTypeIncoming, incomingInvoices) | ||
svc.InvoicePubSub.Subscribe(common.InvoiceTypeOutgoing, outgoingInvoices) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case incoming := <-incomingInvoices: | ||
svc.postToWebhook(incoming, url) | ||
case outgoing := <-outgoingInvoices: | ||
svc.postToWebhook(outgoing, url) | ||
} | ||
} | ||
} | ||
func (svc *LndhubService) postToWebhook(invoice models.Invoice, url string) { | ||
|
||
payload := new(bytes.Buffer) | ||
err := json.NewEncoder(payload).Encode(invoice) | ||
if err != nil { | ||
svc.Logger.Error(err) | ||
return | ||
} | ||
|
||
resp, err := http.Post(svc.Config.WebhookUrl, "application/json", payload) | ||
if err != nil { | ||
svc.Logger.Error(err) | ||
return | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
msg, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
svc.Logger.Error(err) | ||
} | ||
svc.Logger.Errorf("Webhook status code was %d, body: %s", resp.StatusCode, msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters