2021-12-25 02:16:58 +01:00
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"
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 {
name string
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" ,
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
} ,
}
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 {
Token : auth . TokenString ( "seekrit" ) ,
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`)
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-21 02:47:58 +02:00
}
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 )
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
}
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 ) {
2022-06-09 23:04:49 +02:00
// (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)
2021-12-25 02:16:58 +01:00
// 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" )
}
2022-06-09 23:04:49 +02:00
func TestServerValidateWalletRequest ( t * testing . T ) {
2021-12-25 02:16:58 +01:00
// also add a basic test case for this in TestServerAuthHandlerSuccess to make sure it's called at all
2022-06-09 23:04:49 +02:00
t . Fatalf ( "Test me: Implement and test WalletRequest.validate()" )
2021-12-25 02:16:58 +01:00
}