2024-06-24 14:29:40 +00:00
|
|
|
package connector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
gethrpc "github.com/ethereum/go-ethereum/rpc"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
statusRPC "github.com/status-im/status-go/rpc"
|
2024-07-18 15:30:10 +00:00
|
|
|
"github.com/status-im/status-go/services/connector/commands"
|
2024-06-24 14:29:40 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/transactions/fake"
|
2024-07-18 15:30:10 +00:00
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
2024-06-24 14:29:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func createDB(t *testing.T) (*sql.DB, func()) {
|
2024-07-18 15:30:10 +00:00
|
|
|
db, cleanup, err := helpers.SetupTestSQLDB(walletdatabase.DbInitializer{}, "provider-tests-")
|
2024-06-24 14:29:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return db, func() { require.NoError(t, cleanup()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTestAPI(t *testing.T) (*API, func()) {
|
|
|
|
db, cancel := createDB(t)
|
|
|
|
|
|
|
|
txServiceMockCtrl := gomock.NewController(t)
|
|
|
|
server, _ := fake.NewTestServer(txServiceMockCtrl)
|
|
|
|
|
|
|
|
// Creating a dummy status node to simulate what it's done in get_status_node.go
|
|
|
|
upstreamConfig := params.UpstreamRPCConfig{
|
|
|
|
URL: "https://mainnet.infura.io/v3/fake",
|
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
client := gethrpc.DialInProc(server)
|
|
|
|
rpcClient, err := statusRPC.NewClient(client, 1, upstreamConfig, nil, db)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
service := NewService(db, rpcClient, nil)
|
2024-06-24 14:29:40 +00:00
|
|
|
|
2024-07-12 18:12:14 +00:00
|
|
|
return NewAPI(service), cancel
|
2024-06-24 14:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCallRPC(t *testing.T) {
|
|
|
|
api, cancel := setupTestAPI(t)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
tests := []struct {
|
2024-07-18 15:30:10 +00:00
|
|
|
request string
|
|
|
|
expectError error
|
2024-06-24 14:29:40 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-07-18 15:30:10 +00:00
|
|
|
request: "{\"method\": \"eth_chainId\", \"params\": []}",
|
|
|
|
expectError: commands.ErrRequestMissingDAppData,
|
2024-06-24 14:29:40 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-18 15:30:10 +00:00
|
|
|
request: "{\"method\": \"eth_accounts\", \"params\": []}",
|
|
|
|
expectError: commands.ErrRequestMissingDAppData,
|
2024-06-24 14:29:40 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-18 15:30:10 +00:00
|
|
|
request: "{\"method\": \"eth_requestAccounts\", \"params\": []}",
|
|
|
|
expectError: commands.ErrRequestMissingDAppData,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: "{\"method\": \"eth_sendTransaction\", \"params\": []}",
|
|
|
|
expectError: commands.ErrRequestMissingDAppData,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: "{\"method\": \"wallet_switchEthereumChain\", \"params\": []}",
|
|
|
|
expectError: commands.ErrRequestMissingDAppData,
|
2024-06-24 14:29:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.request, func(t *testing.T) {
|
2024-07-18 15:30:10 +00:00
|
|
|
_, err := api.CallRPC(tt.request)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, tt.expectError, err)
|
2024-06-24 14:29:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|