From 897d7f9a81c557e33cf56d8755e8ed198686ae57 Mon Sep 17 00:00:00 2001 From: Crimson Thompson Date: Tue, 8 Oct 2024 16:34:19 +0200 Subject: [PATCH] feat(payments): add wise uninstall tests --- .../plugins/public/wise/client/client.go | 4 +- .../public/wise/client/client_generated.go | 8 +-- .../plugins/public/wise/client/webhooks.go | 35 +++++---- .../connectors/plugins/public/wise/plugin.go | 4 ++ .../plugins/public/wise/uninstall_test.go | 71 +++++++++++++++++++ 5 files changed, 98 insertions(+), 24 deletions(-) create mode 100644 internal/connectors/plugins/public/wise/uninstall_test.go diff --git a/internal/connectors/plugins/public/wise/client/client.go b/internal/connectors/plugins/public/wise/client/client.go index 67693f92..59b5d518 100644 --- a/internal/connectors/plugins/public/wise/client/client.go +++ b/internal/connectors/plugins/public/wise/client/client.go @@ -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) diff --git a/internal/connectors/plugins/public/wise/client/client_generated.go b/internal/connectors/plugins/public/wise/client/client_generated.go index 46020c97..5ade0073 100644 --- a/internal/connectors/plugins/public/wise/client/client_generated.go +++ b/internal/connectors/plugins/public/wise/client/client_generated.go @@ -86,10 +86,10 @@ func (mr *MockClientMockRecorder) CreateTransfer(ctx, quote, targetAccount, tran } // CreateWebhook mocks base method. -func (m *MockClient) CreateWebhook(ctx context.Context, profileID uint64, name, triggerOn, url, version string) (*webhookSubscriptionResponse, error) { +func (m *MockClient) CreateWebhook(ctx context.Context, profileID uint64, name, triggerOn, url, version string) (*WebhookSubscriptionResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "CreateWebhook", ctx, profileID, name, triggerOn, url, version) - ret0, _ := ret[0].(*webhookSubscriptionResponse) + ret0, _ := ret[0].(*WebhookSubscriptionResponse) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -235,10 +235,10 @@ func (mr *MockClientMockRecorder) GetTransfers(ctx, profileID, offset, limit any } // ListWebhooksSubscription mocks base method. -func (m *MockClient) ListWebhooksSubscription(ctx context.Context, profileID uint64) ([]webhookSubscriptionResponse, error) { +func (m *MockClient) ListWebhooksSubscription(ctx context.Context, profileID uint64) ([]WebhookSubscriptionResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ListWebhooksSubscription", ctx, profileID) - ret0, _ := ret[0].([]webhookSubscriptionResponse) + ret0, _ := ret[0].([]WebhookSubscriptionResponse) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/internal/connectors/plugins/public/wise/client/webhooks.go b/internal/connectors/plugins/public/wise/client/webhooks.go index 75633ff6..b5e45630 100644 --- a/internal/connectors/plugins/public/wise/client/webhooks.go +++ b/internal/connectors/plugins/public/wise/client/webhooks.go @@ -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"` @@ -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, @@ -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 { @@ -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 { diff --git a/internal/connectors/plugins/public/wise/plugin.go b/internal/connectors/plugins/public/wise/plugin.go index 360e2c73..8875d0a9 100644 --- a/internal/connectors/plugins/public/wise/plugin.go +++ b/internal/connectors/plugins/public/wise/plugin.go @@ -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{} diff --git a/internal/connectors/plugins/public/wise/uninstall_test.go b/internal/connectors/plugins/public/wise/uninstall_test.go new file mode 100644 index 00000000..edfa22bf --- /dev/null +++ b/internal/connectors/plugins/public/wise/uninstall_test.go @@ -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()) + }) + }) +})