mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
added config formatting and test code
This commit is contained in:
committed by
Adrian Gallagher
parent
06786ed9de
commit
4e6885410c
@@ -15,16 +15,17 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/portfolio"
|
||||
)
|
||||
|
||||
// Constants declared here are filename strings and test strings
|
||||
const (
|
||||
CONFIG_FILE = "config.dat"
|
||||
OLD_CONFIG_FILE = "config.json"
|
||||
CONFIG_TEST_FILE = "../testdata/configtest.dat"
|
||||
|
||||
CONFIG_FILE_ENCRYPTION_PROMPT = 0
|
||||
CONFIG_FILE_ENCRYPTION_ENABLED = 1
|
||||
CONFIG_FILE_ENCRYPTION_DISABLED = -1
|
||||
ConfigFile = "config.dat"
|
||||
OldConfigFile = "config.json"
|
||||
ConfigTestFile = "../testdata/configtest.dat"
|
||||
configFileEncryptionPrompt = 0
|
||||
configFileEncryptionEnabled = 1
|
||||
configFileEncryptionDisabled = -1
|
||||
)
|
||||
|
||||
// Variables here are mainly alerts and a configuration object
|
||||
var (
|
||||
ErrExchangeNameEmpty = "Exchange #%d in config: Exchange name is empty."
|
||||
ErrExchangeAvailablePairsEmpty = "Exchange %s: Available pairs is empty."
|
||||
@@ -47,6 +48,7 @@ var (
|
||||
Cfg Config
|
||||
)
|
||||
|
||||
// WebserverConfig struct holds the prestart variables for the webserver.
|
||||
type WebserverConfig struct {
|
||||
Enabled bool
|
||||
AdminUsername string
|
||||
@@ -54,6 +56,8 @@ type WebserverConfig struct {
|
||||
ListenAddress string
|
||||
}
|
||||
|
||||
// SMSGlobalConfig structure holds all the variables you need for instant
|
||||
// messaging and broadcast used by SMSGlobal
|
||||
type SMSGlobalConfig struct {
|
||||
Enabled bool
|
||||
Username string
|
||||
@@ -65,10 +69,13 @@ type SMSGlobalConfig struct {
|
||||
}
|
||||
}
|
||||
|
||||
type ConfigPost struct {
|
||||
// Post holds the bot configuration data
|
||||
type Post struct {
|
||||
Data Config `json:"Data"`
|
||||
}
|
||||
|
||||
// Config is the overarching object that holds all the information for
|
||||
// prestart management of portfolio, SMSGlobal, webserver and enabled exchange
|
||||
type Config struct {
|
||||
Name string
|
||||
EncryptConfig int
|
||||
@@ -79,6 +86,7 @@ type Config struct {
|
||||
Exchanges []ExchangeConfig `json:"Exchanges"`
|
||||
}
|
||||
|
||||
// ExchangeConfig holds all the information needed for each enabled Exchange.
|
||||
type ExchangeConfig struct {
|
||||
Name string
|
||||
Enabled bool
|
||||
@@ -94,6 +102,7 @@ type ExchangeConfig struct {
|
||||
BaseCurrencies string
|
||||
}
|
||||
|
||||
// GetConfigEnabledExchanges returns the number of exchanges that are enabled.
|
||||
func (c *Config) GetConfigEnabledExchanges() int {
|
||||
counter := 0
|
||||
for i := range c.Exchanges {
|
||||
@@ -104,8 +113,9 @@ func (c *Config) GetConfigEnabledExchanges() int {
|
||||
return counter
|
||||
}
|
||||
|
||||
// GetExchangeConfig returns your exchange configurations by its indivdual name
|
||||
func (c *Config) GetExchangeConfig(name string) (ExchangeConfig, error) {
|
||||
for i, _ := range c.Exchanges {
|
||||
for i := range c.Exchanges {
|
||||
if c.Exchanges[i].Name == name {
|
||||
return c.Exchanges[i], nil
|
||||
}
|
||||
@@ -113,8 +123,9 @@ func (c *Config) GetExchangeConfig(name string) (ExchangeConfig, error) {
|
||||
return ExchangeConfig{}, fmt.Errorf(ErrExchangeNotFound, name)
|
||||
}
|
||||
|
||||
// UpdateExchangeConfig updates exchange configurations
|
||||
func (c *Config) UpdateExchangeConfig(e ExchangeConfig) error {
|
||||
for i, _ := range c.Exchanges {
|
||||
for i := range c.Exchanges {
|
||||
if c.Exchanges[i].Name == e.Name {
|
||||
c.Exchanges[i] = e
|
||||
return nil
|
||||
@@ -123,6 +134,7 @@ func (c *Config) UpdateExchangeConfig(e ExchangeConfig) error {
|
||||
return fmt.Errorf(ErrExchangeNotFound, e.Name)
|
||||
}
|
||||
|
||||
// CheckSMSGlobalConfigValues checks concurrent SMSGlobal configurations
|
||||
func (c *Config) CheckSMSGlobalConfigValues() error {
|
||||
if c.SMS.Username == "" || c.SMS.Username == "Username" || c.SMS.Password == "" || c.SMS.Password == "Password" {
|
||||
return errors.New(WarningSMSGlobalDefaultOrEmptyValues)
|
||||
@@ -143,6 +155,8 @@ func (c *Config) CheckSMSGlobalConfigValues() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckExchangeConfigValues returns configuation values for all enabled
|
||||
// exchanges
|
||||
func (c *Config) CheckExchangeConfigValues() error {
|
||||
if c.Cryptocurrencies == "" {
|
||||
return errors.New(ErrCryptocurrenciesEmpty)
|
||||
@@ -185,6 +199,8 @@ func (c *Config) CheckExchangeConfigValues() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckWebserverConfigValues checks information before webserver starts and
|
||||
// returns an error if values are incorrect.
|
||||
func (c *Config) CheckWebserverConfigValues() error {
|
||||
if c.Webserver.AdminUsername == "" || c.Webserver.AdminPassword == "" {
|
||||
return errors.New(WarningWebserverCredentialValuesEmpty)
|
||||
@@ -206,6 +222,8 @@ func (c *Config) CheckWebserverConfigValues() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RetrieveConfigCurrencyPairs splits, assigns and verifies enabled currency
|
||||
// pairs either cryptoCurrencies or fiatCurrencies
|
||||
func (c *Config) RetrieveConfigCurrencyPairs() error {
|
||||
cryptoCurrencies := common.SplitStrings(c.Cryptocurrencies, ",")
|
||||
fiatCurrencies := common.SplitStrings(currency.DEFAULT_CURRENCIES, ",")
|
||||
@@ -266,22 +284,27 @@ func (c *Config) RetrieveConfigCurrencyPairs() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckConfig checks to see if there is an old configuration filename and path
|
||||
// if found it will change it to correct filename.
|
||||
func CheckConfig() error {
|
||||
_, err := common.ReadFile(OLD_CONFIG_FILE)
|
||||
_, err := common.ReadFile(OldConfigFile)
|
||||
if err == nil {
|
||||
err = os.Rename(OLD_CONFIG_FILE, CONFIG_FILE)
|
||||
err = os.Rename(OldConfigFile, ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf(RenamingConfigFile+"\n", OLD_CONFIG_FILE, CONFIG_FILE)
|
||||
log.Printf(RenamingConfigFile+"\n", OldConfigFile, ConfigFile)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadConfig verifies and checks for encryption and verifies the unencrypted
|
||||
// file contains JSON.
|
||||
func (c *Config) ReadConfig(configPath string) error {
|
||||
defaultPath := ""
|
||||
var defaultPath string
|
||||
|
||||
if configPath == "" {
|
||||
defaultPath = CONFIG_FILE
|
||||
defaultPath = ConfigTestFile
|
||||
} else {
|
||||
defaultPath = configPath
|
||||
}
|
||||
@@ -302,13 +325,13 @@ func (c *Config) ReadConfig(configPath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.EncryptConfig == CONFIG_FILE_ENCRYPTION_DISABLED {
|
||||
if c.EncryptConfig == configFileEncryptionDisabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
if c.EncryptConfig == CONFIG_FILE_ENCRYPTION_PROMPT {
|
||||
if c.EncryptConfig == configFileEncryptionPrompt {
|
||||
if c.PromptForConfigEncryption() {
|
||||
c.EncryptConfig = CONFIG_FILE_ENCRYPTION_ENABLED
|
||||
c.EncryptConfig = configFileEncryptionEnabled
|
||||
return c.SaveConfig("")
|
||||
}
|
||||
}
|
||||
@@ -331,19 +354,21 @@ func (c *Config) ReadConfig(configPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveConfig saves your configuration to your desired path
|
||||
func (c *Config) SaveConfig(configPath string) error {
|
||||
defaultPath := ""
|
||||
var defaultPath string
|
||||
|
||||
if configPath == "" {
|
||||
defaultPath = CONFIG_FILE
|
||||
defaultPath = ConfigFile
|
||||
} else {
|
||||
defaultPath = configPath
|
||||
}
|
||||
|
||||
payload, err := json.MarshalIndent(c, "", " ")
|
||||
|
||||
if c.EncryptConfig == CONFIG_FILE_ENCRYPTION_ENABLED {
|
||||
key, err := PromptForConfigKey()
|
||||
if err != nil {
|
||||
if c.EncryptConfig == configFileEncryptionEnabled {
|
||||
key, err2 := PromptForConfigKey()
|
||||
if err2 != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -360,10 +385,11 @@ func (c *Config) SaveConfig(configPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadConfig loads your configuration file into your configuration object
|
||||
func (c *Config) LoadConfig(configPath string) error {
|
||||
err := c.ReadConfig(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf(ErrFailureOpeningConfig, CONFIG_FILE, err)
|
||||
return fmt.Errorf(ErrFailureOpeningConfig, ConfigFile, err)
|
||||
}
|
||||
|
||||
err = c.CheckExchangeConfigValues()
|
||||
@@ -374,6 +400,7 @@ func (c *Config) LoadConfig(configPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfig returns a pointer to a confiuration object
|
||||
func GetConfig() *Config {
|
||||
return &Cfg
|
||||
}
|
||||
|
||||
@@ -15,11 +15,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
CONFIG_ENCRYPTION_CONFIRMATION_STRING = "THORS-HAMMER"
|
||||
|
||||
ErrConfigDataLessThenRequiredAESBlockSize = "The config file data is too small for the AES required block size."
|
||||
// EncryptConfirmString has a the general confirmation string to allow us to
|
||||
// see if the file is correctly encrypted
|
||||
EncryptConfirmString = "THORS-HAMMER"
|
||||
errAESBlockSize = "The config file data is too small for the AES required block size"
|
||||
errNotAPointer = "Error: parameter interface is not a pointer"
|
||||
)
|
||||
|
||||
// PromptForConfigEncryption asks for encryption key
|
||||
func (c *Config) PromptForConfigEncryption() bool {
|
||||
log.Println("Would you like to encrypt your config file (y/n)?")
|
||||
|
||||
@@ -30,13 +33,14 @@ func (c *Config) PromptForConfigEncryption() bool {
|
||||
}
|
||||
|
||||
if !common.YesOrNo(input) {
|
||||
c.EncryptConfig = CONFIG_FILE_ENCRYPTION_DISABLED
|
||||
c.EncryptConfig = configFileEncryptionDisabled
|
||||
c.SaveConfig("")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// PromptForConfigKey asks for configuration key
|
||||
func PromptForConfigKey() ([]byte, error) {
|
||||
var cryptoKey []byte
|
||||
|
||||
@@ -60,6 +64,8 @@ func PromptForConfigKey() ([]byte, error) {
|
||||
return cryptoKey, nil
|
||||
}
|
||||
|
||||
// EncryptConfigFile encrypts configuration data that is parsed in with a key
|
||||
// and returns it as a byte array with an error
|
||||
func EncryptConfigFile(configData, key []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
@@ -75,11 +81,13 @@ func EncryptConfigFile(configData, key []byte) ([]byte, error) {
|
||||
stream := cipher.NewCFBEncrypter(block, iv)
|
||||
stream.XORKeyStream(ciphertext[aes.BlockSize:], configData)
|
||||
|
||||
appendedFile := []byte(CONFIG_ENCRYPTION_CONFIRMATION_STRING)
|
||||
appendedFile := []byte(EncryptConfirmString)
|
||||
appendedFile = append(appendedFile, ciphertext...)
|
||||
return appendedFile, nil
|
||||
}
|
||||
|
||||
// DecryptConfigFile decrypts configuration data with the supplied key and
|
||||
// returns the un-encrypted file as a byte array with an error
|
||||
func DecryptConfigFile(configData, key []byte) ([]byte, error) {
|
||||
configData = RemoveECS(configData)
|
||||
blockDecrypt, err := aes.NewCipher(key)
|
||||
@@ -88,7 +96,7 @@ func DecryptConfigFile(configData, key []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
if len(configData) < aes.BlockSize {
|
||||
return nil, errors.New(ErrConfigDataLessThenRequiredAESBlockSize)
|
||||
return nil, errors.New(errAESBlockSize)
|
||||
}
|
||||
|
||||
iv := configData[:aes.BlockSize]
|
||||
@@ -100,18 +108,21 @@ func DecryptConfigFile(configData, key []byte) ([]byte, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ConfirmConfigJSON confirms JSON in file
|
||||
func ConfirmConfigJSON(file []byte, result interface{}) error {
|
||||
if !common.StringContains(reflect.TypeOf(result).String(), "*") {
|
||||
return errors.New("ConfirmConfigJSON Error: Parameter interface is not a pointer.")
|
||||
return errors.New(errNotAPointer)
|
||||
}
|
||||
return common.JSONDecode(file, &result)
|
||||
}
|
||||
|
||||
// ConfirmECS confirms that the encryption confirmation string is found
|
||||
func ConfirmECS(file []byte) bool {
|
||||
subslice := []byte(CONFIG_ENCRYPTION_CONFIRMATION_STRING)
|
||||
subslice := []byte(EncryptConfirmString)
|
||||
return bytes.Contains(file, subslice)
|
||||
}
|
||||
|
||||
// RemoveECS removes encryption confirmation string
|
||||
func RemoveECS(file []byte) []byte {
|
||||
return bytes.Trim(file, CONFIG_ENCRYPTION_CONFIRMATION_STRING)
|
||||
return bytes.Trim(file, EncryptConfirmString)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,8 @@ func TestPromptForConfigKey(t *testing.T) {
|
||||
|
||||
func TestEncryptDecryptConfigFile(t *testing.T) { //Dual function Test
|
||||
testKey := []byte("12345678901234567890123456789012")
|
||||
testConfigData, err := common.ReadFile(CONFIG_TEST_FILE)
|
||||
|
||||
testConfigData, err := common.ReadFile(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. EncryptConfigFile: %s", err)
|
||||
}
|
||||
@@ -53,9 +54,9 @@ func TestEncryptDecryptConfigFile(t *testing.T) { //Dual function Test
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmJson(t *testing.T) {
|
||||
func TestConfirmConfigJSON(t *testing.T) {
|
||||
var result interface{}
|
||||
testConfirmJSON, err := common.ReadFile(CONFIG_TEST_FILE)
|
||||
testConfirmJSON, err := common.ReadFile(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. testConfirmJSON: %s", err)
|
||||
}
|
||||
@@ -67,12 +68,16 @@ func TestConfirmJson(t *testing.T) {
|
||||
if result == nil {
|
||||
t.Errorf("Test failed. testConfirmJSON: Error Unmarshalling JSON")
|
||||
}
|
||||
err3 := ConfirmConfigJSON(testConfirmJSON, result)
|
||||
if err3 == nil {
|
||||
t.Errorf("Test failed. testConfirmJSON: %s", err3)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmECS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ECStest := []byte(CONFIG_ENCRYPTION_CONFIRMATION_STRING)
|
||||
ECStest := []byte(EncryptConfirmString)
|
||||
if !ConfirmECS(ECStest) {
|
||||
t.Errorf("Test failed. TestConfirmECS: Error finding ECS.")
|
||||
}
|
||||
@@ -81,7 +86,7 @@ func TestConfirmECS(t *testing.T) {
|
||||
func TestRemoveECS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ECStest := []byte(CONFIG_ENCRYPTION_CONFIRMATION_STRING)
|
||||
ECStest := []byte(EncryptConfirmString)
|
||||
isremoved := RemoveECS(ECStest)
|
||||
|
||||
if string(isremoved) != "" {
|
||||
|
||||
@@ -9,9 +9,11 @@ func TestGetConfigEnabledExchanges(t *testing.T) {
|
||||
|
||||
defaultEnabledExchanges := 17
|
||||
GetConfigEnabledExchanges := GetConfig()
|
||||
err := GetConfigEnabledExchanges.LoadConfig(CONFIG_TEST_FILE)
|
||||
err := GetConfigEnabledExchanges.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Error("Test failed. GetConfigEnabledExchanges load config error: " + err.Error())
|
||||
t.Error(
|
||||
"Test failed. GetConfigEnabledExchanges load config error: " + err.Error(),
|
||||
)
|
||||
}
|
||||
enabledExch := GetConfigEnabledExchanges.GetConfigEnabledExchanges()
|
||||
if enabledExch != defaultEnabledExchanges {
|
||||
@@ -23,13 +25,21 @@ func TestGetExchangeConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
GetExchangeConfig := GetConfig()
|
||||
err := GetExchangeConfig.LoadConfig(CONFIG_TEST_FILE)
|
||||
err := GetExchangeConfig.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. GetExchangeConfig.LoadConfig Error: %s", err.Error())
|
||||
t.Errorf(
|
||||
"Test failed. GetExchangeConfig.LoadConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
r, err := GetExchangeConfig.GetExchangeConfig("ANX")
|
||||
if err != nil && (ExchangeConfig{}) == r {
|
||||
t.Errorf("Test failed. GetExchangeConfig.GetExchangeConfig Error: %s", err.Error())
|
||||
t.Errorf(
|
||||
"Test failed. GetExchangeConfig.GetExchangeConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
r, err = GetExchangeConfig.GetExchangeConfig("Testy")
|
||||
if err == nil && (ExchangeConfig{}) == r {
|
||||
t.Error("Test failed. GetExchangeConfig.GetExchangeConfig Error")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,18 +47,29 @@ func TestUpdateExchangeConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
UpdateExchangeConfig := GetConfig()
|
||||
err := UpdateExchangeConfig.LoadConfig(CONFIG_TEST_FILE)
|
||||
err := UpdateExchangeConfig.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. UpdateExchangeConfig.LoadConfig Error: %s", err.Error())
|
||||
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())
|
||||
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())
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,28 +77,129 @@ func TestCheckSMSGlobalConfigValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
checkSMSGlobalConfigValues := GetConfig()
|
||||
err := checkSMSGlobalConfigValues.LoadConfig(CONFIG_TEST_FILE)
|
||||
err := checkSMSGlobalConfigValues.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. checkSMSGlobalConfigValues.LoadConfig: %s", err)
|
||||
}
|
||||
err2 := checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues()
|
||||
if err2 != nil {
|
||||
t.Error("Test failed. checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues: Incorrect Return Value")
|
||||
err = checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues()
|
||||
if err != nil {
|
||||
t.Error(
|
||||
`Test failed. checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues: Incorrect Return Value`,
|
||||
)
|
||||
}
|
||||
|
||||
checkSMSGlobalConfigValues.SMS.Username = "Username"
|
||||
err = checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues()
|
||||
if err == nil {
|
||||
t.Error(
|
||||
"Test failed. checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues: Incorrect Return Value",
|
||||
)
|
||||
}
|
||||
|
||||
checkSMSGlobalConfigValues.SMS.Username = "1234"
|
||||
checkSMSGlobalConfigValues.SMS.Contacts[0].Name = "Bob"
|
||||
checkSMSGlobalConfigValues.SMS.Contacts[0].Number = "12345"
|
||||
err = checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues()
|
||||
if err == nil {
|
||||
t.Error(
|
||||
"Test failed. checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues: Incorrect Return Value",
|
||||
)
|
||||
}
|
||||
checkSMSGlobalConfigValues.SMS.Contacts = checkSMSGlobalConfigValues.SMS.Contacts[:0]
|
||||
err = checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues()
|
||||
if err == nil {
|
||||
t.Error(
|
||||
"Test failed. checkSMSGlobalConfigValues.CheckSMSGlobalConfigValues: Incorrect Return Value",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckExchangeConfigValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
checkExchangeConfigValues := Config{}
|
||||
err := checkExchangeConfigValues.LoadConfig(CONFIG_TEST_FILE)
|
||||
|
||||
err := checkExchangeConfigValues.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. checkExchangeConfigValues.LoadConfig: %s", err.Error())
|
||||
t.Errorf(
|
||||
"Test failed. checkExchangeConfigValues.LoadConfig: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
err = checkExchangeConfigValues.CheckExchangeConfigValues()
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues: %s",
|
||||
err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
err3 := checkExchangeConfigValues.CheckExchangeConfigValues()
|
||||
if err3 != nil {
|
||||
t.Errorf("Test failed. checkExchangeConfigValues.CheckExchangeConfigValues: %s", err.Error())
|
||||
checkExchangeConfigValues.Exchanges[0].APIKey = "Key"
|
||||
checkExchangeConfigValues.Exchanges[0].APISecret = "Secret"
|
||||
checkExchangeConfigValues.Exchanges[0].AuthenticatedAPISupport = true
|
||||
err = checkExchangeConfigValues.CheckExchangeConfigValues()
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
|
||||
)
|
||||
}
|
||||
|
||||
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",
|
||||
)
|
||||
}
|
||||
|
||||
checkExchangeConfigValues.Exchanges[0].BaseCurrencies = ""
|
||||
err = checkExchangeConfigValues.CheckExchangeConfigValues()
|
||||
if err == nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
|
||||
)
|
||||
}
|
||||
|
||||
checkExchangeConfigValues.Exchanges[0].EnabledPairs = ""
|
||||
err = checkExchangeConfigValues.CheckExchangeConfigValues()
|
||||
if err == nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
|
||||
)
|
||||
}
|
||||
|
||||
checkExchangeConfigValues.Exchanges[0].AvailablePairs = ""
|
||||
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 = ""
|
||||
err = checkExchangeConfigValues.CheckExchangeConfigValues()
|
||||
if err == nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
|
||||
)
|
||||
}
|
||||
|
||||
checkExchangeConfigValues.Exchanges = checkExchangeConfigValues.Exchanges[:0]
|
||||
checkExchangeConfigValues.Cryptocurrencies = "TESTYTEST"
|
||||
err = checkExchangeConfigValues.CheckExchangeConfigValues()
|
||||
if err == nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkExchangeConfigValues.CheckExchangeConfigValues Error",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,13 +207,50 @@ func TestCheckWebserverConfigValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
checkWebserverConfigValues := GetConfig()
|
||||
err := checkWebserverConfigValues.LoadConfig(CONFIG_TEST_FILE)
|
||||
err := checkWebserverConfigValues.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. checkWebserverConfigValues.LoadConfig: %s", err.Error())
|
||||
t.Errorf(
|
||||
"Test failed. checkWebserverConfigValues.LoadConfig: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
err2 := checkWebserverConfigValues.CheckWebserverConfigValues()
|
||||
if err2 != nil {
|
||||
t.Errorf("Test failed. checkWebserverConfigValues.CheckWebserverConfigValues: %s", err2.Error())
|
||||
err = checkWebserverConfigValues.CheckWebserverConfigValues()
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues: %s",
|
||||
err.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",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,43 +258,59 @@ func TestRetrieveConfigCurrencyPairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
retrieveConfigCurrencyPairs := GetConfig()
|
||||
err := retrieveConfigCurrencyPairs.LoadConfig(CONFIG_TEST_FILE)
|
||||
err := retrieveConfigCurrencyPairs.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. checkWebserverConfigValues.LoadConfig: %s", err.Error())
|
||||
t.Errorf(
|
||||
"Test failed. checkWebserverConfigValues.LoadConfig: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
err2 := retrieveConfigCurrencyPairs.RetrieveConfigCurrencyPairs()
|
||||
if err2 != nil {
|
||||
t.Errorf("Test failed. checkWebserverConfigValues.RetrieveConfigCurrencyPairs: %s", err2.Error())
|
||||
err = retrieveConfigCurrencyPairs.RetrieveConfigCurrencyPairs()
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkWebserverConfigValues.RetrieveConfigCurrencyPairs: %s",
|
||||
err.Error(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
readConfig := GetConfig()
|
||||
err := readConfig.ReadConfig(CONFIG_TEST_FILE)
|
||||
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 " + err.Error())
|
||||
}
|
||||
|
||||
err = readConfig.ReadConfig("")
|
||||
if err != nil {
|
||||
t.Error("Test failed. TestReadConfig error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
loadConfig := GetConfig()
|
||||
err := loadConfig.LoadConfig(CONFIG_TEST_FILE)
|
||||
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(CONFIG_TEST_FILE)
|
||||
err := saveConfig.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. TestSaveConfig.LoadConfig: %s", err.Error())
|
||||
}
|
||||
err2 := saveConfig.SaveConfig(CONFIG_TEST_FILE)
|
||||
err2 := saveConfig.SaveConfig(ConfigTestFile)
|
||||
if err2 != nil {
|
||||
t.Errorf("Test failed. TestSaveConfig.SaveConfig, %s", err2.Error())
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func GetAllSettings(w http.ResponseWriter, r *http.Request) {
|
||||
func SaveAllSettings(w http.ResponseWriter, r *http.Request) {
|
||||
//Get the data from the request
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
var responseData config.ConfigPost
|
||||
var responseData config.Post
|
||||
jsonerr := decoder.Decode(&responseData)
|
||||
if jsonerr != nil {
|
||||
panic(jsonerr)
|
||||
|
||||
@@ -24,7 +24,7 @@ const (
|
||||
ACTION_SMS_NOTIFY = "SMS"
|
||||
ACTION_CONSOLE_PRINT = "CONSOLE_PRINT"
|
||||
ACTION_TEST = "ACTION_TEST"
|
||||
CONFIG_PATH_TEST = config.CONFIG_TEST_FILE
|
||||
CONFIG_PATH_TEST = config.ConfigTestFile
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -69,7 +69,7 @@ func TestSetAPIKeys(t *testing.T) {
|
||||
|
||||
func TestUpdateAvailableCurrencies(t *testing.T) {
|
||||
cfg := config.GetConfig()
|
||||
err := cfg.LoadConfig(config.CONFIG_TEST_FILE)
|
||||
err := cfg.LoadConfig(config.ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Log("SOMETHING DONE HAPPENED!")
|
||||
}
|
||||
|
||||
2
main.go
2
main.go
@@ -88,7 +88,7 @@ func setupBotExchanges() {
|
||||
func main() {
|
||||
HandleInterrupt()
|
||||
bot.config = &config.Cfg
|
||||
log.Printf("Loading config file %s..\n", config.CONFIG_FILE)
|
||||
log.Printf("Loading config file %s..\n", config.ConfigFile)
|
||||
|
||||
err := bot.config.LoadConfig("")
|
||||
if err != nil {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func TestGetEnabledSMSContacts(t *testing.T) {
|
||||
cfg := config.GetConfig()
|
||||
err := cfg.LoadConfig(config.CONFIG_TEST_FILE)
|
||||
err := cfg.LoadConfig(config.ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. GetEnabledSMSContacts: \nFunction return is incorrect with, %s.", err)
|
||||
}
|
||||
@@ -21,7 +21,7 @@ func TestGetEnabledSMSContacts(t *testing.T) {
|
||||
|
||||
func TestSMSSendToAll(t *testing.T) {
|
||||
cfg := config.GetConfig()
|
||||
err := cfg.LoadConfig(config.CONFIG_TEST_FILE)
|
||||
err := cfg.LoadConfig(config.ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. SMSSendToAll: \nFunction return is incorrect with, %s.", err)
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func TestSMSSendToAll(t *testing.T) {
|
||||
|
||||
func TestSMSGetNumberByName(t *testing.T) {
|
||||
cfg := config.GetConfig()
|
||||
err := cfg.LoadConfig(config.CONFIG_TEST_FILE)
|
||||
err := cfg.LoadConfig(config.ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. SMSGetNumberByName: \nFunction return is incorrect with, %s.", err)
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func TestSMSGetNumberByName(t *testing.T) {
|
||||
|
||||
func TestSMSNotify(t *testing.T) {
|
||||
cfg := config.GetConfig()
|
||||
err := cfg.LoadConfig(config.CONFIG_TEST_FILE)
|
||||
err := cfg.LoadConfig(config.ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. SMSNotify: \nFunction return is incorrect with, %s.", err)
|
||||
}
|
||||
|
||||
4
testdata/configtest.dat
vendored
4
testdata/configtest.dat
vendored
@@ -19,13 +19,13 @@
|
||||
{
|
||||
"Address": "LgY8ahfHRhvjVQC1zJnBhFMG5pCTMuKRqh",
|
||||
"CoinType": "LTC",
|
||||
"Balance": 3.00000005e+06,
|
||||
"Balance": 3000000.05,
|
||||
"Decscription": ""
|
||||
},
|
||||
{
|
||||
"Address": "0xb794f5ea0ba39494ce839613fffba74279579268",
|
||||
"CoinType": "ETH",
|
||||
"Balance": 5.774999820458524e+06,
|
||||
"Balance": 5774999.820458524,
|
||||
"Decscription": ""
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user