Test getWallet (via handleWallet)
This commit is contained in:
parent
fd7e1988d3
commit
9ddf00d3f3
3 changed files with 59 additions and 4 deletions
|
@ -25,12 +25,11 @@ func TestServerAuthHandlerSuccess(t *testing.T) {
|
||||||
s.getAuthToken(w, req)
|
s.getAuthToken(w, req)
|
||||||
body, _ := ioutil.ReadAll(w.Body)
|
body, _ := ioutil.ReadAll(w.Body)
|
||||||
|
|
||||||
var result auth.AuthToken
|
|
||||||
|
|
||||||
if want, got := http.StatusOK, w.Result().StatusCode; want != got {
|
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)
|
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)
|
err := json.Unmarshal(body, &result)
|
||||||
|
|
||||||
if err != nil || result.Token != testAuth.TestToken {
|
if err != nil || result.Token != testAuth.TestToken {
|
||||||
|
|
|
@ -52,6 +52,12 @@ type TestStore struct {
|
||||||
// Fake store functions will return the errors (including `nil`) specified in
|
// Fake store functions will return the errors (including `nil`) specified in
|
||||||
// the test setup
|
// the test setup
|
||||||
Errors TestStoreFunctionsErrors
|
Errors TestStoreFunctionsErrors
|
||||||
|
|
||||||
|
TestAuthToken auth.AuthToken
|
||||||
|
|
||||||
|
TestEncryptedWallet wallet.EncryptedWallet
|
||||||
|
TestSequence wallet.Sequence
|
||||||
|
TestHmac wallet.WalletHmac
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TestStore) SaveToken(token *auth.AuthToken) error {
|
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) {
|
func (s *TestStore) GetToken(auth.TokenString) (*auth.AuthToken, error) {
|
||||||
s.Called.GetToken = true
|
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) {
|
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) {
|
func (s *TestStore) GetWallet(userId auth.UserId) (encryptedWallet wallet.EncryptedWallet, sequence wallet.Sequence, hmac wallet.WalletHmac, err error) {
|
||||||
s.Called.GetWallet = true
|
s.Called.GetWallet = true
|
||||||
err = s.Errors.GetWallet
|
err = s.Errors.GetWallet
|
||||||
|
if err == nil {
|
||||||
|
encryptedWallet = s.TestEncryptedWallet
|
||||||
|
sequence = s.TestSequence
|
||||||
|
hmac = s.TestHmac
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,56 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"orblivion/lbry-id/auth"
|
||||||
|
"orblivion/lbry-id/wallet"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestServerGetWalletSuccess(t *testing.T) {
|
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) {
|
func TestServerGetWalletErrors(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue