Skip to content

Commit

Permalink
feat(payments): add unit test for wise accounts and balances methods
Browse files Browse the repository at this point in the history
  • Loading branch information
laouji committed Oct 8, 2024
1 parent 897d7f9 commit 9dab70b
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/connectors/plugins/public/wise/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p *Plugin) fetchNextAccounts(ctx context.Context, req models.FetchNextAcco
Name: &balance.Name,
DefaultAsset: pointer.For(currency.FormatAsset(supportedCurrenciesWithDecimal, balance.Amount.Currency)),
Metadata: map[string]string{
metadataProfileIDKey: strconv.FormatUint(from.ID, 10),
MetadataProfileIDKey: strconv.FormatUint(from.ID, 10),
},
Raw: raw,
})
Expand Down
68 changes: 68 additions & 0 deletions internal/connectors/plugins/public/wise/accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package wise

import (
"encoding/json"
"fmt"

"github.com/formancehq/payments/internal/connectors/plugins/public/wise/client"
"github.com/formancehq/payments/internal/models"
"go.uber.org/mock/gomock"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Wise Plugin Accounts", func() {
var (
plg *Plugin
m *client.MockClient
)

BeforeEach(func() {
plg = &Plugin{}

ctrl := gomock.NewController(GinkgoT())
m = client.NewMockClient(ctrl)
plg.SetClient(m)
})

Context("fetch next accounts", func() {
var (
balances []client.Balance
expectedProfileID uint64
)

BeforeEach(func() {
expectedProfileID = 123454
balances = []client.Balance{
{ID: 14556, Type: "type1"},
{ID: 3334, Type: "type2"},
}
})

It("fetches accounts from wise", func(ctx SpecContext) {
req := models.FetchNextAccountsRequest{
State: json.RawMessage(`{}`),
FromPayload: json.RawMessage(fmt.Sprintf(`{"ID":%d}`, expectedProfileID)),
PageSize: len(balances),
}
m.EXPECT().GetBalances(ctx, expectedProfileID).Return(
balances,
nil,
)

res, err := plg.FetchNextAccounts(ctx, req)
Expect(err).To(BeNil())
Expect(res.HasMore).To(BeTrue())
Expect(res.Accounts).To(HaveLen(req.PageSize))
Expect(res.Accounts[0].Reference).To(Equal(fmt.Sprint(balances[0].ID)))
Expect(res.Accounts[1].Reference).To(Equal(fmt.Sprint(balances[1].ID)))

var state accountsState

err = json.Unmarshal(res.NewState, &state)
Expect(err).To(BeNil())
Expect(fmt.Sprint(state.LastAccountID)).To(Equal(res.Accounts[len(res.Accounts)-1].Reference))
})
})
})
2 changes: 1 addition & 1 deletion internal/connectors/plugins/public/wise/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (p *Plugin) fetchNextBalances(ctx context.Context, req models.FetchNextBala
return models.FetchNextBalancesResponse{}, err
}

pID, ok := from.Metadata[metadataProfileIDKey]
pID, ok := from.Metadata[MetadataProfileIDKey]
if !ok {
return models.FetchNextBalancesResponse{}, errors.New("missing profile ID in from payload when fetching balances")
}
Expand Down
73 changes: 73 additions & 0 deletions internal/connectors/plugins/public/wise/balances_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package wise

import (
"encoding/json"
"fmt"
"math/big"

"github.com/formancehq/payments/internal/connectors/plugins/public/wise/client"
"github.com/formancehq/payments/internal/models"
"go.uber.org/mock/gomock"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Wise Plugin Balances", func() {
var (
plg *Plugin
m *client.MockClient
)

BeforeEach(func() {
plg = &Plugin{}

ctrl := gomock.NewController(GinkgoT())
m = client.NewMockClient(ctrl)
plg.SetClient(m)
})

Context("fetch next balances", func() {
var (
balance client.Balance
expectedProfileID uint64
profileVal uint64
)

BeforeEach(func() {
expectedProfileID = 123454
profileVal = 999999
balance = client.Balance{
ID: 14556,
Type: "type1",
Amount: client.BalanceAmount{Value: json.Number("44.99"), Currency: "USD"},
}
})

It("fetches balances from wise client", func(ctx SpecContext) {
req := models.FetchNextBalancesRequest{
State: json.RawMessage(`{}`),
FromPayload: json.RawMessage(fmt.Sprintf(
`{"Reference":"%d","Metadata":{"%s":"%d"}}`,
expectedProfileID,
MetadataProfileIDKey,
profileVal,
)),
PageSize: 10,
}
m.EXPECT().GetBalance(ctx, profileVal, expectedProfileID).Return(
&balance,
nil,
)

res, err := plg.FetchNextBalances(ctx, req)
Expect(err).To(BeNil())
Expect(res.HasMore).To(BeFalse())
Expect(res.Balances).To(HaveLen(1)) // always returns 1
Expect(res.Balances[0].AccountReference).To(Equal(fmt.Sprint(expectedProfileID)))
expectedBalance, err := balance.Amount.Value.Float64()
Expect(err).To(BeNil())
Expect(res.Balances[0].Amount).To(BeEquivalentTo(big.NewInt(int64(expectedBalance * 100))))
})
})
})
18 changes: 10 additions & 8 deletions internal/connectors/plugins/public/wise/client/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"time"
)

type BalanceAmount struct {
Value json.Number `json:"value"`
Currency string `json:"currency"`
}

type Balance struct {
ID uint64 `json:"id"`
Currency string `json:"currency"`
Type string `json:"type"`
Name string `json:"name"`
Amount struct {
Value json.Number `json:"value"`
Currency string `json:"currency"`
} `json:"amount"`
ID uint64 `json:"id"`
Currency string `json:"currency"`
Type string `json:"type"`
Name string `json:"name"`
Amount BalanceAmount `json:"amount"`
ReservedAmount struct {
Value json.Number `json:"value"`
Currency string `json:"currency"`
Expand Down
2 changes: 1 addition & 1 deletion internal/connectors/plugins/public/wise/metadata.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package wise

const (
metadataProfileIDKey = "profile_id"
MetadataProfileIDKey = "profile_id"
)

0 comments on commit 9dab70b

Please sign in to comment.