-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return error before launching workflow if connector not found in forw…
…ard bank account req
- Loading branch information
Showing
5 changed files
with
183 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package engine_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/formancehq/go-libs/v2/logging" | ||
"github.com/formancehq/payments/internal/connectors/engine" | ||
"github.com/formancehq/payments/internal/connectors/engine/activities" | ||
"github.com/formancehq/payments/internal/connectors/engine/plugins" | ||
"github.com/formancehq/payments/internal/connectors/engine/workflow" | ||
"github.com/formancehq/payments/internal/models" | ||
"github.com/formancehq/payments/internal/storage" | ||
"github.com/google/uuid" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"go.temporal.io/sdk/client" | ||
gomock "go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestEngine(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Engine Suite") | ||
} | ||
|
||
func WithWorkflowOptions(idPrefix, taskQueue string) gomock.Matcher { | ||
return workflowOptionsMatcher{expectedIDPrefix: idPrefix, expectedTaskQueue: taskQueue} | ||
} | ||
|
||
type workflowOptionsMatcher struct { | ||
expectedIDPrefix string | ||
expectedTaskQueue string | ||
} | ||
|
||
func (m workflowOptionsMatcher) Matches(options any) bool { | ||
opts, ok := options.(client.StartWorkflowOptions) | ||
if !ok { | ||
return false | ||
} | ||
|
||
if !strings.HasPrefix(opts.ID, m.expectedIDPrefix) { | ||
return false | ||
} | ||
if opts.TaskQueue != m.expectedTaskQueue { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (m workflowOptionsMatcher) String() string { | ||
return "has same options" | ||
} | ||
|
||
var _ = Describe("Engine Tests", func() { | ||
var ( | ||
stackName string | ||
defaultTaskQueue string | ||
eng engine.Engine | ||
store *storage.MockStorage | ||
cl *activities.MockClient | ||
) | ||
BeforeEach(func() { | ||
stackName = "STACKNAME" | ||
defaultTaskQueue = engine.GetDefaultTaskQueue(stackName) | ||
ctrl := gomock.NewController(GinkgoT()) | ||
logger := logging.NewDefaultLogger(GinkgoWriter, false, false, false) | ||
cl = activities.NewMockClient(ctrl) | ||
store = storage.NewMockStorage(ctrl) | ||
plgs := plugins.NewMockPlugins(ctrl) | ||
eng = engine.New(logger, cl, store, plgs, stackName) | ||
}) | ||
|
||
Context("forwarding a bank account to a connector", func() { | ||
var ( | ||
bankID uuid.UUID | ||
connID models.ConnectorID | ||
) | ||
BeforeEach(func() { | ||
connID = models.ConnectorID{Reference: uuid.New(), Provider: "psp"} | ||
bankID = uuid.New() | ||
}) | ||
|
||
It("should return not found error when storage doesn't find connector", func(ctx SpecContext) { | ||
store.EXPECT().ConnectorsGet(gomock.Any(), connID).Return( | ||
nil, fmt.Errorf("some not found err: %w", storage.ErrNotFound), | ||
) | ||
_, err := eng.ForwardBankAccount(ctx, bankID, connID, false) | ||
Expect(err).NotTo(BeNil()) | ||
Expect(err).To(MatchError(engine.ErrNotFound)) | ||
}) | ||
|
||
It("should return storage error when task cannot be upserted", func(ctx SpecContext) { | ||
store.EXPECT().ConnectorsGet(gomock.Any(), connID).Return( | ||
&models.Connector{ID: connID}, nil, | ||
) | ||
expectedErr := fmt.Errorf("fffff") | ||
store.EXPECT().TasksUpsert(gomock.Any(), gomock.AssignableToTypeOf(models.Task{})).Return( | ||
expectedErr, | ||
) | ||
_, err := eng.ForwardBankAccount(ctx, bankID, connID, false) | ||
Expect(err).NotTo(BeNil()) | ||
Expect(err).To(MatchError(expectedErr)) | ||
}) | ||
|
||
It("should return error when workflow cannot be started", func(ctx SpecContext) { | ||
store.EXPECT().ConnectorsGet(gomock.Any(), connID).Return( | ||
&models.Connector{ID: connID}, nil, | ||
) | ||
store.EXPECT().TasksUpsert(gomock.Any(), gomock.AssignableToTypeOf(models.Task{})).Return(nil) | ||
expectedErr := fmt.Errorf("workflow failed") | ||
cl.EXPECT().ExecuteWorkflow(gomock.Any(), WithWorkflowOptions(engine.IDPrefixBankAccountCreate, defaultTaskQueue), | ||
workflow.RunCreateBankAccount, | ||
gomock.AssignableToTypeOf(workflow.CreateBankAccount{}), | ||
).Return(nil, expectedErr) | ||
|
||
_, err := eng.ForwardBankAccount(ctx, bankID, connID, false) | ||
Expect(err).NotTo(BeNil()) | ||
Expect(err).To(MatchError(expectedErr)) | ||
}) | ||
|
||
It("should launch workflow and return task", func(ctx SpecContext) { | ||
store.EXPECT().ConnectorsGet(gomock.Any(), connID).Return( | ||
&models.Connector{ID: connID}, nil, | ||
) | ||
store.EXPECT().TasksUpsert(gomock.Any(), gomock.AssignableToTypeOf(models.Task{})).Return(nil) | ||
cl.EXPECT().ExecuteWorkflow(gomock.Any(), WithWorkflowOptions(engine.IDPrefixBankAccountCreate, defaultTaskQueue), | ||
workflow.RunCreateBankAccount, | ||
gomock.AssignableToTypeOf(workflow.CreateBankAccount{}), | ||
).Return(nil, nil) | ||
|
||
task, err := eng.ForwardBankAccount(ctx, bankID, connID, false) | ||
Expect(err).To(BeNil()) | ||
Expect(task.ID.Reference).To(ContainSubstring(engine.IDPrefixBankAccountCreate)) | ||
Expect(task.ID.Reference).To(ContainSubstring(stackName)) | ||
Expect(task.ConnectorID.String()).To(Equal(connID.String())) | ||
Expect(task.Status).To(Equal(models.TASK_STATUS_PROCESSING)) | ||
}) | ||
}) | ||
}) |
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,16 @@ | ||
package engine | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/formancehq/payments/internal/models" | ||
) | ||
|
||
const ( | ||
IDPrefixBankAccountCreate = "create-bank-account" | ||
) | ||
|
||
func (e *engine) taskIDReferenceFor(prefix string, connectorID models.ConnectorID, objectID string) string { | ||
withStack := fmt.Sprintf("%s-%s", prefix, e.stack) | ||
return models.TaskIDReference(withStack, connectorID, objectID) | ||
} |
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