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/appdatabase"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
statusRPC "github.com/status-im/status-go/rpc"
|
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/transactions/fake"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createDB(t *testing.T) (*sql.DB, func()) {
|
|
|
|
db, cleanup, err := helpers.SetupTestSQLDB(appdatabase.DbInitializer{}, "provider-tests-")
|
|
|
|
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)
|
|
|
|
|
|
|
|
service := NewService(rpcClient, nil)
|
|
|
|
|
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 {
|
|
|
|
request string
|
2024-07-12 18:12:14 +00:00
|
|
|
expectError bool
|
2024-06-24 14:29:40 +00:00
|
|
|
expectedContains string
|
|
|
|
notContains bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
request: "{\"method\": \"eth_blockNumber\", \"params\": []}",
|
2024-07-12 18:12:14 +00:00
|
|
|
expectError: false,
|
2024-06-24 14:29:40 +00:00
|
|
|
expectedContains: "does not exist/is not available",
|
|
|
|
notContains: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: "{\"method\": \"eth_blockNumbers\", \"params\": []}",
|
2024-07-12 18:12:14 +00:00
|
|
|
expectError: false,
|
2024-06-24 14:29:40 +00:00
|
|
|
expectedContains: "does not exist/is not available",
|
|
|
|
notContains: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: "",
|
2024-07-12 18:12:14 +00:00
|
|
|
expectError: true,
|
2024-06-24 14:29:40 +00:00
|
|
|
expectedContains: "does not exist/is not available",
|
|
|
|
notContains: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.request, func(t *testing.T) {
|
|
|
|
response, err := api.CallRPC(tt.request)
|
2024-07-12 18:12:14 +00:00
|
|
|
if tt.expectError {
|
|
|
|
require.Error(t, err)
|
2024-06-24 14:29:40 +00:00
|
|
|
} else {
|
2024-07-12 18:12:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, response)
|
|
|
|
if tt.notContains {
|
|
|
|
require.NotContains(t, response, tt.expectedContains)
|
|
|
|
} else {
|
|
|
|
require.Contains(t, response, tt.expectedContains)
|
|
|
|
}
|
2024-06-24 14:29:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|