Skip to content

Commit

Permalink
[Config] Add "ID" field to BankAccount struct & GetBankAccountByID me…
Browse files Browse the repository at this point in the history
…thod (thrasher-corp#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
  • Loading branch information
xtda authored and thrasher- committed Dec 18, 2019
1 parent e37512e commit edc07f7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit edc07f7

Please sign in to comment.