Files
payments/test/e2e/api_payments_test.go
fgueryandGitHub a63e3a20a8 feat(EN-214): create events via storage instead of manually calling event activity (#594)
* feat(EN-214): add outbox table migration

* feat(EN-214): add outbox models and storage layer

* feat(EN-214): implement outbox publisher activity

* feat(EN-214): implement outbox schedule activity

* feat(EN-214): add outbox publisher workflow

* feat(EN-214): migrate accounts to outbox pattern

* feat(EN-214): migrate balances to outbox pattern

* feat(EN-214): migrate payments to outbox pattern

* feat(EN-214): migrate bank accounts to outbox pattern

* feat(EN-214): migrate pools to outbox pattern

* feat(EN-214): migrate payment initiations to outbox pattern

* feat(EN-214): migrate PSU and connections to outbox pattern

* feat(EN-214): migrate connectors to outbox pattern

* feat(EN-214): remove legacy send_events workflow

* feat(EN-214): update workflows to use outbox pattern

* feat(EN-214): improve outbox idempotency and error handling

* feat(EN-214): update storage tasks and tests

* test(EN-214): update e2e tests for outbox pattern

* test(EN-214): clean up workflow tests

* chore(EN-214): update docker compose configuration

* feat(EN-214): add e2e run config in ide

* fix: add back docker-compose-dev run config

* chore: improve connector uninstall comments & remove connector id being passed

* fix: ensure events are not pushed if balances are not created

* fix: ensure err is not overwritten when using rollback

* fix: ensure we use v3 provider format

* chore: apply the nitpick comments from coderabbit

* chore: consistentely use e() error handling

* feat: optionally disable publisher schedule to make e2e test more consistent

* fix: improve error handling on uninstall connector

* avoid using custom payload and rely on existing interfaces; also avoid using different types for events

* fix: remove unecessary unmarshalling

* fix: ensure that rollbackOnTxError picks up errors on tx.Commit()

* fix: add extra validation on iban & account number in the test

* test: cover marshall/unmarshalling of event payload

* fix: increase test timeout

* try to make test more resiliant

* fix: wrong pointer use in loop

* fix: error formatting issue

* fix: move oubtox schedule creation out of an activity and do it in the worker itself

* feat: OutboxEventDeleteAndSend as batch

* change outbox_event to have id only instead of idempotency key

* make e2e test less brittle, save eventID as idempotency key in publication

* feat: make outbox_event keep an history

* feat(EN-214): added a schedule to cleanup outbox events

* test: make cleanup actually clean

* ignore linting rules for now

* feat: do not add psu metadata to event anymore as it's irrelevant

* fix: create local variable instead of relying on pointers, even if not 100% necessary

* fix: remove temporal schedule describe calls
2025-11-24 16:05:42 +00:00

275 lines
8.0 KiB
Go

//go:build it
package test_suite
import (
"context"
"math/big"
"time"
"github.com/formancehq/go-libs/v3/logging"
"github.com/formancehq/go-libs/v3/pointer"
"github.com/formancehq/go-libs/v3/testing/deferred"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/payments/pkg/client/models/components"
"github.com/formancehq/payments/pkg/events"
"github.com/formancehq/payments/pkg/testserver"
"github.com/google/uuid"
. "github.com/formancehq/payments/pkg/testserver"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Context("Payments API Payments", Serial, func() {
var (
db = UseTemplatedDatabase()
ctx = logging.TestingContext()
app *deferred.Deferred[*Server]
)
app = NewTestServer(func() Configuration {
return Configuration{
Stack: stack,
PostgresConfiguration: db.GetValue().ConnectionOptions(),
NatsURL: natsServer.GetValue().ClientURL(),
TemporalNamespace: temporalServer.GetValue().DefaultNamespace(),
TemporalAddress: temporalServer.GetValue().Address(),
Output: GinkgoWriter,
SkipOutboxScheduleCreation: true,
}
})
AfterEach(func() {
flushRemainingWorkflows(ctx)
})
When("creating a new payment with v3", func() {
var (
connectorID string
createdAt time.Time
initialAmount *big.Int
asset string
err error
)
BeforeEach(func() {
createdAt = time.Now()
initialAmount = big.NewInt(1340)
asset = "USD/2"
connectorID, err = installConnector(ctx, app.GetValue(), uuid.New(), 3)
Expect(err).To(BeNil())
})
AfterEach(func() {
uninstallConnector(ctx, app.GetValue(), connectorID)
})
It("should be ok", func() {
adj := []components.V3CreatePaymentAdjustmentRequest{
{
Reference: "ref_adjustment",
CreatedAt: createdAt,
Amount: big.NewInt(55),
Asset: &asset,
Status: "REFUNDED",
},
}
debtorID, creditorID := setupDebtorAndCreditorV3Accounts(ctx, app.GetValue(), connectorID, createdAt)
createRequest := &components.V3CreatePaymentRequest{
Reference: "ref",
ConnectorID: connectorID,
CreatedAt: createdAt,
InitialAmount: initialAmount,
Amount: initialAmount,
Asset: asset,
Type: "PAY-IN",
SourceAccountID: &debtorID,
DestinationAccountID: &creditorID,
Scheme: models.PAYMENT_SCHEME_CARD_AMEX.String(),
Metadata: map[string]string{"key": "val"},
Adjustments: adj,
}
createResponse, err := app.GetValue().SDK().Payments.V3.CreatePayment(ctx, createRequest)
Expect(err).To(BeNil())
paymentID := createResponse.GetV3CreatePaymentResponse().Data.ID
MustEventuallyOutbox(ctx, app.GetValue(), events.EventTypeSavedPayments,
WithPayloadSubset(struct {
ConnectorID string `json:"connectorID"`
ID string `json:"id"`
}{
ConnectorID: connectorID,
ID: paymentID,
}))
getResponse, err := app.GetValue().SDK().Payments.V3.GetPayment(ctx, createResponse.GetV3CreatePaymentResponse().Data.ID)
Expect(err).To(BeNil())
Expect(getResponse.GetV3GetPaymentResponse().Data.Amount).To(Equal(big.NewInt(0).Sub(createRequest.Amount, adj[0].Amount)))
Expect(getResponse.GetV3GetPaymentResponse().Data.Status).To(Equal(adj[0].Status))
Expect(getResponse.GetV3GetPaymentResponse().Data.Adjustments).To(HaveLen(1))
})
})
When("creating a new payment with v2", func() {
var (
connectorID string
createdAt time.Time
initialAmount *big.Int
asset string
err error
)
BeforeEach(func() {
createdAt = time.Now()
initialAmount = big.NewInt(1340)
asset = "USD/2"
connectorID, err = installConnector(ctx, app.GetValue(), uuid.New(), 2)
Expect(err).To(BeNil())
})
AfterEach(func() {
uninstallConnector(ctx, app.GetValue(), connectorID)
})
It("should be ok", func() {
debtorID, creditorID := setupDebtorAndCreditorV2Accounts(ctx, app.GetValue(), connectorID, createdAt)
createRequest := components.PaymentRequest{
Reference: "ref",
ConnectorID: connectorID,
CreatedAt: createdAt,
Amount: initialAmount,
Asset: asset,
Type: "PAY-IN",
Status: "SUCCEEDED",
Scheme: components.PaymentSchemeAmex,
SourceAccountID: &debtorID,
DestinationAccountID: &creditorID,
}
createResponse, err := app.GetValue().SDK().Payments.V1.CreatePayment(ctx, createRequest)
Expect(err).To(BeNil())
Expect(createResponse.GetPaymentResponse().Data.ID).NotTo(Equal(""))
paymentID := createResponse.GetPaymentResponse().Data.ID
MustEventuallyOutbox(ctx, app.GetValue(), events.EventTypeSavedPayments,
WithPayloadSubset(struct {
ConnectorID string `json:"connectorID"`
ID string `json:"id"`
}{
ConnectorID: connectorID,
ID: paymentID,
}))
getResponse, err := app.GetValue().SDK().Payments.V1.GetPayment(ctx, createResponse.GetPaymentResponse().Data.ID)
Expect(err).To(BeNil())
Expect(getResponse.GetPaymentResponse().Data.Amount).To(Equal(createRequest.Amount))
Expect(getResponse.GetPaymentResponse().Data.Status).To(Equal(createRequest.Status))
})
})
})
func setupDebtorAndCreditorV3Accounts(
ctx context.Context,
app *testserver.Server,
connectorID string,
createdAt time.Time,
) (string, string) {
creditorID, err := createV3Account(ctx, app, &components.V3CreateAccountRequest{
Reference: "creditor",
ConnectorID: connectorID,
CreatedAt: createdAt.Add(-time.Hour),
AccountName: "creditor",
Type: "INTERNAL",
DefaultAsset: pointer.For("USD/2"),
Metadata: map[string]string{
"key": "val",
},
})
Expect(err).To(BeNil())
MustEventuallyOutbox(ctx, app, events.EventTypeSavedAccounts,
WithPayloadSubset(struct {
ConnectorID string `json:"connectorID"`
ID string `json:"id"`
}{
ConnectorID: connectorID,
ID: creditorID,
}))
debtorID, err := createV3Account(ctx, app, &components.V3CreateAccountRequest{
Reference: "debtor",
ConnectorID: connectorID,
CreatedAt: createdAt,
AccountName: "debtor",
Type: "EXTERNAL",
DefaultAsset: pointer.For("USD/2"),
Metadata: map[string]string{
"ping": "pong",
},
})
Expect(err).To(BeNil())
MustEventuallyOutbox(ctx, app, events.EventTypeSavedAccounts,
WithPayloadSubset(struct {
ConnectorID string `json:"connectorID"`
ID string `json:"id"`
}{
ConnectorID: connectorID,
ID: debtorID,
}))
return debtorID, creditorID
}
func setupDebtorAndCreditorV2Accounts(
ctx context.Context,
app *testserver.Server,
connectorID string,
createdAt time.Time,
) (string, string) {
creditorID, err := createV2Account(ctx, app, components.AccountRequest{
Reference: "creditor",
ConnectorID: connectorID,
CreatedAt: createdAt.Add(-time.Hour),
AccountName: pointer.For("creditor"),
Type: "INTERNAL",
DefaultAsset: pointer.For("USD/2"),
Metadata: map[string]string{
"key": "val",
},
})
Expect(err).To(BeNil())
MustEventuallyOutbox(ctx, app, events.EventTypeSavedAccounts,
WithPayloadSubset(struct {
ConnectorID string `json:"connectorID"`
ID string `json:"id"`
}{
ConnectorID: connectorID,
ID: creditorID,
}))
debtorID, err := createV2Account(ctx, app, components.AccountRequest{
Reference: "debtor",
ConnectorID: connectorID,
CreatedAt: createdAt,
AccountName: pointer.For("debtor"),
Type: "EXTERNAL",
DefaultAsset: pointer.For("USD/2"),
Metadata: map[string]string{
"ping": "pong",
},
})
Expect(err).To(BeNil())
MustEventuallyOutbox(ctx, app, events.EventTypeSavedAccounts,
WithPayloadSubset(struct {
ConnectorID string `json:"connectorID"`
ID string `json:"id"`
}{
ConnectorID: connectorID,
ID: debtorID,
}))
return debtorID, creditorID
}