Files
payments/ce/plugins/mangopay/bank_account_creation_test.go

475 lines
14 KiB
Go

package mangopay
import (
"encoding/json"
"errors"
"time"
"github.com/hanzo-fi/go-libs/v5/pkg/types/pointer"
"github.com/hanzo-fi/payments/ce/plugins/mangopay/client"
"github.com/hanzo-fi/payments/pkg/domain/models"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
)
var _ = Describe("Mangopay Plugin Bank Account Creation", func() {
var (
ctrl *gomock.Controller
m *client.MockClient
plg models.Plugin
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
m = client.NewMockClient(ctrl)
plg = &Plugin{client: m}
})
AfterEach(func() {
ctrl.Finish()
})
Context("create bank account", func() {
var (
sampleBankAccount models.BankAccount
sampleClientAddress client.OwnerAddress
now time.Time
)
BeforeEach(func() {
now = time.Now().UTC()
sampleBankAccount = models.BankAccount{
ID: uuid.New(),
CreatedAt: now.UTC(),
Name: "test",
AccountNumber: pointer.For("12345678"),
IBAN: pointer.For("FR9412739000405993414979X56"),
SwiftBicCode: pointer.For("ERAHJP6BT1H"),
Country: pointer.For("FR"),
Metadata: map[string]string{
models.BankAccountOwnerAddressLine1MetadataKey: "address1",
models.BankAccountOwnerAddressLine2MetadataKey: "address2",
models.BankAccountOwnerCityMetadataKey: "city",
models.BankAccountOwnerRegionMetadataKey: "region",
models.BankAccountOwnerPostalCodeMetadataKey: "postal_code",
client.MangopayUserIDMetadataKey: "u1",
client.MangopayTagMetadataKey: "foo=bar",
client.MangopayABAMetadataKey: "aba",
client.MangopayDepositAccountTypeMetadataKey: "deposit_test",
client.MangopayInstitutionNumberMetadataKey: "institution_number_test",
client.MangopayBranchCodeMetadataKey: "branch_code",
client.MangopayBankNameMetadataKey: "bank_name",
client.MangopaySortCodeMetadataKey: "sort_code",
},
}
sampleClientAddress = client.OwnerAddress{
AddressLine1: "address1",
AddressLine2: "address2",
City: "city",
Region: "region",
PostalCode: "postal_code",
Country: "FR",
}
})
It("should return an error - missing userID in bank account metadata", func(ctx SpecContext) {
ba := sampleBankAccount
ba.Metadata = map[string]string{}
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError(models.ErrMissingConnectorMetadata))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should return an error - create IBAN bank account error", func(ctx SpecContext) {
ba := sampleBankAccount
ba.AccountNumber = nil
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
m.EXPECT().CreateIBANBankAccount(gomock.Any(), "u1", &client.CreateIBANBankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sampleClientAddress,
IBAN: *ba.IBAN,
BIC: *ba.SwiftBicCode,
Tag: "foo=bar",
}).Return(nil, errors.New("test error"))
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("failed to create IBAN bank account: test error: failed to create account"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should be ok - create IBAN bank account", func(ctx SpecContext) {
ba := sampleBankAccount
ba.AccountNumber = nil
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
expectedBA := &client.BankAccount{
ID: "id",
OwnerName: ba.Name,
CreationDate: now.UTC().Unix(),
}
m.EXPECT().CreateIBANBankAccount(gomock.Any(), "u1", &client.CreateIBANBankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sampleClientAddress,
IBAN: *ba.IBAN,
BIC: *ba.SwiftBicCode,
Tag: "foo=bar",
}).Return(expectedBA, nil)
raw, _ := json.Marshal(expectedBA)
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).To(BeNil())
Expect(res).To(Equal(models.CreateBankAccountResponse{
RelatedAccount: models.PSPAccount{
Reference: "id",
CreatedAt: time.Unix(expectedBA.CreationDate, 0),
Name: &ba.Name,
Metadata: map[string]string{
"user_id": "u1",
},
Raw: raw,
},
}))
})
It("should return an error - create us bank account with missing account number", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.AccountNumber = nil
ba.Country = pointer.For("US")
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("missing account number in request"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should return an error - createUSBankAccount error", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.Country = pointer.For("US")
sca := sampleClientAddress
sca.Country = "US"
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
m.EXPECT().CreateUSBankAccount(gomock.Any(), "u1", &client.CreateUSBankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sca,
AccountNumber: *ba.AccountNumber,
ABA: "aba",
DepositAccountType: "deposit_test",
Tag: "foo=bar",
}).Return(nil, errors.New("test error"))
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("failed to create US bank account: test error: failed to create account"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should be ok - create US bank account", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.Country = pointer.For("US")
sca := sampleClientAddress
sca.Country = "US"
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
expectedBA := &client.BankAccount{
ID: "id",
OwnerName: ba.Name,
CreationDate: now.UTC().Unix(),
}
m.EXPECT().CreateUSBankAccount(gomock.Any(), "u1", &client.CreateUSBankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sca,
AccountNumber: *ba.AccountNumber,
ABA: "aba",
DepositAccountType: "deposit_test",
Tag: "foo=bar",
}).Return(expectedBA, nil)
raw, _ := json.Marshal(expectedBA)
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).To(BeNil())
Expect(res).To(Equal(models.CreateBankAccountResponse{
RelatedAccount: models.PSPAccount{
Reference: "id",
CreatedAt: time.Unix(expectedBA.CreationDate, 0),
Name: &ba.Name,
Metadata: map[string]string{
"user_id": "u1",
},
Raw: raw,
},
}))
})
It("should return an error - create CA bank account with missing account number", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.AccountNumber = nil
ba.Country = pointer.For("CA")
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("missing account number in request"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should return an error - createCABankAccount error", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.Country = pointer.For("CA")
sca := sampleClientAddress
sca.Country = "CA"
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
m.EXPECT().CreateCABankAccount(gomock.Any(), "u1", &client.CreateCABankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sca,
AccountNumber: *ba.AccountNumber,
InstitutionNumber: "institution_number_test",
BranchCode: "branch_code",
BankName: "bank_name",
Tag: "foo=bar",
}).Return(nil, errors.New("test error"))
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("failed to create CA bank account: test error: failed to create account"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should be ok - create CA bank account", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.Country = pointer.For("CA")
sca := sampleClientAddress
sca.Country = "CA"
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
expectedBA := &client.BankAccount{
ID: "id",
OwnerName: ba.Name,
CreationDate: now.UTC().Unix(),
}
m.EXPECT().CreateCABankAccount(gomock.Any(), "u1", &client.CreateCABankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sca,
AccountNumber: *ba.AccountNumber,
InstitutionNumber: "institution_number_test",
BranchCode: "branch_code",
BankName: "bank_name",
Tag: "foo=bar",
}).Return(expectedBA, nil)
raw, _ := json.Marshal(expectedBA)
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).To(BeNil())
Expect(res).To(Equal(models.CreateBankAccountResponse{
RelatedAccount: models.PSPAccount{
Reference: "id",
CreatedAt: time.Unix(expectedBA.CreationDate, 0),
Name: &ba.Name,
Metadata: map[string]string{
"user_id": "u1",
},
Raw: raw,
},
}))
})
It("should return an error - create GB bank account with missing account number", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.AccountNumber = nil
ba.Country = pointer.For("GB")
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("missing account number in request"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should return an error - createGBBankAccount error", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.Country = pointer.For("GB")
sca := sampleClientAddress
sca.Country = "GB"
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
m.EXPECT().CreateGBBankAccount(gomock.Any(), "u1", &client.CreateGBBankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sca,
AccountNumber: *ba.AccountNumber,
SortCode: "sort_code",
Tag: "foo=bar",
}).Return(nil, errors.New("test error"))
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("failed to create GB bank account: test error: failed to create account"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should be ok - create GB bank account", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.Country = pointer.For("GB")
sca := sampleClientAddress
sca.Country = "GB"
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
expectedBA := &client.BankAccount{
ID: "id",
OwnerName: ba.Name,
CreationDate: now.UTC().Unix(),
}
m.EXPECT().CreateGBBankAccount(gomock.Any(), "u1", &client.CreateGBBankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sca,
AccountNumber: *ba.AccountNumber,
SortCode: "sort_code",
Tag: "foo=bar",
}).Return(expectedBA, nil)
raw, _ := json.Marshal(expectedBA)
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).To(BeNil())
Expect(res).To(Equal(models.CreateBankAccountResponse{
RelatedAccount: models.PSPAccount{
Reference: "id",
CreatedAt: time.Unix(expectedBA.CreationDate, 0),
Name: &ba.Name,
Metadata: map[string]string{
"user_id": "u1",
},
Raw: raw,
},
}))
})
It("should return an error - create other bank account with missing account number", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.AccountNumber = nil
ba.Country = pointer.For("TEST_COUNTRY")
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("missing account number in request"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should return an error - createOtherBankAccount error", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.Country = pointer.For("TEST_COUNTRY")
sca := sampleClientAddress
sca.Country = "TEST_COUNTRY"
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
m.EXPECT().CreateOtherBankAccount(gomock.Any(), "u1", &client.CreateOtherBankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sca,
AccountNumber: *ba.AccountNumber,
BIC: *ba.SwiftBicCode,
Country: "TEST_COUNTRY",
Tag: "foo=bar",
}).Return(nil, errors.New("test error"))
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).ToNot(BeNil())
Expect(err).To(MatchError("failed to create other bank account: test error: failed to create account"))
Expect(res).To(Equal(models.CreateBankAccountResponse{}))
})
It("should be ok - create other bank account", func(ctx SpecContext) {
ba := sampleBankAccount
ba.IBAN = nil
ba.Country = pointer.For("TEST_COUNTRY")
sca := sampleClientAddress
sca.Country = "TEST_COUNTRY"
req := models.CreateBankAccountRequest{
BankAccount: ba,
}
expectedBA := &client.BankAccount{
ID: "id",
OwnerName: ba.Name,
CreationDate: now.UTC().Unix(),
}
m.EXPECT().CreateOtherBankAccount(gomock.Any(), "u1", &client.CreateOtherBankAccountRequest{
OwnerName: ba.Name,
OwnerAddress: &sca,
AccountNumber: *ba.AccountNumber,
BIC: *ba.SwiftBicCode,
Country: "TEST_COUNTRY",
Tag: "foo=bar",
}).Return(expectedBA, nil)
raw, _ := json.Marshal(expectedBA)
res, err := plg.CreateBankAccount(ctx, req)
Expect(err).To(BeNil())
Expect(res).To(Equal(models.CreateBankAccountResponse{
RelatedAccount: models.PSPAccount{
Reference: "id",
CreatedAt: time.Unix(expectedBA.CreationDate, 0),
Name: &ba.Name,
Metadata: map[string]string{
"user_id": "u1",
},
Raw: raw,
},
}))
})
})
})