wallet-sync-server/server/wallet_test.go

124 lines
3.5 KiB
Go
Raw Normal View History

package server
import (
2022-06-20 00:54:59 +02:00
"encoding/json"
2022-06-21 17:52:03 +02:00
"fmt"
2022-06-20 00:54:59 +02:00
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
2022-06-20 00:54:59 +02:00
"orblivion/lbry-id/auth"
2022-06-21 17:52:03 +02:00
"orblivion/lbry-id/store"
2022-06-20 00:54:59 +02:00
"orblivion/lbry-id/wallet"
)
2022-06-21 17:52:03 +02:00
func TestServerGetWallet(t *testing.T) {
tt := []struct {
name string
2022-06-21 17:52:03 +02:00
expectedStatusCode int
expectedErrorString string
storeErrors TestStoreFunctionsErrors
}{
{
2022-06-21 17:52:03 +02:00
name: "success",
expectedStatusCode: http.StatusOK,
},
{
name: "auth error",
expectedStatusCode: http.StatusUnauthorized,
expectedErrorString: http.StatusText(http.StatusUnauthorized) + ": Token Not Found",
storeErrors: TestStoreFunctionsErrors{GetToken: store.ErrNoToken},
},
{
name: "db error getting wallet",
expectedStatusCode: http.StatusInternalServerError,
expectedErrorString: http.StatusText(http.StatusInternalServerError),
storeErrors: TestStoreFunctionsErrors{GetWallet: fmt.Errorf("Some random DB Error!")},
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
2022-06-21 17:52:03 +02:00
testAuth := TestAuth{}
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-21 17:52:03 +02:00
Errors: tc.storeErrors,
}
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)
2022-06-20 00:54:59 +02:00
2022-06-21 17:52:03 +02:00
// Make sure we tried to get an auth based on the `token` param (whether or
// not it was a valid `token`)
if testStore.Called.GetToken != testStore.TestAuthToken.Token {
t.Errorf("Expected Store.GetToken to be called with %s. Got %s",
testStore.TestAuthToken.Token,
testStore.Called.GetToken)
}
2022-06-20 00:54:59 +02:00
2022-06-21 17:52:03 +02:00
expectStatusCode(t, w, tc.expectedStatusCode)
if len(tc.expectedErrorString) > 0 {
// Only check if we're expecting an error, since it reads the body
expectErrorString(t, w, tc.expectedErrorString)
return
}
body, _ := ioutil.ReadAll(w.Body)
var result WalletResponse
err := json.Unmarshal(body, &result)
2022-06-21 17:52:03 +02:00
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")
}
})
2022-06-21 00:10:54 +02:00
}
}
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()")
}