mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
* 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
297 lines
7.2 KiB
Go
297 lines
7.2 KiB
Go
package events
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math/big"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPaymentInitiationMessagePayload_MarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("with amount", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
amount := big.NewInt(5000000)
|
|
payload := PaymentInitiationMessagePayload{
|
|
ID: "pi123",
|
|
ConnectorID: "conn123",
|
|
Provider: "test",
|
|
Reference: "ref123",
|
|
CreatedAt: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
|
ScheduledAt: time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC),
|
|
Description: "Test payment",
|
|
Type: "TRANSFER",
|
|
Amount: amount,
|
|
Asset: "USD/2",
|
|
}
|
|
|
|
data, err := json.Marshal(&payload)
|
|
require.NoError(t, err)
|
|
|
|
var result map[string]interface{}
|
|
err = json.Unmarshal(data, &result)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "pi123", result["id"])
|
|
assert.Equal(t, "5000000", result["amount"])
|
|
})
|
|
|
|
t.Run("with nil amount", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
payload := PaymentInitiationMessagePayload{
|
|
ID: "pi123",
|
|
ConnectorID: "conn123",
|
|
Provider: "test",
|
|
Reference: "ref123",
|
|
CreatedAt: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
|
ScheduledAt: time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC),
|
|
Description: "Test payment",
|
|
Type: "TRANSFER",
|
|
Amount: nil,
|
|
Asset: "USD/2",
|
|
}
|
|
|
|
data, err := json.Marshal(&payload)
|
|
require.NoError(t, err)
|
|
|
|
var result map[string]interface{}
|
|
err = json.Unmarshal(data, &result)
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, result["amount"])
|
|
})
|
|
}
|
|
|
|
func TestPaymentInitiationMessagePayload_UnmarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("with valid amount string", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
jsonData := `{
|
|
"id": "pi123",
|
|
"connectorID": "conn123",
|
|
"provider": "test",
|
|
"reference": "ref123",
|
|
"createdAt": "2024-01-01T12:00:00Z",
|
|
"scheduledAt": "2024-01-02T12:00:00Z",
|
|
"description": "Test payment",
|
|
"type": "TRANSFER",
|
|
"amount": "5000000",
|
|
"asset": "USD/2"
|
|
}`
|
|
|
|
var payload PaymentInitiationMessagePayload
|
|
err := json.Unmarshal([]byte(jsonData), &payload)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "pi123", payload.ID)
|
|
assert.NotNil(t, payload.Amount)
|
|
assert.Equal(t, "5000000", payload.Amount.String())
|
|
})
|
|
|
|
t.Run("with nil amount", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
jsonData := `{
|
|
"id": "pi123",
|
|
"connectorID": "conn123",
|
|
"provider": "test",
|
|
"reference": "ref123",
|
|
"createdAt": "2024-01-01T12:00:00Z",
|
|
"scheduledAt": "2024-01-02T12:00:00Z",
|
|
"description": "Test payment",
|
|
"type": "TRANSFER",
|
|
"asset": "USD/2"
|
|
}`
|
|
|
|
var payload PaymentInitiationMessagePayload
|
|
err := json.Unmarshal([]byte(jsonData), &payload)
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, payload.Amount)
|
|
})
|
|
|
|
t.Run("with invalid amount string", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
jsonData := `{
|
|
"id": "pi123",
|
|
"connectorID": "conn123",
|
|
"provider": "test",
|
|
"reference": "ref123",
|
|
"createdAt": "2024-01-01T12:00:00Z",
|
|
"scheduledAt": "2024-01-02T12:00:00Z",
|
|
"description": "Test payment",
|
|
"type": "TRANSFER",
|
|
"amount": "invalid-number",
|
|
"asset": "USD/2"
|
|
}`
|
|
|
|
var payload PaymentInitiationMessagePayload
|
|
err := json.Unmarshal([]byte(jsonData), &payload)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "invalid amount string")
|
|
})
|
|
|
|
t.Run("round-trip marshalling", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
original := PaymentInitiationMessagePayload{
|
|
ID: "pi123",
|
|
ConnectorID: "conn123",
|
|
Provider: "test",
|
|
Reference: "ref123",
|
|
CreatedAt: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
|
ScheduledAt: time.Date(2024, 1, 2, 12, 0, 0, 0, time.UTC),
|
|
Description: "Test payment",
|
|
Type: "TRANSFER",
|
|
Amount: big.NewInt(5000000),
|
|
Asset: "USD/2",
|
|
}
|
|
|
|
data, err := json.Marshal(&original)
|
|
require.NoError(t, err)
|
|
|
|
var unmarshaled PaymentInitiationMessagePayload
|
|
err = json.Unmarshal(data, &unmarshaled)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, original.ID, unmarshaled.ID)
|
|
assert.Equal(t, original.Amount.String(), unmarshaled.Amount.String())
|
|
})
|
|
}
|
|
|
|
func TestPaymentInitiationAdjustmentMessagePayload_MarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("with amount", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
amount := big.NewInt(3000000)
|
|
asset := "USD/2"
|
|
payload := PaymentInitiationAdjustmentMessagePayload{
|
|
ID: "adj123",
|
|
PaymentInitiationID: "pi123",
|
|
Status: "SUCCEEDED",
|
|
Amount: amount,
|
|
Asset: &asset,
|
|
}
|
|
|
|
data, err := json.Marshal(&payload)
|
|
require.NoError(t, err)
|
|
|
|
var result map[string]interface{}
|
|
err = json.Unmarshal(data, &result)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "adj123", result["id"])
|
|
assert.Equal(t, "3000000", result["amount"])
|
|
})
|
|
|
|
t.Run("with nil amount", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
payload := PaymentInitiationAdjustmentMessagePayload{
|
|
ID: "adj123",
|
|
PaymentInitiationID: "pi123",
|
|
Status: "SUCCEEDED",
|
|
Amount: nil,
|
|
}
|
|
|
|
data, err := json.Marshal(&payload)
|
|
require.NoError(t, err)
|
|
|
|
var result map[string]interface{}
|
|
err = json.Unmarshal(data, &result)
|
|
require.NoError(t, err)
|
|
|
|
// When amount is nil, it should be omitted (omitempty)
|
|
_, exists := result["amount"]
|
|
assert.False(t, exists)
|
|
})
|
|
}
|
|
|
|
func TestPaymentInitiationAdjustmentMessagePayload_UnmarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("with valid amount string", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
jsonData := `{
|
|
"id": "adj123",
|
|
"paymentInitiationID": "pi123",
|
|
"status": "SUCCEEDED",
|
|
"amount": "3000000"
|
|
}`
|
|
|
|
var payload PaymentInitiationAdjustmentMessagePayload
|
|
err := json.Unmarshal([]byte(jsonData), &payload)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "adj123", payload.ID)
|
|
assert.NotNil(t, payload.Amount)
|
|
assert.Equal(t, "3000000", payload.Amount.String())
|
|
})
|
|
|
|
t.Run("with nil amount", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
jsonData := `{
|
|
"id": "adj123",
|
|
"paymentInitiationID": "pi123",
|
|
"status": "SUCCEEDED"
|
|
}`
|
|
|
|
var payload PaymentInitiationAdjustmentMessagePayload
|
|
err := json.Unmarshal([]byte(jsonData), &payload)
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, payload.Amount)
|
|
})
|
|
|
|
t.Run("with invalid amount string", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
jsonData := `{
|
|
"id": "adj123",
|
|
"paymentInitiationID": "pi123",
|
|
"status": "SUCCEEDED",
|
|
"amount": "invalid-number"
|
|
}`
|
|
|
|
var payload PaymentInitiationAdjustmentMessagePayload
|
|
err := json.Unmarshal([]byte(jsonData), &payload)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "invalid amount string")
|
|
})
|
|
|
|
t.Run("round-trip marshalling", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
original := PaymentInitiationAdjustmentMessagePayload{
|
|
ID: "adj123",
|
|
PaymentInitiationID: "pi123",
|
|
Status: "SUCCEEDED",
|
|
Amount: big.NewInt(3000000),
|
|
}
|
|
|
|
data, err := json.Marshal(&original)
|
|
require.NoError(t, err)
|
|
|
|
var unmarshaled PaymentInitiationAdjustmentMessagePayload
|
|
err = json.Unmarshal(data, &unmarshaled)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, original.ID, unmarshaled.ID)
|
|
assert.Equal(t, original.Amount.String(), unmarshaled.Amount.String())
|
|
})
|
|
}
|
|
|