Files
Paul NicolasandGitHub 04517d36e0 fix(connectors): fix webhook idempotency key and tests (#445)
* feat(workflows): handle webhook idempotency in a different way

* feat(adyen): add verify webhook and fix tests

* fix(atlar): ensure plugin functions are tested

* fix(bankingcircle): ensure plugin functions are tested

* feat(column): add verify webhook method and fix tests

* fix(currencycloud): ensure plugin functions are tested

* fix(generic): ensure plugin functions are tested

* feat(mangopay): add verify webhook method and fix tests

* fix(modulr): ensure plugin functions are tested

* fix(moneycorp): ensure plugin functions are tested

* fix(stripe): ensure plugin functions are tested

* feat(wise): add verify webhook method and fix tests

* fix dirty

* fix postgres idempotency key

* fix wrong go libs version

* fix coderabbitai

* webhooks failing verification should stop temporal workflow
2025-05-14 15:27:14 +00:00

207 lines
5.5 KiB
Go

package column
import (
"errors"
"net/http"
"net/http/httptest"
"time"
"github.com/formancehq/payments/internal/connectors/plugins/public/column/client"
"github.com/formancehq/payments/internal/models"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
)
var _ = Describe("Column Plugin Uninstall", func() {
var (
plg models.Plugin
ctrl *gomock.Controller
mockHTTPClient *client.MockHTTPClient
now time.Time
ts *httptest.Server
)
BeforeEach(func() {
now = time.Now().UTC()
ctrl = gomock.NewController(GinkgoT())
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"webhook_urls": []}`))
Expect(err).To(BeNil())
}))
mockHTTPClient = client.NewMockHTTPClient(ctrl)
c := client.New("test", "aseplye", ts.URL)
c.SetHttpClient(mockHTTPClient)
plg = &Plugin{
client: c,
}
})
AfterEach(func() {
ctrl.Finish()
})
Context("uninstalling connector", func() {
It("should handle empty webhooks list", func(ctx SpecContext) {
mockHTTPClient.EXPECT().Do(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(
200,
nil,
).SetArg(
2,
client.ListWebhookResponseWrapper[[]*client.EventSubscription]{
WebhookEndpoints: []*client.EventSubscription{},
},
)
resp, err := plg.Uninstall(ctx, models.UninstallRequest{
ConnectorID: "test-connector",
})
Expect(err).To(BeNil())
Expect(resp).To(Equal(models.UninstallResponse{}))
})
It("should handle webhook list event error", func(ctx SpecContext) {
mockHTTPClient.EXPECT().Do(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(
500,
errors.New("list failed"),
)
resp, err := plg.Uninstall(ctx, models.UninstallRequest{
ConnectorID: "test-connector",
})
Expect(err).To(MatchError("failed to list web hooks: list failed : "))
Expect(resp).To(Equal(models.UninstallResponse{}))
})
It("should handle webhook deletion error", func(ctx SpecContext) {
mockHTTPClient.EXPECT().Do(
gomock.Any(),
NewRequestMatcher("limit=100"),
gomock.Any(),
gomock.Any(),
).Return(
200,
nil,
).SetArg(
2,
client.ListWebhookResponseWrapper[[]*client.EventSubscription]{
WebhookEndpoints: []*client.EventSubscription{
{
ID: "webhook-2",
URL: ts.URL + "/test-connector/webhook",
CreatedAt: now.Add(-time.Duration(5) * time.Minute).UTC().Format(time.RFC3339),
UpdatedAt: now.Add(-time.Duration(5) * time.Minute).UTC().Format(time.RFC3339),
Description: "description",
EnabledEvents: []string{"book.transfer.completed"},
Secret: "secret",
IsDisabled: false,
},
},
},
)
mockHTTPClient.EXPECT().Do(
gomock.Any(),
NewRequestMatcher("limit=100&starting_after=webhook-2"),
gomock.Any(),
gomock.Any(),
).Return(
200,
nil,
).SetArg(
2,
client.ListWebhookResponseWrapper[[]*client.EventSubscription]{
WebhookEndpoints: []*client.EventSubscription{},
},
)
mockHTTPClient.EXPECT().Do(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(
200,
errors.New("deletion failed"),
)
resp, err := plg.Uninstall(ctx, models.UninstallRequest{
ConnectorID: "test-connector",
})
Expect(err).To(MatchError("failed to delete web hooks: deletion failed : "))
Expect(resp).To(Equal(models.UninstallResponse{}))
})
It("should successfully uninstall and delete webhooks", func(ctx SpecContext) {
mockHTTPClient.EXPECT().Do(
gomock.Any(),
NewRequestMatcher("limit=100"),
gomock.Any(),
gomock.Any(),
).Return(
200,
nil,
).SetArg(2, client.ListWebhookResponseWrapper[[]*client.EventSubscription]{
WebhookEndpoints: []*client.EventSubscription{
{
ID: "webhook-1",
URL: "https://example.com/test-connector/webhook",
CreatedAt: now.Add(-time.Duration(5) * time.Minute).UTC().Format(time.RFC3339),
UpdatedAt: now.Add(-time.Duration(5) * time.Minute).UTC().Format(time.RFC3339),
Description: "description",
EnabledEvents: []string{"book.transfer.completed"},
Secret: "secret",
IsDisabled: false,
},
{
ID: "webhook-2",
URL: "https://example.com/other-connector/webhook",
CreatedAt: now.Add(-time.Duration(5) * time.Minute).UTC().Format(time.RFC3339),
UpdatedAt: now.Add(-time.Duration(5) * time.Minute).UTC().Format(time.RFC3339),
Description: "description",
EnabledEvents: []string{"book.transfer.completed"},
Secret: "secret",
IsDisabled: false,
},
},
})
mockHTTPClient.EXPECT().Do(
gomock.Any(),
NewRequestMatcher("limit=100&starting_after=webhook-2"),
gomock.Any(),
gomock.Any(),
).Return(
200,
nil,
).SetArg(2, client.ListWebhookResponseWrapper[[]*client.EventSubscription]{
WebhookEndpoints: []*client.EventSubscription{},
})
mockHTTPClient.EXPECT().Do(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(
200,
nil,
)
resp, err := plg.Uninstall(ctx, models.UninstallRequest{
ConnectorID: "test-connector",
})
Expect(err).To(BeNil())
Expect(resp).To(Equal(models.UninstallResponse{}))
})
})
})