Improve a few tests.

This commit is contained in:
Daniel Krol 2022-06-20 18:10:54 -04:00
parent 9ddf00d3f3
commit 322e4d38f5
3 changed files with 20 additions and 12 deletions

View file

@ -36,8 +36,8 @@ func TestServerAuthHandlerSuccess(t *testing.T) {
t.Errorf("Expected auth response to contain token: result: %+v err: %+v", string(body), err) t.Errorf("Expected auth response to contain token: result: %+v err: %+v", string(body), err)
} }
if !testStore.Called.SaveToken { if testStore.Called.SaveToken != testAuth.TestToken {
t.Errorf("Expected Store.SaveToken to be called") t.Errorf("Expected Store.SaveToken to be called with %s", testAuth.TestToken)
} }
} }

View file

@ -27,12 +27,13 @@ func (a *TestAuth) NewToken(userId auth.UserId, deviceId auth.DeviceId, scope au
return &auth.AuthToken{Token: a.TestToken, UserId: userId, DeviceId: deviceId, Scope: scope}, nil return &auth.AuthToken{Token: a.TestToken, UserId: userId, DeviceId: deviceId, Scope: scope}, nil
} }
// Whether functions are called, and sometimes what they're called with
type TestStoreFunctionsCalled struct { type TestStoreFunctionsCalled struct {
SaveToken bool SaveToken auth.TokenString
GetToken bool GetToken auth.TokenString
GetUserId bool GetUserId bool
CreateAccount bool CreateAccount bool
SetWallet bool SetWallet wallet.EncryptedWallet
GetWallet bool GetWallet bool
} }
@ -60,13 +61,13 @@ type TestStore struct {
TestHmac wallet.WalletHmac TestHmac wallet.WalletHmac
} }
func (s *TestStore) SaveToken(token *auth.AuthToken) error { func (s *TestStore) SaveToken(authToken *auth.AuthToken) error {
s.Called.SaveToken = true s.Called.SaveToken = authToken.Token
return s.Errors.SaveToken return s.Errors.SaveToken
} }
func (s *TestStore) GetToken(auth.TokenString) (*auth.AuthToken, error) { func (s *TestStore) GetToken(token auth.TokenString) (*auth.AuthToken, error) {
s.Called.GetToken = true s.Called.GetToken = token
return &s.TestAuthToken, s.Errors.GetToken return &s.TestAuthToken, s.Errors.GetToken
} }
@ -86,7 +87,7 @@ func (s *TestStore) SetWallet(
sequence wallet.Sequence, sequence wallet.Sequence,
hmac wallet.WalletHmac, hmac wallet.WalletHmac,
) (latestEncryptedWallet wallet.EncryptedWallet, latestSequence wallet.Sequence, latestHmac wallet.WalletHmac, sequenceCorrect bool, err error) { ) (latestEncryptedWallet wallet.EncryptedWallet, latestSequence wallet.Sequence, latestHmac wallet.WalletHmac, sequenceCorrect bool, err error) {
s.Called.SetWallet = true s.Called.SetWallet = encryptedWallet
err = s.Errors.SetWallet err = s.Errors.SetWallet
return return
} }

View file

@ -1,7 +1,6 @@
package server package server
import ( import (
"bytes"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -29,7 +28,10 @@ func TestServerGetWalletSuccess(t *testing.T) {
s := Server{&testAuth, &testStore} s := Server{&testAuth, &testStore}
req := httptest.NewRequest(http.MethodGet, PathWallet+"/?token=seekrit", bytes.NewBuffer([]byte{})) req := httptest.NewRequest(http.MethodGet, PathWallet, nil)
q := req.URL.Query()
q.Add("token", string(testStore.TestAuthToken.Token))
req.URL.RawQuery = q.Encode()
w := httptest.NewRecorder() w := httptest.NewRecorder()
// test handleWallet while we're at it, which is a dispatch for get and post // test handleWallet while we're at it, which is a dispatch for get and post
@ -51,6 +53,11 @@ func TestServerGetWalletSuccess(t *testing.T) {
if !testStore.Called.GetWallet { if !testStore.Called.GetWallet {
t.Errorf("Expected Store.GetWallet to be called") t.Errorf("Expected Store.GetWallet to be called")
} }
// Make sure the right auth was gotten
if testStore.Called.GetToken != testAuth.TestToken {
t.Errorf("Expected Store.GetToken to be called with %s", testAuth.TestToken)
}
} }
func TestServerGetWalletErrors(t *testing.T) { func TestServerGetWalletErrors(t *testing.T) {