mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-23 15:10:15 +00:00
accounts: Move to instance methods, fix races and isolate tests (#1923)
* Bybit: Fix race in TestUpdateAccountInfo and TestWSHandleData * DriveBy rename TestWSHandleData * This doesn't address running with -race=2+ due to the singleton * Accounts: Add account.GetService() * exchange: Assertify TestSetupDefaults * Exchanges: Add account.Service override for testing * Exchanges: Remove duplicate IsWebsocketEnabled test from TestSetupDefaults * Dispatch: Replace nil checks with NilGuard * Engine: Remove deprecated printAccountHoldingsChangeSummary * Dispatcher: Add EnsureRunning method * Accounts: Move singleton accounts service to exchange Accounts * Move singleton accounts service to exchange Accounts This maintains the concept of a global store, whilst allowing exchanges to override it when needed, particularly for testing. APIServer: * Remove getAllActiveAccounts from apiserver Deprecated apiserver only thing using this, so remove it instead of updating it * Update comment for UpdateAccountBalances everywhere * Docs: Add punctuation to function comments * Bybit: Coverage for wsProcessWalletPushData Save
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
|
||||
)
|
||||
|
||||
func TestGetCredentials(t *testing.T) {
|
||||
@@ -18,17 +18,17 @@ func TestGetCredentials(t *testing.T) {
|
||||
require.ErrorIs(t, err, ErrCredentialsAreEmpty)
|
||||
|
||||
b.API.CredentialsValidator.RequiresKey = true
|
||||
ctx := account.DeployCredentialsToContext(t.Context(), &account.Credentials{Secret: "wow"})
|
||||
ctx := accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Secret: "wow"})
|
||||
_, err = b.GetCredentials(ctx)
|
||||
require.ErrorIs(t, err, errRequiresAPIKey)
|
||||
|
||||
b.API.CredentialsValidator.RequiresSecret = true
|
||||
ctx = account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "wow"})
|
||||
ctx = accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "wow"})
|
||||
_, err = b.GetCredentials(ctx)
|
||||
require.ErrorIs(t, err, errRequiresAPISecret)
|
||||
|
||||
b.API.CredentialsValidator.RequiresBase64DecodeSecret = true
|
||||
ctx = account.DeployCredentialsToContext(t.Context(), &account.Credentials{
|
||||
ctx = accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{
|
||||
Key: "meow",
|
||||
Secret: "invalidb64",
|
||||
})
|
||||
@@ -36,7 +36,7 @@ func TestGetCredentials(t *testing.T) {
|
||||
require.ErrorIs(t, err, errBase64DecodeFailure)
|
||||
|
||||
const expectedBase64DecodedOutput = "hello world"
|
||||
ctx = account.DeployCredentialsToContext(t.Context(), &account.Credentials{
|
||||
ctx = accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{
|
||||
Key: "meow",
|
||||
Secret: "aGVsbG8gd29ybGQ=",
|
||||
})
|
||||
@@ -47,12 +47,12 @@ func TestGetCredentials(t *testing.T) {
|
||||
t.Fatalf("received: %v but expected: %v", creds.Secret, expectedBase64DecodedOutput)
|
||||
}
|
||||
|
||||
ctx = context.WithValue(t.Context(), account.ContextCredentialsFlag, "pewpew")
|
||||
ctx = context.WithValue(t.Context(), accounts.ContextCredentialsFlag, "pewpew")
|
||||
_, err = b.GetCredentials(ctx)
|
||||
require.ErrorIs(t, err, common.ErrTypeAssertFailure)
|
||||
|
||||
b.API.CredentialsValidator.RequiresBase64DecodeSecret = false
|
||||
fullCred := &account.Credentials{
|
||||
fullCred := &accounts.Credentials{
|
||||
Key: "superkey",
|
||||
Secret: "supersecret",
|
||||
SubAccount: "supersub",
|
||||
@@ -61,7 +61,7 @@ func TestGetCredentials(t *testing.T) {
|
||||
OneTimePassword: "superOneTimePasssssss",
|
||||
}
|
||||
|
||||
ctx = account.DeployCredentialsToContext(t.Context(), fullCred)
|
||||
ctx = accounts.DeployCredentialsToContext(t.Context(), fullCred)
|
||||
creds, err = b.GetCredentials(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -74,7 +74,7 @@ func TestGetCredentials(t *testing.T) {
|
||||
t.Fatal("unexpected values")
|
||||
}
|
||||
|
||||
lonelyCred := &account.Credentials{
|
||||
lonelyCred := &accounts.Credentials{
|
||||
Key: "superkey",
|
||||
Secret: "supersecret",
|
||||
SubAccount: "supersub",
|
||||
@@ -82,7 +82,7 @@ func TestGetCredentials(t *testing.T) {
|
||||
OneTimePassword: "superOneTimePasssssss",
|
||||
}
|
||||
|
||||
ctx = account.DeployCredentialsToContext(t.Context(), lonelyCred)
|
||||
ctx = accounts.DeployCredentialsToContext(t.Context(), lonelyCred)
|
||||
b.API.CredentialsValidator.RequiresClientID = true
|
||||
_, err = b.GetCredentials(ctx)
|
||||
require.ErrorIs(t, err, errRequiresAPIClientID)
|
||||
@@ -91,7 +91,7 @@ func TestGetCredentials(t *testing.T) {
|
||||
b.API.SetSecret("sir")
|
||||
b.API.SetClientID("1337")
|
||||
|
||||
ctx = context.WithValue(t.Context(), account.ContextSubAccountFlag, "superaccount")
|
||||
ctx = context.WithValue(t.Context(), accounts.ContextSubAccountFlag, "superaccount")
|
||||
overridedSA, err := b.GetCredentials(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -119,7 +119,7 @@ func TestAreCredentialsValid(t *testing.T) {
|
||||
if b.AreCredentialsValid(t.Context()) {
|
||||
t.Fatal("should not be valid")
|
||||
}
|
||||
ctx := account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "hello"})
|
||||
ctx := accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "hello"})
|
||||
if !b.AreCredentialsValid(ctx) {
|
||||
t.Fatal("should be valid")
|
||||
}
|
||||
@@ -226,7 +226,7 @@ func TestCheckCredentials(t *testing.T) {
|
||||
base: &Base{
|
||||
API: API{
|
||||
CredentialsValidator: config.APICredentialsValidatorConfig{RequiresKey: true},
|
||||
credentials: account.Credentials{OneTimePassword: "wow"},
|
||||
credentials: accounts.Credentials{OneTimePassword: "wow"},
|
||||
},
|
||||
},
|
||||
expectedErr: errRequiresAPIKey,
|
||||
@@ -237,7 +237,7 @@ func TestCheckCredentials(t *testing.T) {
|
||||
LoadedByConfig: true,
|
||||
API: API{
|
||||
CredentialsValidator: config.APICredentialsValidatorConfig{RequiresKey: true},
|
||||
credentials: account.Credentials{Key: "k3y"},
|
||||
credentials: accounts.Credentials{Key: "k3y"},
|
||||
},
|
||||
},
|
||||
expectedErr: ErrAuthenticationSupportNotEnabled,
|
||||
@@ -249,7 +249,7 @@ func TestCheckCredentials(t *testing.T) {
|
||||
API: API{
|
||||
AuthenticatedSupport: true,
|
||||
CredentialsValidator: config.APICredentialsValidatorConfig{RequiresKey: true},
|
||||
credentials: account.Credentials{},
|
||||
credentials: accounts.Credentials{},
|
||||
},
|
||||
},
|
||||
expectedErr: ErrCredentialsAreEmpty,
|
||||
@@ -259,7 +259,7 @@ func TestCheckCredentials(t *testing.T) {
|
||||
base: &Base{
|
||||
API: API{
|
||||
CredentialsValidator: config.APICredentialsValidatorConfig{RequiresBase64DecodeSecret: true},
|
||||
credentials: account.Credentials{Secret: "invalid"},
|
||||
credentials: accounts.Credentials{Secret: "invalid"},
|
||||
},
|
||||
},
|
||||
expectedErr: errBase64DecodeFailure,
|
||||
@@ -269,7 +269,7 @@ func TestCheckCredentials(t *testing.T) {
|
||||
base: &Base{
|
||||
API: API{
|
||||
CredentialsValidator: config.APICredentialsValidatorConfig{RequiresBase64DecodeSecret: true},
|
||||
credentials: account.Credentials{Secret: "aGVsbG8gd29ybGQ="},
|
||||
credentials: accounts.Credentials{Secret: "aGVsbG8gd29ybGQ="},
|
||||
},
|
||||
},
|
||||
checkBase64Output: true,
|
||||
@@ -281,7 +281,7 @@ func TestCheckCredentials(t *testing.T) {
|
||||
API: API{
|
||||
AuthenticatedSupport: true,
|
||||
CredentialsValidator: config.APICredentialsValidatorConfig{RequiresKey: true},
|
||||
credentials: account.Credentials{Key: "k3y"},
|
||||
credentials: accounts.Credentials{Key: "k3y"},
|
||||
},
|
||||
},
|
||||
expectedErr: nil,
|
||||
@@ -308,32 +308,32 @@ func TestCheckCredentials(t *testing.T) {
|
||||
func TestAPISetters(t *testing.T) {
|
||||
t.Parallel()
|
||||
api := API{}
|
||||
api.SetKey(account.Key)
|
||||
if api.credentials.Key != account.Key {
|
||||
api.SetKey(accounts.Key)
|
||||
if api.credentials.Key != accounts.Key {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
|
||||
api = API{}
|
||||
api.SetSecret(account.Secret)
|
||||
if api.credentials.Secret != account.Secret {
|
||||
api.SetSecret(accounts.Secret)
|
||||
if api.credentials.Secret != accounts.Secret {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
|
||||
api = API{}
|
||||
api.SetClientID(account.ClientID)
|
||||
if api.credentials.ClientID != account.ClientID {
|
||||
api.SetClientID(accounts.ClientID)
|
||||
if api.credentials.ClientID != accounts.ClientID {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
|
||||
api = API{}
|
||||
api.SetPEMKey(account.PEMKey)
|
||||
if api.credentials.PEMKey != account.PEMKey {
|
||||
api.SetPEMKey(accounts.PEMKey)
|
||||
if api.credentials.PEMKey != accounts.PEMKey {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
|
||||
api = API{}
|
||||
api.SetSubAccount(account.SubAccountSTR)
|
||||
if api.credentials.SubAccount != account.SubAccountSTR {
|
||||
api.SetSubAccount(accounts.SubAccountSTR)
|
||||
if api.credentials.SubAccount != accounts.SubAccountSTR {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user