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)
}
if !testStore.Called.SaveToken {
t.Errorf("Expected Store.SaveToken to be called")
if testStore.Called.SaveToken != testAuth.TestToken {
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
}
// Whether functions are called, and sometimes what they're called with
type TestStoreFunctionsCalled struct {
SaveToken bool
GetToken bool
SaveToken auth.TokenString
GetToken auth.TokenString
GetUserId bool
CreateAccount bool
SetWallet bool
SetWallet wallet.EncryptedWallet
GetWallet bool
}
@ -60,13 +61,13 @@ type TestStore struct {
TestHmac wallet.WalletHmac
}
func (s *TestStore) SaveToken(token *auth.AuthToken) error {
s.Called.SaveToken = true
func (s *TestStore) SaveToken(authToken *auth.AuthToken) error {
s.Called.SaveToken = authToken.Token
return s.Errors.SaveToken
}
func (s *TestStore) GetToken(auth.TokenString) (*auth.AuthToken, error) {
s.Called.GetToken = true
func (s *TestStore) GetToken(token auth.TokenString) (*auth.AuthToken, error) {
s.Called.GetToken = token
return &s.TestAuthToken, s.Errors.GetToken
}
@ -86,7 +87,7 @@ func (s *TestStore) SetWallet(
sequence wallet.Sequence,
hmac wallet.WalletHmac,
) (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
return
}

View file

@ -1,7 +1,6 @@
package server
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
@ -29,7 +28,10 @@ func TestServerGetWalletSuccess(t *testing.T) {
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()
// 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 {
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) {