wallet-sync-server/server/server_test.go

225 lines
6.6 KiB
Go
Raw Normal View History

package server
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"orblivion/lbry-id/auth"
"orblivion/lbry-id/wallet"
"strings"
"testing"
)
// Implementing interfaces for stubbed out packages
type TestAuth struct {
2022-06-21 17:52:03 +02:00
TestNewTokenString auth.TokenString
FailGenToken bool
}
func (a *TestAuth) NewToken(userId auth.UserId, deviceId auth.DeviceId, scope auth.AuthScope) (*auth.AuthToken, error) {
if a.FailGenToken {
return nil, fmt.Errorf("Test error: fail to generate token")
}
2022-06-21 17:52:03 +02:00
return &auth.AuthToken{Token: a.TestNewTokenString, UserId: userId, DeviceId: deviceId, Scope: scope}, nil
}
2022-06-21 22:59:20 +02:00
type SetWalletCall struct {
EncryptedWallet wallet.EncryptedWallet
Sequence wallet.Sequence
Hmac wallet.WalletHmac
}
2022-06-21 00:10:54 +02:00
// Whether functions are called, and sometimes what they're called with
type TestStoreFunctionsCalled struct {
2022-06-21 00:10:54 +02:00
SaveToken auth.TokenString
GetToken auth.TokenString
GetUserId bool
CreateAccount bool
2022-06-21 22:59:20 +02:00
SetWallet SetWalletCall
GetWallet bool
}
type TestStoreFunctionsErrors struct {
SaveToken error
GetToken error
GetUserId error
CreateAccount error
SetWallet error
GetWallet error
}
type TestStore struct {
// Fake store functions will set these to `true` as they are called
Called TestStoreFunctionsCalled
// Fake store functions will return the errors (including `nil`) specified in
// the test setup
Errors TestStoreFunctionsErrors
2022-06-20 00:54:59 +02:00
TestAuthToken auth.AuthToken
TestEncryptedWallet wallet.EncryptedWallet
TestSequence wallet.Sequence
TestHmac wallet.WalletHmac
2022-06-21 22:59:20 +02:00
TestSequenceCorrect bool
}
2022-06-21 00:10:54 +02:00
func (s *TestStore) SaveToken(authToken *auth.AuthToken) error {
s.Called.SaveToken = authToken.Token
return s.Errors.SaveToken
}
2022-06-21 00:10:54 +02:00
func (s *TestStore) GetToken(token auth.TokenString) (*auth.AuthToken, error) {
s.Called.GetToken = token
2022-06-20 00:54:59 +02:00
return &s.TestAuthToken, s.Errors.GetToken
}
func (s *TestStore) GetUserId(auth.Email, auth.Password) (auth.UserId, error) {
s.Called.GetUserId = true
return 0, s.Errors.GetUserId
}
func (s *TestStore) CreateAccount(auth.Email, auth.Password) error {
s.Called.CreateAccount = true
return s.Errors.CreateAccount
}
func (s *TestStore) SetWallet(
UserId auth.UserId,
encryptedWallet wallet.EncryptedWallet,
sequence wallet.Sequence,
hmac wallet.WalletHmac,
) (latestEncryptedWallet wallet.EncryptedWallet, latestSequence wallet.Sequence, latestHmac wallet.WalletHmac, sequenceCorrect bool, err error) {
2022-06-21 22:59:20 +02:00
s.Called.SetWallet = SetWalletCall{encryptedWallet, sequence, hmac}
err = s.Errors.SetWallet
2022-06-21 22:59:20 +02:00
if err == nil {
latestEncryptedWallet = s.TestEncryptedWallet
latestSequence = s.TestSequence
latestHmac = s.TestHmac
sequenceCorrect = s.TestSequenceCorrect
}
return
}
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
2022-06-20 00:54:59 +02:00
if err == nil {
encryptedWallet = s.TestEncryptedWallet
sequence = s.TestSequence
hmac = s.TestHmac
}
return
}
2022-06-21 17:52:03 +02:00
// expectStatusCode: A helper to call in functions that test that request
// handlers responded with a certain status code. Cuts down on noise.
func expectStatusCode(t *testing.T, w *httptest.ResponseRecorder, expectedStatusCode int) {
if want, got := expectedStatusCode, w.Result().StatusCode; want != got {
2022-06-21 17:52:03 +02:00
t.Errorf("StatusCode: expected %s (%d), got %s (%d)", http.StatusText(want), want, http.StatusText(got), got)
}
2022-06-21 17:52:03 +02:00
}
2022-06-21 17:52:03 +02:00
// expectErrorString: A helper to call in functions that test that request
// handlers failed with a certain error string. Cuts down on noise.
func expectErrorString(t *testing.T, w *httptest.ResponseRecorder, expectedErrorString string) {
body, _ := ioutil.ReadAll(w.Body)
var result ErrorResponse
if err := json.Unmarshal(body, &result); err != nil {
2022-06-21 17:52:03 +02:00
t.Fatalf("Error decoding error message: %s: `%s`", err, body)
}
if want, got := expectedErrorString, result.Error; want != got {
t.Errorf("Error String: expected %s, got %s", want, got)
}
}
func TestServerHelperCheckAuthSuccess(t *testing.T) {
t.Fatalf("Test me: checkAuth success")
}
func TestServerHelperCheckAuthErrors(t *testing.T) {
t.Fatalf("Test me: checkAuth failure")
}
func TestServerHelperGetGetDataSuccess(t *testing.T) {
t.Fatalf("Test me: getGetData success")
}
func TestServerHelperGetGetDataErrors(t *testing.T) {
t.Fatalf("Test me: getGetData failure")
}
type TestReqStruct struct{ key string }
func (t *TestReqStruct) validate() bool { return t.key != "" }
func TestServerHelperGetPostDataSuccess(t *testing.T) {
requestBody := []byte(`{}`)
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBuffer(requestBody))
w := httptest.NewRecorder()
success := getPostData(w, req, &TestReqStruct{key: "hi"})
if !success {
t.Errorf("getPostData failed unexpectedly")
}
}
// Test getPostData, including requestOverhead and any other mini-helpers it calls.
func TestServerHelperGetPostDataErrors(t *testing.T) {
tt := []struct {
name string
method string
requestBody string
expectedStatusCode int
expectedErrorString string
}{
{
name: "bad method",
method: http.MethodGet,
requestBody: "",
expectedStatusCode: http.StatusMethodNotAllowed,
expectedErrorString: http.StatusText(http.StatusMethodNotAllowed),
},
{
name: "request body too large",
method: http.MethodPost,
2022-06-19 23:49:05 +02:00
requestBody: fmt.Sprintf(`{"key": "%s"}`, strings.Repeat("a", 100000)),
expectedStatusCode: http.StatusRequestEntityTooLarge,
expectedErrorString: http.StatusText(http.StatusRequestEntityTooLarge),
},
{
name: "malformed request body JSON",
method: http.MethodPost,
requestBody: "{",
expectedStatusCode: http.StatusBadRequest,
2022-06-19 23:49:05 +02:00
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Error parsing JSON",
},
{
name: "body JSON failed validation",
method: http.MethodPost,
requestBody: "{}",
expectedStatusCode: http.StatusBadRequest,
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Request failed validation",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
// Make request
req := httptest.NewRequest(tc.method, PathAuthToken, bytes.NewBuffer([]byte(tc.requestBody)))
w := httptest.NewRecorder()
success := getPostData(w, req, &TestReqStruct{})
if success {
t.Errorf("getPostData succeeded unexpectedly")
}
2022-06-21 17:52:03 +02:00
expectStatusCode(t, w, tc.expectedStatusCode)
expectErrorString(t, w, tc.expectedErrorString)
})
}
}