2021-12-25 02:16:58 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-06-21 22:59:20 +02:00
|
|
|
"bytes"
|
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"
|
2021-12-25 02:16:58 +01:00
|
|
|
"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"
|
2021-12-25 02:16:58 +01:00
|
|
|
)
|
|
|
|
|
2022-06-21 17:52:03 +02:00
|
|
|
func TestServerGetWallet(t *testing.T) {
|
2022-06-21 02:47:58 +02:00
|
|
|
tt := []struct {
|
2022-06-22 00:25:22 +02:00
|
|
|
name string
|
|
|
|
tokenString auth.TokenString
|
2022-06-21 17:52:03 +02:00
|
|
|
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedErrorString string
|
|
|
|
|
|
|
|
storeErrors TestStoreFunctionsErrors
|
2022-06-21 02:47:58 +02:00
|
|
|
}{
|
|
|
|
{
|
2022-06-21 17:52:03 +02:00
|
|
|
name: "success",
|
2022-06-22 00:25:22 +02:00
|
|
|
tokenString: auth.TokenString("seekrit"),
|
2022-06-21 17:52:03 +02:00
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2022-06-22 01:46:10 +02:00
|
|
|
name: "validation error", // missing auth token
|
2022-06-22 00:25:22 +02:00
|
|
|
tokenString: auth.TokenString(""),
|
|
|
|
expectedStatusCode: http.StatusBadRequest,
|
|
|
|
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Missing token parameter",
|
2022-06-22 01:46:10 +02:00
|
|
|
|
|
|
|
// Just check one validation error (missing auth token) to make sure the
|
|
|
|
// validate function is called. We'll check the rest of the validation
|
|
|
|
// errors in the other test below.
|
2022-06-22 00:25:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "auth error",
|
|
|
|
tokenString: auth.TokenString("seekrit"),
|
2022-06-21 17:52:03 +02:00
|
|
|
|
|
|
|
expectedStatusCode: http.StatusUnauthorized,
|
|
|
|
expectedErrorString: http.StatusText(http.StatusUnauthorized) + ": Token Not Found",
|
|
|
|
|
|
|
|
storeErrors: TestStoreFunctionsErrors{GetToken: store.ErrNoToken},
|
|
|
|
},
|
|
|
|
{
|
2022-06-22 00:25:22 +02:00
|
|
|
name: "db error getting wallet",
|
|
|
|
tokenString: auth.TokenString("seekrit"),
|
2022-06-21 17:52:03 +02:00
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
2022-06-21 02:47:58 +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{}
|
2022-06-21 02:47:58 +02:00
|
|
|
testStore := TestStore{
|
|
|
|
TestAuthToken: auth.AuthToken{
|
2022-06-22 00:25:22 +02:00
|
|
|
Token: auth.TokenString(tc.tokenString),
|
2022-06-21 02:47:58 +02:00
|
|
|
Scope: auth.ScopeFull,
|
|
|
|
},
|
2022-06-20 00:54:59 +02:00
|
|
|
|
2022-06-21 02:47:58 +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-21 02:47:58 +02:00
|
|
|
}
|
2022-06-20 00:54:59 +02:00
|
|
|
|
2022-06-21 02:47:58 +02:00
|
|
|
s := Server{&testAuth, &testStore}
|
2022-06-20 00:54:59 +02:00
|
|
|
|
2022-06-21 02:47:58 +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
|
|
|
|
2022-06-21 02:47:58 +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`)
|
2022-06-22 00:25:22 +02:00
|
|
|
// NOTE: For tests that set testStore.TestAuthToken.Token=="", this will
|
|
|
|
// pass even if GetToken isn't called. But we don't care, we expect the
|
|
|
|
// request to fail for other reasons at that point.
|
2022-06-21 22:59:20 +02:00
|
|
|
if want, got := testStore.TestAuthToken.Token, testStore.Called.GetToken; want != got {
|
|
|
|
t.Errorf("testStore.Called.GetToken called with: expected %s, got %s", want, got)
|
2022-06-21 02:47:58 +02:00
|
|
|
}
|
2022-06-20 00:54:59 +02:00
|
|
|
|
2022-06-22 17:37:03 +02:00
|
|
|
body, _ := ioutil.ReadAll(w.Body)
|
|
|
|
|
2022-06-21 17:52:03 +02:00
|
|
|
expectStatusCode(t, w, tc.expectedStatusCode)
|
2022-06-22 17:37:03 +02:00
|
|
|
expectErrorString(t, body, tc.expectedErrorString)
|
|
|
|
|
|
|
|
// In this case, a wallet body is expected iff there is no error string
|
|
|
|
expectWalletBody := len(tc.expectedErrorString) == 0
|
2022-06-21 17:52:03 +02:00
|
|
|
|
2022-06-22 17:37:03 +02:00
|
|
|
if !expectWalletBody {
|
|
|
|
return // The rest of the test does not apply
|
2022-06-21 17:52:03 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 02:47:58 +02:00
|
|
|
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 {
|
2022-06-21 02:47:58 +02:00
|
|
|
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
|
|
|
|
2022-06-21 02:47:58 +02:00
|
|
|
if !testStore.Called.GetWallet {
|
|
|
|
t.Errorf("Expected Store.GetWallet to be called")
|
|
|
|
}
|
|
|
|
})
|
2022-06-21 00:10:54 +02:00
|
|
|
}
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 22:59:20 +02:00
|
|
|
func TestServerPostWallet(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedErrorString string
|
2022-06-23 21:22:31 +02:00
|
|
|
expectSetWalletCall bool
|
2022-06-21 22:59:20 +02:00
|
|
|
|
2022-06-22 00:25:22 +02:00
|
|
|
// This is getting messy, but in the case of validation failures, we don't
|
|
|
|
// even get around to trying to get an auth token, since the token string is
|
|
|
|
// part of what's being validated. So, we want to be able to skip that
|
|
|
|
// check in that case.
|
|
|
|
skipAuthCheck bool
|
|
|
|
|
2022-06-21 22:59:20 +02:00
|
|
|
// `new...` refers to what is being passed into the via POST request (and
|
|
|
|
// what gets passed into SetWallet for the *non-error* cases below)
|
2022-06-23 21:22:31 +02:00
|
|
|
newEncryptedWallet wallet.EncryptedWallet
|
|
|
|
newSequence wallet.Sequence
|
|
|
|
newHmac wallet.WalletHmac
|
2022-06-21 22:59:20 +02:00
|
|
|
|
|
|
|
storeErrors TestStoreFunctionsErrors
|
|
|
|
}{
|
|
|
|
{
|
2022-06-23 21:22:31 +02:00
|
|
|
name: "success",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectSetWalletCall: true,
|
2022-06-21 22:59:20 +02:00
|
|
|
|
|
|
|
// Simulates a situation where the existing sequence is 1, the new
|
2022-06-23 21:22:31 +02:00
|
|
|
// sequence is 2.
|
|
|
|
|
|
|
|
newEncryptedWallet: wallet.EncryptedWallet("my-encrypted-wallet"),
|
|
|
|
newSequence: wallet.Sequence(2),
|
|
|
|
newHmac: wallet.WalletHmac("my-hmac"),
|
|
|
|
}, {
|
|
|
|
name: "conflict",
|
|
|
|
expectedStatusCode: http.StatusConflict,
|
2022-06-21 22:59:20 +02:00
|
|
|
expectedErrorString: http.StatusText(http.StatusConflict) + ": Bad sequence number",
|
2022-06-23 21:22:31 +02:00
|
|
|
expectSetWalletCall: true,
|
2022-06-21 22:59:20 +02:00
|
|
|
|
2022-06-23 21:22:31 +02:00
|
|
|
// Simulates a situation where the existing sequence is *not* 1, the new
|
|
|
|
// proposed sequence is 2, and it thus fails with a conflict.
|
|
|
|
|
|
|
|
newEncryptedWallet: wallet.EncryptedWallet("my-encrypted-wallet-new"),
|
|
|
|
newSequence: wallet.Sequence(2),
|
|
|
|
newHmac: wallet.WalletHmac("my-hmac-new"),
|
|
|
|
|
|
|
|
storeErrors: TestStoreFunctionsErrors{SetWallet: store.ErrWrongSequence},
|
|
|
|
}, {
|
2022-06-22 00:25:22 +02:00
|
|
|
name: "validation error",
|
|
|
|
expectedStatusCode: http.StatusBadRequest,
|
|
|
|
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Request failed validation",
|
|
|
|
skipAuthCheck: true, // we can't get an auth token without the data we just failed to validate
|
|
|
|
|
|
|
|
// Just check one validation error (empty encrypted wallet) to make sure the
|
|
|
|
// validate function is called. We'll check the rest of the validation
|
|
|
|
// errors in the other test below.
|
|
|
|
|
2022-06-23 21:22:31 +02:00
|
|
|
newEncryptedWallet: wallet.EncryptedWallet(""),
|
|
|
|
newSequence: wallet.Sequence(2),
|
|
|
|
newHmac: wallet.WalletHmac("my-hmac"),
|
|
|
|
}, {
|
2022-06-21 22:59:20 +02:00
|
|
|
name: "auth error",
|
|
|
|
expectedStatusCode: http.StatusUnauthorized,
|
|
|
|
expectedErrorString: http.StatusText(http.StatusUnauthorized) + ": Token Not Found",
|
|
|
|
|
|
|
|
// Putting in valid data here so it's clear that this isn't what causes
|
|
|
|
// the error
|
2022-06-23 21:22:31 +02:00
|
|
|
newEncryptedWallet: wallet.EncryptedWallet("my-encrypted-wallet"),
|
|
|
|
newSequence: wallet.Sequence(2),
|
|
|
|
newHmac: wallet.WalletHmac("my-hmac"),
|
2022-06-21 22:59:20 +02:00
|
|
|
|
2022-06-22 00:25:22 +02:00
|
|
|
// What causes the error
|
2022-06-21 22:59:20 +02:00
|
|
|
storeErrors: TestStoreFunctionsErrors{GetToken: store.ErrNoToken},
|
2022-06-23 21:22:31 +02:00
|
|
|
}, {
|
|
|
|
name: "db error setting wallet",
|
2022-06-21 22:59:20 +02:00
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
expectedErrorString: http.StatusText(http.StatusInternalServerError),
|
2022-06-23 21:22:31 +02:00
|
|
|
expectSetWalletCall: true,
|
2022-06-21 22:59:20 +02:00
|
|
|
|
|
|
|
// Putting in valid data here so it's clear that this isn't what causes
|
|
|
|
// the error
|
2022-06-23 21:22:31 +02:00
|
|
|
newEncryptedWallet: wallet.EncryptedWallet("my-encrypted-wallet"),
|
|
|
|
newSequence: wallet.Sequence(2),
|
|
|
|
newHmac: wallet.WalletHmac("my-hmac"),
|
2022-06-21 22:59:20 +02:00
|
|
|
|
2022-06-22 00:25:22 +02:00
|
|
|
// What causes the error
|
2022-06-21 22:59:20 +02:00
|
|
|
storeErrors: TestStoreFunctionsErrors{SetWallet: fmt.Errorf("Some random db problem")},
|
|
|
|
},
|
2022-06-22 00:25:22 +02:00
|
|
|
|
|
|
|
// TODO
|
|
|
|
// Future test case when we get lastSynced back: Error if
|
|
|
|
// lastSynced.device_id doesn't match authToken.device_id
|
|
|
|
|
2022-06-21 22:59:20 +02:00
|
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
|
|
|
testAuth := TestAuth{}
|
|
|
|
testStore := TestStore{
|
|
|
|
TestAuthToken: auth.AuthToken{
|
|
|
|
Token: auth.TokenString("seekrit"),
|
|
|
|
Scope: auth.ScopeFull,
|
|
|
|
},
|
|
|
|
|
|
|
|
Errors: tc.storeErrors,
|
|
|
|
}
|
|
|
|
|
|
|
|
s := Server{&testAuth, &testStore}
|
|
|
|
|
|
|
|
requestBody := []byte(
|
|
|
|
fmt.Sprintf(`{
|
|
|
|
"token": "%s",
|
|
|
|
"encryptedWallet": "%s",
|
|
|
|
"sequence": %d,
|
|
|
|
"hmac": "%s"
|
|
|
|
}`, testStore.TestAuthToken.Token, tc.newEncryptedWallet, tc.newSequence, tc.newHmac),
|
|
|
|
)
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, PathWallet, bytes.NewBuffer(requestBody))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// test handleWallet while we're at it, which is a dispatch for get and post
|
|
|
|
// wallet
|
|
|
|
s.handleWallet(w, req)
|
|
|
|
|
|
|
|
// Make sure we tried to get an auth based on the `token` param (whether or
|
|
|
|
// not it was a valid `token`)
|
2022-06-22 00:25:22 +02:00
|
|
|
if want, got := testStore.TestAuthToken.Token, testStore.Called.GetToken; !tc.skipAuthCheck && want != got {
|
2022-06-21 22:59:20 +02:00
|
|
|
t.Errorf("testStore.Called.GetToken called with: expected %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
|
2022-06-22 17:37:03 +02:00
|
|
|
body, _ := ioutil.ReadAll(w.Body)
|
|
|
|
|
2022-06-21 22:59:20 +02:00
|
|
|
expectStatusCode(t, w, tc.expectedStatusCode)
|
2022-06-22 17:37:03 +02:00
|
|
|
expectErrorString(t, body, tc.expectedErrorString)
|
2022-06-21 22:59:20 +02:00
|
|
|
|
2022-06-23 21:22:31 +02:00
|
|
|
if tc.expectedErrorString == "" && string(body) != "{}" {
|
|
|
|
t.Errorf("Expected post wallet response to be \"{}\": result: %+v", string(body))
|
2022-06-21 22:59:20 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 21:22:31 +02:00
|
|
|
if want, got := (SetWalletCall{tc.newEncryptedWallet, tc.newSequence, tc.newHmac}), testStore.Called.SetWallet; tc.expectSetWalletCall && want != got {
|
2022-06-21 22:59:20 +02:00
|
|
|
t.Errorf("Store.SetWallet called with: expected %+v, got %+v", want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 23:04:49 +02:00
|
|
|
func TestServerValidateWalletRequest(t *testing.T) {
|
2022-06-22 01:27:54 +02:00
|
|
|
walletRequest := WalletRequest{Token: "seekrit", EncryptedWallet: "my-encrypted-wallet", Hmac: "my-hmac", Sequence: 2}
|
|
|
|
if !walletRequest.validate() {
|
|
|
|
t.Fatalf("Expected valid WalletRequest to successfully validate")
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := []struct {
|
2022-06-22 17:06:05 +02:00
|
|
|
walletRequest WalletRequest
|
2022-06-22 01:27:54 +02:00
|
|
|
failureDescription string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
WalletRequest{EncryptedWallet: "my-encrypted-wallet", Hmac: "my-hmac", Sequence: 2},
|
|
|
|
"Expected WalletRequest with missing token to not successfully validate",
|
|
|
|
}, {
|
|
|
|
WalletRequest{Token: "seekrit", Hmac: "my-hmac", Sequence: 2},
|
|
|
|
"Expected WalletRequest with missing encrypted wallet to not successfully validate",
|
|
|
|
}, {
|
|
|
|
WalletRequest{Token: "seekrit", EncryptedWallet: "my-encrypted-wallet", Sequence: 2},
|
|
|
|
"Expected WalletRequest with missing hmac to not successfully validate",
|
|
|
|
}, {
|
2022-06-22 17:06:05 +02:00
|
|
|
WalletRequest{Token: "seekrit", EncryptedWallet: "my-encrypted-wallet", Hmac: "my-hmac", Sequence: 0},
|
2022-06-22 01:27:54 +02:00
|
|
|
"Expected WalletRequest with sequence < 1 to not successfully validate",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
|
|
if tc.walletRequest.validate() {
|
|
|
|
t.Errorf(tc.failureDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|