wallet-sync-server/server/wallet_test.go

107 lines
3.2 KiB
Go
Raw Normal View History

package server
import (
2022-06-20 00:54:59 +02:00
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
2022-06-20 00:54:59 +02:00
"orblivion/lbry-id/auth"
"orblivion/lbry-id/wallet"
)
func TestServerGetWalletSuccess(t *testing.T) {
tt := []struct {
name string
}{
{
name: "success",
2022-06-20 00:54:59 +02:00
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
2022-06-20 00:54:59 +02:00
testAuth := TestAuth{
TestToken: auth.TokenString("seekrit"),
}
testStore := TestStore{
TestAuthToken: auth.AuthToken{
Token: auth.TokenString("seekrit"),
Scope: auth.ScopeFull,
},
2022-06-20 00:54:59 +02:00
TestEncryptedWallet: wallet.EncryptedWallet("my-encrypted-wallet"),
TestSequence: wallet.Sequence(2),
TestHmac: wallet.WalletHmac("my-hmac"),
}
2022-06-20 00:54:59 +02:00
s := Server{&testAuth, &testStore}
2022-06-20 00:54:59 +02:00
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()
2022-06-20 00:54:59 +02:00
// 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)
2022-06-20 00:54:59 +02:00
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)
}
2022-06-20 00:54:59 +02:00
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)
}
2022-06-21 00:10:54 +02:00
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)
}
})
2022-06-21 00:10:54 +02:00
}
}
func TestServerGetWalletErrors(t *testing.T) {
t.Fatalf("Test me: GetWallet fails for various reasons (malformed, auth, db fail)")
}
func TestServerGetWalletParams(t *testing.T) {
t.Fatalf("Test me: getWalletParams")
}
func TestServerPostWalletSuccess(t *testing.T) {
t.Fatalf("Test me: PostWallet succeeds and returns the new wallet, PostWallet succeeds but is preempted")
}
func TestServerPostWalletTooLate(t *testing.T) {
t.Fatalf("Test me: PostWallet fails for sequence being too low, returns the latest wallet")
}
func TestServerPostWalletErrors(t *testing.T) {
// (malformed json, db fail, auth token not found, wallet metadata invalid (via stub, make sure the validation function is even called), sequence too high, device id doesn't match token device id)
// Client sends sequence != 1 for first entry
// Client sends sequence == x + 10 for xth entry or whatever
t.Fatalf("Test me: PostWallet fails for various reasons")
}
func TestServerValidateWalletRequest(t *testing.T) {
// also add a basic test case for this in TestServerAuthHandlerSuccess to make sure it's called at all
t.Fatalf("Test me: Implement and test WalletRequest.validate()")
}
func TestServerHandleWallet(t *testing.T) {
t.Fatalf("Test me: Call the get or post function as appropriate. Alternately: call handleWallet for the existing tests.")
}