Files
gocryptotrader/config/config_test.go
Scott 3a66e99899 Authenticated Websocket support (#315)
* Improves subscribing by not allowing duplicates. Adds bitmex auth support

* Adds coinbase pro support. Partial BTCC support. Adds WebsocketAuthenticatedEndpointsSupported websocket feature. Adds GateIO support

* Adds Coinut support

* Moves Coinut WS types to file. Implements Gemini's secure WS endpoint

* Adds HitBTC ws authenticated support. Fixes var names

* Adds huobi and hadax authenticated websocket support

* Adds auth to okgroup (okex, okcoin). Fixes some linting

* Adds Poloniex support

* Adds ZB support

* Adds proper bitmex support

* Improves bitfinex support, improves websocket functionality definitions

* Fixes coinbasepro auth

* Tests all endpoints

* go formatting, importing, linting run

* Adds wrapper supports

* General clean up. Data race destruction

* Improves testing on all exchanges except ZB

* Fixes ZB hashing, parsing and tests

* minor nits before someone else sees them <_<

* Fixes some nits pertaining to variable usage, comments, typos and rate limiting

* Addresses nits regarding types and test responses where applicable

* fmt import

* Fixes linting issues

* No longer returns an error on failure to authenticate, just logs. Adds new AuthenticatedWebsocketAPISupport config value to allow a user to seperate auth from REST and WS. Prevents WS auth if AuthenticatedWebsocketAPISupport is false, adds additional login check 'CanUseAuthenticatedEndpoints' for when login only occurs once (not per request). Removes unnecessary time.Sleeps from code. Moves WS auth error logic to auth function so that wrappers can get involved in all the auth fun. New-fandangled shared test package, used exclusively in testing, will be the store of all the constant boilerplate things like timeout values. Moves WS test setup function to only run once when there are multiple WS endpoint tests. Cleans up some struct types

* Increases test coverage with tests for config.areAuthenticatedCredentialsValid config.CheckExchangeConfigValues, exchange.SetAPIKeys, exchange.GetAuthenticatedAPISupport, exchange_websocket.CanUseAuthenticatedEndpoitns and exchange_websocket.SetCanUseAuthenticatedEndpoints. Adds b.Websocket.SetCanUseAuthenticatedEndpoints(false) when bitfinex fails to authenticate
Fixes a typo. gofmt and goimport

* Trim Test Typos

* Reformats various websocket types. Adds more specific error messaging to config.areAuthenticatedCredentialsValid
2019-06-19 13:19:01 +10:00

1122 lines
32 KiB
Go

package config
import (
"strings"
"testing"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency"
log "github.com/thrasher-/gocryptotrader/logger"
"github.com/thrasher-/gocryptotrader/ntpclient"
)
const (
// Default number of enabled exchanges. Modify this whenever an exchange is
// added or removed
defaultEnabledExchanges = 27
)
func TestGetCurrencyConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetCurrencyConfig LoadConfig error", err)
}
_ = cfg.GetCurrencyConfig()
}
func TestGetExchangeBankAccounts(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetExchangeBankAccounts LoadConfig error", err)
}
_, err = cfg.GetExchangeBankAccounts("Bitfinex", "USD")
if err != nil {
t.Error("Test failed. GetExchangeBankAccounts error", err)
}
_, err = cfg.GetExchangeBankAccounts("Not an exchange", "Not a currency")
if err == nil {
t.Error("Test failed. GetExchangeBankAccounts, no error returned for invalid exchange")
}
}
func TestUpdateExchangeBankAccounts(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. UpdateExchangeBankAccounts LoadConfig error", err)
}
b := []BankAccount{{Enabled: false}}
err = cfg.UpdateExchangeBankAccounts("Bitfinex", b)
if err != nil {
t.Error("Test failed. UpdateExchangeBankAccounts error", err)
}
var count int
for _, exch := range cfg.Exchanges {
if exch.Name == "Bitfinex" {
if !exch.BankAccounts[0].Enabled {
count++
}
}
}
if count != 1 {
t.Error("Test failed. UpdateExchangeBankAccounts error")
}
err = cfg.UpdateExchangeBankAccounts("Not an exchange", b)
if err == nil {
t.Error("Test failed. UpdateExchangeBankAccounts, no error returned for invalid exchange")
}
}
func TestGetClientBankAccounts(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetClientBankAccounts LoadConfig error", err)
}
_, err = cfg.GetClientBankAccounts("Kraken", "USD")
if err != nil {
t.Error("Test failed. GetClientBankAccounts error", err)
}
_, err = cfg.GetClientBankAccounts("Bla", "USD")
if err == nil {
t.Error("Test failed. GetClientBankAccounts error")
}
_, err = cfg.GetClientBankAccounts("Kraken", "JPY")
if err == nil {
t.Error("Test failed. GetClientBankAccounts error", err)
}
}
func TestUpdateClientBankAccounts(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. UpdateClientBankAccounts LoadConfig error", err)
}
b := BankAccount{Enabled: false, BankName: "test", AccountNumber: "0234"}
err = cfg.UpdateClientBankAccounts(&b)
if err != nil {
t.Error("Test failed. UpdateClientBankAccounts error", err)
}
err = cfg.UpdateClientBankAccounts(&BankAccount{})
if err == nil {
t.Error("Test failed. UpdateClientBankAccounts error")
}
var count int
for _, bank := range cfg.BankAccounts {
if bank.BankName == b.BankName {
if !bank.Enabled {
count++
}
}
}
if count != 1 {
t.Error("Test failed. UpdateClientBankAccounts error")
}
}
func TestCheckClientBankAccounts(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. CheckClientBankAccounts LoadConfig error", err)
}
cfg.BankAccounts = nil
err = cfg.CheckClientBankAccounts()
if err != nil || len(cfg.BankAccounts) == 0 {
t.Error("Test failed. CheckClientBankAccounts error:", err)
}
cfg.BankAccounts = nil
cfg.BankAccounts = append(cfg.BankAccounts, BankAccount{
Enabled: true,
BankName: "test",
})
err = cfg.CheckClientBankAccounts()
if err.Error() != "banking details for test is enabled but variables not set correctly" {
t.Error("Test failed. CheckClientBankAccounts unexpected error:", err)
}
cfg.BankAccounts[0].BankAddress = "test"
err = cfg.CheckClientBankAccounts()
if err.Error() != "banking account details for test variables not set correctly" {
t.Error("Test failed. CheckClientBankAccounts unexpected error:", err)
}
cfg.BankAccounts[0].AccountName = "Thrasher"
cfg.BankAccounts[0].AccountNumber = "1337"
err = cfg.CheckClientBankAccounts()
if err.Error() != "critical banking numbers not set for test in Thrasher account" {
t.Error("Test failed. CheckClientBankAccounts unexpected error:", err)
}
cfg.BankAccounts[0].IBAN = "12345678"
err = cfg.CheckClientBankAccounts()
if err != nil {
t.Error("Test failed. CheckClientBankAccounts error:", err)
}
if cfg.BankAccounts[0].SupportedExchanges == "" {
t.Error("Test failed. CheckClientBankAccounts SupportedExchanges unexpectedly nil, data:",
cfg.BankAccounts[0])
}
}
func TestGetCommunicationsConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetCommunicationsConfig LoadConfig error", err)
}
_ = cfg.GetCommunicationsConfig()
}
func TestUpdateCommunicationsConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. UpdateCommunicationsConfig LoadConfig error", err)
}
cfg.UpdateCommunicationsConfig(&CommunicationsConfig{SlackConfig: SlackConfig{Name: "TEST"}})
if cfg.Communications.SlackConfig.Name != "TEST" {
t.Error("Test failed. UpdateCommunicationsConfig LoadConfig error")
}
}
func TestGetCryptocurrencyProviderConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetCryptocurrencyProviderConfig LoadConfig error", err)
}
_ = cfg.GetCryptocurrencyProviderConfig()
}
func TestUpdateCryptocurrencyProviderConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. UpdateCryptocurrencyProviderConfig LoadConfig error", err)
}
orig := cfg.GetCryptocurrencyProviderConfig()
cfg.UpdateCryptocurrencyProviderConfig(CryptocurrencyProvider{Name: "SERIOUS TESTING PROCEDURE!"})
if cfg.Currency.CryptocurrencyProvider.Name != "SERIOUS TESTING PROCEDURE!" {
t.Error("Test failed. UpdateCurrencyProviderConfig LoadConfig error")
}
cfg.UpdateCryptocurrencyProviderConfig(orig)
}
func TestCheckCommunicationsConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. CheckCommunicationsConfig LoadConfig error", err)
}
cfg.Communications = CommunicationsConfig{}
cfg.CheckCommunicationsConfig()
if cfg.Communications.SlackConfig.Name != "Slack" ||
cfg.Communications.SMSGlobalConfig.Name != "SMSGlobal" ||
cfg.Communications.SMTPConfig.Name != "SMTP" ||
cfg.Communications.TelegramConfig.Name != "Telegram" {
t.Error("Test failed. CheckCommunicationsConfig unexpected data:",
cfg.Communications)
}
cfg.SMS = &SMSGlobalConfig{}
cfg.Communications.SMSGlobalConfig.Name = ""
cfg.CheckCommunicationsConfig()
if cfg.Communications.SMSGlobalConfig.Password != "test" {
t.Error("Test failed. CheckCommunicationsConfig error:", err)
}
cfg.SMS.Contacts = append(cfg.SMS.Contacts, SMSContact{
Name: "Bobby",
Number: "4321",
Enabled: false,
})
cfg.Communications.SMSGlobalConfig.Name = ""
cfg.CheckCommunicationsConfig()
if cfg.Communications.SMSGlobalConfig.Contacts[0].Name != "Bobby" {
t.Error("Test failed. CheckCommunicationsConfig error:", err)
}
cfg.SMS = &SMSGlobalConfig{}
cfg.CheckCommunicationsConfig()
if cfg.SMS != nil {
t.Error("Test failed. CheckCommunicationsConfig unexpected data:",
cfg.SMS)
}
cfg.Communications.SlackConfig.Name = "NOT Slack"
cfg.CheckCommunicationsConfig()
cfg.Communications.SlackConfig.Name = "Slack"
cfg.Communications.SlackConfig.Enabled = true
cfg.CheckCommunicationsConfig()
if cfg.Communications.SlackConfig.Enabled {
t.Error("Test failed. CheckCommunicationsConfig Slack is enabled when it shouldn't be.")
}
cfg.Communications.SlackConfig.Enabled = false
cfg.Communications.SMSGlobalConfig.Enabled = true
cfg.Communications.SMSGlobalConfig.Password = ""
cfg.CheckCommunicationsConfig()
if cfg.Communications.SlackConfig.Enabled {
t.Error("Test failed. CheckCommunicationsConfig SMSGlobal is enabled when it shouldn't be.")
}
cfg.Communications.SMSGlobalConfig.Enabled = false
cfg.Communications.SMTPConfig.Enabled = true
cfg.Communications.SMTPConfig.AccountPassword = ""
cfg.CheckCommunicationsConfig()
if cfg.Communications.SlackConfig.Enabled {
t.Error("Test failed. CheckCommunicationsConfig SMTPConfig is enabled when it shouldn't be.")
}
cfg.Communications.SMTPConfig.Enabled = false
cfg.Communications.TelegramConfig.Enabled = true
cfg.Communications.TelegramConfig.VerificationToken = ""
cfg.CheckCommunicationsConfig()
if cfg.Communications.TelegramConfig.Enabled {
t.Error("Test failed. CheckCommunicationsConfig TelegramConfig is enabled when it shouldn't be.")
}
}
func TestCheckPairConsistency(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. CheckPairConsistency LoadConfig error", err)
}
err = cfg.CheckPairConsistency("asdf")
if err == nil {
t.Error("Test failed. CheckPairConsistency. Non-existent exchange returned nil error")
}
cfg.Exchanges = append(cfg.Exchanges, ExchangeConfig{
Name: "TestExchange",
Enabled: true,
AvailablePairs: currency.NewPairsFromStrings([]string{"DOGE_USD,DOGE_AUD"}),
EnabledPairs: currency.NewPairsFromStrings([]string{"DOGE_USD,DOGE_AUD,DOGE_BTC"}),
ConfigCurrencyPairFormat: &CurrencyPairFormatConfig{
Uppercase: true,
Delimiter: "_",
},
})
tec, err := cfg.GetExchangeConfig("TestExchange")
if err != nil {
t.Error("Test failed. CheckPairConsistency GetExchangeConfig error", err)
}
err = cfg.CheckPairConsistency("TestExchange")
if err != nil {
t.Error("Test failed. CheckPairConsistency error:", err)
}
// Calling again immediately to hit the if !update {return nil}
err = cfg.CheckPairConsistency("TestExchange")
if err != nil {
t.Error("Test failed. CheckPairConsistency error:", err)
}
tec.EnabledPairs = currency.NewPairsFromStrings([]string{"DOGE_LTC,BTC_LTC"})
err = cfg.UpdateExchangeConfig(&tec)
if err != nil {
t.Error("Test failed. CheckPairConsistency Update config failed, error:", err)
}
err = cfg.CheckPairConsistency("TestExchange")
if err != nil {
t.Error("Test failed. CheckPairConsistency error:", err)
}
}
func TestSupportsPair(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestSupportsPair. LoadConfig Error: %s", err.Error(),
)
}
_, err = cfg.SupportsPair("asdf",
currency.NewPair(currency.BTC, currency.USD))
if err == nil {
t.Error(
"Test failed. TestSupportsPair. Non-existent exchange returned nil error",
)
}
_, err = cfg.SupportsPair("Bitfinex",
currency.NewPair(currency.BTC, currency.USD))
if err != nil {
t.Errorf(
"Test failed. TestSupportsPair. Incorrect values. Err: %s", err,
)
}
}
func TestGetAvailablePairs(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestGetAvailablePairs. LoadConfig Error: %s", err.Error())
}
_, err = cfg.GetAvailablePairs("asdf")
if err == nil {
t.Error(
"Test failed. TestGetAvailablePairs. Non-existent exchange returned nil error")
}
_, err = cfg.GetAvailablePairs("Bitfinex")
if err != nil {
t.Errorf(
"Test failed. TestGetAvailablePairs. Incorrect values. Err: %s", err)
}
}
func TestGetEnabledPairs(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestGetEnabledPairs. LoadConfig Error: %s", err.Error())
}
_, err = cfg.GetEnabledPairs("asdf")
if err == nil {
t.Error(
"Test failed. TestGetEnabledPairs. Non-existent exchange returned nil error")
}
_, err = cfg.GetEnabledPairs("Bitfinex")
if err != nil {
t.Errorf(
"Test failed. TestGetEnabledPairs. Incorrect values. Err: %s", err)
}
}
func TestGetEnabledExchanges(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestGetEnabledExchanges. LoadConfig Error: %s", err.Error(),
)
}
exchanges := cfg.GetEnabledExchanges()
if len(exchanges) != defaultEnabledExchanges {
t.Error(
"Test failed. TestGetEnabledExchanges. Enabled exchanges value mismatch",
)
}
if !common.StringDataCompare(exchanges, "Bitfinex") {
t.Error(
"Test failed. TestGetEnabledExchanges. Expected exchange Bitfinex not found",
)
}
}
func TestGetDisabledExchanges(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestGetDisabledExchanges. LoadConfig Error: %s", err.Error(),
)
}
exchanges := cfg.GetDisabledExchanges()
if len(exchanges) != 0 {
t.Error(
"Test failed. TestGetDisabledExchanges. Enabled exchanges value mismatch",
)
}
exchCfg, err := cfg.GetExchangeConfig("Bitfinex")
if err != nil {
t.Errorf(
"Test failed. TestGetDisabledExchanges. GetExchangeConfig Error: %s", err.Error(),
)
}
exchCfg.Enabled = false
err = cfg.UpdateExchangeConfig(&exchCfg)
if err != nil {
t.Errorf(
"Test failed. TestGetDisabledExchanges. UpdateExchangeConfig Error: %s", err.Error(),
)
}
if len(cfg.GetDisabledExchanges()) != 1 {
t.Error(
"Test failed. TestGetDisabledExchanges. Enabled exchanges value mismatch",
)
}
}
func TestCountEnabledExchanges(t *testing.T) {
GetConfigEnabledExchanges := GetConfig()
err := GetConfigEnabledExchanges.LoadConfig(ConfigTestFile)
if err != nil {
t.Error(
"Test failed. GetConfigEnabledExchanges load config error: " + err.Error(),
)
}
enabledExch := GetConfigEnabledExchanges.CountEnabledExchanges()
if enabledExch != defaultEnabledExchanges {
t.Error("Test failed. GetConfigEnabledExchanges is wrong")
}
}
func TestGetConfigCurrencyPairFormat(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestGetConfigCurrencyPairFormat. LoadConfig Error: %s", err.Error(),
)
}
_, err = cfg.GetConfigCurrencyPairFormat("asdasdasd")
if err == nil {
t.Errorf(
"Test failed. TestGetRequestCurrencyPairFormat. Non-existent exchange returned nil error",
)
}
exchFmt, err := cfg.GetConfigCurrencyPairFormat("Yobit")
if err != nil {
t.Errorf("Test failed. TestGetConfigCurrencyPairFormat err: %s", err)
}
if !exchFmt.Uppercase || exchFmt.Delimiter != "_" {
t.Errorf(
"Test failed. TestGetConfigCurrencyPairFormat. Invalid values",
)
}
}
func TestGetRequestCurrencyPairFormat(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestGetRequestCurrencyPairFormat. LoadConfig Error: %s", err.Error(),
)
}
_, err = cfg.GetRequestCurrencyPairFormat("asdasdasd")
if err == nil {
t.Errorf(
"Test failed. TestGetRequestCurrencyPairFormat. Non-existent exchange returned nil error",
)
}
exchFmt, err := cfg.GetRequestCurrencyPairFormat("Yobit")
if err != nil {
t.Errorf("Test failed. TestGetRequestCurrencyPairFormat. Err: %s", err)
}
if exchFmt.Uppercase || exchFmt.Delimiter != "_" || exchFmt.Separator != "-" {
t.Errorf(
"Test failed. TestGetRequestCurrencyPairFormat. Invalid values",
)
}
}
func TestGetCurrencyPairDisplayConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. GetCurrencyPairDisplayConfig. LoadConfig Error: %s", err.Error(),
)
}
settings := cfg.GetCurrencyPairDisplayConfig()
if settings.Delimiter != "-" || !settings.Uppercase {
t.Errorf(
"Test failed. GetCurrencyPairDisplayConfi. Invalid values",
)
}
}
func TestGetAllExchangeConfigs(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetAllExchangeConfigs. LoadConfig error", err)
}
if len(cfg.GetAllExchangeConfigs()) < 26 {
t.Error("Test failed. GetAllExchangeConfigs error")
}
}
func TestGetExchangeConfig(t *testing.T) {
GetExchangeConfig := GetConfig()
err := GetExchangeConfig.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. GetExchangeConfig.LoadConfig Error: %s", err.Error(),
)
}
_, err = GetExchangeConfig.GetExchangeConfig("ANX")
if err != nil {
t.Errorf("Test failed. GetExchangeConfig.GetExchangeConfig Error: %s",
err.Error())
}
_, err = GetExchangeConfig.GetExchangeConfig("Testy")
if err == nil {
t.Error("Test failed. GetExchangeConfig.GetExchangeConfig Error")
}
}
func TestGetForexProviderConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetForexProviderConfig. LoadConfig error", err)
}
_, err = cfg.GetForexProviderConfig("Fixer")
if err != nil {
t.Error("Test failed. GetForexProviderConfig error", err)
}
_, err = cfg.GetForexProviderConfig("this is not a forex provider")
if err == nil {
t.Error("Test failed. GetForexProviderConfig no error for invalid provider")
}
}
func TestGetPrimaryForexProvider(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetPrimaryForexProvider. LoadConfig error", err)
}
primary := cfg.GetPrimaryForexProvider()
if primary == "" {
t.Error("Test failed. GetPrimaryForexProvider error")
}
for i := range cfg.Currency.ForexProviders {
cfg.Currency.ForexProviders[i].PrimaryProvider = false
}
primary = cfg.GetPrimaryForexProvider()
if primary != "" {
t.Error("Test failed. GetPrimaryForexProvider error, expected nil got:", primary)
}
}
func TestUpdateExchangeConfig(t *testing.T) {
UpdateExchangeConfig := GetConfig()
err := UpdateExchangeConfig.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. UpdateExchangeConfig.LoadConfig Error: %s", err.Error(),
)
}
e, err2 := UpdateExchangeConfig.GetExchangeConfig("ANX")
if err2 != nil {
t.Errorf(
"Test failed. UpdateExchangeConfig.GetExchangeConfig: %s", err.Error(),
)
}
e.APIKey = "test1234"
err3 := UpdateExchangeConfig.UpdateExchangeConfig(&e)
if err3 != nil {
t.Errorf(
"Test failed. UpdateExchangeConfig.UpdateExchangeConfig: %s", err.Error(),
)
}
e.Name = "testyTest"
err = UpdateExchangeConfig.UpdateExchangeConfig(&e)
if err == nil {
t.Error("Test failed. UpdateExchangeConfig.UpdateExchangeConfig Error")
}
}
// TestCheckExchangeConfigValues logic test
func TestCheckExchangeConfigValues(t *testing.T) {
checkExchangeConfigValues := Config{}
err := checkExchangeConfigValues.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.LoadConfig: %s", err.Error(),
)
}
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err != nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues: %s",
err.Error(),
)
}
checkExchangeConfigValues.Exchanges[0].HTTPTimeout = 0
checkExchangeConfigValues.CheckExchangeConfigValues()
if checkExchangeConfigValues.Exchanges[0].HTTPTimeout == 0 {
t.Fatalf("Test failed. Expected exchange %s to have updated HTTPTimeout value", checkExchangeConfigValues.Exchanges[0].Name)
}
checkExchangeConfigValues.Exchanges[0].APIKey = "Key"
checkExchangeConfigValues.Exchanges[0].APISecret = "Secret"
checkExchangeConfigValues.Exchanges[0].AuthenticatedAPISupport = true
checkExchangeConfigValues.Exchanges[0].AuthenticatedWebsocketAPISupport = true
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err != nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
if checkExchangeConfigValues.Exchanges[0].AuthenticatedWebsocketAPISupport {
t.Error("Expected AuthenticatedWebsocketAPISupport to be false from invalid API keys")
}
checkExchangeConfigValues.Exchanges[0].AuthenticatedAPISupport = true
checkExchangeConfigValues.Exchanges[0].APIKey = "TESTYTEST"
checkExchangeConfigValues.Exchanges[0].APISecret = "TESTYTEST"
checkExchangeConfigValues.Exchanges[0].Name = "ITBIT"
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err != nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
if checkExchangeConfigValues.Exchanges[0].AuthenticatedAPISupport {
t.Error("Expected AuthenticatedAPISupport to be true from valid API keys")
}
checkExchangeConfigValues.Exchanges[0].AuthenticatedAPISupport = true
checkExchangeConfigValues.Exchanges[0].AuthenticatedWebsocketAPISupport = true
checkExchangeConfigValues.Exchanges[0].APIKey = ""
checkExchangeConfigValues.Exchanges[0].APISecret = ""
checkExchangeConfigValues.Exchanges[0].Name = "ITBIT"
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err != nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
if checkExchangeConfigValues.Exchanges[0].AuthenticatedAPISupport || checkExchangeConfigValues.Exchanges[0].AuthenticatedWebsocketAPISupport {
t.Error("Expected AuthenticatedAPISupport and AuthenticatedWebsocketAPISupport to be false from invalid API keys")
}
checkExchangeConfigValues.Exchanges[0].BaseCurrencies = currency.NewCurrenciesFromStringArray([]string{""})
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err == nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
checkExchangeConfigValues.Exchanges[0].EnabledPairs = currency.NewPairsFromStrings([]string{""})
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err == nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
checkExchangeConfigValues.Exchanges[0].AvailablePairs = currency.NewPairsFromStrings([]string{""})
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err == nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
checkExchangeConfigValues.Exchanges[0].Name = ""
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err == nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
checkExchangeConfigValues.Cryptocurrencies = currency.NewCurrenciesFromStringArray([]string{""})
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err == nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
checkExchangeConfigValues.Exchanges = checkExchangeConfigValues.Exchanges[:0]
checkExchangeConfigValues.Cryptocurrencies = currency.NewCurrenciesFromStringArray([]string{"TESTYTEST"})
err = checkExchangeConfigValues.CheckExchangeConfigValues()
if err == nil {
t.Errorf(
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
)
}
}
func TestCheckWebserverConfigValues(t *testing.T) {
checkWebserverConfigValues := GetConfig()
err := checkWebserverConfigValues.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. checkWebserverConfigValues.LoadConfig: %s", err.Error(),
)
}
err = checkWebserverConfigValues.CheckWebserverConfigValues()
if err != nil {
t.Errorf(
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues: %s",
err.Error(),
)
}
checkWebserverConfigValues.Webserver.WebsocketConnectionLimit = -1
err = checkWebserverConfigValues.CheckWebserverConfigValues()
if err != nil {
t.Errorf(
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues: %s",
err.Error(),
)
}
if checkWebserverConfigValues.Webserver.WebsocketConnectionLimit != 1 {
t.Error(
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues error",
)
}
checkWebserverConfigValues.Webserver.WebsocketMaxAuthFailures = -1
checkWebserverConfigValues.CheckWebserverConfigValues()
if checkWebserverConfigValues.Webserver.WebsocketMaxAuthFailures != 3 {
t.Error(
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues error",
)
}
checkWebserverConfigValues.Webserver.ListenAddress = ":0"
err = checkWebserverConfigValues.CheckWebserverConfigValues()
if err == nil {
t.Error(
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues error",
)
}
checkWebserverConfigValues.Webserver.ListenAddress = ":LOLOLOL"
err = checkWebserverConfigValues.CheckWebserverConfigValues()
if err == nil {
t.Error(
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues error",
)
}
checkWebserverConfigValues.Webserver.ListenAddress = "LOLOLOL"
err = checkWebserverConfigValues.CheckWebserverConfigValues()
if err == nil {
t.Error(
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues error",
)
}
checkWebserverConfigValues.Webserver.AdminUsername = ""
err = checkWebserverConfigValues.CheckWebserverConfigValues()
if err == nil {
t.Error(
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues error",
)
}
}
func TestRetrieveConfigCurrencyPairs(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestRetrieveConfigCurrencyPairs.LoadConfig: %s", err.Error(),
)
}
err = cfg.RetrieveConfigCurrencyPairs(true)
if err != nil {
t.Errorf(
"Test failed. TestRetrieveConfigCurrencyPairs.RetrieveConfigCurrencyPairs: %s",
err.Error(),
)
}
err = cfg.RetrieveConfigCurrencyPairs(false)
if err != nil {
t.Errorf(
"Test failed. TestRetrieveConfigCurrencyPairs.RetrieveConfigCurrencyPairs: %s",
err.Error(),
)
}
}
func TestReadConfig(t *testing.T) {
readConfig := GetConfig()
err := readConfig.ReadConfig(ConfigTestFile)
if err != nil {
t.Errorf("Test failed. TestReadConfig %s", err.Error())
}
err = readConfig.ReadConfig("bla")
if err == nil {
t.Error("Test failed. TestReadConfig error cannot be nil")
}
err = readConfig.ReadConfig("")
if err != nil {
t.Error("Test failed. TestReadConfig error")
}
}
func TestLoadConfig(t *testing.T) {
loadConfig := GetConfig()
err := loadConfig.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. TestLoadConfig " + err.Error())
}
err = loadConfig.LoadConfig("testy")
if err == nil {
t.Error("Test failed. TestLoadConfig ")
}
}
func TestSaveConfig(t *testing.T) {
saveConfig := GetConfig()
err := saveConfig.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf("Test failed. TestSaveConfig.LoadConfig: %s", err.Error())
}
err2 := saveConfig.SaveConfig(ConfigTestFile)
if err2 != nil {
t.Errorf("Test failed. TestSaveConfig.SaveConfig, %s", err2.Error())
}
}
func TestGetFilePath(t *testing.T) {
expected := "blah.json"
result, _ := GetFilePath("blah.json")
if result != "blah.json" {
t.Errorf("Test failed. TestGetFilePath: expected %s got %s", expected, result)
}
expected = ConfigTestFile
result, _ = GetFilePath("")
if result != expected {
t.Errorf("Test failed. TestGetFilePath: expected %s got %s", expected, result)
}
testBypass = true
}
func TestCheckConfig(t *testing.T) {
var c Config
err := c.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf("Test failed. %s", err)
}
err = c.CheckConfig()
if err != nil {
t.Fatal(err)
}
}
func TestUpdateConfig(t *testing.T) {
var c Config
err := c.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf("Test failed. %s", err)
}
newCfg := c
err = c.UpdateConfig(ConfigTestFile, &newCfg)
if err != nil {
t.Fatalf("Test failed. %s", err)
}
err = c.UpdateConfig("//non-existantpath\\", &newCfg)
if err == nil {
t.Fatalf("Test failed. Error should of been thrown for invalid path")
}
newCfg.Currency.Cryptocurrencies = currency.NewCurrenciesFromStringArray([]string{""})
err = c.UpdateConfig(ConfigTestFile, &newCfg)
if err != nil {
t.Errorf("Test failed. %s", err)
}
if c.Currency.Cryptocurrencies.Join() == "" {
t.Fatalf("Test failed. Cryptocurrencies should have been repopulated")
}
}
func BenchmarkUpdateConfig(b *testing.B) {
var c Config
err := c.LoadConfig(ConfigTestFile)
if err != nil {
b.Errorf("Unable to benchmark UpdateConfig(): %s", err)
}
newCfg := c
for i := 0; i < b.N; i++ {
_ = c.UpdateConfig(ConfigTestFile, &newCfg)
}
}
func TestCheckLoggerConfig(t *testing.T) {
c := GetConfig()
err := c.LoadConfig(ConfigTestFile)
if err != nil {
t.Fatal(err)
}
c.Logging = log.Logging{}
err = c.CheckLoggerConfig()
if err != nil {
t.Errorf("Failed to create default logger reason: %v", err)
}
c.LoadConfig(ConfigTestFile)
err = c.CheckLoggerConfig()
if err != nil {
t.Errorf("Failed to create logger with user settings: reason: %v", err)
}
}
func TestDisableNTPCheck(t *testing.T) {
c := GetConfig()
err := c.LoadConfig(ConfigTestFile)
if err != nil {
t.Fatal(err)
}
warn, err := c.DisableNTPCheck(strings.NewReader("w\n"))
if err != nil {
t.Fatalf("test failed to create ntpclient failed reason: %v", err)
}
if warn != "Time sync has been set to warn only" {
t.Errorf("failed expected %v got %v", "Time sync has been set to warn only", warn)
}
alert, _ := c.DisableNTPCheck(strings.NewReader("a\n"))
if alert != "Time sync has been set to alert" {
t.Errorf("failed expected %v got %v", "Time sync has been set to alert", alert)
}
disable, _ := c.DisableNTPCheck(strings.NewReader("d\n"))
if disable != "Future notications for out time sync have been disabled" {
t.Errorf("failed expected %v got %v", "Future notications for out time sync have been disabled", disable)
}
_, err = c.DisableNTPCheck(strings.NewReader(" "))
if err.Error() != "EOF" {
t.Errorf("failed expected EOF got: %v", err)
}
}
func TestCheckNTPConfig(t *testing.T) {
c := GetConfig()
c.NTPClient.Level = 0
c.NTPClient.Pool = nil
c.NTPClient.AllowedNegativeDifference = nil
c.NTPClient.AllowedDifference = nil
c.CheckNTPConfig()
_, err := ntpclient.NTPClient(c.NTPClient.Pool)
if err != nil {
t.Fatalf("test failed to create ntpclient failed reason: %v", err)
}
if c.NTPClient.Pool[0] != "pool.ntp.org:123" {
t.Error("ntpclient with no valid pool should default to pool.ntp.org ")
}
if c.NTPClient.AllowedDifference == nil {
t.Error("ntpclient with nil alloweddifference should default to sane value")
}
if c.NTPClient.AllowedNegativeDifference == nil {
t.Error("ntpclient with nil allowednegativedifference should default to sane value")
}
}
// TestAreAuthenticatedCredentialsValid logic test
func TestAreAuthenticatedCredentialsValid(t *testing.T) {
var c Config
resp := c.areAuthenticatedCredentialsValid(0)
if resp {
t.Error("Expecting false with no exchanges loaded")
}
resp = c.areAuthenticatedCredentialsValid(-1)
if resp {
t.Error("Expecting false with an invalid index")
}
c.Exchanges = []ExchangeConfig{
{
APIKey: "",
APISecret: "",
ClientID: "",
Name: "",
},
}
resp = c.areAuthenticatedCredentialsValid(0)
if resp {
t.Error("Expecting false with no credentials set")
}
c.Exchanges[0].APIKey = DefaultUnsetAPIKey
c.Exchanges[0].APISecret = DefaultUnsetAPISecret
resp = c.areAuthenticatedCredentialsValid(0)
if resp {
t.Error("Expecting false with default credentials set")
}
c.Exchanges[0].Name = "COINUT"
resp = c.areAuthenticatedCredentialsValid(0)
if resp {
t.Error("Expecting false with COINUT and no APIKEY set")
}
c.Exchanges[0].APIKey = "Im a key!"
c.Exchanges[0].ClientID = "Im a Client!"
resp = c.areAuthenticatedCredentialsValid(0)
if !resp {
t.Error("Expecting true with COINUT api credentials set")
}
c.Exchanges[0].Name = "Bitstamp"
resp = c.areAuthenticatedCredentialsValid(0)
if resp {
t.Error("Expecting false with Bitstamp and no APISecret set")
}
c.Exchanges[0].APISecret = "Im a Secret!"
resp = c.areAuthenticatedCredentialsValid(0)
if !resp {
t.Error("Expecting true with Bitstamp api credentials set")
}
c.Exchanges[0].Name = "ANX"
c.Exchanges[0].APIKey = DefaultUnsetAPIKey
c.Exchanges[0].APISecret = "Im a Secret!"
resp = c.areAuthenticatedCredentialsValid(0)
if resp {
t.Error("Expecting false with ANX and no APIKey set")
}
resp = c.areAuthenticatedCredentialsValid(1337)
if resp {
t.Error("Expecting false with an invalid index")
}
}