Files
gocryptotrader/exchanges/account/credentials_test.go
Adrian Gallagher bea16af380 golangci-lint: Enable usetesting and unused linters (#1893)
* golangci-lint: Enable usetesting and unused linters

* tests: Improve assertions in various test cases for clarity and accuracy

* tests: Enhance error assertions in TestExecuteStrategyFromFile for improved clarity

* tests: Update assertions for improved clarity and accuracy

* tests: Replace assert with require for task count checks

* config/versions/v7: Replace context.Background() with t.Context()

* Bithumb: Centralise and consoliate testPair, relax UpdateTickers check

with some glorious Doom Eternal music

* Bithumb: Use UpdatePairsOnce and update remaining pair string

* Bithumb: Add UpdatePairsOnce to TestUpdateTickers
2025-05-01 14:44:29 +10:00

240 lines
5.7 KiB
Go

package account
import (
"errors"
"testing"
"google.golang.org/grpc/metadata"
)
func TestIsEmpty(t *testing.T) {
t.Parallel()
var c *Credentials
if !c.IsEmpty() {
t.Fatalf("expected: %v but received: %v", true, c.IsEmpty())
}
c = new(Credentials)
if !c.IsEmpty() {
t.Fatalf("expected: %v but received: %v", true, c.IsEmpty())
}
c.SubAccount = "woow"
if c.IsEmpty() {
t.Fatalf("expected: %v but received: %v", false, c.IsEmpty())
}
}
func TestParseCredentialsMetadata(t *testing.T) {
t.Parallel()
_, err := ParseCredentialsMetadata(t.Context(), nil)
if !errors.Is(err, errMetaDataIsNil) {
t.Fatalf("received: '%v' but expected: '%v'", err, errMetaDataIsNil)
}
_, err = ParseCredentialsMetadata(t.Context(), metadata.MD{})
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
ctx := metadata.AppendToOutgoingContext(t.Context(),
string(ContextCredentialsFlag), "wow", string(ContextCredentialsFlag), "wow2")
nortyMD, _ := metadata.FromOutgoingContext(ctx)
_, err = ParseCredentialsMetadata(t.Context(), nortyMD)
if !errors.Is(err, errInvalidCredentialMetaDataLength) {
t.Fatalf("received: '%v' but expected: '%v'", err, errInvalidCredentialMetaDataLength)
}
ctx = metadata.AppendToOutgoingContext(t.Context(),
string(ContextCredentialsFlag), "brokenstring")
nortyMD, _ = metadata.FromOutgoingContext(ctx)
_, err = ParseCredentialsMetadata(t.Context(), nortyMD)
if !errors.Is(err, errMissingInfo) {
t.Fatalf("received: '%v' but expected: '%v'", err, errMissingInfo)
}
beforeCreds := Credentials{
Key: "superkey",
Secret: "supersecret",
SubAccount: "supersub",
ClientID: "superclient",
PEMKey: "superpem",
OneTimePassword: "superOneTimePasssssss",
}
flag, outGoing := beforeCreds.GetMetaData()
ctx = metadata.AppendToOutgoingContext(t.Context(), flag, outGoing)
lovelyMD, _ := metadata.FromOutgoingContext(ctx)
ctx, err = ParseCredentialsMetadata(t.Context(), lovelyMD)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
store, ok := ctx.Value(ContextCredentialsFlag).(*ContextCredentialsStore)
if !ok {
t.Fatal("should have processed")
}
afterCreds := store.Get()
if afterCreds.Key != "superkey" &&
afterCreds.Secret != "supersecret" &&
afterCreds.SubAccount != "supersub" &&
afterCreds.ClientID != "superclient" &&
afterCreds.PEMKey != "superpem" &&
afterCreds.OneTimePassword != "superOneTimePasssssss" {
t.Fatal("unexpected values")
}
// subaccount override
subaccount := Credentials{
SubAccount: "supersub",
}
flag, outGoing = subaccount.GetMetaData()
ctx = metadata.AppendToOutgoingContext(t.Context(), flag, outGoing)
lovelyMD, _ = metadata.FromOutgoingContext(ctx)
ctx, err = ParseCredentialsMetadata(t.Context(), lovelyMD)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
sa, ok := ctx.Value(ContextSubAccountFlag).(string)
if !ok {
t.Fatal("should have processed")
}
if sa != "supersub" {
t.Fatal("unexpected value")
}
}
func TestGetInternal(t *testing.T) {
t.Parallel()
flag, store := (&Credentials{}).getInternal()
if flag != "" {
t.Fatal("unexpected value")
}
if store != nil {
t.Fatal("unexpected value")
}
flag, store = (&Credentials{Key: "wow"}).getInternal()
if flag != ContextCredentialsFlag {
t.Fatal("unexpected value")
}
if store == nil {
t.Fatal("unexpected value")
}
if store.Get().Key != "wow" {
t.Fatal("unexpected value")
}
}
func TestString(t *testing.T) {
t.Parallel()
creds := Credentials{}
if s := creds.String(); s != "Key:[...] SubAccount:[] ClientID:[]" {
t.Fatal("unexpected value")
}
creds.Key = "12345678910111234"
creds.SubAccount = "sub"
creds.ClientID = "client"
if s := creds.String(); s != "Key:[1234567891011123...] SubAccount:[sub] ClientID:[client]" {
t.Fatal("unexpected value")
}
}
func TestCredentialsEqual(t *testing.T) {
t.Parallel()
var this, that *Credentials
if this.Equal(that) {
t.Fatal("unexpected value")
}
this = &Credentials{}
if this.Equal(that) {
t.Fatal("unexpected value")
}
that = &Credentials{Key: "1337"}
if this.Equal(that) {
t.Fatal("unexpected value")
}
this.Key = "1337"
if !this.Equal(that) {
t.Fatal("unexpected value")
}
this.ClientID = "1337"
if this.Equal(that) {
t.Fatal("unexpected value")
}
that.ClientID = "1337"
if !this.Equal(that) {
t.Fatal("unexpected value")
}
this.SubAccount = "someSub"
if this.Equal(that) {
t.Fatal("unexpected value")
}
that.SubAccount = "someSub"
if !this.Equal(that) {
t.Fatal("unexpected value")
}
}
func TestProtectedString(t *testing.T) {
t.Parallel()
p := Protected{}
if s := p.String(); s != "Key:[...] SubAccount:[] ClientID:[]" {
t.Fatal("unexpected value")
}
p.creds.Key = "12345678910111234"
p.creds.SubAccount = "sub"
p.creds.ClientID = "client"
if s := p.creds.String(); s != "Key:[1234567891011123...] SubAccount:[sub] ClientID:[client]" {
t.Fatal("unexpected value")
}
}
func TestProtectedCredentialsEqual(t *testing.T) {
t.Parallel()
var this Protected
var that *Credentials
if this.Equal(that) {
t.Fatal("unexpected value")
}
this.creds = Credentials{}
if this.Equal(that) {
t.Fatal("unexpected value")
}
that = &Credentials{Key: "1337"}
if this.Equal(that) {
t.Fatal("unexpected value")
}
this.creds.Key = "1337"
if !this.Equal(that) {
t.Fatal("unexpected value")
}
this.creds.ClientID = "1337"
if this.Equal(that) {
t.Fatal("unexpected value")
}
that.ClientID = "1337"
if !this.Equal(that) {
t.Fatal("unexpected value")
}
this.creds.SubAccount = "someSub"
if this.Equal(that) {
t.Fatal("unexpected value")
}
that.SubAccount = "someSub"
if !this.Equal(that) {
t.Fatal("unexpected value")
}
}