Test getWallet (via handleWallet)

This commit is contained in:
Daniel Krol 2022-06-19 18:54:59 -04:00
parent fd7e1988d3
commit 9ddf00d3f3
3 changed files with 59 additions and 4 deletions

View file

@ -25,12 +25,11 @@ func TestServerAuthHandlerSuccess(t *testing.T) {
s.getAuthToken(w, req)
body, _ := ioutil.ReadAll(w.Body)
var result auth.AuthToken
if want, got := http.StatusOK, w.Result().StatusCode; want != got {
t.Errorf("StatusCode: expected %s (%d), got %s (%d)", http.StatusText(want), want, http.StatusText(got), got)
}
var result auth.AuthToken
err := json.Unmarshal(body, &result)
if err != nil || result.Token != testAuth.TestToken {

View file

@ -52,6 +52,12 @@ type TestStore struct {
// Fake store functions will return the errors (including `nil`) specified in
// the test setup
Errors TestStoreFunctionsErrors
TestAuthToken auth.AuthToken
TestEncryptedWallet wallet.EncryptedWallet
TestSequence wallet.Sequence
TestHmac wallet.WalletHmac
}
func (s *TestStore) SaveToken(token *auth.AuthToken) error {
@ -61,7 +67,7 @@ func (s *TestStore) SaveToken(token *auth.AuthToken) error {
func (s *TestStore) GetToken(auth.TokenString) (*auth.AuthToken, error) {
s.Called.GetToken = true
return nil, s.Errors.GetToken
return &s.TestAuthToken, s.Errors.GetToken
}
func (s *TestStore) GetUserId(auth.Email, auth.Password) (auth.UserId, error) {
@ -88,6 +94,11 @@ func (s *TestStore) SetWallet(
func (s *TestStore) GetWallet(userId auth.UserId) (encryptedWallet wallet.EncryptedWallet, sequence wallet.Sequence, hmac wallet.WalletHmac, err error) {
s.Called.GetWallet = true
err = s.Errors.GetWallet
if err == nil {
encryptedWallet = s.TestEncryptedWallet
sequence = s.TestSequence
hmac = s.TestHmac
}
return
}

View file

@ -1,11 +1,56 @@
package server
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"orblivion/lbry-id/auth"
"orblivion/lbry-id/wallet"
)
func TestServerGetWalletSuccess(t *testing.T) {
t.Fatalf("Test me: GetWallet succeeds")
testAuth := TestAuth{
TestToken: auth.TokenString("seekrit"),
}
testStore := TestStore{
TestAuthToken: auth.AuthToken{
Token: auth.TokenString("seekrit"),
Scope: auth.ScopeFull,
},
TestEncryptedWallet: wallet.EncryptedWallet("my-encrypted-wallet"),
TestSequence: wallet.Sequence(2),
TestHmac: wallet.WalletHmac("my-hmac"),
}
s := Server{&testAuth, &testStore}
req := httptest.NewRequest(http.MethodGet, PathWallet+"/?token=seekrit", bytes.NewBuffer([]byte{}))
w := httptest.NewRecorder()
// test handleWallet while we're at it, which is a dispatch for get and post
// wallet
s.handleWallet(w, req)
body, _ := ioutil.ReadAll(w.Body)
if want, got := http.StatusOK, w.Result().StatusCode; want != got {
t.Errorf("StatusCode: expected %s (%d), got %s (%d)", http.StatusText(want), want, http.StatusText(got), got)
}
var result WalletResponse
err := json.Unmarshal(body, &result)
if err != nil || result.EncryptedWallet != testStore.TestEncryptedWallet || result.Hmac != testStore.TestHmac || result.Sequence != testStore.TestSequence {
t.Errorf("Expected wallet response to have the test wallet values: result: %+v err: %+v", string(body), err)
}
if !testStore.Called.GetWallet {
t.Errorf("Expected Store.GetWallet to be called")
}
}
func TestServerGetWalletErrors(t *testing.T) {