Skip to content

Commit

Permalink
feat(payments): add wise uninstall tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laouji committed Oct 8, 2024
1 parent 04f9f6d commit 897d7f9
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 24 deletions.
4 changes: 2 additions & 2 deletions internal/connectors/plugins/public/wise/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type Client interface {
GetTransfers(ctx context.Context, profileID uint64, offset int, limit int) ([]Transfer, error)
GetTransfer(ctx context.Context, transferID string) (*Transfer, error)
CreateTransfer(ctx context.Context, quote Quote, targetAccount uint64, transactionID string) (*Transfer, error)
CreateWebhook(ctx context.Context, profileID uint64, name, triggerOn, url, version string) (*webhookSubscriptionResponse, error)
ListWebhooksSubscription(ctx context.Context, profileID uint64) ([]webhookSubscriptionResponse, error)
CreateWebhook(ctx context.Context, profileID uint64, name, triggerOn, url, version string) (*WebhookSubscriptionResponse, error)
ListWebhooksSubscription(ctx context.Context, profileID uint64) ([]WebhookSubscriptionResponse, error)
DeleteWebhooks(ctx context.Context, profileID uint64, subscriptionID string) error
TranslateTransferStateChangedWebhook(ctx context.Context, payload []byte) (Transfer, error)
TranslateBalanceUpdateWebhook(ctx context.Context, payload []byte) (balanceUpdateWebhookPayload, error)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 17 additions & 18 deletions internal/connectors/plugins/public/wise/client/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ import (
"time"
)

type WebhookDelivery struct {
Version string `json:"version"`
URL string `json:"url"`
}

type webhookSubscription struct {
Name string `json:"name"`
TriggerOn string `json:"trigger_on"`
Delivery struct {
Version string `json:"version"`
URL string `json:"url"`
} `json:"delivery"`
Name string `json:"name"`
TriggerOn string `json:"trigger_on"`
Delivery WebhookDelivery `json:"delivery"`
}

type webhookSubscriptionResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Delivery struct {
Version string `json:"version"`
URL string `json:"url"`
} `json:"delivery"`
TriggerOn string `json:"trigger_on"`
type WebhookSubscriptionResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Delivery WebhookDelivery `json:"delivery"`
TriggerOn string `json:"trigger_on"`
Scope struct {
Domain string `json:"domain"`
} `json:"scope"`
Expand All @@ -36,7 +35,7 @@ type webhookSubscriptionResponse struct {
CreatedAt string `json:"created_at"`
}

func (c *client) CreateWebhook(ctx context.Context, profileID uint64, name, triggerOn, url, version string) (*webhookSubscriptionResponse, error) {
func (c *client) CreateWebhook(ctx context.Context, profileID uint64, name, triggerOn, url, version string) (*WebhookSubscriptionResponse, error) {
reqBody, err := json.Marshal(webhookSubscription{
Name: name,
TriggerOn: triggerOn,
Expand All @@ -61,7 +60,7 @@ func (c *client) CreateWebhook(ctx context.Context, profileID uint64, name, trig
}
req.Header.Set("Content-Type", "application/json")

var res webhookSubscriptionResponse
var res WebhookSubscriptionResponse
var errRes wiseErrors
statusCode, err := c.httpClient.Do(req, &res, &errRes)
if err != nil {
Expand All @@ -70,14 +69,14 @@ func (c *client) CreateWebhook(ctx context.Context, profileID uint64, name, trig
return &res, nil
}

func (c *client) ListWebhooksSubscription(ctx context.Context, profileID uint64) ([]webhookSubscriptionResponse, error) {
func (c *client) ListWebhooksSubscription(ctx context.Context, profileID uint64) ([]WebhookSubscriptionResponse, error) {
req, err := http.NewRequestWithContext(ctx,
http.MethodGet, c.endpoint(fmt.Sprintf("/v3/profiles/%d/subscriptions", profileID)), http.NoBody)
if err != nil {
return nil, err
}

var res []webhookSubscriptionResponse
var res []WebhookSubscriptionResponse
var errRes wiseErrors
statusCode, err := c.httpClient.Do(req, &res, &errRes)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/connectors/plugins/public/wise/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,8 @@ func (p *Plugin) TranslateWebhook(ctx context.Context, req models.TranslateWebho
}, nil
}

func (p *Plugin) SetClient(cl client.Client) {
p.client = cl
}

var _ models.Plugin = &Plugin{}
71 changes: 71 additions & 0 deletions internal/connectors/plugins/public/wise/uninstall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package wise

import (
"fmt"

"github.com/formancehq/payments/internal/connectors/plugins/public/wise/client"
"github.com/formancehq/payments/internal/models"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
)

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

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

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

Context("uninstall", func() {
var (
profiles []client.Profile
expectedWebhookID = "webhook1"
expectedWebhookID2 = "webhook2"
)

BeforeEach(func() {
profiles = []client.Profile{
{ID: 1, Type: "type1"},
{ID: 2, Type: "type2"},
}
})

It("deletes webhooks related to accounts", func(ctx SpecContext) {
req := models.UninstallRequest{ConnectorID: "dummyID"}
m.EXPECT().GetProfiles(ctx).Return(
profiles,
nil,
)
m.EXPECT().ListWebhooksSubscription(ctx, profiles[0].ID).Return(
[]client.WebhookSubscriptionResponse{
{ID: expectedWebhookID, Delivery: client.WebhookDelivery{
URL: fmt.Sprintf("http://somesite.fr/%s", req.ConnectorID),
}},
{ID: "skipped", Delivery: client.WebhookDelivery{URL: "http://somesite.fr"}},
},
nil,
)
m.EXPECT().ListWebhooksSubscription(ctx, profiles[1].ID).Return(
[]client.WebhookSubscriptionResponse{
{ID: expectedWebhookID2, Delivery: client.WebhookDelivery{
URL: fmt.Sprintf("http://%s.somesite.com", req.ConnectorID),
}},
},
nil,
)
m.EXPECT().DeleteWebhooks(ctx, profiles[0].ID, expectedWebhookID).Return(nil)
m.EXPECT().DeleteWebhooks(ctx, profiles[1].ID, expectedWebhookID2).Return(nil)

_, err := plg.Uninstall(ctx, req)
Expect(err).To(BeNil())
})
})
})

0 comments on commit 897d7f9

Please sign in to comment.