[Config] Add "ID" field to BankAccount struct & GetBankAccountByID method (#402)

* added id field to bank struct

* fixed casing on error message

* whitespace :D

* turns out you shouldn't turn multiple tests at the same time that modify a var
This commit is contained in:
Andrew
2019-12-18 11:38:36 +11:00
committed by Adrian Gallagher
parent e37512e94f
commit edc07f7839
3 changed files with 40 additions and 0 deletions

View File

@@ -110,6 +110,7 @@ func (c *Config) CheckClientBankAccounts() {
if len(c.BankAccounts) == 0 {
c.BankAccounts = append(c.BankAccounts,
BankAccount{
ID: "test-bank-01",
BankName: "Test Bank",
BankAddress: "42 Bank Street",
BankPostalCode: "13337",
@@ -137,6 +138,19 @@ func (c *Config) CheckClientBankAccounts() {
}
}
// GetBankAccountByID Returns a bank account based on its ID
func (c *Config) GetBankAccountByID(id string) (*BankAccount, error) {
m.Lock()
defer m.Unlock()
for x := range c.BankAccounts {
if strings.EqualFold(c.BankAccounts[x].ID, id) {
return &c.BankAccounts[x], nil
}
}
return nil, fmt.Errorf(ErrBankAccountNotFound, id)
}
// PurgeExchangeAPICredentials purges the stored API credentials
func (c *Config) PurgeExchangeAPICredentials() {
m.Lock()

View File

@@ -198,6 +198,30 @@ func TestCheckClientBankAccounts(t *testing.T) {
}
}
func TestGetBankAccountByID(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(TestFile, true)
if err != nil {
t.Error("CheckClientBankAccounts LoadConfig error", err)
}
cfg.BankAccounts = nil
cfg.CheckClientBankAccounts()
if len(cfg.BankAccounts) == 0 {
t.Error("CheckClientBankAccounts error:", err)
}
_, err = cfg.GetBankAccountByID("test-bank-01")
if err != nil {
t.Error(err)
}
_, err = cfg.GetBankAccountByID("invalid-test-bank-01")
if err == nil {
t.Error("error expected for invalid account received nil")
}
}
func TestPurgeExchangeCredentials(t *testing.T) {
t.Parallel()
var c Config

View File

@@ -49,6 +49,7 @@ const (
ErrFailureOpeningConfig = "fatal error opening %s file. Error: %s"
ErrCheckingConfigValues = "fatal error checking config values. Error: %s"
ErrSavingConfigBytesMismatch = "config file %q bytes comparison doesn't match, read %s expected %s"
ErrBankAccountNotFound = "bank account ID: %v not found"
WarningWebserverCredentialValuesEmpty = "webserver support disabled due to empty Username/Password values"
WarningWebserverListenAddressInvalid = "webserver support disabled due to invalid listen address"
WarningExchangeAuthAPIDefaultOrEmptyValues = "exchange %s authenticated API support disabled due to default/empty APIKey/Secret/ClientID values"
@@ -226,6 +227,7 @@ type CurrencyPairFormatConfig struct {
// currency
type BankAccount struct {
Enabled bool `json:"enabled"`
ID string `json:"id,omitempty"`
BankName string `json:"bankName"`
BankAddress string `json:"bankAddress"`
BankPostalCode string `json:"bankPostalCode"`