-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
219 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tests/integration/suite/webhooks-ledger-committed-transaction-collector.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package suite | ||
|
||
import ( | ||
"math/big" | ||
"net/http" | ||
"net/http/httptest" | ||
"time" | ||
"github.com/formancehq/stack/tests/integration/internal/modules" | ||
|
||
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations" | ||
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared" | ||
. "github.com/formancehq/stack/tests/integration/internal" | ||
webhooks "github.com/formancehq/webhooks/pkg/utils" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = WithModules([]*Module{modules.Ledger, modules.Webhooks}, func() { | ||
BeforeEach(func() { | ||
createLedgerResponse, err := Client().Ledger.V2CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ | ||
Ledger: "default", | ||
}) | ||
Expect(err).To(BeNil()) | ||
Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) | ||
}) | ||
var ( | ||
httpServer *httptest.Server | ||
called chan struct{} | ||
secret = webhooks.NewSecret() | ||
count int | ||
) | ||
|
||
BeforeEach(func() { | ||
called = make(chan struct{}) | ||
count = 0 | ||
httpServer = httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
count += 1 | ||
|
||
if count == 1 { | ||
// FOR THE WORKER | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte("401 Unauthorized")) | ||
} else if count == 2 { | ||
// FOR THE COLLECTOR | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("200 OK")) | ||
defer close(called) | ||
} | ||
|
||
})) | ||
DeferCleanup(func() { | ||
httpServer.Close() | ||
}) | ||
|
||
response, err := Client().Webhooks.InsertConfig( | ||
TestContext(), | ||
shared.ConfigUser{ | ||
Endpoint: httpServer.URL, | ||
Secret: &secret, | ||
EventTypes: []string{ | ||
"ledger.committed_transactions", | ||
}, | ||
}, | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(response.StatusCode).To(Equal(http.StatusOK)) | ||
|
||
}) | ||
|
||
When("creating a transaction", func() { | ||
|
||
BeforeEach(func() { | ||
time.Sleep(1*time.Second) | ||
response, err := Client().Ledger.V2CreateTransaction( | ||
TestContext(), | ||
operations.V2CreateTransactionRequest{ | ||
V2PostTransaction: shared.V2PostTransaction{ | ||
Metadata: map[string]string{}, | ||
Postings: []shared.V2Posting{ | ||
{ | ||
Amount: big.NewInt(100), | ||
Asset: "USD", | ||
Source: "world", | ||
Destination: "alice", | ||
}, | ||
}, | ||
}, | ||
Ledger: "default", | ||
}, | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(response.StatusCode).To(Equal(http.StatusOK)) | ||
}) | ||
|
||
It("should trigger a call to the webhook endpoint", func() { | ||
Eventually(ChanClosed(called)).Should(BeTrue()) | ||
}) | ||
}) | ||
}) |
File renamed without changes.
110 changes: 110 additions & 0 deletions
110
tests/integration/suite/webhooks-v2-ledger-committed-transaction-collector.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package suite | ||
|
||
import ( | ||
"math/big" | ||
"net/http" | ||
"net/http/httptest" | ||
"time" | ||
|
||
"github.com/formancehq/stack/tests/integration/internal/modules" | ||
|
||
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations" | ||
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared" | ||
. "github.com/formancehq/stack/tests/integration/internal" | ||
webhooks "github.com/formancehq/webhooks/pkg/utils" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = WithModules([]*Module{modules.Ledger, modules.Webhooks}, func() { | ||
BeforeEach(func() { | ||
createLedgerResponse, err := Client().Ledger.V2CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ | ||
Ledger: "default", | ||
}) | ||
Expect(err).To(BeNil()) | ||
Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) | ||
}) | ||
var ( | ||
httpServer *httptest.Server | ||
called chan struct{} | ||
secret = webhooks.NewSecret() | ||
hook1 shared.V2Hook | ||
count int | ||
) | ||
|
||
BeforeEach(func() { | ||
called = make(chan struct{}) | ||
count = 0 | ||
httpServer = httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
count += 1 | ||
|
||
if count == 1 { | ||
// FOR THE WORKER | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte("401 Unauthorized")) | ||
} else if count == 2 { | ||
// FOR THE COLLECTOR | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("200 OK")) | ||
defer close(called) | ||
} | ||
})) | ||
DeferCleanup(func() { | ||
httpServer.Close() | ||
}) | ||
|
||
|
||
|
||
response, err := Client().Webhooks.InsertHook( | ||
TestContext(), | ||
shared.V2HookBodyParams{ | ||
Endpoint: httpServer.URL, | ||
Secret: &secret, | ||
Events: []string{ | ||
"ledger.committed_transactions", | ||
}, | ||
}, | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(response.StatusCode).To(Equal(http.StatusOK)) | ||
hook1 = response.V2HookResponse.Data | ||
_, err = Client().Webhooks.ActivateHook( | ||
TestContext(), | ||
operations.ActivateHookRequest{ | ||
HookID: hook1.ID, | ||
}, | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
}) | ||
|
||
When("creating a transaction", func() { | ||
BeforeEach(func() { | ||
time.Sleep(1*time.Second) | ||
response, err := Client().Ledger.V2CreateTransaction( | ||
TestContext(), | ||
operations.V2CreateTransactionRequest{ | ||
V2PostTransaction: shared.V2PostTransaction{ | ||
Metadata: map[string]string{}, | ||
Postings: []shared.V2Posting{ | ||
{ | ||
Amount: big.NewInt(100), | ||
Asset: "USD", | ||
Source: "world", | ||
Destination: "alice", | ||
}, | ||
}, | ||
}, | ||
Ledger: "default", | ||
}, | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(response.StatusCode).To(Equal(http.StatusOK)) | ||
}) | ||
|
||
It("should trigger a call to the webhook endpoint", func() { | ||
Eventually(ChanClosed(called)).Should(BeTrue()) | ||
}) | ||
}) | ||
}) |
File renamed without changes.