2021-12-25 02:16:58 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-06-19 21:56:10 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-12-25 02:16:58 +01:00
|
|
|
"fmt"
|
2022-06-19 21:56:10 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-12-25 02:16:58 +01:00
|
|
|
"orblivion/lbry-id/auth"
|
2022-06-07 19:25:14 +02:00
|
|
|
"orblivion/lbry-id/wallet"
|
2022-06-19 21:56:10 +02:00
|
|
|
"strings"
|
2021-12-25 02:16:58 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Implementing interfaces for stubbed out packages
|
|
|
|
|
|
|
|
type TestAuth struct {
|
2022-06-09 23:04:49 +02:00
|
|
|
TestToken auth.TokenString
|
2021-12-25 02:16:58 +01:00
|
|
|
FailGenToken bool
|
|
|
|
}
|
|
|
|
|
2022-06-07 19:25:14 +02:00
|
|
|
func (a *TestAuth) NewToken(userId auth.UserId, deviceId auth.DeviceId, scope auth.AuthScope) (*auth.AuthToken, error) {
|
2021-12-25 02:16:58 +01:00
|
|
|
if a.FailGenToken {
|
|
|
|
return nil, fmt.Errorf("Test error: fail to generate token")
|
|
|
|
}
|
2022-06-07 19:25:14 +02:00
|
|
|
return &auth.AuthToken{Token: a.TestToken, UserId: userId, DeviceId: deviceId, Scope: scope}, nil
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-19 22:40:16 +02:00
|
|
|
type TestStoreFunctionsCalled struct {
|
2022-06-17 18:52:17 +02:00
|
|
|
SaveToken bool
|
|
|
|
GetToken bool
|
|
|
|
GetUserId bool
|
|
|
|
CreateAccount bool
|
|
|
|
SetWallet bool
|
|
|
|
GetWallet bool
|
|
|
|
}
|
|
|
|
|
2022-06-19 22:40:16 +02:00
|
|
|
type TestStoreFunctionsErrors struct {
|
|
|
|
SaveToken error
|
|
|
|
GetToken error
|
|
|
|
GetUserId error
|
|
|
|
CreateAccount error
|
|
|
|
SetWallet error
|
|
|
|
GetWallet error
|
|
|
|
}
|
|
|
|
|
2021-12-25 02:16:58 +01:00
|
|
|
type TestStore struct {
|
2022-06-17 18:52:17 +02:00
|
|
|
// Fake store functions will set these to `true` as they are called
|
2022-06-19 22:40:16 +02:00
|
|
|
Called TestStoreFunctionsCalled
|
2021-12-25 02:16:58 +01:00
|
|
|
|
2022-06-19 22:40:16 +02:00
|
|
|
// Fake store functions will return the errors (including `nil`) specified in
|
|
|
|
// the test setup
|
|
|
|
Errors TestStoreFunctionsErrors
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TestStore) SaveToken(token *auth.AuthToken) error {
|
2022-06-17 18:52:17 +02:00
|
|
|
s.Called.SaveToken = true
|
2022-06-19 22:40:16 +02:00
|
|
|
return s.Errors.SaveToken
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 23:04:49 +02:00
|
|
|
func (s *TestStore) GetToken(auth.TokenString) (*auth.AuthToken, error) {
|
2022-06-17 18:52:17 +02:00
|
|
|
s.Called.GetToken = true
|
2022-06-19 22:40:16 +02:00
|
|
|
return nil, s.Errors.GetToken
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 19:25:14 +02:00
|
|
|
func (s *TestStore) GetUserId(auth.Email, auth.Password) (auth.UserId, error) {
|
2022-06-17 18:52:17 +02:00
|
|
|
s.Called.GetUserId = true
|
2022-06-19 22:40:16 +02:00
|
|
|
return 0, s.Errors.GetUserId
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 19:25:14 +02:00
|
|
|
func (s *TestStore) CreateAccount(auth.Email, auth.Password) error {
|
2022-06-17 18:52:17 +02:00
|
|
|
s.Called.CreateAccount = true
|
2022-06-19 22:40:16 +02:00
|
|
|
return s.Errors.CreateAccount
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 23:04:49 +02:00
|
|
|
func (s *TestStore) SetWallet(
|
2022-06-07 19:25:14 +02:00
|
|
|
UserId auth.UserId,
|
2022-06-09 23:04:49 +02:00
|
|
|
encryptedWallet wallet.EncryptedWallet,
|
|
|
|
sequence wallet.Sequence,
|
|
|
|
hmac wallet.WalletHmac,
|
|
|
|
) (latestEncryptedWallet wallet.EncryptedWallet, latestSequence wallet.Sequence, latestHmac wallet.WalletHmac, sequenceCorrect bool, err error) {
|
2022-06-17 18:52:17 +02:00
|
|
|
s.Called.SetWallet = true
|
2022-06-19 22:40:16 +02:00
|
|
|
err = s.Errors.SetWallet
|
2021-12-25 02:16:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 23:04:49 +02:00
|
|
|
func (s *TestStore) GetWallet(userId auth.UserId) (encryptedWallet wallet.EncryptedWallet, sequence wallet.Sequence, hmac wallet.WalletHmac, err error) {
|
2022-06-17 18:52:17 +02:00
|
|
|
s.Called.GetWallet = true
|
2022-06-19 22:40:16 +02:00
|
|
|
err = s.Errors.GetWallet
|
2021-12-25 02:16:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-19 22:57:37 +02:00
|
|
|
// expectErrorResponse: A helper to call in functions that test that request
|
|
|
|
// handlers fail with a certain status code and error string. Cuts down on
|
|
|
|
// noise.
|
|
|
|
func expectErrorResponse(t *testing.T, w *httptest.ResponseRecorder, expectedStatusCode int, expectedErrorString string) {
|
|
|
|
if want, got := expectedStatusCode, w.Result().StatusCode; want != got {
|
|
|
|
t.Errorf("StatusCode: expected %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, _ := ioutil.ReadAll(w.Body)
|
|
|
|
|
|
|
|
var result ErrorResponse
|
|
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 02:16:58 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-06-19 21:56:10 +02:00
|
|
|
type TestReqStruct struct{ key string }
|
|
|
|
|
|
|
|
func (t *TestReqStruct) validate() bool { return t.key != "" }
|
|
|
|
|
2021-12-25 02:16:58 +01:00
|
|
|
func TestServerHelperGetPostDataSuccess(t *testing.T) {
|
2022-06-19 21:56:10 +02:00
|
|
|
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")
|
|
|
|
}
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
2022-06-19 21:56:10 +02:00
|
|
|
|
2021-12-25 02:16:58 +01:00
|
|
|
func TestServerHelperGetPostDataErrors(t *testing.T) {
|
2022-06-19 21:56:10 +02:00
|
|
|
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)),
|
2022-06-19 21:56:10 +02:00
|
|
|
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",
|
2022-06-19 21:56:10 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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-19 22:57:37 +02:00
|
|
|
expectErrorResponse(t, w, tc.expectedStatusCode, tc.expectedErrorString)
|
2022-06-19 21:56:10 +02:00
|
|
|
})
|
|
|
|
}
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerHelperRequestOverheadSuccess(t *testing.T) {
|
|
|
|
t.Fatalf("Test me: requestOverhead success")
|
|
|
|
}
|
|
|
|
func TestServerHelperRequestOverheadErrors(t *testing.T) {
|
|
|
|
t.Fatalf("Test me: requestOverhead failures")
|
|
|
|
}
|