diff --git a/.github/workflows/integration_tests.yaml b/.github/workflows/integration_tests.yaml index c712caf..57ec275 100644 --- a/.github/workflows/integration_tests.yaml +++ b/.github/workflows/integration_tests.yaml @@ -25,6 +25,13 @@ jobs: --health-interval 10s --health-timeout 5s --health-retries 5 + rabbitmq: + image: rabbitmq:3.11.8 + ports: + - 5672:5672 + env: + RABBITMQ_DEFAULT_USER: "root" + RABBITMQ_DEFAULT_PASS: "password" steps: - name: Install Go uses: actions/setup-go@v1 @@ -34,3 +41,6 @@ jobs: uses: actions/checkout@v2 - name: Run tests run: go test -p 1 -v -cover -coverpkg=./... ./... + env: + RABBITMQ_URI: amqp://root:password@rabbitmq:5672 + diff --git a/integration_tests/rabbitmq_test.go b/integration_tests/rabbitmq_test.go new file mode 100644 index 0000000..77bad13 --- /dev/null +++ b/integration_tests/rabbitmq_test.go @@ -0,0 +1,98 @@ +package integration_tests + +import ( + "bytes" + "context" + "encoding/json" + "log" + "testing" + + "github.com/getAlby/lndhub.go/db/models" + "github.com/getAlby/lndhub.go/lib/service" + amqp "github.com/rabbitmq/amqp091-go" + "github.com/stretchr/testify/assert" +) + +type RabbitMQTestSuite struct { + TestSuite + mlnd *MockLND + invoiceUpdateSubCancelFn context.CancelFunc + userToken string + svc *service.LndhubService +} + +func (suite *RabbitMQTestSuite) SetupSuite() { + mlnd := newDefaultMockLND() + svc, err := LndHubTestServiceInit(mlnd) + if err != nil { + log.Fatalf("could not initialize test service: %v", err) + } + + suite.mlnd = mlnd + + _, userTokens, err := createUsers(svc, 1) + if err != nil { + log.Fatalf("Error creating test users: %v", err) + } + suite.userToken = userTokens[0] + + ctx, cancel := context.WithCancel(context.Background()) + suite.invoiceUpdateSubCancelFn = cancel + go svc.InvoiceUpdateSubscription(ctx) + + go svc.StartRabbitMqPublisher(ctx) +} + +func (suite *RabbitMQTestSuite) TestPublishInvoice(t *testing.T) { + // create incoming invoice and fund account + invoice := suite.createAddInvoiceReq(1000, "integration test webhook", suite.userToken) + err := suite.mlnd.mockPaidInvoice(invoice, 0, false, nil) + assert.NoError(suite.T(), err) + + // Check consume from rabbit + conn, err := amqp.Dial(suite.svc.Config.RabbitMQUri) + if err != nil { + t.Error(err) + } + defer conn.Close() + + ch, err := conn.Channel() + if err != nil { + t.Error(err) + } + + q, err := ch.QueueDeclare( + "test_invoice", + false, + true, + false, + false, + nil, + ) + if err != nil { + t.Error(err) + } + + m, err := ch.Consume( + q.Name, + "", + false, + false, + false, + false, + nil, + ) + if err != nil { + t.Error(err) + } + + t.Log("Blocking on message channel") + msg := <-m + t.Logf("%s/n", string(msg.Body)) + + var recievedInvoice models.Invoice + r := bytes.NewReader(msg.Body) + json.NewDecoder(r).Decode(&recievedInvoice) + + assert.Equal(t, invoice.RHash, recievedInvoice.RHash) +} diff --git a/integration_tests/util.go b/integration_tests/util.go index 5bf606f..346add7 100644 --- a/integration_tests/util.go +++ b/integration_tests/util.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "os" "time" "github.com/getAlby/lndhub.go/db" @@ -44,7 +45,8 @@ const ( ) func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *service.LndhubService, err error) { - dbUri := "postgresql://user:password@localhost/lndhub?sslmode=disable" + //dbUri := "postgresql://user:password@localhost/lndhub?sslmode=disable" + dbUri := "postgresql://postgres:root@localhost:5432/lndhub?sslmode=disable" c := &service.Config{ DatabaseUri: dbUri, JWTSecret: []byte("SECRET"), @@ -53,6 +55,13 @@ func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *servi LNDAddress: mockLNDAddress, LNDMacaroonHex: mockLNDMacaroonHex, } + + rabbitmqUri, ok := os.LookupEnv("RABBITMQ_URI") + if ok { + c.RabbitMQUri = rabbitmqUri + c.RabbitMQInvoiceExchange = "test_lndhub_invoices" + } + dbConn, err := db.Open(c.DatabaseUri) if err != nil { return nil, fmt.Errorf("failed to connect to database: %w", err)