Files
payments/internal/api/v2/handler_pools_balances_latest_test.go
CrimsonandGitHub 5bc95257a2 feat(pools): Add new endpoint that returns latest pool balance (#442)
* Add method to fetch latest balance from storage

* Add functionality to fetch latest pool balance if at value is current time

* Add new API endpoint for fetching latest pool balances

* Generate openapi specs for new latest balances endpoint

* Add e2e test for new pool balances endpoint
2025-05-14 14:57:07 +00:00

69 lines
1.8 KiB
Go

package v2
import (
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"github.com/formancehq/payments/internal/api/backend"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
"go.uber.org/mock/gomock"
)
var _ = Describe("API v2 Pools Balances Latest", func() {
var (
handlerFn http.HandlerFunc
poolID uuid.UUID
)
BeforeEach(func() {
poolID = uuid.New()
})
Context("pools balances latest", func() {
var (
w *httptest.ResponseRecorder
m *backend.MockBackend
)
BeforeEach(func() {
w = httptest.NewRecorder()
ctrl := gomock.NewController(GinkgoT())
m = backend.NewMockBackend(ctrl)
handlerFn = poolsBalancesLatest(m)
})
It("should return a validation request error when poolID is invalid", func(ctx SpecContext) {
req := prepareQueryRequestWithPath("/", "poolID", "invalidvalue")
handlerFn(w, req)
assertExpectedResponse(w.Result(), http.StatusBadRequest, ErrInvalidID)
})
It("should return an internal server error when backend returns error", func(ctx SpecContext) {
req := prepareQueryRequestWithPath("/", "poolID", poolID.String())
m.EXPECT().PoolsBalances(gomock.Any(), gomock.Any()).Return(
[]models.AggregatedBalance{},
fmt.Errorf("balances list error"),
)
handlerFn(w, req)
assertExpectedResponse(w.Result(), http.StatusInternalServerError, "INTERNAL")
})
It("should return a data object", func(ctx SpecContext) {
req := prepareQueryRequestWithPath("/", "poolID", poolID.String())
m.EXPECT().PoolsBalances(gomock.Any(), poolID).Return(
[]models.AggregatedBalance{
models.AggregatedBalance{Asset: "EUR/2", Amount: big.NewInt(1)},
},
nil,
)
handlerFn(w, req)
assertExpectedResponse(w.Result(), http.StatusOK, "data")
})
})
})