mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-03 23:16:53 +00:00
Daily engine improvements:
New GetExchangeOTPs API CLI validation Standardised pairs for GCTCLI Expand test coverage Trim SMS global from name is len > 11 Linter fixes
This commit is contained in:
@@ -29,9 +29,33 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/utils"
|
||||
)
|
||||
|
||||
// GetOTPByExchange returns a OTP code for the desired exchange
|
||||
// GetExchangeOTPs returns OTP codes for all exchanges which have a otpsecret
|
||||
// stored
|
||||
func GetExchangeOTPs() (map[string]string, error) {
|
||||
otpCodes := make(map[string]string)
|
||||
for x := range Bot.Config.Exchanges {
|
||||
if otpSecret := Bot.Config.Exchanges[x].API.Credentials.OTPSecret; otpSecret != "" {
|
||||
exchName := Bot.Config.Exchanges[x].Name
|
||||
o, err := totp.GenerateCode(otpSecret, time.Now())
|
||||
if err != nil {
|
||||
log.Errorf("Unable to generate OTP code for exchange %s. Err: %s",
|
||||
exchName, err)
|
||||
continue
|
||||
}
|
||||
otpCodes[exchName] = o
|
||||
}
|
||||
}
|
||||
|
||||
if len(otpCodes) == 0 {
|
||||
return nil, errors.New("no exchanges found which have a OTP secret stored")
|
||||
}
|
||||
|
||||
return otpCodes, nil
|
||||
}
|
||||
|
||||
// GetExchangeoOTPByName returns a OTP code for the desired exchange
|
||||
// if it exists
|
||||
func GetOTPByExchange(exchName string) (string, error) {
|
||||
func GetExchangeoOTPByName(exchName string) (string, error) {
|
||||
for x := range Bot.Config.Exchanges {
|
||||
if !strings.EqualFold(Bot.Config.Exchanges[x].Name, exchName) {
|
||||
continue
|
||||
@@ -41,7 +65,7 @@ func GetOTPByExchange(exchName string) (string, error) {
|
||||
return totp.GenerateCode(otpSecret, time.Now())
|
||||
}
|
||||
}
|
||||
return "", errors.New("exchange does not have a otpsecret stored")
|
||||
return "", errors.New("exchange does not have a OTP secret stored")
|
||||
}
|
||||
|
||||
// GetAuthAPISupportedExchanges returns a list of auth api enabled exchanges
|
||||
@@ -60,6 +84,7 @@ func GetAuthAPISupportedExchanges() []string {
|
||||
func IsOnline() bool {
|
||||
if Bot.Connectivity == nil {
|
||||
log.Warnf("IsOnline called but Bot.Connectivity is nil")
|
||||
return false
|
||||
}
|
||||
return Bot.Connectivity.IsConnected()
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@ package engine
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/connchecker"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/assets"
|
||||
@@ -42,6 +44,110 @@ func SetupTestHelpers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetExchangeOTPs(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
_, err := GetExchangeOTPs()
|
||||
if err == nil {
|
||||
t.Fatal("Expected err with no exchange OTP secrets set")
|
||||
}
|
||||
|
||||
bfxCfg, err := Bot.Config.GetExchangeConfig("Bitfinex")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bCfg, err := Bot.Config.GetExchangeConfig("Bitstamp")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bfxCfg.API.Credentials.OTPSecret = "JBSWY3DPEHPK3PXP"
|
||||
bCfg.API.Credentials.OTPSecret = "JBSWY3DPEHPK3PXP"
|
||||
result, err := GetExchangeOTPs()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result) != 2 {
|
||||
t.Fatal("Expected 2 OTP results")
|
||||
}
|
||||
|
||||
bfxCfg.API.Credentials.OTPSecret = "°"
|
||||
result, err = GetExchangeOTPs()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result) != 1 {
|
||||
t.Fatal("Expected 1 OTP code with invalid OTP Secret")
|
||||
}
|
||||
|
||||
// Flush settings
|
||||
bfxCfg.API.Credentials.OTPSecret = ""
|
||||
bCfg.API.Credentials.OTPSecret = ""
|
||||
}
|
||||
|
||||
func TestGetExchangeoOTPByName(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
_, err := GetExchangeoOTPByName("Bitstamp")
|
||||
if err == nil {
|
||||
t.Fatal("Expected err with no exchange OTP secrets set")
|
||||
}
|
||||
|
||||
bCfg, err := Bot.Config.GetExchangeConfig("Bitstamp")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bCfg.API.Credentials.OTPSecret = "JBSWY3DPEHPK3PXP"
|
||||
result, err := GetExchangeoOTPByName("Bitstamp")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result == "" {
|
||||
t.Fatal("Expected valid OTP code")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAuthAPISupportedExchanges(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
if result := GetAuthAPISupportedExchanges(); result != nil {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsOnline(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
if r := IsOnline(); r {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
var err error
|
||||
Bot.Connectivity, err = connchecker.New(Bot.Config.ConnectionMonitor.DNSList,
|
||||
Bot.Config.ConnectionMonitor.PublicDomainList,
|
||||
Bot.Config.ConnectionMonitor.CheckInterval)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tick := time.NewTicker(time.Second * 5)
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
t.Fatal("Test timeout")
|
||||
default:
|
||||
if IsOnline() {
|
||||
Bot.Connectivity.Shutdown()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAvailableExchanges(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
if r := len(GetAvailableExchanges()); r == 0 {
|
||||
t.Error("Expected len > 0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSpecificAvailablePairs(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
assetType := assets.AssetTypeSpot
|
||||
@@ -255,7 +361,6 @@ func TestGetExchangeNamesByCurrency(t *testing.T) {
|
||||
}
|
||||
|
||||
result = GetExchangeNamesByCurrency(currency.NewPairFromStrings("BTC", "JPY"), true, assetType)
|
||||
t.Log(result)
|
||||
if !common.StringDataCompare(result, "Bitflyer") {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
@@ -178,10 +178,17 @@ func (s *RPCServer) EnableExchange(ctx context.Context, r *gctrpc.GenericExchang
|
||||
|
||||
// GetExchangeOTPCode retrieves an exchanges OTP code
|
||||
func (s *RPCServer) GetExchangeOTPCode(ctx context.Context, r *gctrpc.GenericExchangeNameRequest) (*gctrpc.GetExchangeOTPReponse, error) {
|
||||
result, err := GetOTPByExchange(r.Exchange)
|
||||
result, err := GetExchangeoOTPByName(r.Exchange)
|
||||
return &gctrpc.GetExchangeOTPReponse{OtpCode: result}, err
|
||||
}
|
||||
|
||||
// GetExchangeOTPCodes retrieves OTP codes for all exchanges which have an
|
||||
// OTP secret installed
|
||||
func (s *RPCServer) GetExchangeOTPCodes(ctx context.Context, r *gctrpc.GetExchangeOTPsRequest) (*gctrpc.GetExchangeOTPsResponse, error) {
|
||||
result, err := GetExchangeOTPs()
|
||||
return &gctrpc.GetExchangeOTPsResponse{OtpCodes: result}, err
|
||||
}
|
||||
|
||||
// GetExchangeInfo gets info for a specific exchange
|
||||
func (s *RPCServer) GetExchangeInfo(ctx context.Context, r *gctrpc.GenericExchangeNameRequest) (*gctrpc.GetExchangeInfoResponse, error) {
|
||||
exchCfg, err := Bot.Config.GetExchangeConfig(r.Exchange)
|
||||
|
||||
Reference in New Issue
Block a user