mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
Expand config test coverage
This commit is contained in:
@@ -171,9 +171,12 @@ func (c *Config) CheckClientBankAccounts() {
|
||||
if len(c.BankAccounts) == 0 {
|
||||
c.BankAccounts = append(c.BankAccounts,
|
||||
BankAccount{
|
||||
BankName: "test",
|
||||
BankAddress: "test",
|
||||
AccountName: "TestAccount",
|
||||
BankName: "Test Bank",
|
||||
BankAddress: "42 Bank Street",
|
||||
BankPostalCode: "13337",
|
||||
BankPostalCity: "Satoshiville",
|
||||
BankCountry: "Japan",
|
||||
AccountName: "Satoshi Nakamoto",
|
||||
AccountNumber: "0234",
|
||||
SWIFTCode: "91272837",
|
||||
IBAN: "98218738671897",
|
||||
@@ -186,29 +189,10 @@ func (c *Config) CheckClientBankAccounts() {
|
||||
|
||||
for i := range c.BankAccounts {
|
||||
if c.BankAccounts[i].Enabled {
|
||||
if c.BankAccounts[i].BankName == "" || c.BankAccounts[i].BankAddress == "" {
|
||||
err := c.BankAccounts[i].Validate()
|
||||
if err != nil {
|
||||
c.BankAccounts[i].Enabled = false
|
||||
log.Warnf(log.ConfigMgr, "banking details for %s is enabled but variables not set correctly\n",
|
||||
c.BankAccounts[i].BankName)
|
||||
continue
|
||||
}
|
||||
|
||||
if c.BankAccounts[i].AccountName == "" || c.BankAccounts[i].AccountNumber == "" {
|
||||
c.BankAccounts[i].Enabled = false
|
||||
log.Warnf(log.ConfigMgr, "banking account details for %s variables not set correctly\n",
|
||||
c.BankAccounts[i].BankName)
|
||||
continue
|
||||
}
|
||||
if c.BankAccounts[i].IBAN == "" && c.BankAccounts[i].SWIFTCode == "" && c.BankAccounts[i].BSBNumber == "" {
|
||||
c.BankAccounts[i].Enabled = false
|
||||
log.Warnf(log.ConfigMgr, "critical banking numbers not set for %s in %s account\n",
|
||||
c.BankAccounts[i].BankName,
|
||||
c.BankAccounts[i].AccountName)
|
||||
continue
|
||||
}
|
||||
|
||||
if c.BankAccounts[i].SupportedExchanges == "" {
|
||||
c.BankAccounts[i].SupportedExchanges = "ALL"
|
||||
log.Warn(log.ConfigMgr, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -834,6 +818,10 @@ func (c *Config) UpdateExchangeConfig(e *ExchangeConfig) error {
|
||||
// CheckExchangeConfigValues returns configuation values for all enabled
|
||||
// exchanges
|
||||
func (c *Config) CheckExchangeConfigValues() error {
|
||||
if len(c.Exchanges) == 0 {
|
||||
return errors.New("no exchange configs found")
|
||||
}
|
||||
|
||||
exchanges := 0
|
||||
for i := range c.Exchanges {
|
||||
if strings.EqualFold(c.Exchanges[i].Name, "GDAX") {
|
||||
@@ -1050,40 +1038,14 @@ func (c *Config) CheckExchangeConfigValues() error {
|
||||
|
||||
c.CheckExchangeAssetsConsistency(c.Exchanges[i].Name)
|
||||
|
||||
if len(c.Exchanges[i].BankAccounts) > 0 {
|
||||
for x := range c.Exchanges[i].BankAccounts {
|
||||
if !c.Exchanges[i].BankAccounts[x].Enabled {
|
||||
continue
|
||||
}
|
||||
bankError := false
|
||||
if c.Exchanges[i].BankAccounts[x].BankName == "" || c.Exchanges[i].BankAccounts[x].BankAddress == "" {
|
||||
log.Warnf(log.ExchangeSys, "banking details for %s is enabled but variables not set\n",
|
||||
c.Exchanges[i].Name)
|
||||
bankError = true
|
||||
}
|
||||
|
||||
if c.Exchanges[i].BankAccounts[x].AccountName == "" || c.Exchanges[i].BankAccounts[x].AccountNumber == "" {
|
||||
log.Warnf(log.ExchangeSys, "banking account details for %s variables not set\n",
|
||||
c.Exchanges[i].Name)
|
||||
bankError = true
|
||||
}
|
||||
|
||||
if c.Exchanges[i].BankAccounts[x].SupportedCurrencies == "" {
|
||||
log.Warnf(log.ExchangeSys, "banking account details for %s acceptable funding currencies not set\n",
|
||||
c.Exchanges[i].Name)
|
||||
bankError = true
|
||||
}
|
||||
|
||||
if c.Exchanges[i].BankAccounts[x].BSBNumber == "" && c.Exchanges[i].BankAccounts[x].IBAN == "" &&
|
||||
c.Exchanges[i].BankAccounts[x].SWIFTCode == "" {
|
||||
log.Warnf(log.ExchangeSys, "banking account details for %s critical banking numbers not set\n",
|
||||
c.Exchanges[i].Name)
|
||||
bankError = true
|
||||
}
|
||||
|
||||
if bankError {
|
||||
c.Exchanges[i].BankAccounts[x].Enabled = false
|
||||
}
|
||||
for x := range c.Exchanges[i].BankAccounts {
|
||||
if !c.Exchanges[i].BankAccounts[x].Enabled {
|
||||
continue
|
||||
}
|
||||
err := c.Exchanges[i].BankAccounts[x].Validate()
|
||||
if err != nil {
|
||||
c.Exchanges[i].BankAccounts[x].Enabled = false
|
||||
log.Warn(log.ConfigMgr, err.Error())
|
||||
}
|
||||
}
|
||||
exchanges++
|
||||
|
||||
@@ -134,16 +134,68 @@ func TestCheckClientBankAccounts(t *testing.T) {
|
||||
|
||||
cfg.BankAccounts = nil
|
||||
cfg.CheckClientBankAccounts()
|
||||
if err != nil || len(cfg.BankAccounts) == 0 {
|
||||
if len(cfg.BankAccounts) == 0 {
|
||||
t.Error("Test failed. CheckClientBankAccounts error:", err)
|
||||
}
|
||||
|
||||
cfg.BankAccounts = nil
|
||||
cfg.BankAccounts = append(cfg.BankAccounts, BankAccount{
|
||||
Enabled: true,
|
||||
BankName: "test",
|
||||
})
|
||||
// TO-DO: Complete test coverage
|
||||
cfg.BankAccounts = []BankAccount{
|
||||
{
|
||||
Enabled: true,
|
||||
},
|
||||
}
|
||||
|
||||
cfg.CheckClientBankAccounts()
|
||||
if cfg.BankAccounts[0].Enabled {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
|
||||
b := BankAccount{
|
||||
Enabled: true,
|
||||
BankName: "Commonwealth Bank of Awesome",
|
||||
BankAddress: "123 Fake Street",
|
||||
BankPostalCode: "1337",
|
||||
BankPostalCity: "Satoshiville",
|
||||
BankCountry: "Genesis",
|
||||
AccountName: "Satoshi Nakamoto",
|
||||
AccountNumber: "1231006505",
|
||||
SupportedCurrencies: "USD",
|
||||
}
|
||||
cfg.BankAccounts = []BankAccount{b}
|
||||
cfg.CheckClientBankAccounts()
|
||||
if cfg.BankAccounts[0].Enabled ||
|
||||
cfg.BankAccounts[0].SupportedExchanges != "ALL" {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
|
||||
// AU based bank, with no BSB number (required for domestic and international
|
||||
// transfers)
|
||||
b.SupportedCurrencies = "AUD"
|
||||
b.SWIFTCode = "BACXSI22"
|
||||
cfg.BankAccounts = []BankAccount{b}
|
||||
cfg.CheckClientBankAccounts()
|
||||
if cfg.BankAccounts[0].Enabled {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
|
||||
// Valid AU bank
|
||||
b.BSBNumber = "061337"
|
||||
cfg.BankAccounts = []BankAccount{b}
|
||||
cfg.CheckClientBankAccounts()
|
||||
if !cfg.BankAccounts[0].Enabled {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
|
||||
// Valid SWIFT/IBAN compliant bank
|
||||
b.Enabled = true
|
||||
b.IBAN = "SI56290000170073837"
|
||||
b.SWIFTCode = "BACXSI22"
|
||||
cfg.BankAccounts = []BankAccount{b}
|
||||
cfg.CheckClientBankAccounts()
|
||||
if !cfg.BankAccounts[0].Enabled {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPurgeExchangeCredentials(t *testing.T) {
|
||||
@@ -1176,6 +1228,10 @@ func TestUpdateExchangeConfig(t *testing.T) {
|
||||
// TestCheckExchangeConfigValues logic test
|
||||
func TestCheckExchangeConfigValues(t *testing.T) {
|
||||
var cfg Config
|
||||
if err := cfg.CheckExchangeConfigValues(); err == nil {
|
||||
t.Error("nil exchanges should throw an err")
|
||||
}
|
||||
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -1453,6 +1509,24 @@ func TestCheckExchangeConfigValues(t *testing.T) {
|
||||
t.Error("Expected AuthenticatedAPISupport and AuthenticatedWebsocketAPISupport to be false from invalid API keys")
|
||||
}
|
||||
|
||||
// Test exchage bank accounts
|
||||
b := BankAccount{
|
||||
Enabled: true,
|
||||
BankName: "Commonwealth Bank of Awesome",
|
||||
BankAddress: "123 Fake Street",
|
||||
BankPostalCode: "1337",
|
||||
BankPostalCity: "Satoshiville",
|
||||
BankCountry: "Genesis",
|
||||
AccountName: "Satoshi Nakamoto",
|
||||
AccountNumber: "1231006505",
|
||||
SupportedCurrencies: "USD",
|
||||
}
|
||||
cfg.Exchanges[0].BankAccounts = []BankAccount{b}
|
||||
cfg.CheckExchangeConfigValues()
|
||||
if cfg.Exchanges[0].BankAccounts[0].Enabled {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
|
||||
// Test empty exchange name for an enabled exchange
|
||||
cfg.Exchanges[0].Enabled = true
|
||||
cfg.Exchanges[0].Name = ""
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
@@ -163,6 +165,9 @@ type BankAccount struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
BankName string `json:"bankName"`
|
||||
BankAddress string `json:"bankAddress"`
|
||||
BankPostalCode string `json:"bankPostalCode"`
|
||||
BankPostalCity string `json:"bankPostalCity"`
|
||||
BankCountry string `json:"bankCountry"`
|
||||
AccountName string `json:"accountName"`
|
||||
AccountNumber string `json:"accountNumber"`
|
||||
SWIFTCode string `json:"swiftCode"`
|
||||
@@ -172,6 +177,44 @@ type BankAccount struct {
|
||||
SupportedExchanges string `json:"supportedExchanges,omitempty"`
|
||||
}
|
||||
|
||||
// Validate validates bank account settings
|
||||
func (b *BankAccount) Validate() error {
|
||||
if b.BankName == "" ||
|
||||
b.BankAddress == "" ||
|
||||
b.BankPostalCode == "" ||
|
||||
b.BankPostalCity == "" ||
|
||||
b.BankCountry == "" ||
|
||||
b.AccountName == "" ||
|
||||
b.SupportedCurrencies == "" {
|
||||
return fmt.Errorf(
|
||||
"banking details for %s is enabled but variables not set correctly",
|
||||
b.BankName)
|
||||
}
|
||||
|
||||
if b.SupportedExchanges == "" {
|
||||
b.SupportedExchanges = "ALL"
|
||||
}
|
||||
|
||||
if strings.Contains(strings.ToUpper(
|
||||
b.SupportedCurrencies),
|
||||
currency.AUD.String()) {
|
||||
if b.BSBNumber == "" ||
|
||||
b.SWIFTCode == "" {
|
||||
return fmt.Errorf(
|
||||
"banking details for %s is enabled but BSB/SWIFT values not set",
|
||||
b.BankName)
|
||||
}
|
||||
} else {
|
||||
// Either IBAN or SWIFT code is OK
|
||||
if b.IBAN == "" && b.SWIFTCode == "" {
|
||||
return fmt.Errorf(
|
||||
"banking details for %s is enabled but SWIFT/IBAN values not set",
|
||||
b.BankName)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BankTransaction defines a related banking transaction
|
||||
type BankTransaction struct {
|
||||
ReferenceNumber string `json:"referenceNumber"`
|
||||
|
||||
@@ -90,8 +90,8 @@ func NewRateLimit(d time.Duration, rate int) *RateLimit {
|
||||
return &RateLimit{Duration: d, Rate: rate}
|
||||
}
|
||||
|
||||
// ToString returns the rate limiter in string notation
|
||||
func (r *RateLimit) ToString() string {
|
||||
// String returns the rate limiter in string notation
|
||||
func (r *RateLimit) String() string {
|
||||
return fmt.Sprintf("Rate limiter set to %d requests per %v", r.Rate, r.Duration)
|
||||
}
|
||||
|
||||
|
||||
@@ -66,15 +66,15 @@ func TestIsRateLimited(t *testing.T) {
|
||||
r := New("bitfinex", NewRateLimit(time.Second*10, 5), NewRateLimit(time.Second*20, 100), new(http.Client))
|
||||
r.StartCycle()
|
||||
|
||||
if r.AuthLimit.ToString() != "Rate limiter set to 5 requests per 10s" {
|
||||
if r.AuthLimit.String() != "Rate limiter set to 5 requests per 10s" {
|
||||
t.Fatal("unexcpted values")
|
||||
}
|
||||
|
||||
if r.UnauthLimit.ToString() != "Rate limiter set to 100 requests per 20s" {
|
||||
if r.UnauthLimit.String() != "Rate limiter set to 100 requests per 20s" {
|
||||
t.Fatal("unexpected values")
|
||||
}
|
||||
|
||||
if r.AuthLimit.ToString() != "Rate limiter set to 5 requests per 10s" {
|
||||
if r.AuthLimit.String() != "Rate limiter set to 5 requests per 10s" {
|
||||
t.Fatal("unexcpted values")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user