Files
payments/internal/api/v2/handler_payments_create_test.go
f420ffbe54 chore: rename adjustement to adjustment sdk generation (#521)
* chore: rename 'adjustement' to 'adjustment'

* chore: rename more 'adjustement' and also redundant 'status'

* chore: regenerate openapi files

* chore: remove all typos referencing 'adjusments'

* chore: fix some typos in comment, allow for backward compatibility

* chore: improve backward comp

* chore: also rename file names

* chore: drop change in payment_status enum to keep backward compatibility

* chore: also revert openapi + sdk

---------

Co-authored-by: Alecz <[email protected]>
Co-authored-by: fabrice guery <[email protected]>
2025-08-29 15:29:20 +00:00

119 lines
4.8 KiB
Go

package v2
import (
"errors"
"math/big"
"net/http"
"net/http/httptest"
"time"
"github.com/formancehq/payments/internal/connectors/engine"
"github.com/formancehq/payments/internal/api/backend"
"github.com/formancehq/payments/internal/api/validation"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
"go.uber.org/mock/gomock"
)
var _ = Describe("API v2 Payments Create", func() {
var (
handlerFn http.HandlerFunc
connID models.ConnectorID
)
BeforeEach(func() {
connID = models.ConnectorID{Reference: uuid.New(), Provider: "psp"}
})
Context("create payments", func() {
var (
w *httptest.ResponseRecorder
m *backend.MockBackend
cpr CreatePaymentRequest
)
BeforeEach(func() {
w = httptest.NewRecorder()
ctrl := gomock.NewController(GinkgoT())
m = backend.NewMockBackend(ctrl)
handlerFn = paymentsCreate(m, validation.NewValidator())
})
It("should return a bad request error when body is missing", func(ctx SpecContext) {
req := httptest.NewRequest(http.MethodPost, "/", nil)
handlerFn(w, req)
assertExpectedResponse(w.Result(), http.StatusBadRequest, ErrMissingOrInvalidBody)
})
It("should return a bad request error when connector is not able to create payments", func(ctx SpecContext) {
notSupportedConnectorID := models.ConnectorID{Reference: uuid.New(), Provider: "stripe"}
expectedErr := &engine.ErrConnectorCapabilityNotSupported{Capability: "CreateFormancePayment", Provider: notSupportedConnectorID.Provider}
m.EXPECT().PaymentsCreate(gomock.Any(), gomock.Any()).Return(expectedErr)
cpr = CreatePaymentRequest{
Reference: "reference-err",
ConnectorID: notSupportedConnectorID.String(),
CreatedAt: time.Now(),
Amount: big.NewInt(3500),
Asset: "JPY/0",
Status: models.PAYMENT_STATUS_AMOUNT_ADJUSTMENT.String(),
Type: models.PAYMENT_TYPE_PAYIN.String(),
Scheme: models.PAYMENT_SCHEME_CARD_AMEX.String(),
}
handlerFn(w, prepareJSONRequest(http.MethodPost, &cpr))
assertExpectedResponse(w.Result(), http.StatusBadRequest, ErrConnectorCapabilityNotSupported)
})
DescribeTable("validation errors",
func(cpr CreatePaymentRequest) {
handlerFn(w, prepareJSONRequest(http.MethodPost, &cpr))
assertExpectedResponse(w.Result(), http.StatusBadRequest, ErrValidation)
},
Entry("reference missing", CreatePaymentRequest{}),
Entry("connectorID missing", CreatePaymentRequest{Reference: "ref"}),
Entry("createdAt missing", CreatePaymentRequest{Reference: "ref", ConnectorID: "id"}),
Entry("amount missing", CreatePaymentRequest{Reference: "ref", ConnectorID: "id", CreatedAt: time.Now()}),
Entry("payment type missing", CreatePaymentRequest{Reference: "ref", ConnectorID: "id", CreatedAt: time.Now(), Amount: big.NewInt(4467)}),
Entry("payment type invalid", CreatePaymentRequest{Reference: "ref", ConnectorID: "id", CreatedAt: time.Now(), Amount: big.NewInt(4467), Type: "invalid"}),
Entry("scheme missing", CreatePaymentRequest{Reference: "ref", ConnectorID: "id", CreatedAt: time.Now(), Amount: big.NewInt(4467), Type: "PAYOUT"}),
Entry("scheme invalid", CreatePaymentRequest{Reference: "ref", ConnectorID: "id", CreatedAt: time.Now(), Amount: big.NewInt(4467), Type: "PAYOUT", Scheme: "invalid"}),
Entry("asset missing", CreatePaymentRequest{Reference: "ref", ConnectorID: "id", CreatedAt: time.Now(), Amount: big.NewInt(4467), Type: "PAYOUT", Scheme: "CARD_VISA"}),
)
It("should return an internal server error when backend returns error", func(ctx SpecContext) {
expectedErr := errors.New("payment create err")
m.EXPECT().PaymentsCreate(gomock.Any(), gomock.Any()).Return(expectedErr)
cpr = CreatePaymentRequest{
Reference: "reference-err",
ConnectorID: connID.String(),
CreatedAt: time.Now(),
Amount: big.NewInt(3500),
Asset: "JPY/0",
Status: models.PAYMENT_STATUS_AMOUNT_ADJUSTMENT.String(),
Type: models.PAYMENT_TYPE_PAYIN.String(),
Scheme: models.PAYMENT_SCHEME_CARD_AMEX.String(),
}
handlerFn(w, prepareJSONRequest(http.MethodPost, &cpr))
assertExpectedResponse(w.Result(), http.StatusInternalServerError, "INTERNAL")
})
It("should return status OK on success", func(ctx SpecContext) {
m.EXPECT().PaymentsCreate(gomock.Any(), gomock.Any()).Return(nil)
cpr = CreatePaymentRequest{
Reference: "reference-ok",
ConnectorID: connID.String(),
CreatedAt: time.Now(),
Amount: big.NewInt(3500),
Asset: "JPY/0",
Status: models.PAYMENT_STATUS_AMOUNT_ADJUSTMENT.String(),
Type: models.PAYMENT_TYPE_PAYIN.String(),
Scheme: models.PAYMENT_SCHEME_CARD_AMEX.String(),
}
handlerFn(w, prepareJSONRequest(http.MethodPost, &cpr))
assertExpectedResponse(w.Result(), http.StatusOK, "data")
})
})
})