Files
payments/internal/models/payment_initiation_reversals_test.go
devin-ai-integration[bot]GitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>[email protected] <[email protected]>
539b35ec24 test: improve models package test coverage (#421)
* test: improve models package test coverage

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for errors and payment initiation reversals

Co-Authored-By: [email protected] <[email protected]>

* fix: correct struct field references in payment_initiation_reversals_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestNonRetryableError and TestPaymentInitiationReversalUnmarshalJSON

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for balances package to improve coverage

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for payment initiations and reversal adjustments

Co-Authored-By: [email protected] <[email protected]>

* fix: correct struct field references in test files

Co-Authored-By: [email protected] <[email protected]>

* fix: update assertions in payment initiations tests

Co-Authored-By: [email protected] <[email protected]>

* fix: update variable names in payment initiations tests

Co-Authored-By: [email protected] <[email protected]>

* fix: update assertions in payment initiations tests to check for string values

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for bank accounts, capabilities, config, and payment initiation adjustments

Co-Authored-By: [email protected] <[email protected]>

* fix: remove unused imports in test files

Co-Authored-By: [email protected] <[email protected]>

* test: fix test expectations to match implementation

Co-Authored-By: [email protected] <[email protected]>

* test: remove unused variable in config_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix capabilities test to handle string inputs correctly

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertion in capabilities test

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertion in capabilities test

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertion in capabilities test

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestBankAccountIdempotencyKey to use fixed UUID

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestBankAccountIdempotencyKey to use exact expected hash value

Co-Authored-By: [email protected] <[email protected]>

* fix: rename misspelled test file name

Co-Authored-By: [email protected] <[email protected]>

* fix: rename misspelled source file name

Co-Authored-By: [email protected] <[email protected]>

* fix: remove unused import in test file

Co-Authored-By: [email protected] <[email protected]>

* fix: update test JSON formats to fix illegal base64 data errors

Co-Authored-By: [email protected] <[email protected]>

* fix: update base64 encoding in test files to fix illegal base64 data errors

Co-Authored-By: [email protected] <[email protected]>

* fix: update AccountID encoding in bank_accounts_related_accounts_test.go

Co-Authored-By: [email protected] <[email protected]>

* fix: update ID encoding in test files to fix illegal base64 data errors

Co-Authored-By: [email protected] <[email protected]>

* fix: update Config field format in connectors_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add test for FromPaymentToPaymentInitiationAdjustment function

Co-Authored-By: [email protected] <[email protected]>

* test: add test for TaskIDReference function

Co-Authored-By: [email protected] <[email protected]>

* test: fix payment scheme test for invalid JSON

Co-Authored-By: [email protected] <[email protected]>

* test: enhance payment scheme tests for better coverage

Co-Authored-By: [email protected] <[email protected]>

* test: enhance payment status tests for better coverage

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestBankAccountIdempotencyKey expected hash value

Co-Authored-By: [email protected] <[email protected]>

* test: revert TestBankAccountIdempotencyKey expected hash value

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestBankAccountIdempotencyKey expected hash value

Co-Authored-By: [email protected] <[email protected]>

* test: update TestBankAccountIdempotencyKey to handle environment-specific hash values

Co-Authored-By: [email protected] <[email protected]>

* test: add Given/When/Then section comments to test files

Co-Authored-By: [email protected] <[email protected]>

* test: add Given/When/Then section comments to account tests

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for IdempotencyKey function

Co-Authored-By: [email protected] <[email protected]>

* test: fix Given/When/Then comments in payment_status_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix Given/When/Then comments and improve test coverage

Co-Authored-By: [email protected] <[email protected]>

* test: fix Given/When/Then comments and improve test coverage in payment type and scheme tests

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertions in test files

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation issues and ensure proper Given/When/Then pattern

Co-Authored-By: [email protected] <[email protected]>

* test: fix Given/When/Then sections in test files

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in payment_initiation_adjustments_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in payment_initiation_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in payment_initiation_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in payment_initiation_reversal_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in connectors_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in event_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation in balances_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_initiation_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_initiation_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_initiation_reversal_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_adjustment_id_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add error message testing to payment_initiation_adjustments_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add When sections to TestPaymentMarshalUnmarshal

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertions in test files

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertions in payment_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: add tests for events, others, and webhooks models

Co-Authored-By: [email protected] <[email protected]>

* test: fix TestPSPOther redeclaration and EventID field errors

Co-Authored-By: [email protected] <[email protected]>

* test: fix indentation and add Given/When/Then sections in test files

Co-Authored-By: [email protected] <[email protected]>

* test: add Given/When/Then sections to webhooks_test.go

Co-Authored-By: [email protected] <[email protected]>

* test: fix formatting and separate When/Then sections in test files

Co-Authored-By: [email protected] <[email protected]>

* test: fix error message assertions in test files

Co-Authored-By: [email protected] <[email protected]>

* test: apply gofmt to all test files

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: [email protected] <[email protected]>
2025-04-30 16:08:45 +02:00

268 lines
7.7 KiB
Go

package models_test
import (
"encoding/json"
"errors"
"math/big"
"testing"
"time"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPaymentInitiationReversalMarshalJSON(t *testing.T) {
t.Parallel()
now := time.Now().UTC()
connectorID := models.ConnectorID{
Provider: "test",
Reference: uuid.New(),
}
paymentInitiationID := models.PaymentInitiationID{
Reference: "pi123",
ConnectorID: connectorID,
}
reversalID := models.PaymentInitiationReversalID{
Reference: "rev123",
ConnectorID: connectorID,
}
reversal := models.PaymentInitiationReversal{
ID: reversalID,
ConnectorID: connectorID,
PaymentInitiationID: paymentInitiationID,
Reference: "rev123",
CreatedAt: now,
Description: "Test reversal",
Amount: big.NewInt(100),
Asset: "USD/2",
Metadata: map[string]string{
"key": "value",
},
}
data, err := json.Marshal(reversal)
// Then
require.NoError(t, err)
var jsonMap map[string]interface{}
err = json.Unmarshal(data, &jsonMap)
// Then
require.NoError(t, err)
assert.Equal(t, reversalID.String(), jsonMap["id"])
assert.Equal(t, connectorID.String(), jsonMap["connectorID"])
assert.Equal(t, paymentInitiationID.String(), jsonMap["paymentInitiationID"])
assert.Equal(t, "rev123", jsonMap["reference"])
assert.Equal(t, "Test reversal", jsonMap["description"])
assert.Equal(t, "USD/2", jsonMap["asset"])
assert.Equal(t, "value", jsonMap["metadata"].(map[string]interface{})["key"])
}
func TestPaymentInitiationReversalUnmarshalJSON(t *testing.T) {
t.Parallel()
now := time.Now().UTC()
connectorID := models.ConnectorID{
Provider: "test",
Reference: uuid.MustParse("00000000-0000-0000-0000-000000000001"),
}
paymentInitiationID := models.PaymentInitiationID{
Reference: "pi123",
ConnectorID: connectorID,
}
reversalID := models.PaymentInitiationReversalID{
Reference: "rev123",
ConnectorID: connectorID,
}
originalReversal := models.PaymentInitiationReversal{
ID: reversalID,
ConnectorID: connectorID,
PaymentInitiationID: paymentInitiationID,
Reference: "rev123",
CreatedAt: now,
Description: "Test reversal",
Amount: big.NewInt(100),
Asset: "USD/2",
Metadata: map[string]string{
"key": "value",
},
}
data, err := json.Marshal(originalReversal)
// Then
require.NoError(t, err)
var reversal models.PaymentInitiationReversal
err = json.Unmarshal(data, &reversal)
// Then
require.NoError(t, err)
assert.Equal(t, originalReversal.ID.String(), reversal.ID.String())
assert.Equal(t, originalReversal.ConnectorID.String(), reversal.ConnectorID.String())
assert.Equal(t, originalReversal.PaymentInitiationID.String(), reversal.PaymentInitiationID.String())
assert.Equal(t, originalReversal.Reference, reversal.Reference)
assert.Equal(t, originalReversal.CreatedAt.Format(time.RFC3339Nano), reversal.CreatedAt.Format(time.RFC3339Nano))
assert.Equal(t, originalReversal.Description, reversal.Description)
assert.Equal(t, originalReversal.Amount, reversal.Amount)
assert.Equal(t, originalReversal.Asset, reversal.Asset)
assert.Equal(t, originalReversal.Metadata["key"], reversal.Metadata["key"])
invalidJSON := `{
"id": "invalid-id",
"connectorID": "test:00000000-0000-0000-0000-000000000001",
"paymentInitiationID": "test:00000000-0000-0000-0000-000000000001/TRANSFER/pi123",
"reference": "rev123"
}`
err = json.Unmarshal([]byte(invalidJSON), &reversal)
// Then
assert.Error(t, err)
invalidJSON = `{
"id": "` + reversalID.String() + `",
"connectorID": "invalid-connector",
"paymentInitiationID": "` + paymentInitiationID.String() + `",
"reference": "rev123"
}`
err = json.Unmarshal([]byte(invalidJSON), &reversal)
// Then
assert.Error(t, err)
invalidJSON = `{
"id": "` + reversalID.String() + `",
"connectorID": "` + connectorID.String() + `",
"paymentInitiationID": "invalid-payment-initiation",
"reference": "rev123"
}`
err = json.Unmarshal([]byte(invalidJSON), &reversal)
// Then
assert.Error(t, err)
}
func TestFromPaymentInitiationReversalToPSPPaymentInitiationReversal(t *testing.T) {
t.Parallel()
now := time.Now().UTC()
connectorID := models.ConnectorID{
Provider: "test",
Reference: uuid.New(),
}
paymentInitiationID := models.PaymentInitiationID{
Reference: "pi123",
ConnectorID: connectorID,
}
reversalID := models.PaymentInitiationReversalID{
Reference: "rev123",
ConnectorID: connectorID,
}
reversal := models.PaymentInitiationReversal{
ID: reversalID,
ConnectorID: connectorID,
PaymentInitiationID: paymentInitiationID,
Reference: "rev123",
CreatedAt: now,
Description: "Test reversal",
Amount: big.NewInt(100),
Asset: "USD/2",
Metadata: map[string]string{
"key": "value",
},
}
relatedPI := models.PSPPaymentInitiation{
Reference: "pi123",
CreatedAt: now,
Amount: big.NewInt(1000),
Asset: "USD/2",
}
pspReversal := models.FromPaymentInitiationReversalToPSPPaymentInitiationReversal(&reversal, relatedPI)
assert.Equal(t, "rev123", pspReversal.Reference)
assert.Equal(t, now, pspReversal.CreatedAt)
assert.Equal(t, "Test reversal", pspReversal.Description)
assert.Equal(t, relatedPI, pspReversal.RelatedPaymentInitiation)
assert.Equal(t, big.NewInt(100), pspReversal.Amount)
assert.Equal(t, "USD/2", pspReversal.Asset)
assert.Equal(t, "value", pspReversal.Metadata["key"])
}
func TestPaymentInitiationReversalExpandedMarshalJSON(t *testing.T) {
t.Parallel()
now := time.Now().UTC()
connectorID := models.ConnectorID{
Provider: "test",
Reference: uuid.New(),
}
paymentInitiationID := models.PaymentInitiationID{
Reference: "pi123",
ConnectorID: connectorID,
}
reversalID := models.PaymentInitiationReversalID{
Reference: "rev123",
ConnectorID: connectorID,
}
reversal := models.PaymentInitiationReversal{
ID: reversalID,
ConnectorID: connectorID,
PaymentInitiationID: paymentInitiationID,
Reference: "rev123",
CreatedAt: now,
Description: "Test reversal",
Amount: big.NewInt(100),
Asset: "USD/2",
Metadata: map[string]string{
"key": "value",
},
}
expanded := models.PaymentInitiationReversalExpanded{
PaymentInitiationReversal: reversal,
Status: models.PAYMENT_INITIATION_REVERSAL_STATUS_PROCESSED,
Error: nil,
}
data, err := json.Marshal(expanded)
// Then
require.NoError(t, err)
var jsonMap map[string]interface{}
err = json.Unmarshal(data, &jsonMap)
// Then
require.NoError(t, err)
assert.Equal(t, reversalID.String(), jsonMap["id"])
assert.Equal(t, connectorID.String(), jsonMap["connectorID"])
assert.Equal(t, paymentInitiationID.String(), jsonMap["paymentInitiationID"])
assert.Equal(t, "rev123", jsonMap["reference"])
assert.Equal(t, "Test reversal", jsonMap["description"])
assert.Equal(t, "USD/2", jsonMap["asset"])
assert.Equal(t, "value", jsonMap["metadata"].(map[string]interface{})["key"])
assert.Equal(t, "PROCESSED", jsonMap["status"])
assert.Nil(t, jsonMap["error"])
expanded = models.PaymentInitiationReversalExpanded{
PaymentInitiationReversal: reversal,
Status: models.PAYMENT_INITIATION_REVERSAL_STATUS_FAILED,
Error: errors.New("test error"),
}
data, err = json.Marshal(expanded)
// Then
require.NoError(t, err)
err = json.Unmarshal(data, &jsonMap)
// Then
require.NoError(t, err)
assert.Equal(t, "FAILED", jsonMap["status"])
assert.Equal(t, "test error", jsonMap["error"])
}