status-go/node/status_node_rpc_client_test.go
Dmitry Shulyak be9c55bc16
Accounts data management (#1530)
* WIP accounts implementation

* Accounts datasore and changes to status mobile API

* Add library changes and method to update config

* Handle error after account selection

* Add two methods to start account to backend

* Use encrypted database for settings and add a service for them

* Resolve linter warning

* Bring back StartNode StopNode for tests

* Add sub accounts and get/save api

* Changes to accounts structure

* Login use root address and fetch necessary info from database

* Cover accounts store with tests

* Refactor in progress

* Initialize status keystore instance before starting ethereum node

* Rework library tests

* Resolve failures in private api test and send transaction test

* Pass pointer to initialized config to unmarshal

* Use multiaccounts/accounts naming consistently

Multiaccount is used as a login identifier
Account references an address and a key, if account is not watch-only.

* Add login timestamp stored in the database to accounts.Account object

* Add photo-path field for multiaccount struct

* Add multiaccoutns rpc with updateAccount method

Update to any other account that wasn't used for login will return an error

* Fix linter in services/accounts

* Select account before starting a node

* Save list of accounts on first login

* Pass account manager to accounts service to avoid selecting account before starting a node

* Add logs to login with save and regualr login
2019-08-20 18:38:40 +03:00

132 lines
2.9 KiB
Go

package node_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
gethnode "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
"github.com/status-im/status-go/node"
"github.com/status-im/status-go/params"
)
type TestServiceAPI struct{}
func (api *TestServiceAPI) SomeMethod(_ context.Context) (string, error) {
return "some method result", nil
}
type testService struct{}
func (s *testService) Protocols() []p2p.Protocol {
return []p2p.Protocol{}
}
func (s *testService) APIs() []rpc.API {
return []rpc.API{
{
Namespace: "pri",
Version: "1.0",
Service: &TestServiceAPI{},
Public: false,
},
{
Namespace: "pub",
Version: "1.0",
Service: &TestServiceAPI{},
Public: true,
},
}
}
func (s *testService) Start(server *p2p.Server) error {
return nil
}
func (s *testService) Stop() error {
return nil
}
func createAndStartStatusNode(config *params.NodeConfig) (*node.StatusNode, error) {
services := []gethnode.ServiceConstructor{
func(_ *gethnode.ServiceContext) (gethnode.Service, error) {
return &testService{}, nil
},
}
statusNode := node.New()
return statusNode, statusNode.Start(config, nil, services...)
}
func TestNodeRPCClientCallOnlyPublicAPIs(t *testing.T) {
var err error
statusNode, err := createAndStartStatusNode(&params.NodeConfig{
APIModules: "", // no whitelisted API modules; use only public APIs
})
require.NoError(t, err)
defer func() {
err := statusNode.Stop()
require.NoError(t, err)
}()
client := statusNode.RPCClient()
require.NotNil(t, client)
var result string
// call public API
err = client.Call(&result, "pub_someMethod")
require.NoError(t, err)
require.Equal(t, "some method result", result)
// call private API with public RPC client
err = client.Call(&result, "pri_someMethod")
require.EqualError(t, err, "The method pri_someMethod does not exist/is not available")
}
func TestNodeRPCClientCallWhitelistedPrivateService(t *testing.T) {
var err error
statusNode, err := createAndStartStatusNode(&params.NodeConfig{
APIModules: "pri",
})
require.NoError(t, err)
defer func() {
err := statusNode.Stop()
require.NoError(t, err)
}()
client := statusNode.RPCClient()
require.NotNil(t, client)
// call private API
var result string
err = client.Call(&result, "pri_someMethod")
require.NoError(t, err)
require.Equal(t, "some method result", result)
}
func TestNodeRPCPrivateClientCallPrivateService(t *testing.T) {
var err error
statusNode, err := createAndStartStatusNode(&params.NodeConfig{})
require.NoError(t, err)
defer func() {
err := statusNode.Stop()
require.NoError(t, err)
}()
client := statusNode.RPCPrivateClient()
require.NotNil(t, client)
// call private API with private RPC client
var result string
err = client.Call(&result, "pri_someMethod")
require.NoError(t, err)
require.Equal(t, "some method result", result)
}