8000 test: (e2e) implement pools e2e tests and stop using pools idempotency key as workflowID by laouji · Pull Request #199 · formancehq/payments · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

test: (e2e) implement pools e2e tests and stop using pools idempotency key as workflowID #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions internal/api/v2/handler_pools_add_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"go.opentelemetry.io/otel/attribute"
)

type poolsAddAccountRequest struct {
type PoolsAddAccountRequest struct {
AccountID string `json:"accountID"`
}

func (c *poolsAddAccountRequest) Validate() error {
func (c *PoolsAddAccountRequest) Validate() error {
if c.AccountID == "" {
return errors.New("accountID is required")
}
Expand All @@ -38,23 +38,23 @@ func poolsAddAccount(backend backend.Backend) http.HandlerFunc {
return
}

var poolsAddAccountRequest poolsAddAccountRequest
err = json.NewDecoder(r.Body).Decode(&poolsAddAccountRequest)
var PoolsAddAccountRequest PoolsAddAccountRequest
err = json.NewDecoder(r.Body).Decode(&PoolsAddAccountRequest)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrMissingOrInvalidBody, err)
return
}

span.SetAttributes(attribute.String("accountID", poolsAddAccountRequest.AccountID))
span.SetAttributes(attribute.String("accountID", PoolsAddAccountRequest.AccountID))

if err := poolsAddAccountRequest.Validate(); err != nil {
if err := PoolsAddAccountRequest.Validate(); err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}

accountID, err := models.AccountIDFromString(poolsAddAccountRequest.AccountID)
accountID, err := models.AccountIDFromString(PoolsAddAccountRequest.AccountID)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrInvalidID, err)
Expand Down
6 changes: 3 additions & 3 deletions internal/api/v2/handler_pools_add_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var _ = Describe("API v2 pools add account", func() {
handlerFn http.HandlerFunc
accID models.AccountID
poolID uuid.UUID
paar poolsAddAccountRequest
paar PoolsAddAccountRequest
)
BeforeEach(func() {
connID := models.ConnectorID{Reference: uuid.New(), Provider: "psp"}
Expand Down Expand Up @@ -53,7 +53,7 @@ var _ = Describe("API v2 pools add account", func() {
It("should return an internal server error when backend returns error", func(ctx SpecContext) {
expectedErr := errors.New("pool add account err")
m.EXPECT().PoolsAddAccount(gomock.Any(), gomock.Any(), gomock.Any()).Return(expectedErr)
paar = poolsAddAccountRequest{
paar = PoolsAddAccountRequest{
AccountID: accID.String(),
}
handlerFn(w, prepareJSONRequestWithQuery(http.MethodPost, "poolID", poolID.String(), &paar))
Expand All @@ -62,7 +62,7 @@ var _ = Describe("API v2 pools add account", func() {

It("should return status no content on success", func(ctx SpecContext) {
m.EXPECT().PoolsAddAccount(gomock.Any(), poolID, accID).Return(nil)
paar = poolsAddAccountRequest{
paar = PoolsAddAccountRequest{
AccountID: accID.String(),
}
handlerFn(w, prepareJSONRequestWithQuery(http.MethodPost, "poolID", poolID.String(), &paar))
Expand Down
28 changes: 14 additions & 14 deletions internal/api/v2/handler_pools_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import (
"go.opentelemetry.io/otel/trace"
)

type createPoolRequest struct {
type CreatePoolRequest struct {
Name string `json:"name"`
AccountIDs []string `json:"accountIDs"`
}

func (r *createPoolRequest) Validate() error {
func (r *CreatePoolRequest) Validate() error {
if len(r.AccountIDs) == 0 {
return errors.New("one or more account id required")
}
return nil
}

type poolResponse struct {
type PoolResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Accounts []string `json:"accounts"`
Expand All @@ -39,30 +39,30 @@ func poolsCreate(backend backend.Backend) http.HandlerFunc {
ctx, span := otel.Tracer().Start(r.Context(), "v2_poolsBalancesAt")
defer span.End()

var createPoolRequest createPoolRequest
err := json.NewDecoder(r.Body).Decode(&createPoolRequest)
var CreatePoolRequest CreatePoolRequest
err := json.NewDecoder(r.Body).Decode(&CreatePoolRequest)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrMissingOrInvalidBody, err)
return
}

populateSpanFromCreatePoolRequest(span, createPoolRequest)
populateSpanFromCreatePoolRequest(span, CreatePoolRequest)

if err := createPoolRequest.Validate(); err != nil {
if err := CreatePoolRequest.Validate(); err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}

pool := models.Pool{
ID: uuid.New(),
Name: createPoolRequest.Name,
Name: CreatePoolRequest.Name,
CreatedAt: time.Now().UTC(),
}

accounts := make([]models.PoolAccounts, len(createPoolRequest.AccountIDs))
for i, accountID := range createPoolRequest.AccountIDs {
accounts := make([]models.PoolAccounts, len(CreatePoolRequest.AccountIDs))
for i, accountID := range CreatePoolRequest.AccountIDs {
aID, err := models.AccountIDFromString(accountID)
if err != nil {
otel.RecordError(span, err)
Expand All @@ -84,13 +84,13 @@ func poolsCreate(backend backend.Backend) http.HandlerFunc {
return
}

data := &poolResponse{
data := &PoolResponse{
ID: pool.ID.String(),
Name: pool.Name,
Accounts: createPoolRequest.AccountIDs,
Accounts: CreatePoolRequest.AccountIDs,
}

err = json.NewEncoder(w).Encode(api.BaseResponse[poolResponse]{
err = json.NewEncoder(w).Encode(api.BaseResponse[PoolResponse]{
Data: data,
})
if err != nil {
Expand All @@ -101,7 +101,7 @@ func poolsCreate(backend backend.Backend) http.HandlerFunc {
}
}

func populateSpanFromCreatePoolRequest(span trace.Span, req createPoolRequest) {
func populateSpanFromCreatePoolRequest(span trace.Span, req CreatePoolRequest) {
span.SetAttributes(attribute.String("name", req.Name))
for i, acc := range req.AccountIDs {
span.SetAttributes(attribute.String(fmt.Sprintf("accountIDs[%d]", i), acc))
Expand Down
12 changes: 6 additions & 6 deletions internal/api/v2/handler_pools_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var _ = Describe("API v2 Pools Create", func() {
var (
w *httptest.ResponseRecorder
m *backend.MockBackend
cpr createPoolRequest
cpr CreatePoolRequest
)
BeforeEach(func() {
w = httptest.NewRecorder()
Expand All @@ -45,18 +45,18 @@ var _ = Describe("API v2 Pools Create", func() {
})

DescribeTable("validation errors",
func(cpr createPoolRequest) {
func(cpr CreatePoolRequest) {
handlerFn(w, prepareJSONRequest(http.MethodPost, &cpr))
assertExpectedResponse(w.Result(), http.StatusBadRequest, ErrValidation)
},
Entry("accountIDs missing", createPoolRequest{}),
Entry("accountIDs invalid", createPoolRequest{AccountIDs: []string{"invalid"}}),
Entry("accountIDs missing", CreatePoolRequest{}),
Entry("accountIDs invalid", CreatePoolRequest{AccountIDs: []string{"invalid"}}),
)

It("should return an internal server error when backend returns error", func(ctx SpecContext) {
expectedErr := errors.New("payment create err")
m.EXPECT().PoolsCreate(gomock.Any(), gomock.Any()).Return(expectedErr)
cpr = createPoolRequest{
cpr = CreatePoolRequest{
Name: "name",
AccountIDs 9E81 : []string{accID.String()},
}
Expand All @@ -66,7 +66,7 @@ var _ = Describe("API v2 Pools Create", func() {

It("should return status ok on success", func(ctx SpecContext) {
m.EXPECT().PoolsCreate(gomock.Any(), gomock.Any()).Return(nil)
cpr = createPoolRequest{
cpr = CreatePoolRequest{
Name: "name",
AccountIDs: []string{accID.String(), accID2.String()},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/v2/handler_pools_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func poolsGet(backend backend.Backend) http.HandlerFunc {
return
}

data := &poolResponse{
data := &PoolResponse{
ID: pool.ID.String(),
Name: pool.Name,
}
Expand All @@ -42,7 +42,7 @@ func poolsGet(backend backend.Backend) http.HandlerFunc {
}
data.Accounts = accounts

err = json.NewEncoder(w).Encode(api.BaseResponse[poolResponse]{
err = json.NewEncoder(w).Encode(api.BaseResponse[PoolResponse]{
Data: data,
})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/api/v2/handler_pools_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func poolsList(backend backend.Backend) http.HandlerFunc {
return
}

data := make([]*poolResponse, len(cursor.Data))
data := make([]*PoolResponse, len(cursor.Data))
for i := range cursor.Data {
data[i] = &poolResponse{
data[i] = &PoolResponse{
ID: cursor.Data[i].ID.String(),
Name: cursor.Data[i].Name,
}
Expand All @@ -52,8 +52,8 @@ func poolsList(backend backend.Backend) http.HandlerFunc {
data[i].Accounts = accounts
}

err = json.NewEncoder(w).Encode(api.BaseResponse[*poolResponse]{
Cursor: &bunpaginate.Cursor[*poolResponse]{
err = json.NewEncoder(w).Encode(api.BaseResponse[*PoolResponse]{
Cursor: &bunpaginate.Cursor[*PoolResponse]{
PageSize: cursor.PageSize,
HasMore: cursor.HasMore,
Previous: cursor.Previous,
Expand Down
20 changes: 10 additions & 10 deletions internal/api/v3/handler_pools_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"go.opentelemetry.io/otel/trace"
)

type createPoolRequest struct {
type CreatePoolRequest struct {
Name string `json:"name"`
AccountIDs []string `json:"accountIDs"`
}

func (r *createPoolRequest) Validate() error {
func (r *CreatePoolRequest) Validate() error {
if len(r.AccountIDs) == 0 {
return errors.New("one or more account id required")
}
Expand All @@ -33,30 +33,30 @@ func poolsCreate(backend backend.Backend) http.HandlerFunc {
ctx, span := otel.Tracer().Start(r.Context(), "v3_poolsCreate")
defer span.End()

var createPoolRequest createPoolRequest
err := json.NewDecoder(r.Body).Decode(&createPoolRequest)
var CreatePoolRequest CreatePoolRequest
err := json.NewDecoder(r.Body).Decode(&CreatePoolRequest)
if err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrMissingOrInvalidBody, err)
return
}

populateSpanFromCreatePoolRequest(span, createPoolRequest)
populateSpanFromCreatePoolRequest(span, CreatePoolRequest)

if err := createPoolRequest.Validate(); err != nil {
if err := CreatePoolRequest.Validate(); err != nil {
otel.RecordError(span, err)
api.BadRequest(w, ErrValidation, err)
return
}

pool := models.Pool{
ID: uuid.New(),
Name: createPoolRequest.Name,
Name: CreatePoolRequest.Name,
CreatedAt: time.Now().UTC(),
}

accounts := make([]models.PoolAccounts, len(createPoolRequest.AccountIDs))
for i, accountID := range createPoolRequest.AccountIDs {
accounts := make([]models.PoolAccounts, len(CreatePoolRequest.AccountIDs))
for i, accountID := range CreatePoolRequest.AccountIDs {
aID, err := models.AccountIDFromString(accountID)
if err != nil {
otel.RecordError(span, err)
Expand All @@ -82,7 +82,7 @@ func poolsCreate(backend backend.Backend) http.HandlerFunc {
}
}

func populateSpanFromCreatePoolRequest(span trace.Span, req createPoolRequest) {
func populateSpanFromCreatePoolRequest(span trace.Span, req CreatePoolRequest) {
span.SetAttributes(attribute.String("name", req.Name))
for i, acc := range req.AccountIDs {
span.SetAttributes(attribute.String(fmt.Sprintf("accountIDs[%d]", i), acc))
Expand Down
12 changes: 6 additions & 6 deletions internal/api/v3/handler_pools_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var _ = Describe("API v3 Pools Create", func() {
var (
w *httptest.ResponseRecorder
m *backend.MockBackend
cpr createPoolRequest
cpr CreatePoolRequest
)
BeforeEach(func() {
w = httptest.NewRecorder()
Expand All @@ -45,18 +45,18 @@ var _ = Describe("API v3 Pools Create", func() {
})

DescribeTable("validation errors",
func(cpr createPoolRequest) {
func(cpr CreatePoolRequest) {
handlerFn(w, prepareJSONRequest(http.MethodPost, &cpr))
assertExpectedResponse(w.Result(), http.StatusBadRequest, ErrValidation)
},
Entry("accountIDs missing", createPoolRequest{}),
Entry("accountIDs invalid", createPoolRequest{AccountIDs: []string{"invalid"}}),
Entry("accountIDs missing", CreatePoolRequest{}),
Entry("accountIDs invalid", CreatePoolRequest{AccountIDs: []string{"invalid"}}),
)

It("should return an internal server error when backend returns error", func(ctx SpecContext) {
expectedErr := errors.New("payment create err")
m.EXPECT().PoolsCreate(gomock.Any(), gomock.Any()).Return(expectedErr)
cpr = createPoolRequest{
cpr = CreatePoolRequest{
Name: "name",
AccountIDs: []string{accID.String()},
}
Expand All @@ -66,7 +66,7 @@ var _ = Describe("API v3 Pools Create", func() {

It("should return status created on success", func(ctx SpecContext) {
m.EXPECT().PoolsCreate(gomock.Any(), gomock.Any()).Return(nil)
cpr = createPoolRequest{
cpr = CreatePoolRequest{
Name: "name",
AccountIDs: []string{accID.String(), accID2.String()},
}
Expand Down
6 changes: 3 additions & 3 deletions internal/connectors/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ func (e *engine) CreatePool(ctx context.Context, pool models.Pool) error {
_, err := e.temporalClient.ExecuteWorkflow(
ctx,
client.StartWorkflowOptions{
ID: fmt.Sprintf("pools-creation-%s-%s", e.stack, pool.IdempotencyKey()),
ID: fmt.Sprintf("pools-creation-%s-%s", e.stack, pool.ID.String()),
TaskQueue: e.workers.GetDefaultWorker(),
WorkflowIDReusePolicy: enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE,
WorkflowExecutionErrorWhenAlreadyStarted: false,
Expand Down Expand Up @@ -802,7 +802,7 @@ func (e *engine) AddAccountToPool(ctx context.Context, id uuid.UUID, accountID m
_, err = e.temporalClient.ExecuteWorkflow(
detachedCtx,
client.StartWorkflowOptions{
ID: fmt.Sprintf("pools-add-account-%s-%s", e.stack, pool.IdempotencyKey()),
ID: fmt.Sprintf("pools-add-account-%s-%s-%s", e.stack, pool.ID.String(), accountID.String()),
TaskQueue: e.workers.GetDefaultWorker(),
WorkflowIDReusePolicy: enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE,
WorkflowExecutionErrorWhenAlreadyStarted: false,
Expand Down Expand Up @@ -848,7 +848,7 @@ func (e *engine) RemoveAccountFromPool(ctx context.Context, id uuid.UUID, accoun
_, err = e.temporalClient.ExecuteWorkflow(
detachedCtx,
client.StartWorkflowOptions{
ID: fmt.Sprintf("pools-remove-account-%s-%s", e.stack, pool.IdempotencyKey()),
ID: fmt.Sprintf("pools-remove-account-%s-%s-%s", e.stack, pool.ID.String(), accountID.String()),
TaskQueue: e.workers.GetDefaultWorker(),
WorkflowIDReusePolicy: enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE,
WorkflowExecutionErrorWhenAlreadyStarted: false,
Expand Down
Loading
Loading
0